By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Sizing an Application Window

P: n/a
Max
Hello,

Using VB.NET, I can activate an application by entering:

AppActivate(intProcessID), where intProcessID is the
process ID of a running application.

Is it possible to set the size and location of the
application window?

Thank you,

Max
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Sure,

you can obtain window handle for app main window and use MoveWindow API
function on it.

[DllImport("User32")]

internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int
nWidth, int nHeight, bool bRepaint);
HTH

Alex
"Max" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
Hello,

Using VB.NET, I can activate an application by entering:

AppActivate(intProcessID), where intProcessID is the
process ID of a running application.

Is it possible to set the size and location of the
application window?

Thank you,

Max

Nov 20 '05 #2

P: n/a
* "Max" <an*******@discussions.microsoft.com> scripsit:
Using VB.NET, I can activate an application by entering:

AppActivate(intProcessID), where intProcessID is the
process ID of a running application.

Is it possible to set the size and location of the
application window?


If you have its Window handle, you can try it with pinvoke on
'SetWindowPos' or 'MoveWindow'.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
Max
-----Original Message-----
* "Max" <an*******@discussions.microsoft.com> scripsit:
Using VB.NET, I can activate an application by entering:
AppActivate(intProcessID), where intProcessID is the
process ID of a running application.

Is it possible to set the size and location of the
application window?
If you have its Window handle, you can try it with

pinvoke on'SetWindowPos' or 'MoveWindow'.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.


Hello Herfried and thank you for the advice.

However, the 'SetWindowPos' and 'MoveWindow' functions
appear to be C functions. I am coding in VB.NET. How
can I access this functionality from VB.NET?

Thanks,

Max
Nov 20 '05 #4

P: n/a
* "Max" <an*******@discussions.microsoft.com> scripsit:
However, the 'SetWindowPos' and 'MoveWindow' functions
appear to be C functions. I am coding in VB.NET. How
can I access this functionality from VB.NET?


\\\
Public Declare Auto Function SetWindowPos Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As UInt32 _
) As Boolean

Public Declare Auto Function MoveWindow Lib "user32.dll"( _
ByVal hWnd As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal bRepaint As Boolean _
) As Boolean
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
Thank You !!!

-----Original Message-----
* "Max" <an*******@discussions.microsoft.com> scripsit:
However, the 'SetWindowPos' and 'MoveWindow' functions
appear to be C functions. I am coding in VB.NET. How
can I access this functionality from VB.NET?
\\\
Public Declare Auto Function SetWindowPos

Lib "user32.dll" ( _ ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As UInt32 _
) As Boolean

Public Declare Auto Function MoveWindow Lib "user32.dll"( _ ByVal hWnd As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal bRepaint As Boolean _
) As Boolean
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.

Nov 20 '05 #6

P: n/a
* <an*******@discussions.microsoft.com> scripsit:
Thank You !!!


If you have problems in calling the functions, please let us know.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #7

P: n/a
Thank you, again for your help.

I've tried the function MoveWindow. The function
executes without reporting an error, however, the window
is not resized and the function returns "False".

I have added System.Runtime.Interopservices to my project
imports.

My code is shown below (the relevant subroutine is
entitled "btnStartCAMMaster_Click"). I have commented
out the custom application that I am trying to start and
used "notepad.exe" instead:

Public Class frmAutoRun
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form
Designer.
InitializeComponent()

'Add any initialization after the
InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents pnlTop As System.Windows.Forms.Panel
Friend WithEvents splHorizontalTop As
System.Windows.Forms.Splitter
Friend WithEvents pnlBottom As
System.Windows.Forms.Panel
Friend WithEvents splHorizontalBottom As
System.Windows.Forms.Splitter
Friend WithEvents pnlLeft As
System.Windows.Forms.Panel
Friend WithEvents splVertical As
System.Windows.Forms.Splitter
Friend WithEvents sbrStatus As
System.Windows.Forms.StatusBar
Friend WithEvents pnlLeftTop As
System.Windows.Forms.Panel
Friend WithEvents Splitter1 As
System.Windows.Forms.Splitter
Friend WithEvents pnlLeftBottom As
System.Windows.Forms.Panel
Friend WithEvents mnuMain As
System.Windows.Forms.MainMenu
Friend WithEvents pnlRight As
System.Windows.Forms.Panel
Friend WithEvents btnStartCAMMaster As
System.Windows.Forms.Button
Friend WithEvents btnShowHideCAMMaster As
System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager
= New System.Resources.ResourceManager(GetType
(frmAutoRun))
Me.pnlTop = New System.Windows.Forms.Panel
Me.splHorizontalTop = New
System.Windows.Forms.Splitter
Me.pnlBottom = New System.Windows.Forms.Panel
Me.sbrStatus = New System.Windows.Forms.StatusBar
Me.splHorizontalBottom = New
System.Windows.Forms.Splitter
Me.pnlLeft = New System.Windows.Forms.Panel
Me.pnlLeftBottom = New System.Windows.Forms.Panel
Me.btnShowHideCAMMaster = New
System.Windows.Forms.Button
Me.btnStartCAMMaster = New
System.Windows.Forms.Button
Me.Splitter1 = New System.Windows.Forms.Splitter
Me.pnlLeftTop = New System.Windows.Forms.Panel
Me.splVertical = New System.Windows.Forms.Splitter
Me.mnuMain = New System.Windows.Forms.MainMenu
Me.pnlRight = New System.Windows.Forms.Panel
Me.pnlBottom.SuspendLayout()
Me.pnlLeft.SuspendLayout()
Me.pnlLeftBottom.SuspendLayout()
Me.SuspendLayout()
'
'pnlTop
'
Me.pnlTop.Dock =
System.Windows.Forms.DockStyle.Top
Me.pnlTop.Location = New System.Drawing.Point(0,
0)
Me.pnlTop.Name = "pnlTop"
Me.pnlTop.Size = New System.Drawing.Size(240, 56)
Me.pnlTop.TabIndex = 3
'
'splHorizontalTop
'
Me.splHorizontalTop.Dock =
System.Windows.Forms.DockStyle.Top
Me.splHorizontalTop.Location = New
System.Drawing.Point(0, 56)
Me.splHorizontalTop.Name = "splHorizontalTop"
Me.splHorizontalTop.Size = New System.Drawing.Size
(240, 3)
Me.splHorizontalTop.TabIndex = 4
Me.splHorizontalTop.TabStop = False
'
'pnlBottom
'
Me.pnlBottom.Controls.Add(Me.sbrStatus)
Me.pnlBottom.Dock =
System.Windows.Forms.DockStyle.Bottom
Me.pnlBottom.Location = New System.Drawing.Point
(0, 494)
Me.pnlBottom.Name = "pnlBottom"
Me.pnlBottom.Size = New System.Drawing.Size(240,
56)
Me.pnlBottom.TabIndex = 5
'
'sbrStatus
'
Me.sbrStatus.Location = New System.Drawing.Point
(0, 34)
Me.sbrStatus.Name = "sbrStatus"
Me.sbrStatus.Size = New System.Drawing.Size(240,
22)
Me.sbrStatus.TabIndex = 0
Me.sbrStatus.Text = "StatusBar1"
'
'splHorizontalBottom
'
Me.splHorizontalBottom.Dock =
System.Windows.Forms.DockStyle.Bottom
Me.splHorizontalBottom.Location = New
System.Drawing.Point(0, 491)
Me.splHorizontalBottom.Name
= "splHorizontalBottom"
Me.splHorizontalBottom.Size = New
System.Drawing.Size(240, 3)
Me.splHorizontalBottom.TabIndex = 6
Me.splHorizontalBottom.TabStop = False
'
'pnlLeft
'
Me.pnlLeft.Controls.Add(Me.pnlLeftBottom)
Me.pnlLeft.Controls.Add(Me.Splitter1)
Me.pnlLeft.Controls.Add(Me.pnlLeftTop)
Me.pnlLeft.Dock =
System.Windows.Forms.DockStyle.Left
Me.pnlLeft.Location = New System.Drawing.Point(0,
59)
Me.pnlLeft.Name = "pnlLeft"
Me.pnlLeft.Size = New System.Drawing.Size(200,
432)
Me.pnlLeft.TabIndex = 7
'
'pnlLeftBottom
'
Me.pnlLeftBottom.BackColor =
System.Drawing.SystemColors.Control
Me.pnlLeftBottom.Controls.Add
(Me.btnShowHideCAMMaster)
Me.pnlLeftBottom.Controls.Add
(Me.btnStartCAMMaster)
Me.pnlLeftBottom.Dock =
System.Windows.Forms.DockStyle.Fill
Me.pnlLeftBottom.Location = New
System.Drawing.Point(0, 99)
Me.pnlLeftBottom.Name = "pnlLeftBottom"
Me.pnlLeftBottom.Size = New System.Drawing.Size
(200, 333)
Me.pnlLeftBottom.TabIndex = 11
'
'btnShowHideCAMMaster
'
Me.btnShowHideCAMMaster.Dock =
System.Windows.Forms.DockStyle.Bottom
Me.btnShowHideCAMMaster.Location = New
System.Drawing.Point(0, 287)
Me.btnShowHideCAMMaster.Name
= "btnShowHideCAMMaster"
Me.btnShowHideCAMMaster.Size = New
System.Drawing.Size(200, 23)
Me.btnShowHideCAMMaster.TabIndex = 1
Me.btnShowHideCAMMaster.Text = "Show CAMMaster"
'
'btnStartCAMMaster
'
Me.btnStartCAMMaster.Dock =
System.Windows.Forms.DockStyle.Bottom
Me.btnStartCAMMaster.Location = New
System.Drawing.Point(0, 310)
Me.btnStartCAMMaster.Name = "btnStartCAMMaster"
Me.btnStartCAMMaster.Size = New
System.Drawing.Size(200, 23)
Me.btnStartCAMMaster.TabIndex = 0
Me.btnStartCAMMaster.Text = "Start CAMMaster"
'
'Splitter1
'
Me.Splitter1.Dock =
System.Windows.Forms.DockStyle.Top
Me.Splitter1.Location = New System.Drawing.Point
(0, 96)
Me.Splitter1.Name = "Splitter1"
Me.Splitter1.Size = New System.Drawing.Size(200,
3)
Me.Splitter1.TabIndex = 10
Me.Splitter1.TabStop = False
'
'pnlLeftTop
'
Me.pnlLeftTop.Dock =
System.Windows.Forms.DockStyle.Top
Me.pnlLeftTop.Location = New System.Drawing.Point
(0, 0)
Me.pnlLeftTop.Name = "pnlLeftTop"
Me.pnlLeftTop.Size = New System.Drawing.Size(200,
96)
Me.pnlLeftTop.TabIndex = 9
'
'splVertical
'
Me.splVertical.Location = New System.Drawing.Point
(200, 59)
Me.splVertical.Name = "splVertical"
Me.splVertical.Size = New System.Drawing.Size(3,
432)
Me.splVertical.TabIndex = 8
Me.splVertical.TabStop = False
'
'pnlRight
'
Me.pnlRight.Dock =
System.Windows.Forms.DockStyle.Fill
Me.pnlRight.Location = New System.Drawing.Point
(203, 59)
Me.pnlRight.Name = "pnlRight"
Me.pnlRight.Size = New System.Drawing.Size(37,
432)
Me.pnlRight.TabIndex = 9
'
'frmAutoRun
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(240, 550)
Me.Controls.Add(Me.pnlRight)
Me.Controls.Add(Me.splVertical)
Me.Controls.Add(Me.pnlLeft)
Me.Controls.Add(Me.splHorizontalBottom)
Me.Controls.Add(Me.pnlBottom)
Me.Controls.Add(Me.splHorizontalTop)
Me.Controls.Add(Me.pnlTop)
Me.Icon = CType(resources.GetObject
("$this.Icon"), System.Drawing.Icon)
Me.Menu = Me.mnuMain
Me.Name = "frmAutoRun"
Me.Text = "PentaLogix AutoPanel Control Center"
Me.pnlBottom.ResumeLayout(False)
Me.pnlLeft.ResumeLayout(False)
Me.pnlLeftBottom.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
Dim CAM As New Process
Dim intCAMProcessID As Integer
Dim hndCAMWindowHandle As IntPtr
Dim CAMStyle As New ProcessWindowStyle
Dim blnCAMMasterShowing As Boolean = False

Public Declare Auto Function SetWindowPos
Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As UInt32 _
) As Boolean

Public Declare Auto Function MoveWindow
Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal bRepaint As Boolean _
) As Boolean

Private Sub frmAutoRun_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Me.btnShowHideCAMMaster.Enabled = False
End Sub

Private Sub btnCheckListDirectory_MouseHover(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles btnStartCAMMaster.MouseHover
Me.btnStartCAMMaster.BackColor =
SystemColors.ControlLightLight
End Sub

Private Sub btnCheckListDirectory_MouseLeave(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles btnStartCAMMaster.MouseLeave
Me.btnStartCAMMaster.BackColor =
SystemColors.Control
End Sub

Private Sub btnStartCAMMaster_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnStartCAMMaster.Click
CAMStyle = ProcessWindowStyle.Normal
'CAM = CAM.Start("C:\Program
Files\Pentalogix\CAMMaster Beta\CAMMaster.exe")
CAM = CAM.Start("notepad.exe")
intCAMProcessID = CAM.Id
hndCAMWindowHandle = CAM.Handle
Dim blnItWorked As Boolean = False
blnItWorked = MoveWindow(hndCAMWindowHandle, 100,
100, 300, 200, True)
Me.btnShowHideCAMMaster.Enabled = True
Me.btnShowHideCAMMaster.Text = "Hide CAMMaster"
blnCAMMasterShowing = True

End Sub

Private Sub btnShowHideCAMMaster_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
btnShowHideCAMMaster.Click
If blnCAMMasterShowing = True Then
Me.btnShowHideCAMMaster.Text = "Show
CAMMaster"
CAMStyle = ProcessWindowStyle.Minimized
blnCAMMasterShowing = False
Else
Me.btnShowHideCAMMaster.Text = "Hide
CAMMaster"
AppActivate(intCAMProcessID)
CAMStyle = ProcessWindowStyle.Normal
blnCAMMasterShowing = True
End If
End Sub

End Class

Nov 20 '05 #8

P: n/a
* <an*******@discussions.microsoft.com> scripsit:
Public Declare Auto Function MoveWindow
Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal bRepaint As Boolean _
) As Boolean [...] Private Sub btnStartCAMMaster_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnStartCAMMaster.Click
CAMStyle = ProcessWindowStyle.Normal
'CAM = CAM.Start("C:\Program
Files\Pentalogix\CAMMaster Beta\CAMMaster.exe")
CAM = CAM.Start("notepad.exe")
intCAMProcessID = CAM.Id
hndCAMWindowHandle = CAM.Handle


'CAM.Handle' will return a process handle, not a window handle. You may
want to use 'CAM.MainWindowHandle' instead. I didn't test it, so please
let us know if it works.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #9

P: n/a
Max
That worked !

Thank you very much !!!

-----Original Message-----
* <an*******@discussions.microsoft.com> scripsit:
Public Declare Auto Function MoveWindow
Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal bRepaint As Boolean _
) As Boolean[...]
Private Sub btnStartCAMMaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnStartCAMMaster.Click
CAMStyle = ProcessWindowStyle.Normal
'CAM = CAM.Start("C:\Program
Files\Pentalogix\CAMMaster Beta\CAMMaster.exe")
CAM = CAM.Start("notepad.exe")
intCAMProcessID = CAM.Id
hndCAMWindowHandle = CAM.Handle


'CAM.Handle' will return a process handle, not a window

handle. You maywant to use 'CAM.MainWindowHandle' instead. I didn't test it, so pleaselet us know if it works.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.