/函数:WndProc /作用:主窗口消息循环 LRESULT CALLBACK WndProc (HWND hwnd,UINT iMsg, WPARAM wParam,LPARAM IParam) static BOOL bNeedSave FALSE; ∥是否保存文件的标志 static char szFileName[MAX PATH];/文件名 static char SzTitleName[MAX FNAME MAX EXT]; static HINSTANCE hInst,./应用程序句柄 static HWND hwndEdit;:/编辑框句柄 static int iOffset; static UINT iMsgFindReplace; LPFINDREPLACE pfr:
//函数:WndProc //作用:主窗口消息循环 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static BOOL bNeedSave = FALSE; //是否保存文件的标志 static char szFileName[_MAX_PATH];//文件名 static char szTitleName[_MAX_FNAME + _MAX_EXT]; static HINSTANCE hInst;//应用程序句柄 static HWND hwndEdit;//编辑框句柄 static int iOffset; static UINT iMsgFindReplace; LPFINDREPLACE pfr;
switch (iMsg) case WM CREATE:/创建窗口 hInst =((LPCREATESTRUCT)IParam)->hInstance /创建编辑框 hwndEdit Create Window ("edit",NULL, WS CHILD WS VISIBLE WS HSCROLL WS VSCROLL WS BORDER ES LEFT ES MULTILINE ES NOHIDESEL ES AUTOHSCROLL ES AUTOVSCROLL, 0,0,0,0 hwnd,(HMENU)EDITID,hInst,NULL)
switch (iMsg) { case WM_CREATE: //创建窗口 hInst = ((LPCREATESTRUCT) lParam) -> hInstance ; //创建编辑框 hwndEdit = CreateWindow ("edit", NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |WS_BORDER | ES_LEFT | ES_MULTILINE |ES_NOHIDESEL | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 0, 0, hwnd, (HMENU) EDITID, hInst, NULL) ;
SendMessage (hwndEdit,EM LIMITTEXT,32000,OL); PopFilelnitialize(hwnd);∥初始化ofn /注册消息FINDMSGSTRING iMsgFindReplace Register WindowMessage (FINDMSGSTRING); Istrcpy(szFileName,(PSTR)(((LPCREATESTRUCT) IParam)->lpCreateParams)); if (strlen (szFileName)>0) { GetFileTitle (szFileName,SZTitleName,sizeof (szTitleName)); /获取文件名 if(!PopFileRead(hwndEdit,.szFileName)/读文件 OkMessage(hwnd,"文件%s不能读取!",SzTitleName); } DoCaption (hwnd,sZTitleName); return 0; case WM SETFOCUS: SetFocus(hwndEdit):/∥使编辑框获得焦点 return 0;
SendMessage (hwndEdit, EM_LIMITTEXT, 32000, 0L); PopFileInitialize (hwnd); //初始化ofn /注册消息FINDMSGSTRING iMsgFindReplace = RegisterWindowMessage (FINDMSGSTRING) ; lstrcpy (szFileName, (PSTR) (((LPCREATESTRUCT) lParam)->lpCreateParams)) ; if (strlen (szFileName) > 0) { GetFileTitle (szFileName, szTitleName, sizeof (szTitleName)); //获取文件名 if (!PopFileRead (hwndEdit, szFileName))//读文件 OkMessage (hwnd, "文件 %s 不能读取!", szTitleName); } DoCaption (hwnd, szTitleName); return 0; case WM_SETFOCUS: SetFocus (hwndEdit); //使编辑框获得焦点 return 0;
case WM SIZE: Move Window (hwndEdit,0,0, LOWORD (IParam), HIWORD (IParam),TRUE): return 0; case WM COMMAND:/菜单命令 if(IParam &LOWORD (wParam)==EDITID) { switch (HIWORD (wParam)) { case EN UPDATE: bNeedSave =TRUE: return 0; case EN ERRSPACE case EN MAXTEXT: MessageBox(hwnd,"编辑框超出边界!.", SzAppName,MB_OK MB ICONSTOP); return 0;
case WM_SIZE: MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE); return 0; case WM_COMMAND:// 菜单命令 if (lParam && LOWORD (wParam) == EDITID) { switch (HIWORD (wParam)) { case EN_UPDATE: bNeedSave = TRUE; return 0; case EN_ERRSPACE: case EN_MAXTEXT: MessageBox (hwnd, "编辑框超出边界!.", szAppName, MB_OK | MB_ICONSTOP); return 0; }
break; switch (LOWORD (wParam)) ∥来自文件菜单的消息 case IDM NEW:/新建 if (bNeedSave &IDCANCEL =AskAboutSave (hwnd,sZTitleName)) return 0; SetWindowText (hwndEdit,"\0"); szFileName[0]=\0'; SZTitleName[0]=0'; DoCaption (hwnd,szTitleName); bNeedSave FALSE; return 0; case IDM OPEN:/打开文件 if(bNeedSave &IDCANCEL ==AskAboutSave (hwnd,szTitleName)) return 0
break; } switch (LOWORD (wParam)) { // 来自文件菜单的消息 case IDM_NEW://新建 if (bNeedSave && IDCANCEL ==AskAboutSave (hwnd, szTitleName)) return 0; SetWindowText (hwndEdit, "\0"); szFileName[0] = '\0'; szTitleName[0] = '\0'; DoCaption (hwnd, szTitleName); bNeedSave = FALSE; return 0; case IDM_OPEN://打开文件 if (bNeedSave && IDCANCEL ==AskAboutSave (hwnd, szTitleName)) return 0 ;
/弹出Open对话框 if (PopFileOpenDlg (hwnd,szFileName,sZTitleName)) /读文件 if (!PopFileRead (hwndEdit,szFileName)) OkMessage(hwnd,"不能读取文件osl",SZTitleName), szFileName[0]=\0'; SZTitleName[0]=\0'; DoCaption (hwnd,szTitleName); bNeedSave FALSE; return 0; case IDM SAVE:/保存 if (szFileName[0])
//弹出Open对话框 if (PopFileOpenDlg (hwnd, szFileName, szTitleName)) { //读文件 if (!PopFileRead (hwndEdit, szFileName)) { OkMessage (hwnd, "不能读取文件%s!",szTitleName); szFileName[0] = '\0'; szTitleName[0] = '\0'; } } DoCaption (hwnd, szTitleName); bNeedSave = FALSE; return 0; case IDM_SAVE://保存 if (szFileName[0]) {
/写文件 if (PopFileWrite (hwndEdit,szFileName)) bNeedSave FALSE; return 1; else OkMessage(hwnd,"不能写文件%s",SzTitleName); return 0;
//写文件 if (PopFileWrite (hwndEdit, szFileName)) { bNeedSave = FALSE; return 1; } else OkMessage (hwnd, "不能写文件 %s",szTitleName) ; return 0; }
case IDM SAVEAS:/另存为 /弹出Save AS对话框 if (PopFileSaveDlg (hwnd,szFileName,szTitleName)) DoCaption (hwnd,sZTitleName); if(PopFile Write (hwndEdit,szFileName)) bNeedSave FALSE; return 1; else OkMessage(hwnd,"不能写文件%s",SZTitleName), return 0; case IDM EXIT:/结束 SendMessage (hwnd,WM CLOSE,0,0); return 0;
case IDM_SAVEAS://另存为 //弹出Save AS对话框 if (PopFileSaveDlg (hwnd, szFileName, szTitleName)) { DoCaption (hwnd, szTitleName) ; if (PopFileWrite (hwndEdit, szFileName)) { bNeedSave = FALSE; return 1; } else OkMessage (hwnd, "不能写文件%s",szTitleName) ; } return 0; case IDM_EXIT://结束 SendMessage (hwnd, WM_CLOSE, 0, 0); return 0;
case IDM UNDO/撤消 SendMessage (hwndEdit,WM UNDO,0,0); return 0; case IDM CUT:/剪切 SendMessage (hwndEdit,WM CUT,0,0): return 0; case IDM COPY:/复制 SendMessage (hwndEdit,WM COPY,0,0); return 0; case IDM PASTE:/粘贴 SendMessage (hwndEdit,WM PASTE,0,0); return 0; case IDM CLEAR:/清空 SendMessage (hwndEdit,WM CLEAR,0,0); return 0; case IDM SELALL:/全选 SendMessage (hwndEdit,EM SETSEL,0,-1); return 0;
case IDM_UNDO ://撤消 SendMessage (hwndEdit, WM_UNDO, 0, 0); return 0; case IDM_CUT: //剪切 SendMessage (hwndEdit, WM_CUT, 0, 0); return 0; case IDM_COPY://复制 SendMessage (hwndEdit, WM_COPY, 0, 0); return 0; case IDM_PASTE://粘贴 SendMessage (hwndEdit, WM_PASTE, 0, 0); return 0; case IDM_CLEAR://清空 SendMessage (hwndEdit, WM_CLEAR, 0, 0); return 0; case IDM_SELALL://全选 SendMessage (hwndEdit, EM_SETSEL, 0, -1); return 0;
case IDM FIND:/查找 SendMessage (hwndEdit,EM GETSEL, NULL,(LPARAM)&iOffset); hDlgModeless PopFindFindDlg (hwnd); return 0; case IDM NEXT:/查找下一处 SendMessage (hwndEdit,EM GETSEL,NULL,(LPARAM)&iOffset); if(PopFind ValidFind () PopFindNextText (hwndEdit,&iOffset); else hDlgModeless PopFindFindDlg (hwnd); return 0; case IDM REPLACE:/替换 SendMessage (hwndEdit,EM GETSEL,NULL,(LPARAM)&iOffset); hDlgModeless PopFindReplaceDlg (hwnd); return O;
case IDM_FIND://查找 SendMessage (hwndEdit, EM_GETSEL, NULL,(LPARAM) &iOffset); hDlgModeless = PopFindFindDlg (hwnd); return 0; case IDM_NEXT://查找下一处 SendMessage (hwndEdit, EM_GETSEL, NULL,(LPARAM) &iOffset); if (PopFindValidFind ()) PopFindNextText (hwndEdit, &iOffset); else hDlgModeless = PopFindFindDlg (hwnd); return 0; case IDM_REPLACE://替换 SendMessage (hwndEdit, EM_GETSEL, NULL,(LPARAM) &iOffset); hDlgModeless = PopFindReplaceDlg (hwnd); return 0; }