Win记事本C代码
/***************************************************program wing_ed.c
wing.edit 1.19
(c) 2008 lingluoz -lingluoz@
****************************************************/
#include <windows.h>
#include <string.h>
#define ID_EDIT 1
#define ID_M_FILE_NEW 40001
#define ID_M_FILE_OPEN 40002
#define ID_M_FILE_SAVE 40003
#define ID_M_FILE_SAVE_AS 40004
#define ID_M_FILE_EXIT 40005
#define ID_M_HELP_ABOUT 40007
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
TCHAR szAppName[] ="wing.edit";
TCHAR szCntFile[MAX_PATH]="";
TCHAR szCntTitle[MAX_PATH]="Untitled.txt";
TCHAR szCommand[MAX_PATH]="";
BOOL bTextUpdate=FALSE;
void DoCaption(HWND hwnd)
{
TCHAR szCap[MAX_PATH+16]="";
strcpy(szCap,szAppName);
strcat(szCap," -");
strcat(szCap,szCntTitle);
if (bTextUpdate==TRUE)
strcat(szCap,"*");
SetWindowText(hwnd,szCap);
}
BOOL FileWrite(HWND hwndEdit,PSTR sFileName)
{
PBYTE p;
HANDLE hFile;
DWORD dwBytesWritten;
hFile=CreateFile(
sFileName,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
0,
NULL
);
if (hFile==INVALID_HANDLE_VALUE)
return FALSE;
p=(PBYTE)malloc(GetWindowTextLength(hwndEdit)+1);
GetWindowText (hwndEdit,(LPSTR)p,GetWindowTextLength(hwndEdit)+1);
WriteFile (hFile,p,GetWindowTextLength(hwndEdit),&dwBytesWritten,NULL);
SetWindowText(hwndEdit,(PTSTR)p);
free(p);
CloseHandle(hFile);
return TRUE;
}
BOOL WingSaveAs(HWND hWndEdit,HWND hwnd)
{
TCHAR szFilter[] = "文本 (*.txt)\0*.txt\ flie (*.*) \0*.*\0\0";
TCHAR szTitle[MAX_PATH]="";
TCHAR szFile[MAX_PATH]="";
OPENFILENAME ofn;
strcpy(szFile,szCntFile);
strcpy(szTitle,szCntTitle);
if (*szCntFile==0)
strcpy(szFile,"Untitled.txt");
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrFileTitle = szTitle;
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = OFN_OVERWRITEPROMPT ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = "txt";
ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
GetSaveFileName(&ofn);
if (*szFile!=0) {
if (FileWrite(hWndEdit,szFile)==TRUE) {
strcpy(szCntFile,szFile);
strcpy(szCntTitle,szTitle);
bTextUpdate=FALSE;
return TRUE;
} else {
MessageBox(
hwnd,
"Can't save this file.",
szAppName,
MB_ICONERROR
);
return FALSE;
}
} else
return FALSE;
}
BOOL WingSave(HWND hWndEdit,HWND hwnd)
{
if (*szCntFile!=0) {
if (FileWrite(hWndEdit,szCntFile)!=TRUE) {
MessageBox(
hwnd,
"Can't save this file.",
szAppName,
MB_ICONERROR
);
return FALSE;
} else {
bTextUpdate=FALSE;
return TRUE;
}
} else
return WingSaveAs(hWndEdit,hwnd);
}
BOOL CheckUpdataToSave(HWND hwndEdit,HWND hwnd)
{
BOOL bReturn;
if (bTextUpdate==TRUE) {
switch (MessageBox(
hwnd,
"This file has been changed,will you save it?",
szAppName,
MB_ICONINFORMATION|MB_YESNOCANCEL|MB_SYSTEMMODAL
)) {
case IDYES:
bReturn=WingSave(hwndEdit,hwnd);
break;
case IDNO:
bReturn=TRUE;
break;
case IDCANCEL:
bReturn=FALSE;
break;
}
} else
bReturn=TRUE;
return bReturn;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
HMENU hMenu,hMenuPopup;
hMenu = CreateMenu () ;
hMenuPopup = CreateMenu () ;
AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_NEW, "新建&N");
AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_OPEN, "打开&O");
AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_SAVE, "保存&S");
AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_SAVE_AS, "另存为&A");
AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL) ;
AppendMenu(hMenuPopup, MF_STRING, ID_M_FILE_EXIT, "退出&E") ;
AppendMenu(hMenu, MF_POPUP, (UINT)hMenuPopup, "文件&F") ;
hMenuPopup = CreateMenu () ;
AppendMenu (hMenuPopup, MF_STRING, ID_M_HELP_ABOUT, "关于&A") ;
AppendMenu (hMenu, MF_POPUP, (UINT)hMenuPopup, "帮助&H") ;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass)) {
MessageBox ( NULL, TEXT ("Unkonow Error!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
if (strlen(szCmdLine)<MAX_PATH)
strcpy(szCommand,szCmdLine);
hwnd=CreateWindow(szAppName,szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,hMenu,hInstance,NULL
);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL FileRead(HWND hwndEdit,PSTR sFileName)
{
PBYTE p;
HANDLE hFile;
DWORD dwBytesRead;
hFile=CreateFile(
sFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,OPEN_EXISTING,
0,
NULL
);
if (hFile==INVALID_HANDLE_VALUE)
return FALSE;
p=(PBYTE)malloc(GetFileSize(hFile,NULL)+1);
ReadFile(hFile,p,GetFileSize(hFile,NULL),&dwBytesRead, NULL);
*(p+GetFileSize(hFile,NULL))=0;
SetWindowText(hwndEdit,(PTSTR)p);
free(p);
CloseHandle(hFile);
return TRUE;
}
void WingOpenFile(HWND hwndEdit,HWND hwnd)
{
OPENFILENAME ofn;
TCHAR szFilter[]="Text Files (*.txt)\0*.txt\0All Files (*.*) \0*.*\0\0";
TCHAR szTitle[MAX_PATH]="";
TCHAR szFile[MAX_PATH]="";
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.hInstance=NULL;
ofn.lpstrFilter=szFilter;
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
ofn.nFilterIndex=0;
ofn.lpstrFile=szFile;
ofn.nMaxFile=MAX_PATH;
ofn.lpstrFileTitle=szTitle;
ofn.nMaxFileTitle=MAX_PATH;
ofn.lpstrInitialDir=NULL;
ofn.lpstrTitle=NULL;
ofn.Flags=OFN_HIDEREADONLY;
ofn.nFileOffset=0;
ofn.nFileExtension=0;
ofn.lpstrDefExt="*";
ofn.lCustData=0L;
ofn.lpfnHook=NULL;
ofn.lpTemplateName=NULL;
GetOpenFileName(&ofn);
if (*szFile!=0) {
if (FileRead(hwndEdit,szFile)==TRUE) {
strcpy(szCntFile,szFile);
strcpy(szCntTitle,szTitle);
bTextUpdate=FALSE;
} else
MessageBox(
hwnd,
"Can't open this file.",
szAppName,
MB_SYSTEMMODAL|MB_ICONERROR
);
}
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static HWND hwndEdit;
LOGFONT logFont;
HFONT hFont;
switch (message) {
case WM_CREATE :
hwndEdit=CreateWindow(TEXT("edit"),NULL,
WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
ES_LEFT|ES_MULTILINE|
ES_AUTOHSCROLL|ES_AUTOVSCROLL,
0,0,0,0,hwnd,(HMENU)ID_EDIT,
((LPCREATESTRUCT)lParam)->hInstance,NULL
);
if (*szCommand!=0) {
if (FileRead(hwndEdit,szCommand)==TRUE) {
strcpy(szCntFile,szCommand);
strcpy(
szCntTitle,
strrchr(szCommand,'\\')==NULL?szCommand:strrchr(szCommand,'\\')+1);
bTextUpdate=FALSE;
} else
MessageBox(
hwnd,
"Can't open this file.",
szAppName,
MB_ICONERROR
);
}
DoCaption(hwnd);
GetObject(
GetStockObject(SYSTEM_FONT),
sizeof(LOGFONT),
(LPVOID)&logFont
);
strcpy(logFont.lfFaceName,"Fixedsys");
logFont.lfHeight=0;
logFont.lfWidth=0;
logFont.lfWeight=FW_NORMAL;
hFont=CreateFontIndirect(&logFont);
SendMessage(hwndEdit,WM_SETFONT,(WPARAM)hFont,0);
return 0 ;
case WM_SETFOCUS:
SetFocus (hwndEdit);
return 0;
case WM_SIZE:
MoveWindow(hwndEdit,0,0,LOWORD(lParam),HIWORD(lParam),TRUE);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam)){
case ID_EDIT:
if (HIWORD(wParam)==EN_ERRSPACE||HIWORD(wParam)==EN_MAXTEXT)
MessageBox (hwnd, TEXT ("Edit control out of space."),szAppName, MB_OK | MB_ICONSTOP) ;
if (HIWORD(wParam)==EN_UPDATE) {
bTextUpdate=TRUE;
DoCaption(hwnd);
}
break;
case ID_M_FILE_OPEN:
if (CheckUpdataToSave(hwndEdit,hwnd)==FALSE)
return 0;
WingOpenFile(hwndEdit,hwnd);
DoCaption(hwnd);
break;
case ID_M_FILE_SAVE_AS:
WingSaveAs(hwndEdit,hwnd);
DoCaption(hwnd);
break;
case ID_M_FILE_SAVE:
WingSave(hwndEdit,hwnd);
DoCaption(hwnd);
break;
case ID_M_FILE_NEW:
if (CheckUpdataToSave(hwndEdit,hwnd)==FALSE)
return 0;
SetWindowText(hwndEdit,"");
strcpy(szCntFile,"");
strcpy(szCntTitle,"Untitled.txt");
DoCaption(hwnd);
break;
case ID_M_FILE_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0);
break;
case ID_M_HELP_ABOUT:
MessageBox(
hwnd,
"wing.edit 1.19\ncopyright (c) 2008 lingluoz\nlingluoz@,
szAppName,
MB_ICONINFORMATION
);
break;
}
return 0;
case WM_CLOSE:
if (CheckUpdataToSave(hwndEdit,hwnd)!=TRUE)
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0 ;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
以上代码用C-Free 5.0直接通过
C-Free 5.0注册码
用户名:tianfang
电子邮件:quart@
注册码:2NnUqd3shO2agta0xNjcusfK1LXO
C-Free 5.0上百度一下就有