#include "stdio.h"
#include "windows.h"
HWND hMainWnd; //主窗口句柄
HWND hButtonWnd; //按钮句柄
WNDPROC DefButtonProc = 0;
ATOM MyRegisterClass(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ButtonProc(HWND, UINT, WPARAM, LPARAM);
TCHAR szWindowClass[32] = TEXT("MyWindowClas");
//窗口类名
//登记窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX ex;
ex.cbSize = sizeof(WNDCLASSEX);
ex.style = CS_HREDRAW | CS_VREDRAW; //窗口式样
ex.lpfnWndProc = (WNDPROC)WndProc; //窗口处理函数
ex.cbClsExtra = 0; //分配给窗口类结构之后的额外字节数,一般为0
ex.cbWndExtra = 0; //分配给窗口实例之后的额外字节数,一般为0
ex.hInstance = hInstance; //实例句柄
ex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_APPLICATION);
//窗口图标
ex.hCursor = LoadCursor(NULL, IDC_ARROW);
//光标
ex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //背景
ex.lpszMenuName = NULL; //菜单
ex.lpszClassName = szWindowClass; //窗口类名
ex.hIconSm = LoadIcon(ex.hInstance,
(LPCTSTR)IDI_APPLICATION);
return RegisterClassEx(&ex);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPWSTR lpstrCmdLine, int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance); //注册窗口类
//创建窗口
HWND hMainWnd = CreateWindow(szWindowClass, //窗口类名
TEXT("一个简单窗口"), //窗口标题
WS_OVERLAPPEDWINDOW, //窗口风格
100, //窗口左上角x坐标
100, //窗口左上角y坐标
420, //窗口宽度
300, //窗口高度
NULL, NULL, hInstance, NULL);
if(NULL == hMainWnd){
return FALSE;
}
//创建按钮
hButtonWnd = CreateWindow(TEXT("BUTTON"), TEXT("按钮"),
WS_CHILD|WS_VISIBLE|BS_TEXT, 150, 80, 100, 40, hMainWnd, 0,
hInstance, 0);
DefButtonProc = reinterpret_cast(GetWindowLongPtr(hButtonWnd,
GWLP_WNDPROC));
SetWindowLongPtr(hButtonWnd, GWLP_WNDPROC,
reinterpret_cast(ButtonProc));
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
//开始消息循环
while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg); //分派消息
}
return msg.wParam;
}
//窗口消息处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM
wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY: //关闭窗口
MessageBox(hWnd, TEXT("程序即将退出"), TEXT("提示框"), MB_OK);
PostQuitMessage(0); //发送关闭消息
break;
case WM_QUIT: //程序退出
OutputStringA("WM_QUIT");
//在这里做一些清理工作, 例如释放系统资源
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK ButtonProc(HWND hDlg, UINT message, WPARAM
wParam, LPARAM lParam)
{
switch(message)
{
case WM_LBUTTONUP:
MessageBox(hDlg, TEXT("按钮按下了"), TEXT("提示框"), MB_OK);
default:
//调用系统默认的消息处理函数来处理其它我们不关心的消息
return CallWindowProc(DefButtonProc, hDlg, message, wParam,
lParam);
}
}