3

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

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

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

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

    // ...
    // ...
}
4

1 回答 1

7

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

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

RECT rect;
GetClientRect(..., &rect);
于 2012-08-17T20:01:41.667 回答