#include <windows.h>
#include <stdio.h>
HANDLE g_hOutput = 0;
void OnKeyDowm(HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf_s(szText, 256, "WM_KEYDOWN:键码值=%d\n", wParam);
WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL);
}
void OnKeyUp(HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf_s(szText, 256, "WM_KEYUP:键码值=%d\n", wParam);
WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL);
}
void OnChar(HWND hWnd, WPARAM wParam)
{
char szText[256] = { 0 };
sprintf_s(szText, 256, "WM_CHAR:键码值=%d\n", wParam);
WriteConsole(g_hOutput, szText, strlen(szText), NULL, NULL);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
{
switch (msgID)
{
case WM_CHAR:
OnChar(hWnd,wParam);
break;
case WM_KEYDOWN:
OnKeyDowm(hWnd,wParam);
break;
case WM_KEYUP:
OnKeyUp(hWnd,wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msgID, wParam, lParam);
}
//入口函数
int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR IpCmdLine, int nCmdShow)
{
AllocConsole();
g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
//注册窗口类
WNDCLASS wc = { 0 };
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hInstance = hIns;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Main";
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
//在内存创建窗口
HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hIns, NULL);
//显示窗口
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
//消息循环
MSG nMsg = { 0 };
while (GetMessage(&nMsg, NULL, 0, 0))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);//将消息给窗口处理函数来处理
}
return 0;
}
键盘 消息 111
最新推荐文章于 2024-08-24 19:54:25 发布