I'm hoping someone has an answer for me on this one. No matter what I try, I can't get the apps that I'm starting then snapping to a tableLayoutPanel1 to stop spamming the main monitor then moving to the second monitor and finally sizing to the panels. How can you force an exe to open on another monitor other than the primary monitor? I can't believe .NET has no way to do this without frantically trying to detect the window as fast as possible then move it.
Here's what I tried so far.
Process vncProcess = new Process();
vncProcess.StartInfo.FileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"VNCViewer.exe ");
//vncProcess.StartInfo.Arguments = @"-KeepAliveResponseTimeout=9999999 -UriSuppressConnectionPrompt=TRUE -Scaling=Fit -EnableToolbar=False " + ipAddress;
vncProcess.StartInfo.Arguments = @"-Monitor=" + devicename + "@ -KeepAliveResponseTimeout=9999999 -UriSuppressConnectionPrompt=TRUE -Scaling=Fit -EnableToolbar=False " + ipAddress;
vncProcess.StartInfo.UseShellExecute = true;
vncProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
vncProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
vncProcess.Start(); // Start the process
//vncProcess.WaitForInputIdle();
vncProcesses.Add(vncProcess);
// Function to check if the VNC Viewer's window title contains "LibVNCServer"
Task<bool> IsWindowReadyWithTitle(Process process)
{
// Refresh the process info
process.Refresh();
// Check if the main window handle is ready and the title contains "LibVNCServer"
return Task.FromResult(process.MainWindowHandle != IntPtr.Zero && (process.MainWindowTitle.Contains("LibVNCServer") || process.MainWindowTitle.Contains("(10")));
//|| process.MainWindowTitle.Contains("RealVNC")
}
while (!await IsWindowReadyWithTitle(vncProcess))
{
vncProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
vncProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
await Task.Delay(1); // Wait for 100ms before checking again
}
//ShowWindow(vncProcess.MainWindowHandle, SW_MINIMIZE);
// Poll until the process has a window handle.
//while (vncProcess.MainWindowHandle == IntPtr.Zero)
//{
// await Task.Delay(100); // Wait for 100ms before checking again.
// vncProcess.Refresh();
//}
MoveToMonitor(vncProcess.MainWindowHandle, monitorstart);
// Adjust window style first
int style = GetWindowLong(vncProcess.MainWindowHandle, GWL_STYLE);
style &= ~WS_MINIMIZEBOX; // Disable minimize button
style &= ~WS_MAXIMIZEBOX; // Disable maximize button
style &= ~WS_SYSMENU; // Disable system menu (and close button)
style &= ~WS_THICKFRAME; // Disable resizable border
style &= ~WS_CAPTION; // Remove window caption
style |= WS_CHILD; // Ensure it's a child window
SetWindowLong(vncProcess.MainWindowHandle, GWL_STYLE, style);
//SetParent(vncProcess.MainWindowHandle, this.Handle);
// Now set VNC as a child of the panel
SetParent(vncProcess.MainWindowHandle, vncPanel.Handle);
vncProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
// Resize event
vncPanel.Resize += (sender, e) =>
{
if (vncProcess != null && !vncProcess.HasExited)
{
MoveWindow(vncProcess.MainWindowHandle, 0, 0, vncPanel.Width, vncPanel.Height, true);
}
};
// Initial resize
MoveWindow(vncProcess.MainWindowHandle, 0, 0, vncPanel.Width, vncPanel.Height, true);