Reputation: 16148
I am starting an executable several times like this
Process proc = new Process();
proc.StartInfo.FileName = path + "/BuiltGame.exe";
proc.Start();
Process proc1 = new Process();
proc1.StartInfo.FileName = path + "/BuiltGame.exe";
proc1.Start();
Now I want to resize and move the spawned windows.
I am currently using MoveWindow and FindWindow
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string windowClass, string title);
Initially I thought that I could just use the handle from the spawned process
MoveWindow(proc.Handle, 0, 0, 100, 100, true);
But it didn't work and I tried to use FindWindow
IntPtr Handle = FindWindow(null,"MyWindowTitle")
Which indeed worked and the returned handle from FindWindow
is a different one that from Process.Handle
After that I tried to use
MoveWindow(proc.MainWindowHandle, 0, 0, 100, 100, true);
But MainWindowHandle
is just 0.
The problem that I now have is that I want to start multiple processes and get the correct window handle from each window but FindWindow
only returns the first one.
How would I do this?
Upvotes: 1
Views: 5301
Reputation: 56
I used this when launching multiple windows for a game, but I'm also waiting a few seconds for it to start up and show a window. If you're getting the MainWindowHandle before the window has a chance to initialize, then this may be some of the issue. This lets me set each position as I'm cycling through processes to launch, before I continue to the next
// Launch each process
using (Process myProcess = new Process())
{
myProcess.StartInfo.FileName = Folder + "\\executable.exe";
myProcess.StartInfo.WorkingDirectory = Folder;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
// Wait for process to start up, default 2000ms set elsewhere
await Task.Delay(LaunchDelayTime);
// If found, position it.
if (myProcess.MainWindowHandle != IntPtr.Zero)
{
// Move to WindowLeft x WindowTop, set elsewhere
SetWindowPos(myProcess.MainWindowHandle, IntPtr.Zero, (int)WindowLeft, (int)WindowTop, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
}
Upvotes: 0
Reputation: 612794
Call EnumWindows
to enumerate top-level windows. For each such window, call GetWindowText
to find out its text which you can then compare against your target value.
If you are looking for windows in a specific process, use GetWindowThreadProcessId
.
Upvotes: 3