setwindowdisplayaffinity窗口过滤完整工程
时间: 2023-07-19 07:43:32 浏览: 63
这里提供一个完整的使用setwindowdisplayaffinity函数实现窗口过滤的Windows桌面应用程序工程示例,供参考:
```
#include <Windows.h>
// 枚举所有的显示器,找到次要显示器
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
HMONITOR* phMonitor = (HMONITOR*)dwData;
MONITORINFO mi = { sizeof(mi) };
GetMonitorInfo(hMonitor, &mi);
if (!(mi.dwFlags & MONITORINFOF_PRIMARY))
{
*phMonitor = hMonitor;
return FALSE;
}
return TRUE;
}
// 设置窗口的显示亲和性,使其只显示在指定的显示器上
void SetWindowDisplayAffinity(HWND hWnd, HMONITOR hMonitor)
{
RECT rcWork;
// 获取指定显示器的工作区域
MONITORINFO mi = { sizeof(mi) };
GetMonitorInfo(hMonitor, &mi);
rcWork = mi.rcWork;
// 将窗口的显示亲和性设置为不显示在除指定显示器外的其他所有显示器上
SetWindowDisplayAffinity(hWnd, WDA_MONITOR | WDA_EXCLUDEFROMCAPTURE, hMonitor);
}
// 窗口过滤测试程序
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口
HWND hWnd = CreateWindowW(L"BUTTON", L"Window Filter Test", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 找到次要显示器
HMONITOR hMonitor = NULL;
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&hMonitor);
// 设置窗口的显示亲和性,使其只在次要显示器上显示
SetWindowDisplayAffinity(hWnd, hMonitor);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
```
该程序会创建一个按钮窗口,并将其显示在主显示器上。然后,通过调用setwindowdisplayaffinity函数,将窗口的显示亲和性设置为不显示在次要显示器上,使其只在主显示器上显示。最后,程序进入消息循环,等待用户操作。