模式对话框程序:
LRESULT CALLBACK DialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
//MoveWindow(hWnd,100,100,500,300,FALSE);
SetWindowPos(hWnd,NULL,200,200,0,0,SWP_NOSIZE);
return TRUE; // 表示已经初始化
case WM_COMMAND:
if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hWnd,TRUE); //只用于模式对话框
return TRUE;
}
break;
}
return FALSE;//消息没有被处理,交给父窗口继续处理
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR lpCmdLine, int nShowCmd)
{
DialogBox(hInstance, (LPCTSTR)(IDD_DIALOG1), NULL, (DLGPROC)DialogProc);//IDD_DIALOG1是对话框资源的ID
return 0;
}
非模式对话框程序:
LRESULT CALLBACK DialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
//MoveWindow(hWnd,100,100,500,300,FALSE);
SetWindowPos(hWnd,NULL,200,200,0,0,SWP_NOSIZE);
return TRUE; // 表示已经初始化
case WM_COMMAND:
if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
PostQuitMessage(0);
return TRUE;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
//可以试试加上此段代码的效果!
//default:
// return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return FALSE;//消息没有被处理,交给父窗口继续处理
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR lpCmdLine, int nShowCmd)
{
HWND hDialog =
CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc);
ShowWindow(hDialog, SW_SHOW);
UpdateWindow(hDialog);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}