Hello, I'm working on some project and I need your help guys. I'm just a beginner. I searched the forums for a related topic but couldn't found any resolved answer. So how can I make start my external application "DXSETUP.exe" at center screen position using this code? Thanks.

Private Sub Panel3_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseClick
    Try
      Me.Hide()
      Threading.Thread.Sleep(2000)
      With dx.StartInfo
        .UseShellExecute = True
        .FileName = Application.StartupPath & "\redist\directx_Jun2010_redist\DXSETUP.exe"
      End With
      dx.Start()
      dx.WaitForExit()
      dx.Close()
      Threading.Thread.Sleep(2000)
      Try
        Shell(Application.StartupPath & "\Setup.exe", vbNormalFocus)
      Catch ex As Exception
        gui_error1.ShowDialog()
      End Try
      Application.Exit()
    Catch ex As Exception
      gui_error2.ShowDialog()
    End Try
  End Sub

Recommended Answers

All 11 Replies

Have a look at THIS. under the vb.net heading.

Have a look at the code below. I'm not sure if it will work for you.

Imports System.Windows

Module FileParse

Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Int32, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Boolean) As Boolean

Friend ApplicationProcess As System.Diagnostics.Process

Friend ApplicationHandle As Long

Public Function OpenApplication()

ApplicationProcess = System.Diagnostics.Process.Start("c:\testapp.exe")

        System.Threading.Thread.Sleep(1000)

        ApplicationHandle = ApplicationProcess.MainWindowHandle

        MoveWindow(ApplicationHandle, 600, 600, 600, 600, True)'Change the dimensions to get the center of your screen

        Return (True)

    End Function

End Module

Thanks for trying but that didn't work. I got errors within a code.

Declarations expected.

Imports System.Windows

Module FileParse


Using this code: MoveWindow(ApplicationHandle, 600, 600, 600, 600, True) doesn't satisfy me very much 'cause if user has smaller monitor than mine it won't automatically detect end users screen size. And it might not start at center screen position.

Let me play around with the code and post a solution for you a bit later on.:)

Much appreceated :)

See if THIS site will help.

Also try and determine the screen resolution and center the window according to this using the code I first supplied...

Public Function ScreenResolution() As String
        Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
        Return intX & " X " & intY
End Function

Thanks that's very helpful.

I received a code that works from other forum. Here it is:

Imports System.Runtime.InteropServices
Imports System.Diagnostics

 <DllImport("user32.dll")> _
 Public Shared Function 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) As Boolean
 End Function

 <DllImport("user32.dll")> _
 Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
 End Function
  
 <DllImport("user32.dll", SetLastError:=True)> _
 Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, <Out()> ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
 End Function

 <StructLayoutAttribute(LayoutKind.Sequential)> _
 Private Structure RECT
  Public left As Integer
  Public top As Integer
  Public right As Integer
  Public bottom As Integer
 End Structure

Public Sub OpenExternalAtCenter(ByVal appName As String)
    Dim proc As New System.Diagnostics.Process
    proc.StartInfo.FileName = appName
    proc.Start()
    proc.WaitForInputIdle(3000)
    Try
      If SetForegroundWindow(proc.MainWindowHandle) Then 'check if there is handle to the window
        Dim rec As RECT 'structure to get size and location 
        If Not proc.MainWindowHandle.Equals(IntPtr.Zero) Then
          If GetWindowRect(proc.MainWindowHandle, rec) Then
            Dim WidthSize As Integer = rec.right - rec.left 'Width size
            Dim HeigthSize As Integer = rec.bottom - rec.top 'Heigth Size
            'x and y location : divide screen width/3 and heigth/ 3
            Dim x As Integer = CInt(SystemInformation.WorkingArea.Width / 3)
            Dim y As Integer = CInt(SystemInformation.WorkingArea.Height / 3)
            'call MoveWindow ApI to move the windows
            MoveWindow(proc.MainWindowHandle, x, y, WidthSize, HeigthSize, True)
          End If
        End If
      End If
    Catch ex As Exception

    End Try


  End Sub

'============USAGE===========
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  OpenExternalAtCenter(Application.StartupPath & "\redist\directx_Jun2010_redist\DXSETUP.exe")

End Sub

However if I change this line as seen in screenshot
http://img408.imageshack.us/img408/8159/10807423.png

from

.FileName = OpenExternalAtCenter(Application.StartupPath & "\redist\directx_Jun2010_redist\DXSETUP.exe")

to

OpenExternalAtCenter(Application.StartupPath & "\redist\directx_Jun2010_redist\DXSETUP.exe")

I get an error "Cannot start process because a file name has not been provided." I think it shows because I deleted this line ".FileName =" Is there any other way to get around this issue?

commented: Well done in doing some research as well! +4

You have to let the system (App) know that you want to open another file by using -

.FileName ....

.

OpenExternalAtCentre only calls the way the file will be opened AFTER the filename has been specified.:)

I gave you some rep for trying on your own AND posting it here for all to see....!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.