Sign in to follow this  
nivlekio

Right click on listbox?

Recommended Posts

Hey all im trying to right click on a listbox and have a menu pop. But for some strange reason windows does not detect a right click on a listbox or edit control, I tried putting the popup menu code in the WM_RBUTTONUP message switch case it only detected if i did not right click on a listbox or editcontrol. I then tried making an array then doing the old WM_KEYDOWN and WM_KEYUP and again it does not get detected if i click on a list box. how do u detect a right click on a list box? btw im not using mfc

Share this post


Link to post
Share on other sites
I got it to work i used GetAsyncKeyState(VK_RBUTTON) it detects the mouse right click any where even on a list box :).

Erm is it possible to get the dimentions and position of a list box during runtime? also change its position?

Share this post


Link to post
Share on other sites
You can use GetWindowRect to retrieve the listbox's (or any HWNDs) position and size in screen coordinates.
If you only need the inner size you use GetClientRect (these coordinates are relative to the inner rect, so you basically only get the size).
You can use SetWindowPos to move/resize a HWND.

Using GetAsyncKeyState sounds like a weird way, since you'll have to keep polling all the time.

Like MJP said, you'd be better off subclassing the control and watch for WM_RBUTTONDOWN.
If you're using MFC you can use PreTranslateMessage (inbuilt subclassing for you).

Share this post


Link to post
Share on other sites
Yeah subclassing is a much better idea in this case, polling is a waste of of CPU time. No need to do something Windows already does for you (hit detection with mouse clicks). To subclass a window in Win32, you need to use SetWindowLongPtr to set the ListBox's WindowProc to your new window procedure. When you set this, also store the return value because it is a pseudo-pointer to the ListBox's actual window procedure (it's not actually a function pointer for complicated reasons). So then what you do is in your new window procedure you check for WM_RBUTTONDOWN, and in every other case you pass on the message to the real window procedure using CallWindowProc.

Share this post


Link to post
Share on other sites
Polling is repeatedly asking the computer for information. The computer doesn't get to rest and relax when you poll, it gets tired, and then takes it out on the user in the form of performance problems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this