今天Bili发现网上好多MFC代码都不能用,给大家分享一个简单的MFC窗口语言:
复制代码ing:
#include
LRESULT CALLBACK WindowProc(HWND hand,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) {
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hand);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN://鼠标左键按下
{
int xPos = LOWORD(lParam);
int yPos = HIWORD(lParam);
char buf[1024];
wsprintf(buf, TEXT("x = %d,y = %d"),xPos,yPos);
MessageBox(hand, buf, TEXT("按下鼠标左键"), MB_OK);
break;
}
case WM_KEYDOWN:
{
MessageBox(hand, TEXT("键盘"), TEXT("按下键盘"), MB_OK);
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hand, &ps);
TextOut(hdc, 100, 100,TEXT("hello"), strlen("hello"));
EndPaint(hand, &ps);
break;
}
default:
break;
}
return DefWindowProc(hand, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd
) {
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_HAND);
wc.hIcon = LoadIcon(NULL, IDI_ERROR);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = TEXT("WIN");
wc.lpszMenuName = NULL;
wc.style = 0;
RegisterClass(&wc);
HWND hwnd = CreateWindow(wc.lpszClassName, TEXT("WINDOWS"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}