15,471
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "DesktopHook.h"
#pragma data_seg("MySection")
HHOOK g_hMsgHook = NULL;
HWND g_hDesktopWnd = NULL;
BOOL g_bShow = TRUE;
#pragma data_seg()
#pragma comment(linker, "/section:MySection,RWS")
HWND FindDesktopWindow()
{
HWND ParentWnd;
HWND ViewWnd = NULL;
HWND DesktopWnd;
ParentWnd = ::FindWindowEx(0, 0, L"WorkerW", L""); // 获得第一个WorkerW类的窗口
while ((!ViewWnd) && ParentWnd)
{
ViewWnd = ::FindWindowEx(ParentWnd, 0, L"SHELLDLL_DefView", 0);
ParentWnd = ::FindWindowEx(0, ParentWnd, L"WorkerW", L""); // until找到正确的WorderW类
}
// 如果失败,尝试在Program Manager下搜索
if (ViewWnd == NULL)
{
ParentWnd = ::FindWindow(L"Progman", L"Program Manager");
ViewWnd = ::FindWindowEx(ParentWnd, 0, L"SHELLDLL_DefView", 0);
}
DesktopWnd = ::FindWindowEx(ViewWnd, 0, L"SysListView32", L"FolderView");
return DesktopWnd;
}
LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
{
MSG* pMsg = (MSG*)lParam;
switch (pMsg->message)
{
case WM_LBUTTONDBLCLK:
g_bShow = !g_bShow;
::ShowWindow(g_hDesktopWnd, g_bShow ? SW_SHOW : SW_HIDE);
break;
default:
break;
}
return ::CallNextHookEx(g_hMsgHook, code, wParam, lParam);
}
void SetDesktopHook()
{
// Get desktop hwnd
g_hDesktopWnd = FindDesktopWindow();
// Hook post message
g_hMsgHook = ::SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, GetModuleHandle(L"DesktopHook"), 0);
}
void FreeDesktopHook()
{
::UnhookWindowsHookEx(g_hMsgHook);
}