#include <Windows.h>
#include <tchar.h>
////////////////////////////////
// 常量定义
//
#define _WINDOWS_CLASS_NAME _T("Win32")
#define _WINDOWS_TITLE_NAME _T("SampleWindow by wjh")
const UINT _WINDOWS_WIDTH = 640;
const UINT _WINDOWS_HEIGHT = 480;
////////////////////////////////
// 全局变量定义
//
HWND g_hWndMain; //窗口句柄
HINSTANCE g_hInsApp; //窗口进程实例
HDC g_RenderDC; //绘图用
////////////////////////////////
// 函数声明
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); //窗口回调函数
WNDCLASSEX WinClassRegister(HINSTANCE hInstance);
int APIENTRY _tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wndclassex = WinClassRegister(hInstance);
MSG msg;
HWND hwnd;
//向系统注册窗口
if (!RegisterClassEx(&wndclassex))
{
//若注册失败显示提示并结束程序
MessageBox(NULL, L"注册窗口失败!", L"Error", MB_OK);
return -1;
}
hwnd = CreateWindowEx(
0, _WINDOWS_CLASS_NAME, _WINDOWS_TITLE_NAME, //窗口附加风格参数 窗口类名 窗口标题名
WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, //窗口风格
CW_USEDEFAULT, CW_USEDEFAULT, //窗口初始位置x y
_WINDOWS_WIDTH, _WINDOWS_HEIGHT, //窗口宽高
NULL, NULL, hInstance, NULL //父窗口句柄 菜单句柄 当前进程实例 附加参数
);
if (hwnd == NULL)
{
//若窗口创建失败 显示提示并结束程序
MessageBox(NULL, L"创建窗口失败!", L"Error", MB_OK);
return -2;
}
g_hWndMain = hwnd;
g_hInsApp = hInstance;
UpdateWindow(hwnd); //更新窗口
ShowWindow(hwnd, SW_SHOW); //显示窗口
//消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);;
}
WNDCLASSEX WinClassRegister(HINSTANCE hInstance)
{
WNDCLASSEX wndclassex;
wndclassex.cbSize = sizeof(WNDCLASSEX); //窗口类大小
wndclassex.cbClsExtra = 0; //窗口附加风格参数
wndclassex.cbWndExtra = 0; //窗口附加参数
wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //窗口背景色
wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW); //鼠标样式
wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION); //应用程序图标
wndclassex.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //应用程序小图标
wndclassex.hInstance = hInstance; //当前进程实例句柄
wndclassex.lpfnWndProc = WndProc; //窗口回调函数
wndclassex.lpszClassName = _WINDOWS_CLASS_NAME; //窗口类名
wndclassex.lpszMenuName = NULL; //窗口菜单名
wndclassex.style = CS_OWNDC; //窗口风格
return wndclassex;
}
win32确实很难学啊。