Link to home
Start Free TrialLog in
Avatar of jaredg27
jaredg27

asked on

Getting a hold of and manipulating windows in VB

I'm trying to get the handle (or whatever VB calls it) of a few windows whose title captions I know.  Once I have a window or windows I want to be able to change their captions, hide them, miminize them, etc...  Some general information on this topic and the associated commands would be great :).

Thanks!

~Jared
Avatar of JonFish85
JonFish85

What I did was use Spy++ to get the windows "name". THen use FindWindow to get the window's handle, and use SendMessage to send commands to the window
Avatar of jaredg27

ASKER

I'm playing around with Spy++ now and through MSDN I just came across Find++ (just before I saw your response).  What are some other functions that could be used?  And do all of these need to be imported from VC?

I think I'm going to find most of the functions.  But as I rarely program in VB is there any chance you/someone could write just a few lines that would show a sample of how to catch a window of a certain type once it pops up and then minimize it.

For example, IE's main window has class IEFrame, and "Microsoft Internet Explorer" is always in the caption.  What would one write to capture an IE window when it opens and then minimize it?

That's probably a lot to ask, but if it's too much then let me know.
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
www.allapi.net is a cool ressource for such (download the API guide and API toolshed). There (and at http://search.microsoft.com) search for the API functions declared below.
Other ressources:
HOWTO: Suppress Maximize & Minimize Buttons on MDI Parent Form
http://support.microsoft.com/support/kb/articles/Q137/0/33.asp
HOWTO: Limit a Window's Minimum and Maximum Size
http://support.microsoft.com/support/kb/articles/Q185/7/33.ASP
And, of course, the VS API Text Viewer.

Here's a sample I tinkered:
-----------------------------------------------------------
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SetWindowText Lib "User32" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String) As Long

Private Declare Function SetWindowLong Lib "User32" Alias _
   "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
   ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "User32" Alias _
   "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) _
   As Long

Private Declare Sub 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)

Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40

Const SW_MAXIMIZE = 3
Const SW_MINIMIZE = 6
Const SW_NORMAL = 1
Const SW_SHOWMAXIMIZED = 3

Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const GWL_STYLE = (-16)

Private Sub Form_Load()
    Dim WinWnd As Long
    Dim Ret As String
   
    ' Ask for a Window title
    Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
   
    ' Search the window
    WinWnd = FindWindow(vbNullString, Ret)
    If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
   
    ' Change the window's caption
    Call SetWindowText(WinWnd, "some caption")

    ' Remove min and max buttons
    Dim L As Long
    L = GetWindowLong(WinWnd, GWL_STYLE)
    L = L And Not (WS_MINIMIZEBOX)
    L = L And Not (WS_MAXIMIZEBOX)
    L = SetWindowLong(WinWnd, GWL_STYLE, L)
   
    ' Manipulate window state
    ' Happy experimenting!
    SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SW_MINIMIZE
End Sub
Thank you so much!
glad i could help  :-)
for some reason my browser seems to be posting every other comment i type...  but i had written:

thank you robbert, as well.  i had read your response after I had already tried out AzraSounds' suggestions and given him points (even though it posted it afterwards).  Both of you answered my question and, again, thanks.