我的第一个WIN32 Platform SDK 程序

    第一次写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, 00 ) ) 
    
{
        
//判断是否获取消息返回出错状态
        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, 
1010"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.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值