Windows interface: Right-click menu implementation

Source: Internet
Author: User

1. First, create the menu to be used in the menu item in the resource bar;

2. for common windows and controls, you can use classwizard to create its message processing function oncontextmenu ();

3. You can also right-click a menu in a specific area to create a message response;

4. In the message response function, the menu is displayed:

Void cscanfilelist: oncontextmenu (cwnd */* pwnd */, cpoint point)
{
// Todo: add your message handler code here
// Right-click to display the menu
Cmenu menu;
Menu. loadmenu (idr_mainframe );
Cmenu * ppopupmenu = menu. getsubmenu (1 );

// Show menu
Ppopupmenu-> trackpopupmenu (tpm_leftalign, point. X, point. Y, afxgetmainwnd ());
}

5. If you are right-clicking a response message, you must use the API to obtain the position parameter of the pop-up menu. That is, you must obtain the current cursor position:

Case wm_rbuttondown:
Point pt;
If (! Getcursorpos (& pt ))
{
// Error
Cstring strerr;
Strerr. Format (_ T ("cannot pop mouse right button MEMU, getcursorpos failed! /R/n/R/nerror: % s ")
, Geterrormsgandcode () // for this function, see my other article: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 2071783
);
Afxmessagebox (strerr );
Return;
}
Cmenu rbmenu;
Rbmenu. loadmenu (idr_menu_rbutton );
Cmenu * ppopupmenu = rbmenu. getsubmenu (0 );
Ppopupmenu-> trackpopupmenu (tpm_leftalign, Pt. X, Pt. Y, this );
Break;

6. That's all. (PS: If you are free, please reply to me! :)

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.