ListCtrl Right click
Guru's
I have a list ctrl created through the Win32 API architecture. I am able to highlight a row in my list control by left clicking the row - as you would expect.
However, does anyone know how I can have the same behaviour by doing right click on the row? So basically I like to right click on a row and that row will become highlighted.
If you're unsure what I'm trying to do, go into your windows explorer and right click on a file - that file will become highlighted - this is what I'd like to achieve.
Please note, this is not MFC. I am using win 32 API only - just to avoid any confusion.
Kindest regards
John
# 1 Re: ListCtrl Right click
Methinks you're looking for the listview control. Straight-up list boxes have limited functionality, though they are still very useful.
# 2 Re: ListCtrl Right click
First of all your topic title is a bit confusing :) There is no such thing called List Control. It can be one of following:
1. ListBox control
2. ListView control
If you need t otrack right-clicks within listbox control, you obviously need to subclass the control, but I suggest you use listview control with LVS_REPORT style.
Topics of interest:
my reply : http://www.dev-archive.com/forum/showthread.php?t=396036
reply of Kirants : http://www.dev-archive.com/forum/showthread.php?t=405789
regards
# 3 Re: ListCtrl Right click
Hi Imran,
Yes you're right, its a Listbox. I have subclassed the control to handle the WM_RBUTTONDOWN, and thats not the problem here as it works as intended.
The problem is that I cannot seem to be able to select AND highlight the row that I've clicked using the right click button.
So you think I should use a ListView and not a listbox to achieve this behaviour?
Regards
John
# 4 Re: ListCtrl Right click
Not necessary to use ListView if ListBox fullfills your requirement.
Actually when right click occurs you might have put subclassed window procedure in a loop and did not let the message forward to default procedure first. You might want to display a context menu probably. So, what I suggest you is, first call actual procedure, and then perform your action, e.g.:
case WM_RBUTTONDOWN: {
CallWindowProc(ActualOldProcedure, hwnd, WM_RBUTTONDOWN, wParam, lParam);
//do your functioning here
}
EDIT : ADDITION
Another way is define custom message, and forward this message to parent window, then operate that message in parent window, e.g.:
#define WM_MYMESSAGE WM_USER+100 / * custom message */
case WM_RBUTTONDOWN: {
PostMessage(GetParent(hwnd), WM_MYMESSAGE,wParam, lParam);
}
WHY PostMessage instead of SendMessage, becuase again the listbox will not gain control of rightClick and select row becuase SendMessage needs a return value from target window.
Then in procedure of parent window:
case WM_MYMESSAGE: {
/* it is custom message from my subclassed listbox */
// do your stuff here
// TIP : LOWORD(lParam) is xmouse, HIWORD(lParam) is ymouse
}
I hope this solves the problem.
regards
# 5 Re: ListCtrl Right click
Thanks for the reply Imran. This is what I'm doing already, except the CallWindowProc is being returned to ActualOldProcedure at the end of the subclassed function.
I do have a context menu message, in the ActualOldProcedure, and this works, but the problem I am having is being able to have that row, when I press right click, not only just to have the context menu displayed, but to have the row highlighted too.
Brief
In short I am having problems in getting the position of the row in the listbox by right clicking on it. Once I am able to get this then it's just the simple job of sending a message to highlight it.
I hope that clears things up.
Cheers
John
# 6 Re: ListCtrl Right click
Although the subclassed procedure is producing call to old procedure at end, the code I provided cals before function exits, as:
case WM_RBUTTONDOWN: {
CallWindowProc(ActualOldProcedure, hwnd, WM_RBUTTONDOWN, wParam, lParam);
//do your functioning here
}
Another wau is, get item from position, select it, and then do what you want, either show context menu or whatever.
case WM_RBUTTONDOWN: {
//get item index under mouse pointer
short index = (short)SendMessage(hwnd, LB_ITEMFROMPOINT, (WPARAM)0, MAKELONG(LOWORD(lParam), HIWORD(lParam));
//make selection based on index we obtained
SendMessage(hwnd, LB_SETSEL, (WPARAM)TRUE, (LPARAM)index);
// do you stuff here, either show context menu or any possible process
}
If it does nto help, you must have some mistake in project, better is post prototype of project so we can fix it for you.
regards
# 7 Re: ListCtrl Right click
LB_ITEMFROMPOINT!! Exactly what I'm looking for! thanks Imran, it now works.
Kindest regards
John
# 8 Re: ListCtrl Right click
You are welcomed, I am glad it helped...btw my name is not just Imran, it is Ali Imran ;)
regards