定義
函式功能:該函式根據對話框模板資源創建一個模態的對話框。在顯示對話框之前,函式把一個應用程式定義的值作為WM_INITDIALOG訊息的IParam參數傳到對話框過程,應用程式可用此值來初始化對話框控制。函式原型:int DialogBoxParam(HINSTANCE hlnstance,LPCTSTR IpTemplateName,HWND hWndParent, DLGPROC IPDialogFunc,LPARAM dwlnitParam);
參數:
hlnstance:標識一個模組的事例,該模組的執行檔含有對話框模板。
IpTemplateName:標識對話框模板。此參數可以指向一個以NULL結尾的字元串的指針,該字元串指定對話框模扳名,或是指定對話框模板的資源標識符的一個整型值。如果此參數指定了一個資源標識符,則它的高位字一定為零,且低位字一定含有標識符。一定用MAKEINTRESOURCE宏指令創建此值。
hWndParent:指定擁有對話框的視窗。
IpDirlogFunc:指向對話框過程的指針。有關更詳細的關於對話框過程的信息,請參見DialogProc。
dwlnitaram:指定傳遞到WM_INITDIALOG訊息的IParam參數中的對話框過程的值。
返回值:如果函式調用成功則返回值為在對函式EndDialog的調用中的nResult參數,該EndDialog函式用於中止對話框。如果函式調用失敗,則返回值為C1。若想獲得錯誤信息,請調用GetLastError函式。
備註:DialogBoxParam函式用CreateWindowEx函式創建對話框。然後把一個WM_INITDIALOGG訊息傳遞到對話框過程。如果模板指定DS_SETFONT類型,DialogBoxParam函式把一個WM_SETFONT訊息傳遞到對話框過程。(不管模板是否指定WS_VISIBLE類型),函式顯示對話框使擁有視窗失效,且為對話框啟動它本身的訊息循環來檢取和傳遞訊息。當對話框應用程式調用EndDialog函式時,DialogBoxParam函式清除對話框中止訊息循環;使擁有視窗生效(如果以前有效),且返回函式EndDialog的調用中的nReSUlt參數。Windows 95和以後版本:系統可支持每個對話框模板最多為255個控制。為把多於255個控制放入對話框,可以在WM_INITDLALOG訊息處理器中創建控制,而不是把它們放入模板中。Windows CE:IPTemplateName參數指向的對話框模板中DLGTEMPLATE結構並不支持所有的類型。
速查:Windows NT:3.1及以上版本:Windows:95及以上版本;Windows CE:1.0及以上版本;頭檔案:Winuser.h;庫檔案:USer32.lib:Unicode:在Windows NT上實現為Unicode和ANSI兩種版本。
win32編程實例
FirstWindow.cpp#include "Windows.h"
#include "tchar.h"
TCHAR ClassName[] = _T("DLGCLASS");
TCHAR MenuName[] = _T("MyMenu");
TCHAR DlgName[] = _T("MyDialog");
TCHAR AppName[] = _T("Our First Dialog Box");
TCHAR TestString[] = _T("Wow! I'm in an edit box now");
TCHAR buffer[512];
HINSTANCE g_hInstance;
#define IDC_EDIT 3000
#define IDC_BUTTON 3001
#define IDC_EXIT 3002
#define IDM_GETTEXT 32000
#define IDM_CLEAR 32001
#define IDM_EXIT 32002
LRESULT CALLBACK ProcWinMain( HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch(Msg)
{
case WM_CREATE:
{
SetDlgItemText(hWnd,IDC_EDIT,AppName);
}
break;
case WM_COMMAND:
{
if(lParam == 0)
{
switch(LOWORD(wParam))
{
case IDM_GETTEXT:
GetDlgItemText(hWnd,IDC_EDIT,buffer,512);
MessageBox(NULL,buffer,AppName,MB_OK);
break;
case IDM_CLEAR:
SetDlgItemText(hWnd,IDC_EDIT,NULL);
break;
default:
DestroyWindow(hWnd);
break;
}
}
else
{
if(HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case IDC_BUTTON:
SetDlgItemText(hWnd,IDC_EDIT,TestString);
break;
case IDC_EXIT:
SendMessage(hWnd,WM_COMMAND,IDM_EXIT,0);
break;
}
}
}
}
break;
case WM_DESTROY:
{
PostQuitMessage(NULL);
}
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam );
}
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
HICON hIcon;
HCURSOR hCursor;
MSG stMsg;
WNDCLASSEX stdWndClass;
HWND hDlg;
g_hInstance = hInstance;
RtlZeroMemory(&stdWndClass, sizeof(stdWndClass));
stdWndClass.hCursor = LoadCursor(0,IDC_ARROW);
stdWndClass.cbSize = sizeof(stdWndClass);
stdWndClass.style = CS_HREDRAW|CS_VREDRAW;
stdWndClass.lpfnWndProc = ProcWinMain;
stdWndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
stdWndClass.lpszClassName = ClassName;
stdWndClass.hInstance = hInstance;
stdWndClass.lpszMenuName = MenuName;
stdWndClass.cbClsExtra = NULL;
stdWndClass.cbWndExtra = DLGWINDOWEXTRA;
hIcon = LoadIcon(NULL,IDI_APPLICATION);
stdWndClass.hIcon = hIcon;
stdWndClass.hIconSm = hIcon;
hCursor = LoadCursor(NULL,IDC_ARROW);
stdWndClass.hCursor = hCursor;
RegisterClassEx(&stdWndClass);
hDlg = CreateDialogParam(hInstance,DlgName,NULL,NULL,NULL);
if(!hDlg)
return 0;
SetFocus(GetDlgItem(hDlg,IDC_EDIT));
ShowWindow(hDlg,SW_SHOWNORMAL);
UpdateWindow(hDlg);
while(GetMessage(&stMsg,NULL,0,0))
{
if(!IsDialogMessage(hDlg,&stMsg))
{
TranslateMessage(&stMsg);
DispatchMessage(&stMsg);
}
}
return stMsg.wParam;
}
rsrc.rc
//#include "resource.h"
#include
#define IDC_EDIT 3000
#define IDC_BUTTON 3001
#define IDC_EXIT 3002
#define IDM_GETTEXT 32000
#define IDM_CLEAR 32001
#define IDM_EXIT 32003
MyDialog DIALOG 10, 10, 205, 60
STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "Our First Dialog Box"
CLASS "DLGCLASS"
BEGIN
EDITTEXT IDC_EDIT, 15,17,111,13, ES_AUTOHSCROLL | ES_LEFT
DEFPUSHBUTTON "Say Hello", IDC_BUTTON, 141,10,52,13
PUSHBUTTON "Eξt", IDC_EXIT, 141,26,52,13, WS_GROUP
END
MyMenu MENU
BEGIN
POPUP "Test Controls"
BEGIN
MENUITEM "Get Text", IDM_GETTEXT
MENUITEM "Clear Text", IDM_CLEAR
MENUITEM "", , 0x0800 /*MFT_SEPARATOR*/
MENUITEM "Eξt", IDM_EXIT
END
END