我创建了一个流程。 那个有一个MainWindow我想要SendKeys.Send(“+ F”)(CTRL + F)来,但我不知道怎么做。
那怎么办呢?
答案 0 :(得分:2)
答案 1 :(得分:1)
您需要以下内容将焦点设置到外部窗口:
public class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
Process[] process = Process.GetProcessesByName("notepad");
if (process.Length > 0)
SetForegroundWindow(process[0].MainWindowHandle);
}
}
答案 2 :(得分:0)
希望以下有所帮助。这个最大化的WMP然后发送Ctrl + P来播放暂停的音乐:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
IntPtr handle = FindWindow(null, "Windows Media Player");
if (handle != IntPtr.Zero)
{
// Maximize WMP
ShowWindow(handle, (uint) WindowShowStyle.Maximize);
// Use SwitchToThisWindow(handle, false) OR SetForegroundWindow(handle)
SetForegroundWindow(handle);
// Make sure the window is brought to the froeground
Thread.Sleep(200);
// Use SendKeys OR SendInput API
SendKeys.SendWait("^p");
// Minimize WMP
ShowWindow(handle, (uint)WindowShowStyle.Minimize);
}