gpt4 book ai didi

C++WinApi : GetClientRect fails

转载 作者:太空狗 更新时间:2023-10-29 20:15:46 26 4
gpt4 key购买 nike

我正在编写一个在 Windows 上运行的屏幕保护程序。

在预览模式下,Windows 这样调用程序:
Screensaver.exe/p ParentWindowHandle

但是,当我在我的程序中进行此调用时:
BOOL res = GetClientRect(parentWindowHandle, rect)
res 为 FALSE,rect 为 NULL,我使用 GetLastError()

得到 ERROR_INVALID_WINDOW_HANDLE

GetWindowRect 给我相同的结果。

但是,如果我改为调用 BOOL res = IsWindow(parentWindowHandle),我会得到 res == TRUE。这是否意味着我有一个有效的窗口句柄?

代码如下:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
unsigned int handle = GetHandleFromCommandLine(pCmdLine); // Custom function (tested and approved :) )
HWND parentWindowHandle = (HWND) handle;
LPRECT rect = NULL;
BOOL res = GetClientRect(parentWindowHandle, rect);
// here, rect == NULL, res == FALSE and GetLastError() returns ERROR_INVALID_WINDOW_HANDLE

// ...
// ...
}

最佳答案

在 64 位 Windows 上,窗口句柄是 64 位的,不能放入 unsigned int 中,因此您的转换生成的值是无效的窗口句柄。您应该修改 GetHandleFromCommandLine 函数,以便它返回正确的 HWND,而不是 unsigned int,并且不需要类型转换。

此外,GetClientRect 通过将矩形存储到第二个参数指向的值中来返回矩形。如果您将 NULL 传递给它,它就没有地方可以存储它,因此它要么崩溃,要么因参数无效错误而失败。为避免这种情况,请传入局部变量的地址:

RECT rect;
GetClientRect(..., &rect);

关于C++WinApi : GetClientRect fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12012318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com