[英]wpf how to get program focus form windows
i made program it work in background, i want this program only work on program i select it example: i created Auto clicker and i want to use this program just in games if the user go to any other program it doesn't work我让程序在后台工作,我希望这个程序只在程序 i select 上工作它的例子:我创建了自动点击器,我想在游戏中使用这个程序,如果用户 go 到任何其他程序它不起作用
this way i hope it is there这样我希望它在那里
if(Windows.Focus.NameProgram.ToString() == "Call of Duty Cold War")
{
// here all commands i will put it later.
}
here i mean what windows focus it now if it is Call of Duty Cold War then work, if not dont work (of course still running in the background)在这里我的意思是 windows 现在关注它,如果它是使命召唤冷战然后工作,如果不是不工作(当然仍在后台运行)
Use a function called GetForegroundWindow
from Windows API.使用来自 Windows API 的名为
GetForegroundWindow
的 function。
The implementation further down gets the current focused window.进一步向下的实现得到当前的焦点 window。
Documentation: GetForegroundWindow |文档: GetForegroundWindow | MS Docs
文档
public class GetFocusedWindow
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private static string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
static void Main(string[] args)
{
while (true)
{
Thread.Sleep(2000);
Console.WriteLine(GetActiveWindowTitle());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.