我正在使用 win32 API,并使用它來制作一個視窗。這個視窗可以作業,但是當我打開它時,游標是一個加載游標,每次我將游標移到邊緣以調整它的大小時,游標都會因為調整大小的游標而“卡住”,它不會恢復正常. 這是一個視頻來解釋我在說什么:

這是可重現的示例(使用 編譯g reproducible_example.cpp -mwindows -O3 -o reproducible_example.exe):
#undef UNICODE
#undef _UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
bool isRunning = true;
void *buffer; // buffer memory
BITMAPINFO bmi; // bit map information, needed for rendering
int width, height; // main window's width and height
LRESULT __stdcall WindowProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR pCmdLine, int nCmdShow) {
LPCSTR CLASS_NAME = "Class Name";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowExA(0, // Optional window styles.
"Class Name", // Window class
"Window", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
return 0;
ShowWindow(hwnd, nCmdShow);
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
// Run the message loop.
HDC hdc = GetDC(hwnd);
while (isRunning) {
MSG msg;
if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
StretchDIBits(hdc, 0, 0, width, height, 0, 0, width, height, buffer, &bmi,
DIB_RGB_COLORS, SRCCOPY);
}
ReleaseDC(hwnd, hdc);
return 0;
}
LRESULT __stdcall WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
isRunning = false;
return 0;
case WM_SIZE: {
// Calculate window height and width
width = LOWORD(lParam);
height = HIWORD(lParam);
if (buffer) // If memory already exists
// free it
VirtualFree(buffer, 0, MEM_RELEASE);
// Allocate buffer memory
buffer = VirtualAlloc(0, width * height * sizeof(unsigned int),
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
現在,我知道這不是一些奇怪的 Windows 錯誤,而且絕對是我的代碼的問題,因為它不會發生在我打開的其他應用程式中,而且當我使用 SFML 制作等效視窗時也不會發生這種情況(可能因為 windows api 和 SFML 是完全不同的東西)。該視窗的代碼:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1280, 720), "Window");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
}
window.clear();
window.display();
}
}
我希望第一個視窗像第二個視窗一樣,但游標是我能找到的一個差異。我試過用谷歌搜索這個,但無濟于事。我針對我遇到問題的視窗關注了一個youtube 教程系列。此外,在下載教程的源代碼并洗掉一些使其全屏并隱藏游標的代碼時,它也有同樣的問題。我該如何解決?先感謝您。
uj5u.com熱心網友回復:
設定wc.hCursor為 null 以外的值。如果它為空,作業系統將在游標進入您的視窗時不理會游標,您應該將其設定為您想要的游標。
您很可能想要無聊的舊箭頭游標。您可以通過呼叫LoadCursor(NULL, IDC_ARROW)來獲取該游標。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/477618.html
下一篇:Ctypes資料型別轉換
