none
How to start and then control the properties of an application from my form? RRS feed

  • Question

  • Hello,

    I was wondering if somebody could help me out with code for actually controlling the properties (size, location on screen and, finally, visibility of window) of a window that I open from the vb form.

    I've got this so far; I have no idea how I mustered up the mental strength to think of such an advanced line of code, hopefully you will all understand it! :P

        Private Sub Project_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Process.Start("C:\ProgramData\Example.exe")
        End Sub

    Thanks for the help everyone!

    Saturday, October 21, 2017 11:07 PM

All replies

  • Oh, and one final thing I'd like to control is the window state of the opened process - I'd like to make it borderless.

    Thanks so much and Merry 21st of Oct!

    Saturday, October 21, 2017 11:09 PM
  • I was wondering if somebody could help me out with code for actually controlling the properties (size, location on screen and, finally, visibility of window) of a window that I open from the vb form.

    You need to use the .Start overload of the Process class that returns a Process object:
    https://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx

    With that object you can manipulate some properties.
    https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx

    For instance, if you access the window handle, you can send a message to the window message queue:
    http://www.pinvoke.net/default.aspx/user32.sendmessage


    Saturday, October 21, 2017 11:23 PM
  • Use of the Windows API is not as easy as you would like. You might need to spend some time learning. See Window Features to get started. The following VB console program will execute Notepad the wait for the window to be shown (that is usually very important) then use GetWindowRect to get the window position and size then wait a second then reduce the size of the window. Note that if you execute this multiple times then the size will be reduced to a minimum but you can just resize Notepad to a normal size. If we needed to we could add code that increases the size if the size is too small but this is just a sample.

    Imports System
    Imports System.Diagnostics
    Imports System.Runtime.InteropServices
    Imports System.Threading
    
    Namespace StartProcessControl_Window
    	Friend Class Program
    
    		<DllImport("user32.dll")>
    		Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
    		End Function
    
    		<DllImport("user32.dll", CharSet := CharSet.Auto, CallingConvention := CallingConvention.StdCall, ExactSpelling := True, SetLastError := True)>
    		Friend Shared Sub MoveWindow(ByVal hwnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean)
    		End Sub
    
    		<StructLayout(LayoutKind.Sequential)>
    		Public Structure RECT
    			Public Left As Integer ' x position of upper-left corner
    			Public Top As Integer ' y position of upper-left corner
    			Public Right As Integer ' x position of lower-right corner
    			Public Bottom As Integer ' y position of lower-right corner
    		End Structure
    
    		Shared Sub Main(ByVal args() As String)
    			Dim Notepad As Process = Process.Start("Notepad")
    			Notepad.WaitForInputIdle()
    			Dim rct As RECT = Nothing
    
    			If Not GetWindowRect(Notepad.MainWindowHandle, rct) Then
    				Console.WriteLine("GetWindowRect error")
    				Return
    			End If
    			Console.WriteLine("Top:{0}, Left:{1}, Bottom:{2}, Right:{3}", rct.Top, rct.Left, rct.Bottom, rct.Right)
    			Thread.Sleep(1000)
    			rct.Top += 200
    			rct.Left += 200
    			rct.Bottom -= 200
    			rct.Right -= 200
    			MoveWindow(Notepad.MainWindowHandle, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, True)
    		End Sub
    	End Class
    End Namespace
    



    Sam Hobbs
    SimpleSamples.Info

    Sunday, October 22, 2017 3:38 AM