Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not allow right click selection on the list control items mfc?

Tags:

visual-c++

mfc

I want not to allow selection on the list control items when I try to select the item using right click and should show the selection if left click on the item.

I tried handling it in NM_RCLICK event to prevent right click selection as follows:

 void CTestDlg::OnNMRClickList1(NMHDR *pNMHDR, LRESULT *pResult)
        {
        LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

        if((pNMItemActivate->uChanged & LVIF_STATE) && 
           (pNMItemActivate->uNewState & LVNI_SELECTED))
        {
            *pResult = 1;
        }
        else
        {
            *pResult = 0;
        }
      }

Please refer screenshot for more information:

enter image description here

Blue color highlight should not come if I do right click on the item where as that highlight should come only for left click.

But still I am able to select the item if it is right click.

Could anyone please help me to solve this issue.

like image 307
Siva Avatar asked Nov 26 '25 21:11

Siva


2 Answers

You are handling the notification for the right-click event; it is too late to prevent anything at that point, as the selection is changed and previous selection is lost.

You should handle the WM_RBUTTONDOWN/UP messages and do whatever you want there, without passing it through to the default window procedure.

like image 164
Vlad Feinstein Avatar answered Nov 30 '25 23:11

Vlad Feinstein


Finally solved the issue with the suggestion given by @Vlad Feinstein, I tried handling WM_RBUTTONDOWN as below.

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)  
    {    
        long  lFocus = ::GetDlgCtrlID(pMsg->hwnd);
        if(IDC_LIST1 == lFocus)
        {
            if (pMsg->message == WM_RBUTTONDOWN)
            {
                return true; 
            }
        }

        return CDialog::PreTranslateMessage(pMsg);  
    }
like image 36
Siva Avatar answered Nov 30 '25 22:11

Siva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!