Window handle analysis and method for obtaining window handle

Source: Internet
Author: User

Handle is an important concept in a Windows program and is frequently used. In a Windows program, there are various resources (Windows, icons, cursors, and so on). The system allocates memory for these resources and returns the ID number that identifies them, handle.

A Windows handle is an important handle. In Windows applications, windows are identified by Windows handles (hwnd. To operate a window, first obtain the handle of the window.

The concept of a window handle is abstract. In short, a window handle represents a window. Let's get to know it through several examples.

1. lresult callback wndproc (hwnd, uint message, wparam, lparam );

Windows window processing function. The windows message mechanism means that every time a window changes (such as the size and Movement), the Windows system sends the corresponding message (wm_size, wm_move) for this window. That is, call the sendmessage function (hwnd, uint MSG, wparam, lparam). Note that the first parameter of wndproc and sendmessage is hwnd, and the window handle represents this window.

2. hwnd = createwindow (szwindowclass, sztitle, ws_overlappedwindow,

Cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null );

Here we are talking about the generation of window handles. We can think of hwnd as a pointer, createwindow as new in C ++, and create a window. The system will allocate some resources to this window object, as you can imagine, a window is composed of a large structure. The window handle is used to identify the window.

3. If you have a window handle, you can control everything in the window.

Win32 APIs that manage a large number of Windows use hwnd as the parameter. Below are some examples:

Bringwindowtotop

Finddomainwex

Getparent

Setwindowpos

Getwindowrect

And so on.

4. not only the window, the controls on the window, such as buttons and edit boxes, but also the window handles, because they are also a special window, most of the APIs in the Operation window above also apply to these control windows.

How to get the handle:

This article describes how to obtain the window handle and what functions are available for use! It can be applied to VC and BCB (I have not tried it, I guess it can be). I have tested it in the BCB environment.

First, I will list some Win32 API functions for obtaining the handle, and then briefly talk about their usage! Finally, let's talk about how I understand and apply it. Laugh!

Available Win32 API functions:

1. hwnd findwindow (maid, lpwindowname)

Hwnd find1_wex (hwnd hwndparent, hwnd hwndchildafter, lpctstr lpclassname, lptstr lpwindowname)

2. hwnd windowfrompoint (point & point)

3. bool callback enumchildproc (hwnd, lparam)

Bool callback enumchildwindows (hwnd hwndparent, wndenumproc lpenumfunc, lparam)

Bool callback enumwindows (wndenumproc lpenumfunc, lparam)

Bool callback enumwindowsproc (hwnd, lparam)

General purpose:

The first one is familiar to everyone. It is a conventional weapon for capturing handles. The findwindow brothers can accept the class name or window title of the object to be captured. As a parameter, an hwnd is returned. However, for general users, they do not necessarily know the class names of all windows (including the title bar, buttons, and so on! -- A simple example is provided. Do you know the class name of the window of the desktop icon? For the window title, the same title may appear. There are two windows-two processes of a program. This is a problem! Now, let's move on to the next group.

The second group uses the point structure defined by Win32 (typedef struct tagpoint {long X;

Long y;} Point) to obtain the Windows hwnd at the current cursor position, which is the most intuitive weapon! The general operation is as follows: first obtain the cursor's point (bool getcursorpos (lppoint) function) and then use windowfrompoint. In this way, we can obtain the hwnd of any function with window opened! Then, you can obtain the class name through the Win32 API function (INT getclassname (hwnd, lptstr lpclassname, int nmaxcount) of the class name. We recommend that you use the character array address for the lpclassname here, nmaxcount is the size of the array. At the same time, this method solves the first problem! -- I can place the mouse anywhere! * ^_^ *

Group 3: these are super weapons used to list and process any window! By combining enumwindows and enumwindowsproc, enumchildwindows and enumchildproc, you can scan and process all windows on the desktop!

My understanding: (this part uses the task-driven teaching method-who makes the younger brother a teacher! Xi)

Task: Get the class names of all windows.

Solution 1: we first thought about the third group, which can start from the desktop window (which is the ancestor of all windows) and scan in sequence to get the coexistence of class names. It is a bit like visual stdio's spy ++ or Borland's winsight32. The specific method is as follows: (BCB)

In the main program, call enumwindows and input the youenumproc function address as the first parameter. Do not forget to convert it to the wndenumproc type. The second parameter can be null. : Enumwindows (reinterpret_cast <wndenumproc> youenumproc, null );

In the youenumproc function, if the first parameter hwnd = NULL, it will jump off (return false;) and it will end!

Then, prepare the class name array to get the class name and save it.

Returns the true value and continues the next scan.

It does not seem complicated. It is a function recursion. But I will explain it! Noodles! : P

Solution 2: simple and intuitive-self-developed and proud

First, prepare a clock, a memory class name method (I use tmemo)

In the timer processing function:

1. Obtain the point position of the current cursor.

2. Use windowfrompoint again,

3. Get the class name and put it in tmemo.

In this way, you can use the mouse to get the desired window (including buttons), as long as you move the mouse in the window for a while... Haha

Method 3: Actually, the findwindow and loop structure should also be available.

Summary: in fact, there are many methods to obtain hwnd. For example, if you know the window level, scan down in sequence... Let's talk about the third type! But I think my method is the most effective. What do you mean?

You are welcome to contact me and discuss this issue! I still have many questions about this issue, such as converting hwnd to ID, hwnd or ID of the Form Control on the IE page, or something else, in short, it is something that can recognize him... I am confused!

For details, refer to msdn.

Other methods for obtaining the handle:

1. Obtain the window handle by ID

Hwnd = getdlgitem (hwndparent, ID ).

2. Obtain the id based on the window handle

Id = getwindowlong (hwndchild, gwl_id)

Id = getdlgctrlid (hwndchild ).

3. Several methods for obtaining instance handle

Hinst = getwindowlong (hwnd, gwl_hinstance)

Save hinstance as a global variable.

When a window is created, the parameter lparam points to an address for creating a structure.

You can obtain the instance handle in this way.

Hinst = (lpcreatestruct) lparam)-> hinstance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.