
Moving Windows Without Captions
Quote:
>> Richard,
>> All you need to do is handle WM_NCHITTEST (OnNcHitTest()) and return
>HTCLIENT when the mouse is in an area where you want the user to drag the
>window.
>> John
> How do I get the WM_NCHITTEST message with out using SetCapture()?
Easy; you just use the ClassWizard to add a WM_NCHITTEST handler to
your window class. You don't need SetCapture to receive mouse events
when the mouse is over your window; that happens by default. You only
need SetCapture to receive mouse events when the mouse _isn't_ over your
window.
It looks like you're making this more complicated than it is. If you
use this approach, you don't need a button-down or button-up handler. You
don't need a drag handler. Windows handles everything for you, just like it
normally does when the user clicks on your window's caption bar.
Quote:
> Question 2: What it the proper way to use SetCapture and
>ReleaseCapture?
> I tried putting the calls in WM_MOUSEMOVE, it looks wrong.
[code snipped]
Yes, that is wrong. :)
Again, you seem to be a little off on what SetCapture is used for.
You don't need to use SetCapture to get WM_MOUSEMOVE messages, unless you
want to get those messages while the cursor isn't over your window. And
you generally want to avoid using SetCapture whenever possible, since it
prevents other windows from getting mouse-related messages as they normally
would. Typically, the primary use for SetCapture is when you're handling
a drag-and-drop. In your WM_LBUTTONDOWN handler, you do whatever is necessary
to start the drag. But you now probably need to get WM_MOUSEMOVE messages
(so you can update the cursor) and WM_LBUTTONUP messages (to perform the
drop) regardless of which window the mouse is over. So you call SetCapture
from your WM_LBUTTONDOWN handler, and ReleaseCapture from your WM_LBUTTONUP
handler (since the drop is then over, and you don't care about mouse messages
for other windows anymore).
By the way, one other note based on your code: You have to be careful
when you have your own "I've captured the mouse" flag. The problem is that
there are circumstances where you can lose the mouse capture without your
knowledge or consent. :) For example, if a modal dialog suddenly pops up
and requires the user's attention, the system will send your window a
WM_CANCELMODE message, and the default handler for that message will
automatically respond by releasing the mouse capture, among other things.
If you want to know whether you currently have the mouse captured, you can
use GetCapture to find out. Alternatively, if you do want to have your own
flag, you should add a handler for the WM_CAPTURECHANGED message. When you
get this message, you're losing the mouse capture (either because you called
ReleaseCapture, or because the system did it for you), and you should update
your mouse-capture flag accordingly.
--
\o\ If you're interested in books and stories with transformation themes, \o\
/o/ please have a look at <URL:http://www.halcyon.com/phaedrus>. Thanks! /o/
\o\ FC1.21:FC(W/C)p6arw A- C->++ D>++ H+ M>+ P R T++++ W** Z+ Sm RLCT \o\
/o/ a cmn++++$ d e++ f+++ h- i++wf p-- sm# /o/