码迷,mamicode.com
首页 > 其他好文 > 详细

创建窗口

时间:2015-03-15 10:52:12      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

#include <Windows.h>

HINSTANCE hinst;
HWND hwnd;
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
    //定义窗口类
    WNDCLASSEX wcx;

    MSG msg;
    BOOL fGotMessage;
    hinst = hinstance;
    wcx.lpszClassName = "MainWClass";
    wcx.cbSize = sizeof(wcx);
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    wcx.lpfnWndProc = MainWndProc;
    wcx.cbClsExtra = 0;
    wcx.cbWndExtra = 0;
    wcx.hInstance = hinstance;
    wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wcx.hCursor = LoadCursor(NULL,IDC_ARROW);
    wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wcx.lpszMenuName = NULL;
    wcx.hIconSm = (HICON)LoadImage(hinstance, 
        MAKEINTRESOURCE(5),IMAGE_ICON,
        GetSystemMetrics(SM_CXSMICON),
        GetSystemMetrics(SM_CYSMICON),
        LR_DEFAULTCOLOR);

    if(!RegisterClassEx(&wcx))
    {
        return 1;
    }
    //使用窗口类创建窗口
    hwnd = CreateWindow("MainWClass",
        "Hello",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        (HWND)NULL,
        (HMENU)NULL,
        hinst,
        (LPVOID)NULL);
    if(!hwnd)
    {
        return 1;
    }

    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);

    while((fGotMessage = GetMessage(&msg,(HWND)NULL,0,0))!=0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

//               窗口过程函数,处理消息
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_LBUTTONDOWN:
        MessageBox(hwnd,"Hello窗口!","hello",MB_OK);
        return 0;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
}

 定义窗口类,注册窗口类,使用窗口类创建窗口

创建窗口

标签:

原文地址:http://www.cnblogs.com/gongyan/p/4338869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!