Skip to content

Commit

Permalink
添加桌面遮挡事件
Browse files Browse the repository at this point in the history
  • Loading branch information
xfgryujk committed Jan 2, 2017
1 parent 871e8ce commit 75414fe
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 4 deletions.
1 change: 1 addition & 0 deletions CustomDesktop/CDAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace cd
}


// 实现ExecInMainThread
static bool OnFileListWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_EXEC_FUNCTION)
Expand Down
4 changes: 4 additions & 0 deletions CustomDesktop/CDEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace cd
#define DEF_VARIABLE(name) decltype(name) name

CD_API DEF_VARIABLE(g_preUnloadEvent);
CD_API DEF_VARIABLE(g_desktopCoveredEvent);
CD_API DEF_VARIABLE(g_desktopUncoveredEvent);

CD_API DEF_VARIABLE(g_preDrawBackgroundEvent);
CD_API DEF_VARIABLE(g_postDrawBackgroundEvent);
Expand All @@ -25,6 +27,8 @@ namespace cd
// 外部模块可注册的事件集合,用来自动卸载外部listener
std::vector<EventBase*> g_externalEvents{
&g_preUnloadEvent,
&g_desktopCoveredEvent,
&g_desktopUncoveredEvent,

&g_preDrawBackgroundEvent,
&g_postDrawBackgroundEvent,
Expand Down
92 changes: 92 additions & 0 deletions CustomDesktop/CheckCovered.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "stdafx.h"
#include "CheckCovered.h"
#include <CDEvents.h>
#include <CDAPI.h>
#include <string>


namespace cd
{
CheckCovered::CheckCovered()
{
Init();
}

CheckCovered::~CheckCovered()
{
Uninit();
}

bool CheckCovered::Init()
{
m_runThreadFlag = true;
ExecInMainThread([this]{ m_thread = std::make_unique<std::thread>(&CheckCovered::CheckCoveredThread, this); });
return true;
}

bool CheckCovered::Uninit()
{
m_runThreadFlag = false;
if (m_thread != nullptr && m_thread->joinable())
m_thread->join();
m_thread = nullptr;
return true;
}


void CheckCovered::CheckCoveredThread()
{
while (m_runThreadFlag)
{
if (IsDesktopCovered())
{
if (!m_isCovered)
{
m_isCovered = true;
ExecInMainThread([]{ g_desktopCoveredEvent(); });

#ifdef _DEBUG
WCHAR windowName[100], className[100];
GetWindowTextW(m_coveredByHwnd, windowName, _countof(windowName));
GetClassNameW(m_coveredByHwnd, className, _countof(className));
_RPTW2(_CRT_WARN, L"桌面被 %s (%s) 遮挡\n", windowName, className);
#endif
}
}
else
{
if (m_isCovered)
{
m_isCovered = false;
ExecInMainThread([]{ g_desktopUncoveredEvent(); });

_RPT0(_CRT_WARN, "桌面从被遮挡恢复\n");
}
}

for (int i = 0; i < 10; i++)
{
if (!m_runThreadFlag)
break;
Sleep(100);
}
}
}

bool CheckCovered::IsDesktopCovered()
{
m_coveredByHwnd = NULL;

EnumWindows([](HWND hwnd, LPARAM pCoveredByHwnd)->BOOL{
if (IsZoomed(hwnd) && IsWindowVisible(hwnd))
{

*(HWND*)pCoveredByHwnd = hwnd;
return FALSE;
}
return TRUE;
}, (LPARAM)&m_coveredByHwnd);

return m_coveredByHwnd != NULL;
}
}
33 changes: 33 additions & 0 deletions CustomDesktop/CheckCovered.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "Singleton.h"
#include <thread>
#include <memory>


namespace cd
{
// 检测桌面是否被遮挡了
class CheckCovered final : public Singleton<CheckCovered>
{
DECL_SINGLETON(CheckCovered);
public:
bool IsReady() { return m_runThreadFlag; }

bool Init();
bool Uninit();

private:
CheckCovered();
~CheckCovered();


std::unique_ptr<std::thread> m_thread;
bool m_runThreadFlag = true;
bool m_isCovered = false;
HWND m_coveredByHwnd = NULL;


void CheckCoveredThread();
bool IsDesktopCovered();
};
}
2 changes: 2 additions & 0 deletions CustomDesktop/CustomDesktop.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<ClInclude Include="..\Include\EventHelper.h" />
<ClInclude Include="BufferedRendering.h" />
<ClInclude Include="CDAPIModule.h" />
<ClInclude Include="CheckCovered.h" />
<ClInclude Include="Global.h" />
<ClInclude Include="HookDesktop.h" />
<ClInclude Include="IATHook.h" />
Expand All @@ -173,6 +174,7 @@
<ItemGroup>
<ClCompile Include="BufferedRendering.cpp" />
<ClCompile Include="CDAPI.cpp" />
<ClCompile Include="CheckCovered.cpp" />
<ClCompile Include="Global.cpp" />
<ClCompile Include="HookDesktop.cpp" />
<ClCompile Include="dllmain.cpp">
Expand Down
6 changes: 6 additions & 0 deletions CustomDesktop/CustomDesktop.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<ClInclude Include="InlineHook.h">
<Filter>头文件\Helper</Filter>
</ClInclude>
<ClInclude Include="CheckCovered.h">
<Filter>头文件\Module</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand Down Expand Up @@ -104,5 +107,8 @@
<ClCompile Include="InlineHook.cpp">
<Filter>源文件\Helper</Filter>
</ClCompile>
<ClCompile Include="CheckCovered.cpp">
<Filter>源文件\Module</Filter>
</ClCompile>
</ItemGroup>
</Project>
7 changes: 5 additions & 2 deletions CustomDesktop/PluginManager.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "stdafx.h"
#include "PluginManager.h"
#include <EventHelper.h>
#include "Global.h"
#include <CDEvents.h>
#include <CDAPI.h>
#include <sstream>


namespace cd
Expand All @@ -20,7 +20,10 @@ namespace cd
plugin.m_module = LoadLibraryW(path);
if (plugin.m_module == NULL)
{
_RPTFW1(_CRT_ERROR, L"加载插件失败:%s\n", path);
_RPTFW1(_CRT_WARN, L"加载插件失败:%s\n", path);
std::wostringstream oss;
oss << L"加载插件失败" << path;
MessageBoxW(NULL, oss.str().c_str(), L"CustomDesktop", MB_OK);
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions CustomDesktop/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "BufferedRendering.h"
#include "CDAPIModule.h"
#include "PluginManager.h"
#include "CheckCovered.h"
#include <CDEvents.h>
using namespace cd;

Expand Down Expand Up @@ -46,6 +47,8 @@ namespace
_RPTF0(_CRT_WARN, "HookDesktop::GetInstance().Uninit();\n");
HookDesktop::GetInstance().Uninit();

CheckCovered::GetInstance().Uninit();

_RPTF0(_CRT_WARN, "g_preUnloadEvent();\n");
g_preUnloadEvent();
// 卸载本模块之前要释放所有插件否则卸载不掉
Expand Down Expand Up @@ -76,12 +79,13 @@ namespace
if (pos != std::string::npos)
g_global.m_cdDir.resize(pos + 1);

g_fileListWndProcEvent.AddListener(OnFileListWndProc);

InitModule(BufferedRendering)
InitModule(HookDesktop)
InitModule(CDAPIModule)
InitModule(PluginManager)

g_fileListWndProcEvent.AddListener(OnFileListWndProc);
InitModule(CheckCovered)

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions Include/CDEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace cd
{
extern CD_API PostEvent<> g_preUnloadEvent; // 准备卸载,在处理消息时触发
extern CD_API PostEvent<> g_desktopCoveredEvent; // 桌面被遮挡了,在处理消息时触发
extern CD_API PostEvent<> g_desktopUncoveredEvent; // 桌面从被遮挡恢复,在处理消息时触发

extern CD_API PreEvent<HDC&> g_preDrawBackgroundEvent; // 画桌面背景前被调用,用来取消画背景节省CPU,参数:目标DC
extern CD_API PostEvent<HDC&> g_postDrawBackgroundEvent; // 画完桌面背景后被调用,参数:目标DC
Expand Down
2 changes: 2 additions & 0 deletions Plugin/VideoDesktop/VideoDesktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ VideoDesktop::VideoDesktop(HMODULE hModule) :
WM_GRAPHNOTIFY(cd::GetCustomMessageID())
{
// 监听事件
cd::g_desktopCoveredEvent.AddListener([this]{ m_curPlayer->PauseVideo(); return true; }, m_module);
cd::g_desktopUncoveredEvent.AddListener([this]{ m_curPlayer->RunVideo(); return true; }, m_module);
cd::g_preDrawBackgroundEvent.AddListener([](HDC&){ return false; }, m_module);
cd::g_postDrawBackgroundEvent.AddListener(std::bind(&VideoDesktop::OnDrawBackground, this, std::placeholders::_1), m_module);
cd::g_fileListWndProcEvent.AddListener(std::bind(&VideoDesktop::OnFileListWndProc, this, std::placeholders::_1,
Expand Down

0 comments on commit 75414fe

Please sign in to comment.