现在的位置: 首页 > 综合 > 正文

使用API创建窗体

2012年12月19日 ⁄ 综合 ⁄ 共 3370字 ⁄ 字号 评论关闭
#include <windows.h>

// 声明消息处理函数
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

// 全局变量
char windowName[] = "一个默默无闻的窗体";
char className[] = "WinClass";

/* WinMain函数,程序入口 */
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     
int nCmdShow)
{
    HWND hwnd;               
/* This is the handle for our window */
    MSG messages;            
/* Here messages to the application are saved */

    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);                                        //WNDCLASSEX 的大小。我们可以用sizeof(WNDCLASSEX)来获得准确的值。
    wcex.style            = CS_HREDRAW | CS_VREDRAW;                           //从这个窗口类派生的窗口具有的风格。您可以用“or”操作符来把几个风格或到一起。
    wcex.lpfnWndProc    = WindowProcedure;                                   //窗口处理函数的指针。
    wcex.cbClsExtra        = 0;                                                 //指定紧跟在窗口类结构后的附加字节数。
    wcex.cbWndExtra        = 0;                                                 //指定紧跟在窗口事例后的附加字节数。如果一个应用程序在资源中用CLASS伪指令注册一个对话框类时,则必须把这个成员设成DLGWINDOWEXTRA。
    wcex.hInstance        = hThisInstance;                                     //本模块的事例句柄。
    wcex.hIcon            = LoadIcon(hThisInstance, IDI_APPLICATION);          //图标的句柄。
    wcex.hIconSm        = LoadIcon(hThisInstance, IDI_APPLICATION);          //和窗口类关联的小图标。如果该值为NULL。则把hIcon中的图标转换成大小合适的小图标。
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);                       //光标的句柄。
    wcex.hbrBackground    = CreateSolidBrush(RGB(236,233,216));                //背景画刷的句柄。
    wcex.lpszMenuName    = NULL;                                              //指向菜单的指针。
    wcex.lpszClassName    = className;                                         //指向类名称的指针。

    // 注册窗体类,如果失败退出程序
    if (!RegisterClassEx (&wcex))
        
return 0;

    // 注册窗体类成功后,创建窗体
    hwnd = CreateWindowEx (
        
0,                   /* Extended possibilites for variation */
        className,           
/* 类名 */
        windowName,          
/* 标题 */
        WS_OVERLAPPEDWINDOW, 
/* 窗体样式 */
        CW_USEDEFAULT,       
/* X坐标 */
        CW_USEDEFAULT,       
/* Y坐标 */
        
544,                 /* 宽 */
        
375,                 /* 长 */
        HWND_DESKTOP,        
/* The window is a child-window to desktop */
        NULL,                
/* No menu */
        hThisInstance,       
/* Program Instance handler */
        NULL                 
/* No Window Creation data */
        );

    ShowWindow (hwnd, nCmdShow); //在屏幕上显示窗体

    // 运行消息循环,直到GetMessage()返回0时退出。
    while (GetMessage (&messages, NULL, 00))
    {
        TranslateMessage(
&messages); //将虚拟键消息转换为字符消息。
        DispatchMessage(&messages);  //分发一个消息给窗口程序的消息处理函数(WindowProcedure)
    }
    
    
// 调用PostQuitMessage()后返回值为0。
    return (int)messages.wParam;
}

/* 调用函数DispatchMessage()后会相应此消息处理机制 */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    
switch (message)                  /* handle the messages */
    {
    
case WM_CREATE:
        ::MessageBox(NULL, 
"创建窗体成功!""", MB_OK);
        
break;
    
case WM_DESTROY:
        PostQuitMessage(
0);       /* send a WM_QUIT to the message queue */
        
break;
    
default:                      /* for messages that we don't deal with */
        
return DefWindowProc (hwnd, message, wParam, lParam);
    }
    
return 0;
}

 

下载源代码: http://files.cnblogs.com/project/2011-08-16.7z

抱歉!评论已关闭.