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.