『壹』 用c語言編寫一個文本編輯器(記事本)
如果是基於字元界面的話,給你個思路
1、整個程序的邏輯是一個循環
while(0)
{
捕獲鍵盤輸入
根據輸入確定執行的功能:1、功能操作
2、編輯輸入或修改操作
刷新顯示緩沖區的內容
}
2、鍵盤輸入包括字元、功能鍵(Fx、del、bk等等)、方向鍵
3、緩沖區是一個動態鏈表,鏈表的每個元素是一個字元串,表示是屏幕上一行的內容,可以根據要求限制長度或者調整顯示方式
『貳』 c語言文本編輯器編輯命令(自動導入一個文本)急求!!!
去找vi或者vim的源碼看看吧。。。這東西幾句話說不清楚。。。簡單來說你要做一個簡單的命令行處理程序,它能夠解析你題目里邊提到的命令字和參數,並根據命令調用具體的功能函數。這個題目如果深入做的話,估計可以算是一個大約兩周左右的課程設計了,在網上找找有沒有以前別人做好的參考一下吧。
簡單說一下你提到的"L
n"命令的實現:
1、首先你要從用戶輸入讀入命令字元串,可以用scanf,getch...甚至是直接用
main函數
的argv和argc兩個入參。
2、這個字元串可以分為兩部分,用空格分隔,第一部分是"L",他是命令,這里可以用swich語句實現:
switch(cCmdName)
{
case
'L':
調用顯示函數,把第二部分"n"作為函數參數傳進去;
}
3、選一個自己熟悉的方式,在一個函數里完成「顯示第n行」的功能,然後在上面調用。
其他的命令類似。
『叄』 c語言編寫簡易的文本編輯器
我這里有一個功能強大文本編譯器程序的完整c代碼,是外國人寫的。不好意思,很長,發不上來。
不過這里有一個簡易文本編譯器。雖說是簡易,也不是那麼好弄的,給你:
http://..com/question/79338502.html
『肆』 C語言如何實現對文本文件的輸入和輸出功能
把文本文件讀出來
存成數組
在數組中執行刪除操作
將數組寫迴文本文件
c的文件不提供直接刪除操作
只能這樣做
『伍』 如何用C語言構建文本編輯器
本題的一個完整的c程序如下,win-tc和Dev-c++下運行通過。
#include <stdio.h>
#define MAXLEN 80
#define MAXLINE 200
char buffer[MAXLEN],fname[120];
char *lineptr[MAXLINE];
FILE *fp;
void edit(),replace(),insert(),delete(),quit();
char comch[]="EeRrIiDdQq";/*命令符*/
void(*comfun[])()={edit,replace,insert,delete,quit};/*對應處理函數*/
int modified=0,/*正文被修改標志*/
last;/*當前正文行數*/
char *chpt;/*輸入命令行字元指針*/
main()
{
int j;
last=0;
while(1)
{
printf("\nInput a command:[e,r,i,d,q].\n");
gets(buffer);/*讀入命令行*/
for(chpt=buffer;*chpt==''||*chpt=='\t';chpt++);/*掠過空白符*/
if(*chpt=='\0') continue;/*空行重新輸入*/
for(j=0;comch[j]!='\0'&&comch[j]!=*chpt;j++);/*查命令符*/
if(comch[j]=='\0') continue;/*非法命令符*/
chpt++;/*掠過命令符,指向參數*/
(*comfun[j/2])();/*執行對應函數*/
fprintf(stdout,"The text is:\n");
for(j=0;j<last;j++)/*顯示正文*/
fputs(lineptr[j],stdout);
}
}
void quit()
{
int c;
if(modified)/* 如正文被修改 */
{
printf("Save? (y/n)");
while(!(((c=getchar())>='a'&&c<='z')||(c>='A'&&c<='Z')));
if(c=='y'||c=='Y')
save(fname); /* 保存被修改過的正文 */
}
for(c=0;c<last;c++)
free(lineptr[c]); /* 釋放內存 */
exit(0);
}
void insert()
{
int k,m,i;
sscanf(chpt,"%d%d",&k,&m); /* 讀入參數 */
if(m<0||m>last||last+k>=MAXLINE)/* 檢查參數合理性 */
{
printf("Error!\n");
return;
}
for(i=last;i>m;i--)/* 後繼行向後移 */
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++) /* 讀入k行正文,並插入 */
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i],buffer);
}
last+=k; /* 修正正文行數 */
modified=1; /* 正文被修改 */
}
void delete()
{
int i,j,m,n;
sscanf(chpt,"%d%d",&m,&n); /* 讀入參數 */
if(m<=0||m>last||n<m) /* 檢查參數合理性 */
{
printf("Error!\n");
return;
}
if(n>last)
n=last; /* 修正參數 */
for(i=m;i<=n;i++) /* 刪除正文 */
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1; /* 修正正文行數 */
modified=1; /* 正文被修改 */
}
void replace()
{
int k,m,n,i,j;
sscanf(chpt,"%d%d%d",&k,&m,&n); /* 讀入參數 */
if(m<=0||m>last||n<m||last-(n-m+1)+k>=MAXLINE)/* 檢查參數合理性 */
{
printf("Error!\n");
return;
}
/* 先完成刪除 */
if(n>last)
n=last; /* 修正參數 */
for(i=m;i<=n;i++) /* 刪除正文 */
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1;
/* 以下完成插入 */
for(i=last;i>=m;i--)
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++)
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i-1]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i-1],buffer);
}
last+=k; /* 修正正文行數 */
modified=1; /* 正文被修改 */
}
save(char *fname) /* 保存文件 */
{
int i;
FILE *fp;
if((fp=fopen(fname,"w"))==NULL)
{
fprintf(stderr,"Can't open %s.\n",fname);
exit(1);
}
for(i=0;i<last;i++)
{
fputs(lineptr[i],fp);
free(lineptr[i]);
}
fclose(fp);
}
void edit() /* 編輯命令 */
{
int i;
FILE *fp;
i=sscanf(chpt,"%s",fname); /* 讀入文件名 */
if(i!=1)
{
printf("Enter file name.\n");
scanf("%s",fname);
}
if((fp=fopen(fname,"r"))==NULL) /* 讀打開 */
{
fp=fopen(fname,"w"); /* 如不存在,則創建文件 */
fclose(fp);
fp=fopen(fname,"r"); /* 重新讀打開 */
}
i=0;
while(fgets(buffer,MAXLEN,fp)==buffer)
{
lineptr[i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[i++],buffer);
}
fclose(fp);
last=i;
}
『陸』 如何用C++實現文本編輯器
1.簡易文本編輯器
2.用鏈表實現,保存到文件中
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctype.h>
#include<cstdio>
#include<fstream>
using namespace std;
int NumberCount=0;//數字個數
int CharCount=0;//字母個數
int PunctuationCount=0;//標點符號個數
int BlankCount=0;//空白符個數
class Node
{
public:
string character;
int cursor;
int offset;
Node* next;
Node(){
cursor=0;//每行的游標初始位置
offset=0;//每行的初始偏移位置
next=NULL;
}
};
class TextEditor
{
private:
Node* head;
string name;
int line;//可更改的行數
int length;//行數
public:
TextEditor();
~TextEditor();
string GetName();
void SetName(string name);
int GetCursor();
int MoveCursor(int offset);
int SetCursor(int line,int offset);
void AddText(const string s);
void InsertText(int seat,string s);
int FindText(string s);
void DeleteText(string s);
int GetLine();
void Count();
friend ostream& operator<<(ostream& out,TextEditor &text);
Node* Gethead(){
return head;
}
//int GetLength()
//{
// return length;
// }
// int FindText(string s);
// void DeleteText(int seat,string s);
};
TextEditor::TextEditor()
{
head=NULL;
name="test";//文件初始名
//tail=NULL;
line=1;
length=0;
}
TextEditor::~TextEditor()
{
Node* p=head;
Node* q;
while(p!=NULL){
q=p->next;
delete p;
p=q;
}
}
int TextEditor::GetLine()
{
return line;
}
string TextEditor::GetName()
{
return name;
}
void TextEditor::SetName(string name)
{
this->name=name;
}
int TextEditor::GetCursor()
{
Node *p=head;
while(p->next!=NULL)
p=p->next;
return p->cursor;
}
int TextEditor::MoveCursor(int offset)
{
Node *p=head;
int i=1;
if(length+1<line){
cout<<"輸入錯誤!"<<endl;
exit(0);
}
else{
while(p->next!=NULL&&i<line){
p=p->next;
i++;
}
}
if(offset>p->character.length()){
cout<<"移動位置太大!"<<endl;
exit(0);
}
else
p->cursor+=offset;
//cout<<"p ->cursor="<<p->cursor<<endl;
return p->cursor;
}
int TextEditor::SetCursor(int line,int offset)
{
this->line=line;
//cout<<"line="<<this->line<<endl;
return MoveCursor(offset);
}
void TextEditor::AddText(const string s)
{
line=length+1;
Node* p=new Node;
Node* q=head;
p->character=s;
p->next=NULL;
if(head==NULL)
head=p;
else{
while(q->next!=NULL)
q=q->next;
q->next=p;
}
length++;
// line++;
}
void TextEditor::InsertText(int seat,string s)
{
Node *p=head;
int i=1;
if(length+1<line){
cout<<"輸入錯誤!"<<endl;
exit(0);
}
else{
while(p->next!=NULL&&i<line){
p=p->next;
i++;
}
}
//MoveCursor(seat);
//cout<<"p->cursor="<<p->cursor<<endl;
string substr;
for(int i=seat;i<s.length()+seat&&i<=p->character.length();i++)
substr+=p->character[i];
p->character.insert(p->cursor,s);
cout<<"substr="<<substr<<endl;
DeleteText(substr);//覆蓋子串
p->cursor=0;//游標清零
}
ostream& operator<<(ostream& out,TextEditor &text)
{
int i=1;
Node* p=text.Gethead();
while(p!=NULL){
out<<p->character<<endl;
p=p->next;
}
// cout<<"length="<<text.GetLength()<<endl;
return out;
}
int TextEditor::FindText(string P)
{
Node* q=head;
//int templine=1;
line=1;
int p=0;
int t=0;
int plen=P.length()-1;
//cout<<"P="<<P<<endl;
//cout<<"plen="<<plen<<endl;
int tlen=q->character.length();
while(q!=NULL){
p=0;
t=0;
tlen=q->character.length();
if(tlen<plen){
line++;
q=q->next;
}
while(p<plen&&t<tlen){
if(q->character[t]==P[p]){
t++;
p++;
}
else{
t=t-p+1;
p=0;
}
}
// cout<<"P="<<P<<endl;
// cout<<"p="<<p<<endl;
// cout<<"plen="<<plen<<endl;
if(p>=plen){
return t-plen+1;
}
else{
line++;
q=q->next;
}
}
return -1;
}
void TextEditor::DeleteText(string s)
{
Node *p=head;
int i=1;
int k=FindText(s);
if(k==-1)
cout<<"未出現該字元串!"<<endl;
else{
while(p!=NULL&&i<line){
p=p->next;
// cout<<p->character<<endl;
i++;
}
p->character.erase(k-1,s.length());
cout<<"刪除成功!"<<endl;
}
}
void TextEditor::Count()
{
Node *p=head;
NumberCount=0;
CharCount=0;
PunctuationCount=0;
BlankCount=0;
while(p!=NULL){
for(int i=0;i<p->character.length();i++){
if(p->character[i]>='0'&&p->character[i]<='9')
NumberCount++;
else if(p->character[i]>'a'&&p->character[i]<'z'||p->character[i]>'A'&&p->character[i]<'Z')
CharCount++;
else if(ispunct(p->character[i]))
PunctuationCount++;
else if(p->character[i]==' ')
BlankCount++;
}
p=p->next;
}
}
int main()
{
int i,j,k,n=2;
string s,t,name;
TextEditor text;
cout<<"---------------------------------------"<<endl;
cout<<"1.添加字元"<<endl;
cout<<"2.設置文檔名字"<<endl;
cout<<"3.獲取文檔名字"<<endl;
cout<<"4.顯示游標位置"<<endl;
cout<<"5.設置游標位置,在游標位置處插入文本"<<endl;
cout<<"6.在文檔中查找文本"<<endl;
cout<<"7.在文檔中刪除文本"<<endl;
cout<<"8.統計字母、數字、標點符號、空白符號及總字元個數"<<endl;
cout<<"9.輸入文本"<<endl;
cout<<"0.退出"<<endl;
while(n){
// cout<<endl;
cout<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"請輸入:";
cin>>n;
getchar();
switch(n){
case 1: cout<<"請輸入字元:"; getline(cin,s,' '); text.AddText(s); break;
case 2: cout<<"請輸入文檔名字:"; cin>>name; text.SetName(name); break;
case 3: cout<<text.GetName()<<endl; break;
case 4: cout<<"游標在第"<<text.GetLine()<<"行,第"<<text.GetCursor()<<"個字元前!"<<endl; break;
case 5:{
cout<<"輸入行數:";
cin>>i;
cout<<"游標在第"<<text.GetCursor()<<"個字元前!"<<endl;
cout<<"輸入移動位數:";
cin>>j;
cout<<"輸入插入字元:";
getchar();
getline(cin,s);
text.InsertText(text.SetCursor(i,j),s); break;
}
case 6: {
cout<<"輸入查找的字元串:";
getline(cin,s);
int k=text.FindText(s);
if(k==-1)
cout<<"查找失敗!"<<endl;
else
cout<<"所查找文本首次出現在:"<<text.GetLine()<<"行,第"<<k<<"個字元處!"<<endl;
break;
}
case 7: cout<<"輸入要刪除的字元串:"; getline(cin,s); text.DeleteText(s); break;
case 8: {
text.Count();
cout<<"文檔中共有:"<<endl;
cout<<NumberCount<<"個數字"<<endl;
cout<<CharCount<<"個字母"<<endl;
cout<<PunctuationCount<<"個標點符號"<<endl;
cout<<BlankCount<<"個空白字元"<<endl;
cout<<"共有"<<NumberCount+CharCount+PunctuationCount+BlankCount<<"個字元!"<<endl;
break;
}
case 9: cout<<text; break;
case 0:{
string ss=text.GetName();
ss+=".txt";
cout<<ss<<endl;
ofstream outFile(ss.c_str());
Node* p=text.Gethead();
while(p!=NULL){
outFile<<p->character<<endl;
p=p->next;
}
exit(0);
break;
}
default: cout<<"輸入錯誤,請重新輸入!"<<endl; break;
}
}
}
『柒』 C語言編寫一個文本編輯器基本知識,初學者求教
這不算文本編輯器吧。
說一下我的思路:
用一個二維數組保存參數,char cmd[N][M]= {"-o","-h"........};
然後根據用戶輸入的參數執行相應的操作,
char ch[3];
while(1)
{
scanf("%s", ch);
// 判斷參數輸入是否正確,如果不正確做相應的處理
switch (ch[1])
{
case 'o':
//打開文件,調用子程序
case 'h':
//獲取幫助,調用子程序
...............
case 'q':
//退出程序
}
}
『捌』 c語言實現文本編輯器的插入實例
舉手之勞,幫你弄了。
像這個題,沒有點分一般是沒人來的,但小可貴在釣,不在魚。弄了算了。
本題的一個完整的c程序如下,win-tc和Dev-c++下運行通過。
#include <stdio.h>
#define MAXLEN 80
#define MAXLINE 200
char buffer[MAXLEN],fname[120];
char *lineptr[MAXLINE];
FILE *fp;
void edit(),replace(),insert(),delete(),quit();
char comch[]="EeRrIiDdQq";/*命令符*/
void(*comfun[])()={edit,replace,insert,delete,quit};/*對應處理函數*/
int modified=0,/*正文被修改標志*/
last;/*當前正文行數*/
char *chpt;/*輸入命令行字元指針*/
main()
{
int j;
last=0;
while(1)
{
printf("\nInput a command:[e,r,i,d,q].\n");
gets(buffer);/*讀入命令行*/
for(chpt=buffer;*chpt==''||*chpt=='\t';chpt++);/*掠過空白符*/
if(*chpt=='\0') continue;/*空行重新輸入*/
for(j=0;comch[j]!='\0'&&comch[j]!=*chpt;j++);/*查命令符*/
if(comch[j]=='\0') continue;/*非法命令符*/
chpt++;/*掠過命令符,指向參數*/
(*comfun[j/2])();/*執行對應函數*/
fprintf(stdout,"The text is:\n");
for(j=0;j<last;j++)/*顯示正文*/
fputs(lineptr[j],stdout);
}
}
void quit()
{
int c;
if(modified)/* 如正文被修改 */
{
printf("Save? (y/n)");
while(!(((c=getchar())>='a'&&c<='z')||(c>='A'&&c<='Z')));
if(c=='y'||c=='Y')
save(fname); /* 保存被修改過的正文 */
}
for(c=0;c<last;c++)
free(lineptr[c]); /* 釋放內存 */
exit(0);
}
void insert()
{
int k,m,i;
sscanf(chpt,"%d%d",&k,&m); /* 讀入參數 */
if(m<0||m>last||last+k>=MAXLINE)/* 檢查參數合理性 */
{
printf("Error!\n");
return;
}
for(i=last;i>m;i--)/* 後繼行向後移 */
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++) /* 讀入k行正文,並插入 */
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i],buffer);
}
last+=k; /* 修正正文行數 */
modified=1; /* 正文被修改 */
}
void delete()
{
int i,j,m,n;
sscanf(chpt,"%d%d",&m,&n); /* 讀入參數 */
if(m<=0||m>last||n<m) /* 檢查參數合理性 */
{
printf("Error!\n");
return;
}
if(n>last)
n=last; /* 修正參數 */
for(i=m;i<=n;i++) /* 刪除正文 */
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1; /* 修正正文行數 */
modified=1; /* 正文被修改 */
}
void replace()
{
int k,m,n,i,j;
sscanf(chpt,"%d%d%d",&k,&m,&n); /* 讀入參數 */
if(m<=0||m>last||n<m||last-(n-m+1)+k>=MAXLINE)/* 檢查參數合理性 */
{
printf("Error!\n");
return;
}
/* 先完成刪除 */
if(n>last)
n=last; /* 修正參數 */
for(i=m;i<=n;i++) /* 刪除正文 */
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1;
/* 以下完成插入 */
for(i=last;i>=m;i--)
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++)
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i-1]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i-1],buffer);
}
last+=k; /* 修正正文行數 */
modified=1; /* 正文被修改 */
}
save(char *fname) /* 保存文件 */
{
int i;
FILE *fp;
if((fp=fopen(fname,"w"))==NULL)
{
fprintf(stderr,"Can't open %s.\n",fname);
exit(1);
}
for(i=0;i<last;i++)
{
fputs(lineptr[i],fp);
free(lineptr[i]);
}
fclose(fp);
}
void edit() /* 編輯命令 */
{
int i;
FILE *fp;
i=sscanf(chpt,"%s",fname); /* 讀入文件名 */
if(i!=1)
{
printf("Enter file name.\n");
scanf("%s",fname);
}
if((fp=fopen(fname,"r"))==NULL) /* 讀打開 */
{
fp=fopen(fname,"w"); /* 如不存在,則創建文件 */
fclose(fp);
fp=fopen(fname,"r"); /* 重新讀打開 */
}
i=0;
while(fgets(buffer,MAXLEN,fp)==buffer)
{
lineptr[i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[i++],buffer);
}
fclose(fp);
last=i;
}
『玖』 用C語言編寫一個簡單的文本編輯器.
我的C語言是自學的,懂一小點。雖然我沒有你說的那種源代碼,但我有記事本的源代碼,你想看看嗎?
記事本(主程序)
#include <windows.h>
#include "sample.h"
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
#define IDC_MAIN_TEXT 1001
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0;
if(SetWindowText(hEdit, pszFileText))
bSuccess = TRUE;
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0)
{
LPSTR pszText;
pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
if(pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwTextLength + 1))
{
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = 0;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
if(bSave)
{
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&ofn))
{
if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "保存文件失敗.", "錯誤信息",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
else
{
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "打開文件失敗.", "錯誤信息",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
ES_WANTRETURN,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
break;
case WM_SIZE:
if(wParam != SIZE_MINIMIZED)
MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
HIWORD(lParam), TRUE);
break;
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case CM_FILE_OPEN:
DoFileOpenSave(hwnd, FALSE);
break;
case CM_FILE_SAVEAS:
DoFileOpenSave(hwnd, TRUE);
break;
case CM_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case CM_ABOUT:
MessageBox (NULL, "歡迎使用jiyingjun的源代碼" , "關於...", 0);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
g_hInst = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = "MAINMENU";
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "注冊窗口失敗", "錯誤信息",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"記事本",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, g_hInst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "創建窗口失敗", "錯誤信息",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
記事本(資源文件)
#include "sample.h"
A ICON MOVEABLE PURE LOADONCALL DISCARDABLE "sample.ico"
MAINMENU MENU
{
POPUP "文件(&F)"
{
MENUITEM "打開(&O)...", CM_FILE_OPEN
MENUITEM "另存為(&S)...", CM_FILE_SAVEAS
MENUITEM SEPARATOR
MENUITEM "關閉", CM_FILE_EXIT
}
POPUP "幫助(&H)"
{
MENUITEM "關於(&A)", CM_ABOUT
}
}
記事本(頭文件)
#define CM_FILE_SAVEAS 9072
#define CM_FILE_EXIT 9071
#define CM_FILE_OPEN 9070
#define CM_ABOUT 9069