第八講對話框_第1頁
第八講對話框_第2頁
第八講對話框_第3頁
第八講對話框_第4頁
第八講對話框_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、對話框?qū)υ捒蚴菓?yīng)用程序創(chuàng)建的用來獲取用戶輸入的一個臨時窗口。應(yīng)用程序一般用對話框來提示用戶輸入命令的附加信息。如果有很多輸入超出了菜單可以處理的程度,那么我們可以使用對話框來取得輸入信息。對話框的一般形式是包含多種子窗口控件的彈出式窗口,這些控件的大小和位置在程序資源描述文件的對話框模板中指定。雖然程序員能夠手工定義對話框模板,但是現(xiàn)在通常是在Visual C+ Developer Studio中以交互式操作的方式設(shè)計(jì)的,然后由Developer Studio建立對話框模板。當(dāng)程序調(diào)用依據(jù)模板建立的對話框時,Windows 系統(tǒng)負(fù)責(zé)建立彈出式對話框窗口和子窗口控件,并提供處理對話框消息(包括所

2、有鍵盤和鼠標(biāo)輸入)的窗口過程。完成這些功能的windows內(nèi)部代碼被稱為對話框管理器。Windows的內(nèi)部對話框窗口消息處理程序所處理的許多消息也傳遞給程序員所編寫程序中的函數(shù),這個函數(shù)即是所謂的對話框過程或者對話過程。對話過程與普通的窗口過程程序類似,但是也存在著一些重要區(qū)別。一般來說,除了在建立對話框時初始化子窗口控件,處理來自子窗口控件的消息以及結(jié)束對話框之外,程序員不需要再給對話框過程增加其它功能。對話過程通常不處理WM_PAINT消息,也不直接處理鍵盤和鼠標(biāo)輸入。對話框的類型:模態(tài)對話框:在應(yīng)用程序能繼續(xù)運(yùn)行之前,要求用戶必須顯式地提供信息或取消對話框,對話框不能在同一應(yīng)用程序的其它

3、窗口之間進(jìn)行切換,把模態(tài)對話框與不輸入附加信息就不能繼續(xù)執(zhí)行的命令一起使用。非模態(tài)對話框:允許用戶提供信息并返回前一任務(wù),但不關(guān)閉該對話框。模態(tài)對話框通過調(diào)用單個函數(shù)就可以創(chuàng)建、執(zhí)行它們的任務(wù)和銷毀,因此比無模態(tài)對話框更容易管理。無論是創(chuàng)建模態(tài)對話框還是非模態(tài)對話框,應(yīng)用程序都必須提供一個對話框模板來描述對話框的樣式和內(nèi)容,并提供一個對話框過程來完成任務(wù)。應(yīng)用程序一般用DialogBox或CreateDialog函數(shù)來創(chuàng)建對話框。DialogBox用來創(chuàng)建模態(tài)對話框,CreateDialog用來創(chuàng)建無模態(tài)對話框。通用對話框:為實(shí)現(xiàn)標(biāo)準(zhǔn)用戶界面,提供統(tǒng)一、通用的功能,而實(shí)現(xiàn)的一組增強(qiáng)對話框;開發(fā)

4、人員可以根據(jù)應(yīng)用程序的特點(diǎn),對已有的這些通用對話框進(jìn)行定制擴(kuò)展。每一個對話框都有一個屬主窗口,應(yīng)用程序在創(chuàng)建對話框時應(yīng)指定一個屬主窗口。若屬主窗口被隱藏或銷毀,對話框同時被隱藏或銷毀。一個對話框可以沒有屬主窗口,但不建議這樣使用,例如一個模態(tài)對話框沒有屬主,Windows就不會使其他窗口無效,這樣就達(dá)不到使用模態(tài)對話框的目的。如果一個非模態(tài)對話框沒有屬主,那么當(dāng)應(yīng)用程序的其他窗口被隱藏或銷毀時,Windows不會隱藏和銷毀該對話框,這就要求應(yīng)用程序完成特殊的處理工作,以便在合適的時機(jī)隱藏和銷毀該對話框。消息框:消息框也是一個對話框,但是Windows完全控制了消息框的創(chuàng)建和管理。這就意味著應(yīng)用

5、程序不用提供對話框模板和對話框過程。應(yīng)用程序在創(chuàng)建該對話框時還可以指定MB_SYSTEMMODA來彈出一個系統(tǒng)消息框,這個消息框會掛起所有運(yùn)行中的應(yīng)用程序,用來向用戶提出嚴(yán)重警告等信息。模態(tài)對話框:模態(tài)對話框是一個彈出窗口,包含System菜單、標(biāo)題欄和粗邊界。對于模態(tài)對話框WS_VISIBLE風(fēng)格沒有效果。應(yīng)用程序決不能創(chuàng)建一個具有WS_CHILD樣式的模態(tài)對話框,具有這種風(fēng)格的模式對話框?qū)⑹棺约簾o效,從而使應(yīng)用程序不能繼續(xù)。應(yīng)用程序調(diào)用DialogBox或者DialogBoxIndirect就可以創(chuàng)建模態(tài)對話框。所不同處在于DialogBox要求一個模板資源名,而DialogBoxIndi

6、rect要求一個模板的內(nèi)存對象句柄。模態(tài)對話框一創(chuàng)建,Windows自動將它變成活動窗口。屬主窗口以及其子窗口都被變成無效的。當(dāng)模態(tài)對話框活動時,用戶或應(yīng)用程序都不應(yīng)使屬主窗口成為活動的。為了處理模態(tài)對話框的消息,Windows啟動一個臨時消息環(huán)接管整個應(yīng)用程序消息隊(duì)列。如果Windows收到一條明顯不是對話框的消息,就把它發(fā)送給正確的窗口(非排隊(duì)消息)。如果發(fā)現(xiàn)一條WM_QUIT消息,則把它投遞給應(yīng)用程序的消息隊(duì)列,以便使應(yīng)用程序主消息環(huán)最終能夠收到該消息。只要應(yīng)用程序的消息隊(duì)列為空,Windows就把WM_ENTERIDLE消息發(fā)送給屬主窗口,使得應(yīng)用程序能夠在對話框仍然存在的情況下進(jìn)行后

7、臺任務(wù)處理。但是這樣并不理想,可以使用PeekMessage來交出控制權(quán),也可以對對話框使用DS_NOIDLEMSG風(fēng)格來禁止Windows發(fā)送該消息。使用EndDialog來終止一個對話框,并且返回一個值來指明是否成功的完成了任務(wù)。模態(tài)對話框的內(nèi)部實(shí)現(xiàn)原理DialogBox/#define DialogBox DialogBoxParamDialogBoxParam逐層向上查找祖先窗口,直到找到一個具有popup或overlapped風(fēng)格的窗 口作為父窗口 禁用父窗口 /父窗口不再處理鼠標(biāo)和鍵盤消息 CreateDialogParam /創(chuàng)建模態(tài)對話框(父、子窗口)SetWindowWord

8、(hwndDlg, DWL_ENDDIALOGCALLED, FALSE);/設(shè)置 私有數(shù)據(jù)while(!GetWindowWord(hwndDlg, DWL_ENDDIALOGCALLED)/有自己的消息環(huán)GetMessage(&msg, NULL, 0, 0);if (!IsDialogMessage(hwndDlg, &msg) TranslateMessage(&msg);DispatchMessage(&msg);DestroyWindow(hwndDlg); /銷毀窗口結(jié)束模態(tài)對話框:EndDialog獲取父窗口句柄 啟用父窗口 /父窗口繼續(xù)處理鼠標(biāo)和鍵盤消息 SetWindowL

9、ong(hwnd, DWL_ENDDIALOGCALLED, TRUE); /設(shè)置私有數(shù)據(jù)SetWindowLong(hwnd, DWL_DLGRESULT, nResult); /設(shè)置返回值 模態(tài)對話框舉例:You create a modal dialog box by using the DialogBox function. You must specify the identifier or name of a dialog box template resource and a pointer to the dialog box procedure. The DialogBox f

10、unction loads the template, displays the dialog box, and processes all user input until the user closes the dialog box. In the following example, the application displays a modal dialog box when the user clicks Delete Item from an application menu. The dialog box contains an edit control (in which t

11、he user enters the name of an item) and OK and Cancel buttons. The control identifiers for these controls are ID_ITEMNAME, IDOK, and IDCANCEL, respectively. The first part of the example consists of the statements that create the modal dialog box. These statements, in the window procedure for the ap

12、plications main window, create the dialog box when the system receives a WM_COMMAND message having the IDM_DELETEITEM menu identifier. The second part of the example is the dialog box procedure, which retrieves the contents of the edit control and closes the dialog box upon receiving a WM_COMMAND me

13、ssage. The following statements create the modal dialog box. The dialog box template is a resource in the applications executable file and has the resource identifier DLG_DELETEITEM: case WM_COMMAND: switch (LOWORD(wParam) case IDM_DELETEITEM: if (DialogBox(hinst, MAKEINTRESOURCE(DLG_DELETEITEM), hw

14、nd, (DLGPROC)DeleteItemProc)=IDOK) / Complete the command; szItemName contains the / name of the item to delete. else / Cancel the command. break; return 0L; In this example, the application specifies its main window as the owner window for the dialog box. When the system initially displays the dial

15、og box, its position is relative to the upper left corner of the owner windows client area. The application uses the return value from DialogBox to determine whether to proceed with the operation or cancel it. The following statements define the dialog box procedure. char szItemName80; / receives na

16、me of item to delete. BOOL CALLBACK DeleteItemProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) switch (message) case WM_COMMAND: switch (LOWORD(wParam) case IDOK: if (!GetDlgItemText(hwndDlg, ID_ITEMNAME, szItemName, 80) *szItemName=0; / Fall through. case IDCANCEL: EndDialog(hwndDlg,

17、wParam); return TRUE; return FALSE; In this example, the procedure uses GetDlgItemText to retrieve the current text from the edit control identified by ID_ITEMNAME. The procedure then calls the EndDialog function to set the dialog boxs return value to either IDOK or IDCANCEL, depending on the messag

18、e received, and to begin the process of closing the dialog box. The IDOK and IDCANCEL identifiers correspond to the OK and Cancel buttons. After the procedure calls EndDialog, the system sends additional messages to the procedure to destroy the dialog box and returns the dialog boxs return value bac

19、k to the function that created the dialog box. 非模態(tài)對話框:使用CreateDialog或CreateDialogIndirect創(chuàng)建非模態(tài)對話框。這兩個函數(shù)返回對話框的句柄。非模態(tài)對話框既不使屬主窗口無效,也不給他發(fā)送消息。應(yīng)用程序要負(fù)責(zé)為對話框撿取和分發(fā)輸入消息(共用一個消息隊(duì)列)。為了允許用戶用鍵盤移動到并選擇控制框,應(yīng)用程序必須調(diào)用IsDialogMessage來判斷消息是否來自對話框。應(yīng)用程序通過DestroyWindow來銷毀一個無模式對話框。CreateDialog函數(shù)內(nèi)部利用CreateWindowEx創(chuàng)建非模態(tài)對話框,然后向?qū)υ?/p>

20、框窗口過程發(fā)送WM_INITDIALOG消息,如果在對話框模板中指定了WS_VISIBLE風(fēng)格,CreateDialog函數(shù)就馬上顯示這個新創(chuàng)建的對話框,最后返回對話框的窗口句柄。 非模態(tài)對話框的內(nèi)部實(shí)現(xiàn)原理CreateDialog/#define CreateDialog CreateDialogParamCreateDialogParamFindResource /在exe文件中查找并定位對話框模板LoadResource CreateDialogIndirectParam /根據(jù)內(nèi)存塊中的對話框模 /板創(chuàng)建父、子窗口FreeResource返回創(chuàng)建的對話框窗口句柄 /注意與模態(tài)對話框的區(qū)

21、別 修改創(chuàng)建此對話框線程的消息環(huán) while(GetMessage(&msg, NULL, 0, 0) if (hDlgModeless = 0 & !IsDialogMessage(hDlgModeless, &msg) if(!TranslateAccelerator(hwnd, hAccel, &msg) TranslateMessage(&msg); DispatchMessage(& 非模態(tài)對話框的銷毀 case WM_CLOSE: DestroyWindow(); hDlgModeless = NULL; break; 對話框過程:對話框過程類似窗口過程,但并不一樣。對話框過程處理

22、一些程序設(shè)計(jì)者需要處理的消息,并返回TRUE,對于他不處理的消息,返回FALSE,由Windows傳遞給默認(rèn)的窗口過程處理。對話框過程不應(yīng)當(dāng)調(diào)用DefWindowProc。絕大部分情況下,對話框過程通常只需要處理如下兩種消息:WM_INITDIALOG:是對話框過程函數(shù)在對話框被顯示之前接收到的消息,它是對話框過程函數(shù)接收到的第一個消息,用于完成初始化工作。WM_COMMAND:在對話框過程函數(shù)中處理來自子控件的消息。非模態(tài)對話框舉例:You create a modeless dialog box by using the CreateDialog function, specifying

23、the identifier or name of a dialog box template resource and a pointer to the dialog box procedure. CreateDialog loads the template, creates the dialog box, and optionally displays it. Your application is responsible for retrieving and dispatching user input messages to the dialog box procedure. In

24、the following example, the application displays a modeless dialog box if it is not already displayed when the user clicks Go To from an application menu. The dialog box contains an edit control, a check box, and OK and Cancel buttons. The dialog box template is a resource in the applications executa

25、ble file and has the resource identifier DLG_GOTO. The user enters a line number in the edit control and checks the check box to specify that the line number is relative to the current line. The control identifiers are ID_LINE, ID_ABSREL, IDOK, and IDCANCEL. The statements in the first part of the e

26、xample create the modeless dialog box. These statements, in the window procedure for the applications main window, create the dialog box when the window procedure receives a WM_COMMAND message having the IDM_GOTO menu identifier, but only if the global variable hwndGoto does not already contain a va

27、lid handle. The second part of the example is the applications main message loop. The loop includes the IsDialogMessage function to ensure that the user can use the dialog box keyboard interface in this modeless dialog box. The third part of the example is the dialog box procedure. The procedure ret

28、rieves the contents of the edit control and check box when the user clicks the OK button. The procedure destroys the dialog box when the user clicks the Cancel button. HWND hwndGoto = NULL; / window handle of dialog box . . . case WM_COMMAND: switch (LOWORD(wParam) case IDM_GOTO: if (!IsWindow(hwndG

29、oto) hwndGoto = CreateDialog(hinst, MAKEINTRESOURCE(DLG_GOTO), hwnd, (DLGPROC) GoToProc); ShowWindow(hwndGoto, SW_SHOW); break; return 0L; In the preceding statements, CreateDialog is called only if hwndGoto does not contain a valid window handle. This ensures that the application does not display t

30、wo dialog boxes at the same time. To support this method of checking, the dialog procedure must set hwndGoto to NULL when it destroys the dialog box. The message loop for an application consists of the following statements: BOOL bRet;while ( (bRet = GetMessage(&msg, NULL, 0, 0) != 0 ) if (bRet = -1

31、) / handle the error and possibly exit else if (!IsWindow(hwndGoto) | !IsDialogMessage(hwndGoto, &msg) TranslateMessage(&msg); DispatchMessage(&msg); The loop checks the validity of the window handle for the dialog box and only calls the IsDialogMessage function if the handle is valid. IsDialogMessa

32、ge only processes the message if it belongs to the dialog box. Otherwise, it returns FALSE and the loop dispatches the message to the appropriate window. The following statements define the dialog box procedure: int iLine; / receives line number BOOL fRelative; / receives check box status BOOL CALLB

33、ACK GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) BOOL fError; switch (message) case WM_INITDIALOG: CheckDlgButton(hwndDlg, ID_ABSREL, fRelative); return TRUE; case WM_COMMAND: switch (LOWORD(wParam) case IDOK: fRelative = IsDlgButtonChecked(hwndDlg, ID_ABSREL); iLine = GetDlgIt

34、emInt(hwndDlg, ID_LINE, &fError, fRelative); if (fError) MessageBox(hwndDlg, SZINVALIDNUMBER, SZGOTOERR, MB_OK); SendDlgItemMessage(hwndDlg, ID_LINE, EM_SETSEL, 0, -1L); else / Notify the owner window to carry out the task. return TRUE; case IDCANCEL: DestroyWindow(hwndDlg); hwndGoto = NULL; return

35、TRUE; return FALSE; In the preceding statements, the procedure processes the WM_INITDIALOG and WM_COMMAND messages. During WM_INITDIALOG processing, the procedure initializes the check box by passing the current value of the global variable fRelative to CheckDlgButton. The procedure then returns TRU

36、E to direct the system to set the default input focus. During WM_COMMAND processing, the procedure closes the dialog box only if the user clicks the Cancel button that is, the button having the IDCANCEL identifier. The procedure must call DestroyWindow to close a modeless dialog box. Notice that the

37、 procedure also sets the hwndGoto variable to NULL to ensure that other statements that depend on this variable operate correctly. If the user clicks the OK button, the procedure retrieves the current state of the check box and assigns it to the fRelative variable. It then uses the variable to retri

38、eve the line number from the edit control. GetDlgItemInt translates the text in the edit control into an integer. The value of fRelative determines whether the function interprets the number as a signed or unsigned value. If the edit control text is not a valid number, GetDlgItemInt sets the value o

39、f the fError variable to nonzero. The procedure checks this value to determine whether to display an error message or carry out the task. In the event of an error, the dialog box procedure sends a message to the edit control, directing it to select the text in the control so that the user can easily

40、 replace it. If GetDlgItemInt does not return an error, the procedure can either carry out the requested task itself or send a message to the owner window, directing it to carry out the operation. 模態(tài)對話框與非模態(tài)對話框的區(qū)別 模態(tài)非模態(tài)無標(biāo)題欄和系統(tǒng)菜單有標(biāo)題欄和系統(tǒng)菜單創(chuàng)建對話框:DialogBox創(chuàng)建對話框:CreateDialog不允許在同一應(yīng)用程序的窗口之間切換允許在同一應(yīng)用程序的窗口之間

41、切換對話框消息不經(jīng)過應(yīng)用程序消息隊(duì)列對話框消息經(jīng)過應(yīng)用程序消息隊(duì)列結(jié)束對話框:EndDialog()結(jié)束對話框:DestroyWindow()通用對話框:打開 GetOpenFileName(LPOPENFILENAME lpofn)保存 GetSaveFileName(LPOPENFILENAME lpofn)查找 FindText(LPFINDREPLACE lpfr)替換 ReplaceText(LPFINDREPLACE lpfr)頁面設(shè)置 PageSetupDlg(LPPAGESETUPDLG lppsd)打印 PrintDlg(LPPRINTDLG lppd)、PrintDlgEx(LPPRINTDLGEX lppd)字體 ChooseColor(LPCHOOSECOLOR lpcc)顏色 ChooseFont(LPCHOOSEFONT lpcf) 這些通用對話框由相應(yīng)的函數(shù)調(diào)用打開,為了使用這些函數(shù),都要初始化某一結(jié)構(gòu)的各個字段,并將該結(jié)構(gòu)的指針傳送給通用對話框的某個函數(shù),該函數(shù)創(chuàng)建并顯示通用對話框。當(dāng)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論