我正在使用服务中的 GetForegroundWindow(),因为我想监视焦点窗口,但由于交互用户问题,该函数在 Windows 7 上返回 null。有什么方法可以让我在服务中获得焦点窗口?
HWINSTA hWinStaUser = OpenWindowStation(_T("WinSta0"), FALSE, MAXIMUM_ALLOWED);
if (SetProcessWindowStation(hWinStaUser))
{
/* -- Open user's desktop "Default". -- */
HDESK hDeskUser = OpenInputDesktop(0,FALSE,MAXIMUM_ALLOWED);
//HDESK hDeskUser = OpenDesktop(_T("Default"), 0, FALSE, MAXIMUM_ALLOWED);
/* -- Set thread desktop to "Default". -- */
if (SetThreadDesktop(hDeskUser))
{
HWND hwndActiveWin = GetForegroundWindow();
int idActive = GetWindowThreadProcessId(hwndActiveWin, NULL);
if ( AttachThreadInput(GetCurrentThreadId(), idActive, TRUE) )
{
HWND hwndFocused = GetFocus (); // focused control within the active window, i.e. focus throughout the screen
if(hwndFocused != NULL)
{
DWORD dwProcessID = 0;
GetWindowThreadProcessId(hwndFocused, &dwProcessID);
processPid = dwProcessID;
}
AttachThreadInput(GetCurrentThreadId(), idActive, FALSE);
}
}
最佳答案
您需要了解问题本身。由于安全风险以及可能有许多桌面(许多登录用户),服务无法与桌面交互。 GetForegroundWindow() 是与桌面的交互。没有简单的解决方法。复杂的涉及单独的进程作为 Windows 应用程序(不是服务)运行并与您的服务通信。
关于c++ - GetForegroundWindow 从 Windows 7 上的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582709/