I created a Process. That one has a MainWindow I want to SendKeys.Send("+F") (CTRL+F) to, but I don't know how to do this.
So how is this done?
I created a Process. That one has a MainWindow I want to SendKeys.Send("+F") (CTRL+F) to, but I don't know how to do this.
So how is this done?
For Ctrl key you need to precede the key code with ^. something like:
SendKeys.Send("^F");
Check here for more information.
You'll need something like the following to set focus to an external window:
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);
}
}
Hope the following helps. This maximized WMP then sends Ctrl+P to play the paused music:
[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);
}