Detailed explanation of the right-click menu for creating the MFC ListBox (very detailed instructions)

Source: Internet
Author: User

Hello, everyone. I am using vs2010 and will not use csdn to insert images.

In the past few days, we have to have a right-click menu because of the customer's requirement (TMD. There are indeed many versions on the Internet, but most of them are not detailed. So today I will write this detailed step for your reference.

You can create a shortcut menu in three steps.

Step 1: Create a menu resource:

1. In Solution Explorer, right-click "resource file", select "resource" from "add", and select "menu", and name it idc_menu1.

2. Add a "delete" item in the menu. This is too simple, so I will not go into details.

3. Right-click "delete" and select "add event handler ". Name the time handler "m_delete ".

The Code is as follows:
Int spy_index;
Spy_index = m_spy.getcursel ();
If (spy_index = cb_err)
{
MessageBox ("select a record ");
}
Else
{
// Delete records in ListBox
M_spy.deletestring (spy_index );

}

 

In this way, the menu and its processing program are successfully written.

 

Step 2: derive a new class from the clistbox class and add a response function for the wm_rbuttondown message to the derived class:

1. derived class:

In the Class Wizard, select "New Class" and select the base class as ListBox.

After creating a class, you need to write a message response function yourself.

2. functions are as follows:

Afx_msg lresult m_list: onrbuttondown (wparam, lparam)
{

Point pt;
Getcursorpos (& pt );
Int ncount = getcount ();
Screentoclient (& pt );
// Right-click the target
For (INT I = 0; I <ncount; I ++)
{
Bool;
Crect RC;

Getitemrect (I, & rc );
If (RC. ptinrect (PT ))
{
Setcursel (I );
Cmenu temp, * PTR;
Temp. loadmenu (idr_menu1 );
PTR = temp. getsubmenu (0 );
Clienttoscreen (& pt );
PTR-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, Pt. X, Pt. Y, getparent ());

Break;
}
}

 

In this way, right-click a row and delete it.

 

 

Step 3: Create a ListBox control and delete it.

1. Create a ListBox control from the toolbar and change its base class in the Class Wizard, for example:

 

Idc_list2 is The ListBox control I created.

 

 

 

 

 

Based on the above three steps, we have completed the function of right-clicking the ListBox.

 

All of the above are handwritten. Thank you :-(!

 

~~~~~~~~~~~~~~~~ So tired.

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.