第一个 Windows 应用程序
#include <windows.h>
//窗口过程声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int nCmdShow)
{
const char* szWindowName = "第一个 Windows 应用程序"; //窗口标题
const char* szClassName = "WindowClass"; //窗口类名
//创建一个 WNDCLASSEX 结构体变量, 为其设置成员的值
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX); //结构大小
wndclass.style = CS_HREDRAW | CS_VREDRAW; //水平重画,竖直重画
wndclass.lpfnWndProc = (WNDPROC)WndProc; //窗口过程
wndclass.cbClsExtra = 0; //没有额外的类内存
wndclass.cbWndExtra = 0; //没有额外的窗口内存
wndclass.hInstance = hInstance; //实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //程序图标
wndclass.hCursor = LoadCursor(NULL, IDC_CROSS); //鼠标样式
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH); //背景白色
wndclass.lpszMenuName = NULL; //没有菜单名
wndclass.lpszClassName = szClassName; //窗口类名
wndclass.hIconSm = NULL; //没有类的小图标
//注册 WNDCLASSEX 结构体
::RegisterClassEx(&wndclass);
//创建主窗口
HWND hWnd = ::CreateWindowEx(
0, //dwExstyle, 扩展样式
szClassName, //lpClassName, 类名
szWindowName, //lpWindowName, 标题
WS_OVERLAPPEDWINDOW, //dwStyle, 窗口风格
CW_USEDEFAULT, //X, 初始 X 坐标
CW_USEDEFAULT, //Y, 初始 Y 坐标
CW_USEDEFAULT, //nWidth, 宽度
CW_USEDEFAULT, //nHeight, 高度
NULL, //hWndParent, 父窗口句柄
NULL, //hMenu, 菜单句柄
hInstance, //hInstance, 程序实例句柄
NULL); //lpparam, 用户数据
if(hWnd == NULL)
{
::MessageBox(NULL, "创建窗口出错!", "error", MB_OK);
return FALSE;
}
//显示窗口
::ShowWindow(hWnd, nCmdShow);
//刷新窗口
::UpdateWindow(hWnd);
//消息循环
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
//窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
const char* szHello = "Hello World";
switch(message)
{
//重绘窗口
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = ::BeginPaint(hWnd, &ps);
::TextOut(hdc, 10, 10, szHello, strlen(szHello));
::EndPaint(hWnd, &ps);
break;
//销毁窗口
case WM_DESTROY:
::PostQuitMessage(0);
break;
}
//交给系统做默认处理
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
//窗口过程声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int nCmdShow)
{
const char* szWindowName = "第一个 Windows 应用程序"; //窗口标题
const char* szClassName = "WindowClass"; //窗口类名
//创建一个 WNDCLASSEX 结构体变量, 为其设置成员的值
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX); //结构大小
wndclass.style = CS_HREDRAW | CS_VREDRAW; //水平重画,竖直重画
wndclass.lpfnWndProc = (WNDPROC)WndProc; //窗口过程
wndclass.cbClsExtra = 0; //没有额外的类内存
wndclass.cbWndExtra = 0; //没有额外的窗口内存
wndclass.hInstance = hInstance; //实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //程序图标
wndclass.hCursor = LoadCursor(NULL, IDC_CROSS); //鼠标样式
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH); //背景白色
wndclass.lpszMenuName = NULL; //没有菜单名
wndclass.lpszClassName = szClassName; //窗口类名
wndclass.hIconSm = NULL; //没有类的小图标
//注册 WNDCLASSEX 结构体
::RegisterClassEx(&wndclass);
//创建主窗口
HWND hWnd = ::CreateWindowEx(
0, //dwExstyle, 扩展样式
szClassName, //lpClassName, 类名
szWindowName, //lpWindowName, 标题
WS_OVERLAPPEDWINDOW, //dwStyle, 窗口风格
CW_USEDEFAULT, //X, 初始 X 坐标
CW_USEDEFAULT, //Y, 初始 Y 坐标
CW_USEDEFAULT, //nWidth, 宽度
CW_USEDEFAULT, //nHeight, 高度
NULL, //hWndParent, 父窗口句柄
NULL, //hMenu, 菜单句柄
hInstance, //hInstance, 程序实例句柄
NULL); //lpparam, 用户数据
if(hWnd == NULL)
{
::MessageBox(NULL, "创建窗口出错!", "error", MB_OK);
return FALSE;
}
//显示窗口
::ShowWindow(hWnd, nCmdShow);
//刷新窗口
::UpdateWindow(hWnd);
//消息循环
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
//窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
const char* szHello = "Hello World";
switch(message)
{
//重绘窗口
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = ::BeginPaint(hWnd, &ps);
::TextOut(hdc, 10, 10, szHello, strlen(szHello));
::EndPaint(hWnd, &ps);
break;
//销毁窗口
case WM_DESTROY:
::PostQuitMessage(0);
break;
}
//交给系统做默认处理
return ::DefWindowProc(hWnd, message, wParam, lParam);
}