VC++ 生成窗口模板

VC++经常要生成窗口,那么多函数,记不住啦,写个模板,以后COPY一下就可以了.

 

#include "stdafx.h"

#include <windows.h>

//———————————————————————–
// 
//  Name: HelloWorld2 example project
// 
//  Author: liyan
//
//————————————————————————
//——————————— Globals ——————————
//
//————————————————————————

char* g_szApplicationName = "Hello World!";
char*    g_szWindowClassName = "MyWindowClass";

#define DEFINES_H

#define WINDOW_WIDTH  400
#define WINDOW_HEIGHT 400

//—————————- WindowProc ———————————

//————————————————————————-

LRESULT CALLBACK WindowProc (HWND   hwnd,
                             UINT   msg,
                             WPARAM wParam,
                             LPARAM lParam)
{
        static int cxClient, cyClient;
    switch (msg)
    {

        //A WM_CREATE msg is sent when your application window is first
        //created
    case WM_CREATE://创建时调用
        {

        }
        break;
    case WM_PAINT://重画时触发
        {

        }
        break;

    case WM_SIZE://改变大小时触发
        {

        }
        break;
    case WM_DESTROY:
        {       
            // 杀掉应用程序, 这一步发送一个  WM_QUIT 消息 
            PostQuitMessage (0);
        }
        break;
    }//end switch
         return DefWindowProc (hwnd, msg, wParam, lParam);
}

//——————————– WinMain ——————————-

//————————————————————————
int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     szCmdLine,
                    int       iCmdShow)
{
     //handle to our window
         HWND                        hWnd;
         //our window class structure
         WNDCLASSEX     winclass;

     // first fill in the window class stucture
         winclass.cbSize        = sizeof(WNDCLASSEX);    //WNDCLASSEX结构的大小
         winclass.style         = CS_HREDRAW | CS_VREDRAW;    //窗口应具有的显示样式
         winclass.lpfnWndProc   = WindowProc;    //Windows 过程的指针
         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 (WHITE_BRUSH);    //背景色的区域
         winclass.lpszMenuName  = NULL;    //设置菜单的名字
         winclass.lpszClassName = g_szWindowClassName;    //窗口类的名字
         winclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);    //较小的图标的句柄

         //register the window class
        if (!RegisterClassEx(&winclass))
        {
            MessageBox(NULL, "Class Registration Failed!", "Error", 0);

            //exit the application
            return 0;
        }

         //create the window and assign its ID to hwnd   
     hWnd = CreateWindowEx (NULL,                 // 扩展窗口样式
                            g_szWindowClassName,  // 指向所注册类名的指针
                            g_szApplicationName,  // 指向窗口名的指针
                            WS_OVERLAPPEDWINDOW,  // 窗口的样式
                            0,                    // 窗口左上角的X位置
                            0,                    // 窗口左上角的y位置
                            WINDOW_WIDTH,         // 窗口的width
                            WINDOW_HEIGHT,        // 窗口的height
                            NULL,                 // 指向父窗口或OWNER窗口的句柄
                            NULL,                 // 菜单句柄或子窗口的标识符
                            hInstance,            // 指向应用程序实例的句柄
                            NULL);                // 指向窗口创建数据的指针
     if(!hWnd)
     {
       MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0);
     }

     ShowWindow (hWnd, iCmdShow);

     UpdateWindow(hWnd);

     UnregisterClass( g_szWindowClassName, winclass.hInstance );

         MSG msg;

         while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
     }

     return msg.wParam;
}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: