Obtain the menu handle from the window handle (obtain hmenu from hwnd)

Source: Internet
Author: User

1. If hwnd is the main window, you can use API: getmenu (...) to obtain the handle of the main window. The prototype is as follows:

Hmenu getmenu (hwnd );


The original type in MFC is as follows:

Cmenu * getmenu () const;



2. If hwnd is the menu window and you want to directly convert it into a menu handle, you need to send a message:


Message ID: mn_gethmenu 0x01e5

Header file: # include <winuser. h>

But I don't know why. In Vista, vc9 always says that mn_gethmenu is not defined, so I have to define it manually:
# Ifndef mn_gethmenu

# Define mn_gethmenu 0x01e5

# Endif


Then the corresponding hmenu is obtained through the return value of sendmessage.


Example:
// If hwnd is the window handle of the menu
Hmenu = (hmenu) sendmessage (hwnd, mn_gethmenu, 0, 0 );

 

3,

If you want to use a handle like this, please write a few more words at the end to implement a simple function.
Hwnd;
Hwnd = (hwnd): getmenu (m_hwnd); you have no purpose. Of course you don't know how to use it to obtain the handle!
All APIs for window operations require a handle as a parameter, basically.
For example, you can add, delete, and modify menus after obtaining the menu handle. If you use the above to add a menu, I will only get it and will not use it. It depends on how you want to use it.
For example, you get a window handle.
: Getwindowtext (hwnd, lpwintitle, 256-1); in this way, you can get the window title. :) who gave me a simple example and I am still confused about this handle, for example?
: Postmessage (hwnd, wm_close, null, null)

If you want to send a wm_close message to close a window, you need to know the handle to close the window. Because the first parameter of the postmessage function is a handle, you need to get the handle first,

Then, you can call the above function to close the window. The difference between using a handle and using a pointer is big. The pointer only encapsulates the handle for convenient calling and is more oriented to the image. If

Generally, it is a system API, while a pointer calls a class method. It refers to the encapsulation of an API. I don't know how to explain it clearly if there is no foundation? To learn programming, we should start from the basics and be honest.

Read books. Hwnd;
Hwnd = (hwnd): getmenu (m_hwnd );

You can associate the handle with a menu object, and then you can operate the menu through this menu object.
For example
Cmenu: attach
Bool attach (hmenu );

Cmenu m_menu1;
M_menu1.attach (hwnd );
...................
M_menu1.detach (); // separates the menu object from the handle resource because the menu object is a local variable.

Or
Cmenu: fromhandle
Static cmenu * Pascal fromhandle (hmenu );

Cmenu * m_pmenu2 = cmenu: fromhandle (hwnd); // associate the handle with the menu pointer
You can use the menu pointer to operate the menu below
The handle is the ID of the window identifier. With this ID, it is easy to get the handle for him to work in the future, and then convert fromhandle () to the corresponding pointer. In the future, it will be easy to do things.

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.