#include<Windows.h>
//一个windows应用程序应该包含的头文件
#include<iostream> //标准输入输出流文件
using namespace std;
LRESULT CALLBACK WinMainProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
//设计一个窗口类
char szClassName[] = "MainWClass";
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass); //结构的大小
wndclass.style = CS_VREDRAW | CS_HREDRAW; //窗口风格
wndclass.lpfnWndProc = WinMainProc; //窗口消息处理函数指针
wndclass.cbClsExtra = 0; //额外内存
wndclass.cbWndExtra = 0; //额外内存
wndclass.hInstance = hInstance; //实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //左上角图标的句柄
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //光标句柄
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH); //背景画刷的句柄
wndclass.lpszMenuName = NULL; //菜单名
wndclass.lpszClassName = szClassName; //该窗口类的名称
wndclass.hIconSm = NULL; //小图标句柄
//注册
RegisterClassEx(&wndclass);
//创建
HWND hwnd = CreateWindowEx
(
0, //dwExStyle 扩展样式
szClassName, //lpClassName类名
"My first window!", //lpWindowName 标题
WS_OVERLAPPEDWINDOW, //dwStyle 窗口风格
CW_USEDEFAULT, //X
CW_USEDEFAULT, //Y
CW_USEDEFAULT, //宽
CW_USEDEFAULT, //高
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //程序实例句柄
NULL //用户数据
);
if (hwnd == NULL)
{
MessageBox(NULL, "创建出错", "error", MB_OK);
return -1;
}
ShowWindow(hwnd, nShowCmd); //显示窗口
UpdateWindow(hwnd); //刷新窗口
MSG msg; //定义消息结构体,开始消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//编写窗口过程函数
LRESULT CALLBACK WinMainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
{
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, "char", 0);
break;
}
case WM_LBUTTONDOWN:
{
MessageBox(hwnd, "mouse clicked", "message", 0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 0, 50, "哈哈", strlen("哈哈"));
ReleaseDC(hwnd, hdc);
break;
}
case WM_PAINT:
{
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);
TextOut(hDC, 150, 10, "xxx", strlen("xxx"));
EndPaint(hwnd, &ps);
return 0;
}
case WM_CLOSE:
{
if (IDYES == MessageBox(hwnd, "是否真的结束", "message", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
#include<iostream> //标准输入输出流文件
using namespace std;
LRESULT CALLBACK WinMainProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
//设计一个窗口类
char szClassName[] = "MainWClass";
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass); //结构的大小
wndclass.style = CS_VREDRAW | CS_HREDRAW; //窗口风格
wndclass.lpfnWndProc = WinMainProc; //窗口消息处理函数指针
wndclass.cbClsExtra = 0; //额外内存
wndclass.cbWndExtra = 0; //额外内存
wndclass.hInstance = hInstance; //实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //左上角图标的句柄
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //光标句柄
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH); //背景画刷的句柄
wndclass.lpszMenuName = NULL; //菜单名
wndclass.lpszClassName = szClassName; //该窗口类的名称
wndclass.hIconSm = NULL; //小图标句柄
//注册
RegisterClassEx(&wndclass);
//创建
HWND hwnd = CreateWindowEx
(
0, //dwExStyle 扩展样式
szClassName, //lpClassName类名
"My first window!", //lpWindowName 标题
WS_OVERLAPPEDWINDOW, //dwStyle 窗口风格
CW_USEDEFAULT, //X
CW_USEDEFAULT, //Y
CW_USEDEFAULT, //宽
CW_USEDEFAULT, //高
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //程序实例句柄
NULL //用户数据
);
if (hwnd == NULL)
{
MessageBox(NULL, "创建出错", "error", MB_OK);
return -1;
}
ShowWindow(hwnd, nShowCmd); //显示窗口
UpdateWindow(hwnd); //刷新窗口
MSG msg; //定义消息结构体,开始消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//编写窗口过程函数
LRESULT CALLBACK WinMainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
{
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, "char", 0);
break;
}
case WM_LBUTTONDOWN:
{
MessageBox(hwnd, "mouse clicked", "message", 0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 0, 50, "哈哈", strlen("哈哈"));
ReleaseDC(hwnd, hdc);
break;
}
case WM_PAINT:
{
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);
TextOut(hDC, 150, 10, "xxx", strlen("xxx"));
EndPaint(hwnd, &ps);
return 0;
}
case WM_CLOSE:
{
if (IDYES == MessageBox(hwnd, "是否真的结束", "message", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}