您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > 吉林大学远程教育课件--WindowsAPI编
吉林大学远程教育课件WindowsAPI编程主讲人:翟慧杰学时:48第三十讲)//函数:WndProc//作用:主窗口消息循环LRESULTCALLBACKWndProc(HWNDhwnd,UINTiMsg,WPARAMwParam,LPARAMlParam){staticBOOLbNeedSave=FALSE;//是否保存文件的标志staticcharszFileName[_MAX_PATH];//文件名staticcharszTitleName[_MAX_FNAME+_MAX_EXT];staticHINSTANCEhInst;//应用程序句柄staticHWNDhwndEdit;//编辑框句柄staticintiOffset;staticUINTiMsgFindReplace;LPFINDREPLACEpfr;switch(iMsg){caseWM_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,0L);PopFileInitialize(hwnd);//初始化ofn/注册消息FINDMSGSTRINGiMsgFindReplace=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);return0;caseWM_SETFOCUS:SetFocus(hwndEdit);//使编辑框获得焦点return0;caseWM_SIZE:MoveWindow(hwndEdit,0,0,LOWORD(lParam),HIWORD(lParam),TRUE);return0;caseWM_COMMAND://菜单命令if(lParam&&LOWORD(wParam)==EDITID){switch(HIWORD(wParam)){caseEN_UPDATE:bNeedSave=TRUE;return0;caseEN_ERRSPACE:caseEN_MAXTEXT:MessageBox(hwnd,编辑框超出边界!.,szAppName,MB_OK|MB_ICONSTOP);return0;}break;}switch(LOWORD(wParam)){//来自文件菜单的消息caseIDM_NEW://新建if(bNeedSave&&IDCANCEL==AskAboutSave(hwnd,szTitleName))return0;SetWindowText(hwndEdit,\0);szFileName[0]='\0';szTitleName[0]='\0';DoCaption(hwnd,szTitleName);bNeedSave=FALSE;return0;caseIDM_OPEN://打开文件if(bNeedSave&&IDCANCEL==AskAboutSave(hwnd,szTitleName))return0;//弹出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;return0;caseIDM_SAVE://保存if(szFileName[0]){//写文件if(PopFileWrite(hwndEdit,szFileName)){bNeedSave=FALSE;return1;}elseOkMessage(hwnd,不能写文件%s,szTitleName);return0;}caseIDM_SAVEAS://另存为//弹出SaveAS对话框if(PopFileSaveDlg(hwnd,szFileName,szTitleName)){DoCaption(hwnd,szTitleName);if(PopFileWrite(hwndEdit,szFileName)){bNeedSave=FALSE;return1;}elseOkMessage(hwnd,不能写文件%s,szTitleName);}return0;caseIDM_EXIT://结束SendMessage(hwnd,WM_CLOSE,0,0);return0;caseIDM_UNDO://撤消SendMessage(hwndEdit,WM_UNDO,0,0);return0;caseIDM_CUT://剪切SendMessage(hwndEdit,WM_CUT,0,0);return0;caseIDM_COPY://复制SendMessage(hwndEdit,WM_COPY,0,0);return0;caseIDM_PASTE://粘贴SendMessage(hwndEdit,WM_PASTE,0,0);return0;caseIDM_CLEAR://清空SendMessage(hwndEdit,WM_CLEAR,0,0);return0;caseIDM_SELALL://全选SendMessage(hwndEdit,EM_SETSEL,0,-1);return0;caseIDM_FIND://查找SendMessage(hwndEdit,EM_GETSEL,NULL,(LPARAM)&iOffset);hDlgModeless=PopFindFindDlg(hwnd);return0;caseIDM_NEXT://查找下一处SendMessage(hwndEdit,EM_GETSEL,NULL,(LPARAM)&iOffset);if(PopFindValidFind())PopFindNextText(hwndEdit,&iOffset);elsehDlgModeless=PopFindFindDlg(hwnd);return0;caseIDM_REPLACE://替换SendMessage(hwndEdit,EM_GETSEL,NULL,(LPARAM)&iOffset);hDlgModeless=PopFindReplaceDlg(hwnd);return0;}break;caseWM_CLOSE:if(!bNeedSave||IDCANCEL!=AskAboutSave(hwnd,szTitleName))DestroyWindow(hwnd);return0;caseWM_QUERYENDSESSION:if(!bNeedSave||IDCANCEL!=AskAboutSave(hwnd,szTitleName))return1;return0;caseWM_DESTROY:PostQuitMessage(0);return0;default://处理Find-Replace消息if(iMsg==iMsgFindReplace){pfr=(LPFINDREPLACE)lParam;if(pfr-Flags&FR_DIALOGTERM)hDlgModeless=NULL;if(pfr-Flags&FR_FINDNEXT)if(!PopFindFindText(hwndEdit,&iOffset,pfr))OkMessage(hwnd,文本没找到!,\0);if(pfr-Flags&FR_REPLACE||pfr-Flags&FR_REPLACEALL)if(!PopFindReplaceText(hwndEdit,&iOffset,pfr))OkMessage(hwnd,文本没找到!,\0);if(pfr-Flags&FR_REPLACEALL)while(PopFindReplaceText(hwndEdit,&iOffset,pfr));return0;}break;}returnDefWindowProc(hwnd,iMsg,wParam,lParam);}//函数:PopFileInitialize//作用:初始化全局变量ofnvoidPopFileInitialize(HWNDhwnd){staticcharszFilter[]=文本文件(*.TXT)\0*.txt\0\ASCII文件(*.ASC)\0*.asc\0\所有文件(*.*)\0*.*\0\0;ofn.lStructSize=sizeof(OPENFILENAME);ofn.hwndOwner=hwnd;ofn.hInstance=NULL;ofn.lpstrFilter=szFilter;ofn.lpstrCustomFilter=NULL;ofn.nMaxCustFilter=0;ofn.nFilterIndex=0;ofn.lpstrFile=NULL;ofn.nMaxFile=_MAX_PATH;ofn.lpstrFileTitle=NULL;ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;ofn.lpstrInitialDir=NULL;ofn.lpstrTitle=NULL;ofn.Flags=0;ofn.nFileOffset=0;ofn.nFileExtension=0;ofn.lpstrDefExt=txt;ofn.lCustData=0L;ofn.lpfnHook=NULL;ofn.lpTemplateName=NULL;}//函数:PopFileOpenDlg//作用:弹出Open对话框BOOLPopFileOpenDlg(HWNDhwnd,PSTRpstrFileName,PSTRpstrTitleName){ofn.hwndOwner=hwnd;ofn.lpstrFile=pstrFileName;ofn.lpstrFileTitle=pstrTitleName;ofn.Flags=OFN_HIDEREADONLY;returnGetOpenFileName(&ofn);//弹出Open对话框}//函数:PopFileSaveDlg//作用:弹出SaveAs对话框BOOLPopFileSaveDlg(HWNDhwnd,PSTRpstrFileName,PSTRpstrTitleName){ofn.hwndOwner=hwnd;ofn.lpstrFile=pstrFileName;ofn.lpstrFileTitle=pstrTitleName;ofn.Flags=OFN_OVERWRITEPROMPT;returnGetSaveFileName(&ofn);}//函数:PopFileLength//作用:计算文件长度staticlongPopFileL
本文标题:吉林大学远程教育课件--WindowsAPI编
链接地址:https://www.777doc.com/doc-1085208 .html