#include <windows.h> #include <commdlg.h> #include "resource.h" #define UNTITLED "untitled.txt" static TCHAR szAppName[] = TEXT("我的记事本"); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HACCEL hAccel; HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(hInstance, szAppName); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = szAppName; wndclass.lpszClassName = szAppName; if(!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("Register failure..."), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); // 增加快捷键接口 hAccel = LoadAccelerators(hInstance, szAppName); while(GetMessage(&msg, NULL, 0, 0)) { if(!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } // 这个函数用于当每次新的操作而需要保存文件 // 时弹出的对话框。 short AskAboutSave(HWND hwnd, TCHAR* szFileName) { TCHAR buffer[64 + MAX_PATH]; int iReturn; // 当还没有文件名的时候,我们假定文件名就是untitled.txt wsprintf(buffer, "您没有保存%s文件,想要保存么?", szFileName[0] ? szFileName : UNTITLED); // yes to save file, no to not save file, cancel to cancel the save operation. iReturn = MessageBox(hwnd, buffer, szAppName, MB_YESNOCANCEL | MB_ICONQUESTION); if(iReturn == IDYES) SendMessage(hwnd, WM_COMMAND, IDM_FILE_SAVE, 0); return iReturn; } void DoCaption(HWND hwnd, TCHAR* szCaptionName) { TCHAR buffer[64 + MAX_PATH]; wsprintf(buffer, "%s - %s", szAppName, szCaptionName[0] ? szCaptionName : UNTITLED); SetWindowText(hwnd, buffer); } void OkMessage(HWND hwnd, TCHAR* message, TCHAR* szFileName) { TCHAR buffer[64 + MAX_PATH]; wsprintf(buffer, message, szFileName[0] ? szFileName : UNTITLED); MessageBox(NULL, buffer, szAppName, MB_OK | MB_ICONEXCLAMATION); } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static TCHAR fullPath[MAX_PATH], fileName[MAX_PATH]; TCHAR* pText; static HWND hwndEdit; static BOOL bNeedSave = FALSE; switch(message) { case WM_CREATE: hwndEdit = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE | ES_LEFT | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 0, 0, 0, 0, hwnd, (HMENU)1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); // 这一步是必须的,初始化OPENFILENAME popFileInit(hwnd); return 0; case WM_SIZE: MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_SETFOCUS: SetFocus(hwndEdit); return 0; case WM_COMMAND: // 学习一下它的处理方法 if(lParam && LOWORD(wParam) == 1) { switch(HIWORD(wParam)) { case EN_UPDATE: bNeedSave = TRUE; return 0; case EN_ERRSPACE: case EN_MAXTEXT: MessageBox(NULL, TEXT("记事本已经超出了范围"), szAppName, MB_OK | MB_ICONSTOP); return 0; } break; } switch(LOWORD(wParam)) { case IDM_FILE_NEW: // 当使用者按下了YES,文件将保存,下面的条件将为FALSE,执行NEW操作 // 当按下了NO,下面的条件将会为FALSE,执行NEW操作 // 当按下的CANCEL,下面的条件将会为真,不执行NEW操作 if(bNeedSave && IDCANCEL == AskAboutSave(hwnd, fileName)) return 0; SetWindowText(hwndEdit, "\0"); fullPath[0] = '\0'; fileName[0] = '\0'; DoCaption(hwnd, fileName); bNeedSave = FALSE; return 0; case IDM_FILE_OPEN: if(bNeedSave && IDCANCEL == AskAboutSave(hwnd, fileName)) return 0; // 我们不处理按下了CANCEL的情况 if(openDlg(hwnd, fullPath, fileName)) if(!openReadFile(hwndEdit, fileName)) { OkMessage(hwnd, "无法打开文件%s!", fileName); fullPath[0] = '\0'; fileName[0] = '\0'; } DoCaption(hwnd, fileName); bNeedSave = FALSE; return 0; case IDM_FILE_SAVE: // 以前保存过,如果是第一次保存, // 下面的条件为FALSE,转到下一个CASE if(fileName[0]) { if(saveWriteFile(hwndEdit, fileName)) { bNeedSave = FALSE; return 1; } else { OkMessage(hwnd, "无法保存文件%s", fileName); return 0; } } case IDM_FILE_SAVE_AS: if(saveDlg(hwnd, fullPath, fileName)) { if(saveWriteFile(hwndEdit, fileName)) { bNeedSave = FALSE; DoCaption(hwnd, fileName); return 1; } else { OkMessage(hwnd, "无法保存文件%s", fileName); return 0; } } return 0; case IDM_FILE_EXIT: SendMessage(hwnd, WM_CLOSE, 0, 0); return 0; } break; case WM_CLOSE: if(bNeedSave && IDCANCEL == AskAboutSave(hwnd, fileName)) return 0; DestroyWindow(hwnd); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } |