Jalaj

January 29, 2007

WinAPI : Working with Windows Handles

Filed under: Visual Basic, WinAPI — Jalaj @ 5:40 am

We last saw how to get handles to the windows. Now once you got the handle you can call various functions to do a variety of jobs as getting information regarding the window, or changing the window’s state or style. Let’s see some of these functions.

IsHungAppWindow, IsIconic, IsWindow, IsWindowVisible, IsZoomed functions take for parameter the handle to a window and returns “True” (non-zero) or “False” (zero).

IsHungAppWindow	- Checks if the window has stopped responding.
IsIconic		- Checks is the window is minimized
IsWindow		- Checks if given handle corresponds to valid window
IsWindowUnicode	- Checks if the windows is a native unicode window
IsWindowVisible	- Checks if the window is visible or not
IsZoomed		- Checks if the window is Maximized or not.

Declare Function IsHungAppWindow Lib “user32″ Alias “IsHungAppWindow” (ByVal hwnd As Long) As Long
Declare Function IsIconic Lib “user32″ Alias “IsIconic” (ByVal hwnd As Long) As Long
Declare Function IsWindow Lib “user32″ Alias “IsWindow” (ByVal hwnd As Long) As Long
Declare Function IsWindowUnicode Lib “user32″ Alias “IsWindowUnicode” (ByVal hwnd As Long) As Long
Declare Function IsWindowVisible Lib “user32″ Alias “IsWindowVisible” (ByVal hwnd As Long) As Long
Declare Function IsZoomed Lib “user32″ Alias “IsZoomed” (ByVal hwnd As Long) As Long

IsChild function determines if the given window is the child window of the window whose handle is passed through the second parameter. The function returns non-zero if true otherwise zero.

Declare Function IsChild Lib "user32" Alias "IsChild" (ByVal hWndParent As Long, ByVal hwnd As Long) As Long

AnimateWindow function allows you to produce special effects while showing(default) or hiding the window. The first parameter is the handle to the window. The second parameter is the time in milliseconds that the animation plays (default 200 milliseconds). The third parameter is flag that determines the animation to be payed and may be one or more of the constants as described below.

Public Declare Function AnimateWindow Lib "user32" (ByVal hwnd As Long, _
     ByVal dwTime As Long, ByVal dwFlags As Long) As Long

Const AW_SLIDE = &H40000 	' Uses Slide animation
Const AW_ACTIVATE = &H20000	' Activates the Window
Const AW_BLEND = &H80000	' Uses a Fade effect
Const AW_HIDE = &H10000		' Hides the window
Const AW_CENTER = &H10		' Makes window to Collapse/Expand
Const AW_HOR_POSITIVE = &H1	' Animates from Left to Right
Const AW_HOR_NEGATIVE = &H2	' Animates from Right to Left
Const AW_VER_POSITIVE = &H4	' Animates from Top to Bottom
Const AW_VER_NEGATIVE = &H8	' Animates from Bottom to Top

ArrangeIconicWindows function arranges all iconic (minimized) windows that are child windows of the the given window. The function returns zero if it fails otherwise a non-zero. BringWindowToTop brings the given window to top on the z-order. If the window is a top level window, it is activated otherwise it’s parent window gets activated.

CloseWindow function minimizes the given window, while OpenIcon function restores the minimized window and activates it. The DestroyWindow function destroys the given window, however before doing so it suitably messages the window so that the necessary routines can be carried by it.

These functions take for input the handle to the window and return non-zero when the function succeeds and zero when it fails.

Declare Function ArrangeIconicWindows Lib "user32" Alias "ArrangeIconicWindows" (ByVal hwnd As Long) As Long
Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long
Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long
Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long
Declare Function OpenIcon Lib "user32" Alias "OpenIcon" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long

EndTask function is called to forcibly close the given window. It requires, other than the handle to the window, a boolean for carrying a shutdown (must be false) and other boolean to force the closure.

Declare Function EndTask Lib "user32" Alias "EndTask" (ByVal hwnd As Long, _
    ByVal nShutDown as Long, ByVal nForce as Long, ) As Long

ShowWindow function sets the Show status of the given window. Other than the handle to the window, it takes a flag which specifies the manner in which the window is to be shown. It can be one of the constants as below.

Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_FORCEMINIMIZE = 11	' Forcefully minimize the window
Const SW_HIDE = 0		' Hides the window
Const SW_MAXIMIZE = 3		' Maximizes the window but does not activate it.
Const SW_SHOWMAXIMIZED = 3	' Maximizes adn activates the window
Const SW_MINIMIZE = 6		' Minimizes the window and activates the next top level window
Const SW_SHOWMINIMIZED = 2	' Minimises the window still keeping it activated
Const SW_RESTORE = 9		' Restore the window to current size and position
Const SW_SHOW = 5		' Activates and shows the window in current size and position
Const SW_SHOWDEFAULT = 10	' Activates and shows the window in default size and position
Const SW_SHOWMINNOACTIVE = 7	' Minimizes the window but does not activate it.
Const SW_SHOWNA = 8		' Show to current size and location but not activate it.
Const SW_SHOWNOACTIVATE = 4	' Show to recent size and location but not activate it.
Const SW_SHOWNORMAL = 1		' Activates and displays the window to normal size and position

ShowWindowAsync sets the state of the window created by a different thread. Teh parameters are similar to the above.

Declare Function ShowWindowAsync Lib "user32" Alias "ShowWindowAsync" ( _
    ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

MoveWindow changes the position and dimesion of the window. The function takes the handle to the window, x and y co-ordinates of the top-left corner and the width and the height, and a boolean to specify if the window needs to be repainted. The co-ordinates are releative to the top-left corner of the screen if the window is a top level window, otherwise relative to the client area of the parent window.

Declare Function MoveWindow Lib "user32" Alias "MoveWindow" ( _
    ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, _
    ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

SwitchToThisWindow function switches the focus to the given window and brings it to the foregraound. The function takes other than the handle to the window, a boolean which specifies if the action also alters the Alt-Tab sequence accordingly.

Declare Function SwitchToThisWindow(hWnd As Long, chState As Long) As Long

ShowOwnedPopups shows or hides(depending on the second parameter which is a boolean) all the pop-up windows owned by the specified window.

Declare Function ShowOwnedPopups Lib "user32" Alias "ShowOwnedPopups" ( _
    ByVal hwnd As Long, ByVal fShow As Long) As Long

4 Comments »

  1. Realy thanks for helpfull and clear information :) Nice page :)

    Comment by Michal — May 11, 2007 @ 8:38 am

  2. Thanks Michal

    Comment by Jalaj — May 11, 2007 @ 9:36 am

  3. [...] WinAPI : Working with Windows Handles [...]

    Pingback by Just Looking Back « Jalaj — May 28, 2007 @ 6:27 am

  4. [...] how to get them and more are described in posts Windows and Handles, Getting Window Handles, WinAPI : Working with Windows Handles, WinAPI : Prevent Setting foreground window, WinAPI : Getting Window [...]

    Pingback by The Blog Revisited - 4 « Jalaj — October 29, 2007 @ 7:04 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.