Starting a process and then modifying its size and position

tbfreefall

Member
Joined
Oct 17, 2007
Messages
11
Programming Experience
Beginner
I am using process.start() to start an application from withing mine. I need to be able to modify is size/position properties.
 
I assume that you mean a Windows app with a main form. In that case you can the MainWindowHandle of your process and use it to call the SetWindowPlacement API function.
 
SetWindowPos is a bit easier in ths case, as it doesn't use any structures...

VB.NET:
Expand Collapse Copy
<DllImport("User32.dll")> _
Public Shared Function SetWindowPos(ByVal hwnd As Intptr, ByVal hWndInsertAfter As Int32, 
ByVal x As Int32, ByVal y As Int32, ByVal cx As Int32, ByVal cy As Int32, 
ByVal wFlags As Int32) As Int32
 
SetWindowPos is a bit easier in ths case, as it doesn't use any structures...

VB.NET:
Expand Collapse Copy
<DllImport("User32.dll")> _
Public Shared Function SetWindowPos(ByVal hwnd As Intptr, ByVal hWndInsertAfter As Int32, 
ByVal x As Int32, ByVal y As Int32, ByVal cx As Int32, ByVal cy As Int32, 
ByVal wFlags As Int32) As Int32
That is quite true and I should have known that because I made that switch myself. I was thinking that SetWindowPos would set the position but not the size, which is obviously not the case. At least someone's on the ball. :thumb:
 
I assume that you mean a Windows app with a main form. In that case you can the MainWindowHandle of your process and use it to call the SetWindowPlacement API function.


MainWindowHandle is definately the piece that i was looking for, and thank you. However, when i use it, it returns a zero for the window handle. I am not sure what i am doing wrong.

Here is my code:
Dim myProcess As New Process
Dim myhWnd As Integer
Const HWND_NOTOPMOST As Long = -2
Const SWP_SHOWWINDOW As Long = &H40
myProcess.StartInfo.FileName = lstApplications.SelectedItem.ToString
myProcess.Start()
myhWnd = myProcess.MainWindowHandle
SetWindowPos(myhWnd, HWND_NOTOPMOST, txtAppLeft.Text, txtAppTop.Text, txtAppWidth.Text, txtAppHeight.Text, SWP_SHOWWINDOW)
 
Last edited:
VB.NET:
Expand Collapse Copy
Dim myProcess As Process 
Dim myhWnd As [B]Intptr[/B]
Const HWND_NOTOPMOST As [B]Int32[/B] = -2
Const SWP_SHOWWINDOW As [B]Int32[/B] =  &H40
myprocess = system.diagnostics.process.Start(lstApplications.SelectedItem.ToString)
myprocess.WaitForInputIdle()
myhWnd = myProcess.MainWindowHandle
SetWindowPos(myhWnd, HWND_NOTOPMOST, Cint(txtAppLeft.Text), 
Cint(txtAppTop.Text), Cint(txtAppWidth.Text), Cint(txtAppHeight.Text), SWP_SHOWWINDOW)

Try that, I've made changes to the SWP flags and the return value for the mainwindowhandle is an Intptr. Also added the WaitForInputIdle() method to ensure that the HWND has been created.
 
This is the exception i get when i run that:

************** Exception Text **************
System.InvalidOperationException: Process has exited, so the requested information is not available.
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.get_MainWindowHandle()
at VWC.frmApplication.lstApplications_DoubleClick(Object sender, EventArgs e) in Application.vb:line 629
at System.Windows.Forms.Control.OnDoubleClick(EventArgs e)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
 
Okay actually it only throws that exception after you try to run it a second time. However, the myhWnd = myProcess.MainWindowHandle still returns a "0" for the handle.
 
With the new code i still get the same exception. However, it now throws this exception even on the first time running this code. The exception occurs on the line "myhWnd = myProcess.MainWindowHandle".
 
Last edited:
I'll have to try this out. There's no reason why you shouldn't be getting a vaild HWND as long as the application you are trying to start has a GUI.

Check the HasExited Property to make sure that the process hasn't exited before you try to get the HWND.
 
Yes it has a GUI. I am testing it by opening a web browser. Judging by the error it is saying that the process is exiting before it retrieves the hWnd of its Main Window. I am not sure why this is happening. The window is opening and staying open.
 
Okay, i think that error was my fault. However, even without the error my hWnd value still remains at zero. I was trying to troubleshoot the problem and added a MsgBox(myhWnd) right after the myhWnd = myProcess.MainWindowHandle line and noticed that although the value in the MsgBox is zero, the MsgBox appears to be coming up before the main window has become visible. I think that it is a problem with the myProcess.WaitForInputIdle() line. I am trying to figure it out.
 
Back
Top