当前位置:首页 » 编程语言 » c语言实现文本编辑器的输入
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现文本编辑器的输入

发布时间: 2022-07-28 21:07:21

‘壹’ 用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