这个会在鼠标左击的地方画点,但拖动后窗口会变全白
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
int wdLeft, wdTop, wdWidth = 800, wdHeight = 500;
HWND wdHWND;
HDC hdc;
void Exit() {
ReleaseDC(wdHWND, hdc);
cout << "[System] Exit\n";
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
if (Msg == WM_PAINT) {
BitBlt(hdc, 0, 0, wdWidth, wdHeight, NULL, 0, 0, SRCAND);
} else if (Msg == WM_DESTROY) {
PostQuitMessage(0), Exit();
} else if (Msg == WM_CHAR) {
if (wParam == VK_ESCAPE) {
DestroyWindow(hwnd), Exit();
}
} else if (Msg == WM_MOVING) {
RECT* _rect = (RECT*) lParam;
wdLeft = _rect -> left, wdTop = _rect -> top;
} else if (Msg == WM_SIZING) {
RECT* _rect = (RECT*) lParam;
if (!wdLeft && !wdTop) {
wdLeft = _rect -> left, wdTop = _rect -> top;
}
_rect -> left = wdLeft, _rect -> top = wdTop;
_rect -> right = _rect -> left + wdWidth;
_rect -> bottom = _rect -> top + wdHeight;
} else if (Msg == WM_LBUTTONDOWN) {
short _cursorY = lParam >> 16, _cursorX = lParam - (_cursorY << 16);
cout << "[Message] LButton down at (" << _cursorX << ", " << _cursorY << ")\n";
//PAINTSTRUCT ps;
//BeginPaint(hwnd, &ps);
SetPixel(hdc, _cursorX, _cursorY, RGB(255, 255, 255));
//EndPaint(hwnd, &ps);
} else {
return DefWindowProc(hwnd, Msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = wdHWND = CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", "Sb(R) Window", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, wdWidth, wdHeight, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
cout << "[System] Window created successfully, HWND: " << hwnd << '\n';
hdc = GetDC(hwnd);
/*for (int i = 0; i <= wdHeight; i += 2) {
for (int j = 0; j <= wdWidth; j += 2) {
SetPixel(hdc, j, i, RGB(0, 0, 0));
}
}*/
for (short i = 0; i < 128; i++) {
for (short j = 0; j < 128; j++) {
SetPixel(hdc, j, i, RGB(i, j, i + j));
}
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
悬赏一关注,感谢