Archived

This topic is now archived and is closed to further replies.

Koen

capturing the mouse and WM_COMMAND

Recommended Posts

Koen    697
I have a pushbutton in a window. When the user presses the left mouse button over the pushbutton, the mouse gets captured by the pushbutton. Once he releases the button, the capture is released. I need this to be able to detect dragging. The problem is that the pushbutton doesn''t generate any WM_COMMAND messages when the mouse is captured. Does anyone know a way around this? Thanks.

Share this post


Link to post
Share on other sites
felisandria    739
I suppose a good question is "what are you trying to do that for?"

I can think of a few scenarios when you might want something looking like that, but in all of them it tends to be better to do something a tad more custom than trying to force a button to behave.

I suppose you can always process the WM_LBUTTONDOWN and see if you''re over the pushbutton area, then call the button''s usual message handler.

-fel

Share this post


Link to post
Share on other sites
Koen    697
I''m trying to make a AWT-like set of classes to make GUIs with. I don''t really like any of the toolkits I''ve seen, and I really like the AWT way of doing things. I thought it would be a good (and hopefully useful) exercise. AWT allows all its components to listen to MouseMotion (moving and dragging). To detect dragging correctly I need to capture the mouse every time the left mouse button is pressed. (There probably are other methods that involve more state-information, but it would be nice to use just the windows messages and functions).
Does anyone know why it''s impossible for the button to detect the click if the mouse is captured?

Share this post


Link to post
Share on other sites
felisandria    739
Why do you feel you need to do a mouse capture, first off?

Generally mouse captures are used to handle the possibility of moving your cursor entirely outside of the window you''re dealing with.

Unless you''re using "mouse capture" as a term for something other than an actual mouse capture?

-fel

Share this post


Link to post
Share on other sites
Koen    697
No, I mean the same thing you do: using SetCapture(componentHWND) so the mouse can be dragged outside of the control. I want to use this because I want my events to be as close as possible to the java-events. And mousedragging in java lasts as long as the left mousebutton is pressed (even if the mouse is moved outside of the control).

Share this post


Link to post
Share on other sites
felisandria    739
Have you considered not capturing until after the button''s event fires? Caputuring on WM_LBUTTONDOWN is generally a bad idea, because it entirely lacks context. That''s why you''re having trouble capturing the click... a CButton is technically a CWnd, and does a good deal of processing internal to its own window scope... if the mouse is captured the messages are forced to the capture context, so the CButton''s CWnd underbelly isn''t going to be getting it to process (which is technically the point of mouse capture, though more to prevent desktop and third party windows from processing mouse events if you want a specific window to be in control regardless).

-fel

Share this post


Link to post
Share on other sites
Koen    697
quote:
Original post by felisandria
if the mouse is captured the messages are forced to the capture context

And why would that be a problem? That''s exactly what I want: every mouse-related message should be sent to the button (even if it actually occured on the parent window, or somewhere else)...
Or am I missing something here?

Share this post


Link to post
Share on other sites
Koen    697
I Think you are talking about mfc, right? I''m not using mfc at all. Only the pure win32 api. So all I have for this button is it''s window handle.

Share this post


Link to post
Share on other sites
felisandria    739
That''s why I put the "or" in there. The point of the question is whether or not you''re using the proper window context for the button, rather than whatever construct it''s stuck on.

-fel

Share this post


Link to post
Share on other sites
Koen    697
I only use the handle. Whenever I receive WM_LBUTTONDOWN, I do SetCapture(hWnd) and if I receive WM_LBUTTONUP, I do ReleaseCapture().

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
you''ll need to create a subclass of the BUTTON WndClass in order to install a custom message handler to get at the events. How to do that is explained in the MSDN manual under Subclassing a Window.

Share this post


Link to post
Share on other sites
Koen    697
Here is a window-procedure that shows the problem:

LRESULT CALLBACK WndProc( HWND handle, UINT messg,WPARAM wParam, LPARAM lParam )
{
if(handle==hWnd2) { //hWnd2 is the handle for the button
if(messg==WM_LBUTTONDOWN) {
SetCapture(hWnd2);
}
if(messg==WM_LBUTTONUP) {
ReleaseCapture();
}
return CallWindowProc(old,hWnd2,messg,wParam,lParam); //old is the button-wndproc
}

switch(messg)
{
case WM_DESTROY:
PostQuitMessage( 0 );
break;

case WM_COMMAND:
SetWindowText(hWnd,"OK"); //hWnd is the handle for the parent window, but this line never gets executed
break;

default:
return( DefWindowProc( handle, messg, wParam, lParam ) );
}

return( 0L );
}


[edited by - Koen on January 28, 2003 7:07:18 PM]

Share this post


Link to post
Share on other sites
felisandria    739
Okay, and where are you posting the button notification that it''s been clicked to your main window? You''re either going to have to explicitly post it yourself, or call the control''s click handler, since you''ve overridden the WM_LBUTTONDOWN processing.

-fel

Share this post


Link to post
Share on other sites
Koen    697
After processing WM_LBUTTONDOWN, CallWindowProc(.....) is executed. If the SetCapture(..) and ReleaseCapture() are commented out, everything works fine. So that can''t be the problem.

Share this post


Link to post
Share on other sites
felisandria    739
Post it anyway and see if it works. *shrug*

Messing with mouse capture can seriously screw things up.
The reason why that happens is tucked deeply away inside the .dll''s for visual studio.

-fel

Share this post


Link to post
Share on other sites
Koen    697
quote:
Original post by felisandria
Post it anyway and see if it works. *shrug*

I guess that will be the only option.
Still think they should''ve put something in their docs, though. I hate uncertainty ;-)

Share this post


Link to post
Share on other sites