Question Process.Start() specify programs window size

vodkasoda

Member
Joined
Jun 16, 2011
Messages
15
Programming Experience
10+
From a VB.net program I am opening a .txt file ...

VB.NET:
Process.Start("Notepad.exe", myTables)

is there anyway of specifying the window size of the .txt file's window from my VB.net code ?!?

It's not going to be a show-stopper if I can't do it, but it would be nice to be able to ...
 
You can use SetWindowPos Windows API function with Process.MainWindowHandle.
 
Here is what you need to know: SetWindowPos function (Windows)
and here is VB.Net definitions you can use to make the call: pinvoke.net: setwindowpos (user32)

For your information, both these links were among the first three matches I got when I searched web for "SetWindowPos".

and an example call:
        Dim p = Process.Start("Notepad.exe", myTables)
        p.WaitForInputIdle(500)
        SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 10, 10, 400, 400, SetWindowPosFlags.DoNotChangeOwnerZOrder)
 
Appreciated, JohnH, and I did Google it, but it's difficult to know where to start ... I particularly dislike the MSDN Microsoft site for something that you don't already understand, the explanation is normally to vague for the beginner.

I'll give this a shot, thanks ...
 
Back again ...

OK, I have put this into my program but it doesn't appear to be working ...

VB.NET:
If File.Exists(myTables) Then            
    Dim p = Process.Start("Notepad.exe", myTables)
    p.WaitForInputIdle(2000)
    SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 0, 0, 60, 360, 0) 'SetWindowPosFlags.DoNotChangeOwnerZOrder)
End If

VB.NET:
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function

The .txt file opens on the "Dim p=" statement, and is the same size as the last .txt document that was closed, I have messed around with the parameters in the Function call but nothing seems to make any difference ... what am I doing wrong ?!?
 
Sorry, I can't make head nor tail of it, when I make the change that is on the pinvoke.net setwindowpos(user32) page, I just get an error (as I did in the first place which is why I wasn't using it) that says "'SetWindowPosFlags' is not defined." ... I don't know what I am supposed to define, every example I find is for C# !!!
 
Back again ...

OK, I have put this into my program but it doesn't appear to be working ...

VB.NET:
If File.Exists(myTables) Then            
    Dim p = Process.Start("Notepad.exe", myTables)
    p.WaitForInputIdle(2000)
    SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 0, 0, 60, 360, 0) 'SetWindowPosFlags.DoNotChangeOwnerZOrder)
End If

VB.NET:
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function

The .txt file opens on the "Dim p=" statement, and is the same size as the last .txt document that was closed, I have messed around with the parameters in the Function call but nothing seems to make any difference ... what am I doing wrong ?!?
Try this:
Dll import:
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean
End Function

<Flags> _
Private Enum SetWindowPosFlags As UInteger
    ''' <summary>If the calling thread and the thread that owns the window are attached to different input queues,
    ''' the system posts the request to the thread that owns the window. This prevents the calling thread from
    ''' blocking its execution while other threads process the request.</summary>
    ''' <remarks>SWP_ASYNCWINDOWPOS</remarks>
    SynchronousWindowPosition = &H4000
    ''' <summary>Prevents generation of the WM_SYNCPAINT message.</summary>
    ''' <remarks>SWP_DEFERERASE</remarks>
    DeferErase = &H2000
    ''' <summary>Draws a frame (defined in the window's class description) around the window.</summary>
    ''' <remarks>SWP_DRAWFRAME</remarks>
    DrawFrame = &H20
    ''' <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to
    ''' the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE
    ''' is sent only when the window's size is being changed.</summary>
    ''' <remarks>SWP_FRAMECHANGED</remarks>
    FrameChanged = &H20
    ''' <summary>Hides the window.</summary>
    ''' <remarks>SWP_HIDEWINDOW</remarks>
    HideWindow = &H80
    ''' <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the
    ''' top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter
    ''' parameter).</summary>
    ''' <remarks>SWP_NOACTIVATE</remarks>
    DoNotActivate = &H10
    ''' <summary>Discards the entire contents of the client area. If this flag is not specified, the valid
    ''' contents of the client area are saved and copied back into the client area after the window is sized or
    ''' repositioned.</summary>
    ''' <remarks>SWP_NOCOPYBITS</remarks>
    DoNotCopyBits = &H100
    ''' <summary>Retains the current position (ignores X and Y parameters).</summary>
    ''' <remarks>SWP_NOMOVE</remarks>
    IgnoreMove = &H2
    ''' <summary>Does not change the owner window's position in the Z order.</summary>
    ''' <remarks>SWP_NOOWNERZORDER</remarks>
    DoNotChangeOwnerZOrder = &H200
    ''' <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to
    ''' the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent
    ''' window uncovered as a result of the window being moved. When this flag is set, the application must
    ''' explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary>
    ''' <remarks>SWP_NOREDRAW</remarks>
    DoNotRedraw = &H8
    ''' <summary>Same as the SWP_NOOWNERZORDER flag.</summary>
    ''' <remarks>SWP_NOREPOSITION</remarks>
    DoNotReposition = &H200
    ''' <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary>
    ''' <remarks>SWP_NOSENDCHANGING</remarks>
    DoNotSendChangingEvent = &H400
    ''' <summary>Retains the current size (ignores the cx and cy parameters).</summary>
    ''' <remarks>SWP_NOSIZE</remarks>
    IgnoreResize = &H1
    ''' <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary>
    ''' <remarks>SWP_NOZORDER</remarks>
    IgnoreZOrder = &H4
    ''' <summary>Displays the window.</summary>
    ''' <remarks>SWP_SHOWWINDOW</remarks>
    ShowWindow = &H40
End Enum


'Code
If File.Exists(myTables) Then            
    Dim p = Process.Start("Notepad.exe", myTables)
    SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 0, 0, 60, 360, SetWindowPosFlags.DoNotChangeOwnerZOrder)
    p.WaitForInputIdle(2000)
End If
 
Sorry, I can't make head nor tail of it, when I make the change that is on the pinvoke.net setwindowpos(user32) page, I just get an error (as I did in the first place which is why I wasn't using it) that says "'SetWindowPosFlags' is not defined." ... I don't know what I am supposed to define, every example I find is for C# !!!
SetWindowPosFlags is defined where I directed you, you're supposed to copy that as you did with the managed function definition.
 
Thank you, both of you ... my program now runs again, but it still doesn't do anything to the size of the .txt window, it opens it the exact same size as the last .txt window that was closed.

The code now says :

VB.NET:
Dim aaa As Int16 = 60
Dim bbb As Int16 = 80


If File.Exists(myTables) Then
    Dim p = Process.Start("Notepad.exe", myTables)
    SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 0, 0, aaa, bbb, SetWindowPosFlags.DoNotChangeOwnerZOrder)
    p.WaitForInputIdle(500)
End If

As you can see, I made the cx & cy parameters into variables so that I can play around with the size as I debug, but nothing changes :confusion: !!!
 
vodkasoda said:
Dim p = Process.Start("Notepad.exe", myTables)
SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 0, 0, aaa, bbb, SetWindowPosFlags.DoNotChangeOwnerZOrder)
p.WaitForInputIdle(500)
There is a vital difference with your last code and my example. I will give you one clue, you can't resize a window that does not yet exist.
 
I copied and pasted the code from above earlier, I have it back as your code now & it works, but seemingly only intermittently ... will it help to increase the WaitForInputIdle parameter ?
 
If file to be loaded is large, or computer is busy, it can take longer than half a second before it is ready. You can wait for it indefinitely by not specifying a timeout, but then you should spin this off from a secondary thread and not risk tying up your UI thread.
 
OK John, that's great, I moved the code up a bit to before I was opening a Word document & it now works great, thanks for all your help ...
 
Back
Top