//  INCLUDES  ////////////////////////////////////////////// /
#define  WIN32_LEAN_AND_MEAN   //  just say no to MFC

#include 
< windows.h >     //  include all the windows headers
#include  < windowsx.h >    //  include useful macros
#include  < stdio.h >      
#include 
< math.h >

//  DEFINES  ////////////////////////////////////////////////

//  defines for windows 
#define  WINDOW_CLASS_NAME "WINCLASS1"

//  GLOBALS  ////////////////////////////////////////////////


//  FUNCTIONS  //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, 
                            UINT msg, 
                            WPARAM wparam, 
                            LPARAM lparam)
{
    
//  this is the main message handler of the system
    PAINTSTRUCT        ps;         //  used in WM_PAINT
    HDC                hdc;     //  handle to a device context

    
//  what is the message 
     switch (msg)
    
{    
    
case  WM_CREATE: 
        
{
            
//  do initialization stuff here

            
//  return success
             return ( 0 );
        }
  break ;

    
case  WM_PAINT: 
        
{
            
//  simply validate the window
            hdc  =  BeginPaint(hwnd, & ps);     
            
//  you would do all your painting here
            EndPaint(hwnd, & ps);

            
//  return success
             return ( 0 );
        }
  break ;

    
case  WM_DESTROY: 
        
{
            
//  kill the application, this sends a WM_QUIT message 
            PostQuitMessage( 0 );

            
//  return success
             return ( 0 );
        }
  break ;

    
default : break ;

    }
  //  end switch

    
//  process any messages that we didn't take care of 
     return  (DefWindowProc(hwnd, msg, wparam, lparam));

}
  //  end WinProc

//  WINMAIN  ////////////////////////////////////////////////
int  WINAPI WinMain(    HINSTANCE hinstance,
                   HINSTANCE hprevinstance,
                   LPSTR lpcmdline,
                   
int  ncmdshow)
{

    WNDCLASSEX winclass; 
//  this will hold the class we create
    HWND       hwnd;      //  generic window handle
    MSG           msg;          //  generic message

    
//  first fill in the window class stucture
    winclass.cbSize          =   sizeof (WNDCLASSEX);
    winclass.style            
=  CS_DBLCLKS  |  CS_OWNDC  |  
        CS_HREDRAW 
|  CS_VREDRAW;
    winclass.lpfnWndProc    
=  WindowProc;
    winclass.cbClsExtra        
=   0 ;
    winclass.cbWndExtra        
=   0 ;
    winclass.hInstance        
=  hinstance;
    winclass.hIcon            
=  LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor        
=  LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground    
=  (HBRUSH)GetStockObject(BLACK_BRUSH);
    winclass.lpszMenuName    
=  NULL;
    winclass.lpszClassName    
=  WINDOW_CLASS_NAME;
    winclass.hIconSm        
=  LoadIcon(NULL, IDI_APPLICATION);

    
//  register the window class
     if  ( ! RegisterClassEx( & winclass))
        
return ( 0 );

    
//  create the window
     if  ( ! (hwnd  =  CreateWindowEx(NULL,                   //  extended style
        WINDOW_CLASS_NAME,      //  class
         " Your Basic Window++ " //  title
        WS_OVERLAPPEDWINDOW  |  WS_VISIBLE,
        
0 , 0 ,         //  initial x,y
         400 , 400 ,   //  initial width, height
        NULL,         //  handle to parent 
        NULL,         //  handle to menu
        hinstance, //  instance of this application
        NULL)))     //  extra creation parms
         return ( 0 );

    
//  enter main event loop, but this time we use PeekMessage()
    
//  instead of GetMessage() to retrieve messages
     while (TRUE)
    
{
        
//  test if there is a message in queue, if so get it
         if  (PeekMessage( & msg,NULL, 0 , 0 ,PM_REMOVE))
        

            
//  test if this is a quit
             if  (msg.message  ==  WM_QUIT)
                
break ;

            
//  translate any accelerator keys
            TranslateMessage( & msg);

            
//  send the message to the window proc
            DispatchMessage( & msg);
        }
  //  end if

        
//  main game processing goes here
        
//  Game_Main();  //  or whatever your loop is called
    }
  //  end while

    
//  return to Windows like this
     return (msg.wParam);

}
  //  end WinMain

一  include头文件和宏定义
#define WIN32_LEAN_AND_MEAN  // 不使用mfc
#include 
<windows.h>   // 包含所有的windows头文件,
#include <windowsx.h>  //包含有用的宏定义
二 winmain()函数
int WINAPI WinMain(    HINSTANCE hinstance,
                   HINSTANCE hprevinstance,
                   LPSTR lpcmdline,
                   
int ncmdshow);
函数原型如上,其中hinstance是windows为应用程序生成的句柄,hprevinstance参数现在一般不用,用来向以前的兼容,lpcmdline就相当于dos程序的命令行参数,ncmdshow枚举类型指出如何打开主应用程序窗口,比如最大化,最小化,最前端等
WINAPI 不能少,相当于以前的pascal 关键字
三 WNDCLASSEX 结构
      WNDCLASSEX winclass; // this will hold the class we create
    
    
// first fill in the window class stucture
    winclass.cbSize         = sizeof(WNDCLASSEX);                     //指她本身的大小
    winclass.style            
= CS_DBLCLKS | CS_OWNDC |        //窗口样式,一般选这4个
        CS_HREDRAW 
| CS_VREDRAW;
    winclass.lpfnWndProc    
= WindowProc;                                 //要回调的函数指针
    winclass.cbClsExtra        
= 0;                                                   //额外的类信息空间,现在一般不用
    winclass.cbWndExtra        
= 0;                                                //额外的窗口信息空间,现在一般不用
    winclass.hInstance        
= hinstance;                                          //窗口的实例句柄,从winmain()传来
    winclass.hIcon            
= LoadIcon(NULL, IDI_APPLICATION); //图标
    winclass.hCursor        
= LoadCursor(NULL, IDC_ARROW);       //鼠标
    winclass.hbrBackground    
= (HBRUSH)GetStockObject(BLACK_BRUSH); //背景刷,用于刷新窗口的画刷句柄,可以用GetStockObject()
    winclass.lpszMenuName    
= NULL;                       //菜单,要加入到窗口中的菜单名称
    winclass.lpszClassName    
= WINDOW_CLASS_NAME;   //要创建的窗口类的类名
    winclass.hIconSm        
= LoadIcon(NULL, IDI_APPLICATION); //图标,显示在标题兰和状态兰

四 注册windows类
RegisterClassEx(&winclass); 传入指向类的指针
五 创建窗口
CreateWindowEx(NULL,                  // extended style       扩张的窗口样式,高级,一般不用
        WINDOW_CLASS_NAME,     // class        创建窗口需要的类指针
        "Your Basic Window++"// title            标题兰的文本
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,         //窗口样式
        
0,0,        // initial x,y   窗口的左上角,象素表示
        400,400,  // initial width, height 宽高,象素表示
        NULL,        // handle to parent  父窗口句柄
        NULL,        // handle to menu        菜单句柄或子窗口标示
        hinstance,// instance of this application  winmain(0中的instance
        NULL))   // extra creation parms  高级参数,一般不用
六 显示窗口且刷新一下
ShowWindow()//可以控制不显示,或在状态兰也不显示
UpdateWindow() //刷新窗口,就是生成一个WM_PAINT消息
七 主消息循环
LRESULT CALLBACK WindowProc(HWND hwnd,  //窗口句柄
                            UINT msg,                   //消息id
                            WPARAM wparam,    //用于进一步确定msg指定的消息
                            LPARAM lparam)      //用于进一步确定msg指定的消息
LRESULT CALLBACK 不能少,以下是简单的几种消息:
WM_CREATE: //可以此时执行各种初始化
WM_PAINT:     hdc  = BeginPaint(hwnd,&ps);     //确认窗口是否有效
            
           // you would do all your painting here  //绘制工作
                      EndPaint(hwnd,&ps);                        //结束绘制

WM_KEYDOWN:  //处理键盘按下
WM_DESTROY:     //将要关闭应用程序,发出WM_QUIT消息
WM_QUIT:            //推出程序
注意 函数DefWindowProc(),是处理其他的消息,除了WindowProc()已经处理的其他消息.

GetMessage(LPMSG,       //消息结构的地址
                    hWnd,                //窗口的句柄
                     uint,                  //first message
                       uint)                   //last messge
她从事件队列获得下一个消息,然后调用TranslateMessage()函数,进行消息的转换和处理,然后通过DispatchMessage()来调用winproc()函数.

PeekMessage( &msg,NULL,0,0,PM_REMOVE))中参数:消息结构的指针,窗口的句柄,第一条消息,最后一条消息,删除标记 ,其中参数删除标记是GetMessage()中没有的,该标记有2个值,PM_NOREMOVE和PM_REMOVE,前一个经过peekmessage()后不将其从消息队列中删除,后一个表示经过peekmessage()后从消息队列中删除.
Posted on 2006-09-14 15:10 艾凡赫 阅读(569) 评论(0)  编辑 收藏 引用 所属分类: win32 sdk 编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理