以前一直用的书上的代码,今天试了下WinMain里直接DialogBox也行。省去了那些retister class什么的,代码如下:#include <windows.h>
#include "resource.h"LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
DialogBox( hInstance, ( LPCTSTR ) IDD_DIALOG1, NULL, ( DLGPROC ) WndProc ); return 0;
}
LRESULT CALLBACK WndProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch(uMsg){
  case WM_INITDIALOG:
{
}
break;
  case WM_COMMAND:
switch(LOWORD( wParam )) {
  case IDOK:
{
EndDialog( hDlg, 0 );
}
break;
};
break;
  case WM_CLOSE:
EndDialog( hDlg, 0 );
break;
};
return 0;
}比起先register class,CreateWindow,然后GetMessage,TranslateMessage,DispatchMessage , 直接把所有工作交给DialogBox不是更方便?请问这样做有什么缺点?

解决方案 »

  1.   

    没什么缺点。这样写比自己注册类简单一点,比用MFC麻烦。主要看个人爱好。
      

  2.   

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
        DialogBox( hInstance, ( LPCTSTR ) IDD_DIALOG1, NULL, ( DLGPROC ) WndProc );    return 0;
    }
    =================================================================================================
    Windows uses its own internal window procedure to process messages to a dialog box window. Windows then passes these messages to a dialog box procedure within the program that creates the dialog box. so application cannot catch such as WM_KEYUP,WM_KEYDOWN messages in its own DialogProc.
    ( try to catch them)