Windows and Handles
In my last post we saw how to get the rectangle area occupied by a window given its handle. Now what is a handle and what exactly is a Window?
The Microsoft Windows was developed to be a multitasking environment where you can open more than one application or more than one instance of the same application or a combination of both. Though all applications are visible on screen only one of then will receive the inputs from the keyboard, mouse and other input devices. Microsoft Windows manages the task of communicating with the applications. Noticed how all open application minimizes when the "Show Desktop" icon on the quick launch toolbar is pressed and returns to original position when pressed again? This happens because the MS Windows communicated with each of the application to change their state, and that MS Windows recognizes each of the Window by a unique number (of type long) which is termed as Handle.
Now are Windows same as the application window? No! MS Windows not only communicates with the main form of the application but also with most of the controls placed on that form. Thus every entity that the Window is capable of communicating with, is the Window. That may be visible or invisible and visibility is not a criteria for an object to be called Window. The label that's placed on the form, though is visible, is not a window thus does not receive any communication for the Window, the textBox or the Command Button does and hence are Windows.
If Form, Textbox, CommandButtons are all windows then how do we differentiate between them? We differentiate them by their "Class". It is the Class that defines the general appearance and behavior of the Window. A Class must be registered with the operating system before creating a window of that type.
To differentiate between windows of similar class, Window name can also be used which is a text string, generally the Caption property in VB. However not all windows have them. For a form or a Command Button you use one as they are displayed, but for TextBox or Lists there are none.
Windows styles are ways to improve the visibility of the window. The minimize, maximize, close buttons, and the title bar, the borders are all styles that may or may not be applied to a window changing it's appearance.
However these properties Class, Window name, Style may not be unique, for example multiple instances of the same application have the same class, window name, and the styles. It's always the Window Handle that's unique for each window.
How to get handle of a Window?
If you require the handle of a window existing within the particular instance of the application then it's easiest to get it by accessing the hWnd property of that control. However if the window is in another instance of the app or in some other application then you need to get it by using Windows API functions. Let's know some of them.
GetDesktopWindow function returns the handle to the Desktop window over which all the windows are shown. The function requires no parameter
Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
FindWindow function can find handle to a top level window given either the Class name of the window or the window name or both. The function does not search for child windows. If more than one istance of same application are running (thus more than one window having the same class name and window name) the handle to first one in the Z-order is returned.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
FindWindowEx function retrieves handle to child windows. The first parameter it takes is the handle to the parent Window. If it's passed as NULL the children of desktop window are searched. The second parameter is the handle to the child window, giving which search is carried only within those falling next in Z-order. Giving NULL to this parameter starts search from the first child. The third and the fourth parameters are the class name and the window name resp.
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long




March 13th, 2007 - 09:09
I have a form created with Visual FoxPro 5. I wonder why I can’t get any handle for the child windows? (I’m using Delphi). FindWindow functions finds the parent window, but FindWindowEx doesn’t get the child window.
I also use WinID to identify this form, it also can not find the child window..
Any idea?
March 14th, 2007 - 06:09
If you are looking forward to get the handle to a specific control, as I did in Adding File Names to a ListBox – 2 then, using FindWindowEx giving the Class Name is ok… otherwise if you are looking for getting handles to all child windows then I suggest using EnumChildWindows which will make callback for every child windows (and their children).
A little code for illustration, assumes you have a form and a list box on it.
Code on Form:
Private Declare Function FindWindow Lib “user32″ Alias “FindWindowA” ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function EnumChildWindows Lib “user32″ ( _
ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Sub Form_Click()
Dim x As Long
x = FindWindow(“OpusApp”, “Untitled Message”)
EnumChildWindows x, AddressOf EnumWindowsProc, 0
End Sub
Code in Module:
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Form1.List1.AddItem hwnd
EnumWindowsProc = True
End Function
June 12th, 2007 - 08:55
Urgent :
The problem is I have a seprate web page running. It has no linked with VB.
Now we have a vb form , On vb form there is some textbox and button, what we have to do , we have to paste the value from vb textbox to web page textbox after click on button in vb form..
June 12th, 2007 - 10:27
@Santosh,
The solution might be. Enumerate all windows, look for all windows with Window Text containing “Internet Explorer” and any other text that determines the page you are looking to update from VB. The Form controls on the web page are not independent windows so you will not get handle to those.
Once you have found the top-level window handle for Internet Explorer may be you can use sendkeys function on press of button on VB form to send the text to IE Form…
June 22nd, 2008 - 10:30
Hi,
In my project, i have to access the controls of a running application for ex, a Accessing all controls of a running windows
application developed in .net.I have to get all those controls of the running application and change its caption.
ex(buttonName,LabelName etc).I tried to implemented that ,but only caption of label alone gets changed and Button captions
and other control captions are not changed.I am working in C# windows applications.Please provide me the code in C#.Thanks in
advance.
regards,
Sherin