第一次写SDK的WIN32程序,照着MSDN写了半天,还好没碰到什么问题,挺顺利的就完成了,只是想不到MSDN的例子里面有的 ghInstance 全局变量是 HANDLE 类型的,结果在 DialogBox() 中就出现了类型转换出错,后来把类型改成 HINSTANCE 就没事了。
/**/ /***************************
generic.cpp
***************************/
#include < windows.h >
#include " generic.h "
LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
LRESULT WINAPI AboutDlgProc( HWND, UINT, WPARAM, LPARAM );
HINSTANCE ghInstance;
int PASCAL WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow )
... {
WNDCLASS wc;
HWND hWnd;
//单态判断
if( !hPrevInstance )
...{
wc.lpszClassName = "GenericAppClass";
wc.lpfnWndProc = MainWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
wc.lpszMenuName = "GenericAppMenu";
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
RegisterClass( &wc );
}
//获取该注册窗口的句柄
ghInstance = hInstance;
//创建窗口
hWnd = CreateWindow( "GenericAppClass",
"Generic Application",
WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//显示窗口
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
MSG msg;
BOOL bRet;
//创建信息循环
while( bRet = GetMessage( &msg, hWnd, 0, 0 ) )
...{
//判断是否获取消息返回出错状态
if( bRet == -1 )
...{
return -1;
}
else
...{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return msg.wParam;
}
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
... {
PAINTSTRUCT ps;
HDC hDC;
switch( msg )
...{
case WM_PAINT:
hDC = BeginPaint( hWnd, &ps );
TextOut( hDC, 10, 10, "Hello, World!", 13 );
EndPaint( hWnd, &ps );
break;
case WM_COMMAND:
switch( wParam )
...{
case IDM_ABOUT:
DialogBox(ghInstance, "AboutDlg", hWnd, (DLGPROC)AboutDlgProc);
//MessageBox( hWnd, "菜单激活", "Message",0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return( DefWindowProc( hWnd, msg, wParam, lParam ));
}
return 0;
}
LRESULT CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
... {
switch( uMsg )
...{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch( wParam )
...{
case IDOK:
EndDialog( hDlg, TRUE );
return TRUE;
}
break;
default:
return FALSE;
}
return FALSE;
}
/**/
/***************************
generic.h
***************************/
#define IDM_ABOUT 1000
generic.h
***************************/
#define IDM_ABOUT 1000
/**/
/***************************
generic.dlg
***************************/
1 DLGINCLUDE " generic.h "
AboutDlg DIALOG FIXED 6 , 21 , 198 , 99
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION " About Generic "
FONT 8 , " MS Shell Dlg "
BEGIN
DEFPUSHBUTTON " &OK " , IDOK, 72 , 74 , 40 , 14
LTEXT " Generic Application " , 104 , 45 , 14 ,
128 , 8
LTEXT " Written as a sample " , 105 , 45 , 35 , 59 , 8
LTEXT " Microsoft Corporation " , 106 , 45 , 45 , 98 , 8
LTEXT " Copyright (c) 1996 " , 107 , 45 ,
54 , 138 , 8
END
generic.dlg
***************************/
1 DLGINCLUDE " generic.h "
AboutDlg DIALOG FIXED 6 , 21 , 198 , 99
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION " About Generic "
FONT 8 , " MS Shell Dlg "
BEGIN
DEFPUSHBUTTON " &OK " , IDOK, 72 , 74 , 40 , 14
LTEXT " Generic Application " , 104 , 45 , 14 ,
128 , 8
LTEXT " Written as a sample " , 105 , 45 , 35 , 59 , 8
LTEXT " Microsoft Corporation " , 106 , 45 , 45 , 98 , 8
LTEXT " Copyright (c) 1996 " , 107 , 45 ,
54 , 138 , 8
END