Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to position and resize an external program 1

Status
Not open for further replies.

FrankMars

Technical User
Dec 20, 2010
67
US
Hello, I am bringing to top the window of an external program (Microsoft Outlook) with the following code:

Code:
Private Declare Function BringWindowToTop Lib "user32" _
                         (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                         (ByVal lpClassName As Any, _
                          ByVal lpWindowName As Any) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1

Code:
Public Function WinToTop(WindowTitle As String)
   Dim iret As Long, THandle As Long
   
   THandle = FindWindow(vbEmpty, WindowTitle)
   If THandle <> 0 Then
      If IsIconic(THandle) > 0 Then
         ShowWindow THandle, SW_SHOWNORMAL
      Else
         BringWindowToTop (THandle)
      End If
   End If

End Function

Code:
Private Sub Command104_Click()

WinToTop "Microsoft Outlook"

End Sub

This works fine but I'd like to change the position and size of Outlook. How can this be done?

Thanks in advance.
 
Well, the easisiest way is to use SetWindowPos instead of BringWindowToTop - but if you do so then then you need to find the proper Outlook Window, not the hidden proxie window that you are currently Finding. And since the Title of the genuine window can change, it is probably easier to use the Outlook Class name rather than the Window name. SO something like the following:

Code:
[blue]Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" _
                         (ByVal lpClassName As Any, _
                          ByVal lpWindowName As Any) As Long
                          
Private Declare Function IsIconic Lib "User32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "User32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
[b]Private Declare Function SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long[/b]

Const SW_SHOWNORMAL = 1
[b]Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40[/b][/blue]

Code:
[blue]Public Function WinToTop([b]WindowClass[/b] As String)
   Dim iret As Long, THandle As Long
   
   THandle = FindWindow([b]WindowClass, 0&[/b])
   If THandle <> 0 Then
      If IsIconic(THandle) > 0 Then
         ShowWindow THandle, SW_SHOWNORMAL
      Else
         [b]SetWindowPos THandle, HWND_TOPMOST, 100, 100, 640, 480, SWP_SHOWWINDOW[/b]
      End If
   End If

End Function[/blue]


Code:
[blue]Private Sub Command104_Click()

WinToTop [b]"rctrl_renwnd32"[/b] [green]' Outlook class name[/green]

End Sub[/blue]
 
Thanks strongm, works great. Still developing so may be back with additional questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top