2-设计窗口过程函数

设计窗口过程函数

回调函数指:用户每进行一次操作,就调用的函数(即响应)。

LRESULT CALLBACK my_callback(
__in HWND hwnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam)
{
int ret;
HDC hdc;
switch (uMsg)
{
case WM_CHAR:
char szchar[20];
sprintf_s(szchar, "您刚刚按下了:%c", wParam);
MessageBox(hwnd, szchar, "char", NULL);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "检测鼠标左键按下", "mes", NULL);
break;
case WM_PAINT:
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, "the window has repainted", strlen("the window has repainted"));
EndPaint(hwnd, &ps);
MessageBox(hwnd, "重绘", "mes", NULL);
break;
case WM_CLOSE:
ret = MessageBox(hwnd, "是否真的结束", "msg", MB_YESNO);
if (ret == IDYES)
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
break;
}

return 0;
}

整体代码

1在项目——链接器——系统——子系统中,改为窗口

2改为多字节字符集

#include<windows.h>
#include<stdio.h>

LPCSTR classname = "MY_C";
LPCSTR msgname = "first_window";
LRESULT CALLBACK my_callback(
__in HWND hWindow,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam);


int WINAPI wWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PWSTR pCmdLine,
int nCmdShow)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = NULL;
wndcls.cbWndExtra = NULL;
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndcls.hInstance = hInstance;
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = my_callback;
wndcls.lpszClassName = classname;

RegisterClass(&wndcls);
HWND hwnd; //创建一个句柄
hwnd = CreateWindow(
classname, // name of window class
msgname, // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND)NULL, // no owner window
(HMENU)NULL, // use class menu
hInstance, // handle to application instance
(LPVOID)NULL); // no window-creation data

ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);

MSG msg;
while (GetMessage(&msg, NULL, NULL, NULL)) //只有接收到WM_QUIT才会返回0
{
TranslateMessage(&msg); //将消息转化为相应格式,并投递到消息队列中
DispatchMessage(&msg); //分派消息
}
return msg.wParam;
}

LRESULT CALLBACK my_callback(
__in HWND hwnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam)
{
int ret;
HDC hdc;
switch (uMsg)
{
case WM_CHAR:
char szchar[20];
sprintf_s(szchar, "您刚刚按下了:%c", wParam);
MessageBox(hwnd, szchar, "char", NULL);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "检测鼠标左键按下", "mes", NULL);
break;
case WM_PAINT:
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, "the window has repainted", strlen("the window has repainted"));
EndPaint(hwnd, &ps);
MessageBox(hwnd, "重绘", "mes", NULL);
break;
case WM_CLOSE:
ret = MessageBox(hwnd, "是否真的结束", "msg", MB_YESNO);
if (ret == IDYES)
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
break;
}

return 0;
}