将焦点设置到窗口 [英] Set focus to a window

查看:337
本文介绍了将焦点设置到窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

1. 我有一个Windows服务应用程序。我正在尝试设置 使用进程名称和进程ID从一个窗口聚焦到另一个窗口。怎么能用vc ++做到这一点。 ?

1.  I have an windows service application.I am trying to set  focus from one window to another using process name and process id. How can do that using vc++. ?

2。如何使用进程名称,进程ID和进程句柄将焦点从一个窗口设置到其他窗口?

2. How to to set focus from one window to other window using process name , process id and process handle ?

提前致谢 

Thanks in advance 

推荐答案

您好,

感谢您在此处发帖。

这是一个简单的演示。

Here is a simple demo.

#include "pch.h"
#include <windows.h>
#include <iostream>

BOOL SetFocusWindowByPID(DWORD dwProcessID)
{
	BOOL bResult = FALSE;
	HWND h = GetTopWindow(0);
	while (h)
	{
		DWORD pid = 0;
		DWORD dwThreadId = GetWindowThreadProcessId(h, &pid);

		if (dwThreadId != 0)
		{
			if (pid == dwProcessID)
			{
				bResult = SetForegroundWindow(h);
				return bResult;
			}
		}

		h = GetNextWindow(h, GW_HWNDNEXT);
	}
	return FALSE;
}

int main()
{

    BOOL b = SetFocusWindowByPID(25468);

    std::cout << "b = " << b << std::endl;

    return 0;
}

当我使用记事本,计算器,任务管理器进程ID来测试它时,似乎工作正常。

Seems work fine when I use the Notepad, Calculator, Task Manager process id to test it.

也许它会帮助你。

祝福,

杰克


这篇关于将焦点设置到窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆