奇怪的光标行为:几乎总是调整大小光标。 (win32 api)
Weird cursor behavior: Almost always the resize cursor. (win32 api)
我正在使用 win32 API,并用它来制作 window。这个 window 有效,但是当我打开它时,光标是一个加载光标,每次我将光标移到边缘以调整它的大小时,光标都会变成 'stuck' 作为调整大小的光标,它不会恢复正常。这是一个视频来解释我在说什么:
这是可重现的示例(使用 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 做了一个等效的 window(可能是因为 windows api 和 SFML 是完全不同的东西)。 window:
的代码
#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();
}
}
我希望第一个 window 像第二个 window 一样工作,但光标是我能找到的唯一差异。我试过谷歌搜索这个,但无济于事。对于我遇到问题的 window,我遵循了 youtube tutorial series。此外,在下载教程的源代码并删除一些使其全屏显示并隐藏光标的代码时,它也有同样的问题。我该如何解决?提前谢谢你。
将 wc.hCursor
设置为空以外的值。 If it's null, the operating system will leave the cursor alone when it enters your window, and you're supposed to set it to the cursor you want.
您很可能想要无聊的旧箭头光标。您可以获得该光标 by calling LoadCursor(NULL, IDC_ARROW)
.
我正在使用 win32 API,并用它来制作 window。这个 window 有效,但是当我打开它时,光标是一个加载光标,每次我将光标移到边缘以调整它的大小时,光标都会变成 'stuck' 作为调整大小的光标,它不会恢复正常。这是一个视频来解释我在说什么:
这是可重现的示例(使用 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 做了一个等效的 window(可能是因为 windows api 和 SFML 是完全不同的东西)。 window:
的代码#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();
}
}
我希望第一个 window 像第二个 window 一样工作,但光标是我能找到的唯一差异。我试过谷歌搜索这个,但无济于事。对于我遇到问题的 window,我遵循了 youtube tutorial series。此外,在下载教程的源代码并删除一些使其全屏显示并隐藏光标的代码时,它也有同样的问题。我该如何解决?提前谢谢你。
将 wc.hCursor
设置为空以外的值。 If it's null, the operating system will leave the cursor alone when it enters your window, and you're supposed to set it to the cursor you want.
您很可能想要无聊的旧箭头光标。您可以获得该光标 by calling LoadCursor(NULL, IDC_ARROW)
.