我正在尝试了解如何检测Windows桌面Aero Peek模式是否已启用。特别是我需要检测我的窗口内容是否显示或绘制为具有透明背景的框架。我知道我可以从Aero Peek中排除我的窗口,但这不是我现在所需要的。
TIA
答案 0 :(得分:3)
这就是你想要的吗?
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
public bool IsAeroActive()
{
// Check if Aero is enabled;
if (DwmIsCompositionEnabled())
{
return true;
}
else
{
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
bool aeroEnabled = IsAeroActive();
if (aeroEnabled)
{
MessageBox.Show("Aero is enabled.");
}
else
{
MessageBox.Show("Aero is disabled.");
}
}
答案 1 :(得分:2)
当用户通过将鼠标悬停在任务栏图标上偷看窗口时,您的桌面将进入此“ Aero Peek ”模式。您可以使用windows event hook跟踪是否显示“任务切换器”对象,并结合DWM模式,它应该告诉您用户是否正在窥视窗口。下面是我为测试这个想法而做的一个应用程序(c ++,如果将它转换为c#,请告诉我。)
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <objbase.h>
#include <Oleacc.h>
#include <iostream>
#define THREAD_MESSAGE_EXIT WM_USER + 2000
HWINEVENTHOOK eventHook;
HWND taskSwitcherHwnd = 0;
// process event
void CALLBACK HandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
LONG idObject, LONG idChild,
DWORD dwEventThread, DWORD dwmsEventTime)
{
if (event == EVENT_OBJECT_SHOW)
{
IAccessible* pAcc = NULL;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);
if (hr == S_OK && pAcc != NULL)
{
BSTR accName;
pAcc->get_accName(varChild, &accName);
if (wcscmp(accName, L"Task Switcher")==0)
{
std::cout << "Aero Peek on\n";
taskSwitcherHwnd = hwnd;
}
SysFreeString(accName);
pAcc->Release();
}
}
else if (event == EVENT_OBJECT_HIDE && taskSwitcherHwnd!=0 && taskSwitcherHwnd==hwnd)
{
std::cout << "Aero Peek off\n";
taskSwitcherHwnd = 0;
}
}
// thread proc for messages processing
// needed for event's hook to work
DWORD WINAPI TreadProc(LPVOID n)
{
std::cout << "InitializeEventHook\n";
CoInitialize(NULL);
eventHook = SetWinEventHook(
EVENT_OBJECT_SHOW, EVENT_OBJECT_HIDE,
NULL, HandleWinEvent, 0, 0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message==THREAD_MESSAGE_EXIT)
{
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
std::cout << "ShutdownEventHook\n";
UnhookWinEvent(eventHook);
CoUninitialize();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Detect Aero Peek\n";
DWORD threadId;
int value = 0;
HANDLE hThread = CreateThread( NULL, 0, TreadProc, &value, 0, &threadId);
char a;
std::cin >> a;
PostThreadMessage(threadId, THREAD_MESSAGE_EXIT, 0, 0);
WaitForSingleObject(hThread, 10000);
CloseHandle(hThread);
return 0;
}
希望这有帮助,尊重
答案 2 :(得分:0)
如果您从Windows注册表中读取,则可以找到Aero Peek的状态
\ HKEY_CURRENT_USER \ SOFTWARE \微软\的Windows \ DWM
名为 EnableAeroPeek 的DWORD值是否设置如下:
1 =已启用 0 =已禁用
只需比较0或1即可了解AeroPeek是否已开启。
在C#中是这样的:
Using Microsoft.Win32;
...
RegistryKey AeroPeek = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\DWM", true);
if ((int)AeroPeek.GetValue("EnableAeroPeek") == 1)
{
MessageBox.Show("Aero Peek is ON");
}
else MessageBox.Show("Aero Peek is OFF");
您也可以更改这些值,并立即改变Aero Peek状态。