【发布时间】:2011-04-12 19:11:18
【问题描述】:
有没有什么办法可以在WIN32的桌面背景上绘制,并且在桌面背景重绘时也能收到通知?
我试过这个:
desk = GetDesktopWindow();
dc = GetDC(desk);
MoveToEx(dc,0,0,NULL);
LineTo(dc,1680,1050);
ReleaseDC(desk,dc);
但它会在整个屏幕上绘制,甚至在屏幕上的窗口上。
【问题讨论】:
有没有什么办法可以在WIN32的桌面背景上绘制,并且在桌面背景重绘时也能收到通知?
我试过这个:
desk = GetDesktopWindow();
dc = GetDC(desk);
MoveToEx(dc,0,0,NULL);
LineTo(dc,1680,1050);
ReleaseDC(desk,dc);
但它会在整个屏幕上绘制,甚至在屏幕上的窗口上。
【问题讨论】:
您可以使用 Spy++ 来查找哪个窗口是桌面背景窗口。
在我的系统上,我看到以下层次结构:
我猜你指的是 SysListView32 - 带有所有图标的窗口。您可以使用FindWindowEx 找到此窗口。
编辑
您应该结合使用 FindWindowEx 和 EnumerateChildWindows。下面给出的代码可以像这样在命令行框中编译:cl /EHsc finddesktop.cpp /DUNICODE /link user32.lib
#include <windows.h>
#include <iostream>
#include <string>
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
std::wstring windowClass;
windowClass.resize(255);
unsigned int chars = ::RealGetWindowClass(hwnd, &*windowClass.begin(), windowClass.size());
windowClass.resize(chars);
if (windowClass == L"SysListView32")
{
HWND* folderView = reinterpret_cast<HWND*>(lParam);
*folderView = hwnd;
return FALSE;
}
return TRUE;
}
int wmain()
{
HWND parentFolderView = ::FindWindowEx(0, 0, L"Progman", L"Program Manager");
if (parentFolderView == 0)
{
std::wcout << L"Couldn't find Progman window, error: 0x" << std::hex << GetLastError() << std::endl;
}
HWND folderView = 0;
::EnumChildWindows(parentFolderView, EnumChildProc, reinterpret_cast<LPARAM>(&folderView));
if (folderView == 0)
{
std::wcout << L"Couldn't find FolderView window, error: 0x" << std::hex << GetLastError() << std::endl;
}
HWND desktopWindow = ::GetDesktopWindow();
std::wcout << L"Folder View: " << folderView << std::endl;
std::wcout << L"Desktop Window: " << desktopWindow << std::endl;
return 0;
}
这是运行 finddesktop.exe 后的结果
Folder View: 000100A0
Desktop Window: 00010014
如您所见,窗口句柄完全不同。
【讨论】:
只是引用MSDN:
GetDesktopWindow 函数返回 桌面窗口的句柄。这 桌面窗口覆盖整个 屏幕。桌面窗口是区域 在其他窗户之上 画了。
所以你得到了一个带有嵌套窗口的窗口。 我不是一个WIN32用户,但我认为这里的方法是进入较低级别,控制正在绘制背景图片的图形对象,然后在那里绘制。
【讨论】: