Moving Windows Without Captions 
Author Message
 Moving Windows Without Captions

I have an obect that is the child of a CView.  If this object had a caption,
the user could move it by grabing the caption with the mouse.  I don't want
my window object to have a caption.  The user should be allowed to move the
window.  This should be simple as detecting mouse button down and mouse move
messages where the button is down.

void CMyWindowObject::OnLButtonDown(UINT nFlags, CPoint point)
{
 m_bHaveMouse = true;
 CWnd::OnLButtonUp(nFlags, point);

Quote:
}

void CMyWindowObject::OnLButtonUp(UINT nFlags, CPoint point)
{
 m_bHaveMouse = false;
 CWnd::OnLButtonUp(nFlags, point);

Quote:
}

void CMyWindowObject::OnMouseMove(UINT nFlags, CPoint point)
{
 if (MK_LBUTTON & nFlags)
 {
  static int count = 0;
  count++;
  TRACE("Mouse Move %d, From (%d, %d) To (%d, %d)\n", count,
   m_pointLastMouse.x, m_pointLastMouse.y, point.x, point.y);

  m_bMoving = true;

  LPARAM lParam;
  CRect rect;
 // CWnd::Default();
  GetWindowRect(&rect);
  GetParent()->ScreenToClient(&rect);
  rect.OffsetRect(point.x - m_pointLastMouse.x, point.y - m_pointLastMouse.y);
  MoveWindow(&rect);
  lParam = MAKELPARAM(rect.left, rect.top);
 // SendMessage(WM_MOVE, NULL, lParam);
 // PostMessage(WM_MOVE);
  m_pointLastMouse = point;

 }
 CWnd::OnMouseMove(nFlags, point);

Quote:
}

I get mouse move messages fast enough for this method to work.  Some thing is wrong with the MoveWindow(....) function.  (There must be a function that will move the window without generating mouse move messages.)

An alternative solution to this problem is to not have a CMyWindowObject.  Simply do all drawing in the view and repaint the view when the object is moved by the mouse.

I would like to have self contained objects.  There must be a way to move the object as if it is the caption.



Mon, 19 Nov 2001 03:00:00 GMT  
 Moving Windows Without Captions
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

Quote:

> I have an obect that is the child of a CView.  If this object had a caption,
> the user could move it by grabing the caption with the mouse.  I don't want
> my window object to have a caption.  The user should be allowed to move the
> window.  This should be simple as detecting mouse button down and mouse move
> messages where the button is down.

> void CMyWindowObject::OnLButtonDown(UINT nFlags, CPoint point)
> {
>  m_bHaveMouse = true;
>  CWnd::OnLButtonUp(nFlags, point);
> }

> void CMyWindowObject::OnLButtonUp(UINT nFlags, CPoint point)
> {
>  m_bHaveMouse = false;
>  CWnd::OnLButtonUp(nFlags, point);
> }

> void CMyWindowObject::OnMouseMove(UINT nFlags, CPoint point)
> {
>  if (MK_LBUTTON & nFlags)
>  {
>   static int count = 0;
>   count++;
>   TRACE("Mouse Move %d, From (%d, %d) To (%d, %d)\n", count,
>    m_pointLastMouse.x, m_pointLastMouse.y, point.x, point.y);

>   m_bMoving = true;

>   LPARAM lParam;
>   CRect rect;
>  // CWnd::Default();
>   GetWindowRect(&rect);
>   GetParent()->ScreenToClient(&rect);
>   rect.OffsetRect(point.x - m_pointLastMouse.x, point.y - m_pointLastMouse.y);
>   MoveWindow(&rect);
>   lParam = MAKELPARAM(rect.left, rect.top);
>  // SendMessage(WM_MOVE, NULL, lParam);
>  // PostMessage(WM_MOVE);
>   m_pointLastMouse = point;

>  }
>  CWnd::OnMouseMove(nFlags, point);
> }

> I get mouse move messages fast enough for this method to work.  Some thing is wrong with the MoveWindow(....) function.  (There must be a function that will move the window without generating mouse move messages.)

> An alternative solution to this problem is to not have a CMyWindowObject.  Simply do all drawing in the view and repaint the view when the object is moved by the mouse.

> I would like to have self contained objects.  There must be a way to move the object as if it is the caption.



Mon, 19 Nov 2001 03:00:00 GMT  
 Moving Windows Without Captions

Quote:

>I have an obect that is the child of a CView.  If this object had a caption,
>the user could move it by grabing the caption with the mouse.  I don't want
>my window object to have a caption.  The user should be allowed to move the
>window.  This should be simple as detecting mouse button down and mouse move
>messages where the button is down.

     Actually, it's usually even simpler than that.  This routine will make
a window movable by dragging any empty point in its client area:

UINT CMyWindowObject::OnNcHitTest(CPoint point)
{
        UINT nRet = CWnd::OnNcHitTest(point);
        if (nRet == HTCLIENT) {
                nRet = HTCAPTION;
        }
        return nRet;

Quote:
}

     Basically, this tells the system to treat any clicks on the window's
client area as if they were clicks on the caption bar, even if the window
doesn't actually have a caption bar.
--
\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/


Mon, 19 Nov 2001 03:00:00 GMT  
 Moving Windows Without Captions
Ok, first check if your moving function is right,
i do always set the reference point at WM_MOUSEDOWN.
Also, if the MoveWindow is REALLY the problem, simply add an object wide
variable that will be
set to false before moving the window, and back to true after, and detect at
the beginning of
mouseMove wether it's true or not.


Quote:
> I have an obect that is the child of a CView.  If this object had a
caption,
> the user could move it by grabing the caption with the mouse.  I don't
want
> my window object to have a caption.  The user should be allowed to move
the
> window.  This should be simple as detecting mouse button down and mouse
move
> messages where the button is down.

> void CMyWindowObject::OnLButtonDown(UINT nFlags, CPoint point)
> {
>  m_bHaveMouse = true;
>  CWnd::OnLButtonUp(nFlags, point);
> }

> void CMyWindowObject::OnLButtonUp(UINT nFlags, CPoint point)
> {
>  m_bHaveMouse = false;
>  CWnd::OnLButtonUp(nFlags, point);
> }

> void CMyWindowObject::OnMouseMove(UINT nFlags, CPoint point)

>  if (MK_LBUTTON & nFlags)
>  {
>   static int count = 0;
>   count++;
>   TRACE("Mouse Move %d, From (%d, %d) To (%d, %d)\n", count,
>    m_pointLastMouse.x, m_pointLastMouse.y, point.x, point.y);

>   m_bMoving = true;

>   LPARAM lParam;
>   CRect rect;
>  // CWnd::Default();
>   GetWindowRect(&rect);
>   GetParent()->ScreenToClient(&rect);
>   rect.OffsetRect(point.x - m_pointLastMouse.x, point.y -

m_pointLastMouse.y);
Quote:
>   MoveWindow(&rect);
>   lParam = MAKELPARAM(rect.left, rect.top);
>  // SendMessage(WM_MOVE, NULL, lParam);
>  // PostMessage(WM_MOVE);
>   m_pointLastMouse = point;

>  }
>  CWnd::OnMouseMove(nFlags, point);
> }

> I get mouse move messages fast enough for this method to work.  Some thing

is wrong with the MoveWindow(....) function.  (There must be a function that
will move the window without generating mouse move messages.)
Quote:

> An alternative solution to this problem is to not have a CMyWindowObject.

Simply do all drawing in the view and repaint the view when the object is
moved by the mouse.
Quote:

> I would like to have self contained objects.  There must be a way to move

the object as if it is the caption.

- Show quoted text -



Mon, 19 Nov 2001 03:00:00 GMT  
 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.

Quote:

> John

    How do I get the WM_NCHITTEST message with out using SetCapture()?

    Question 2:  What it the proper way to use SetCapture and
ReleaseCapture?

    I tried putting the calls in WM_MOUSEMOVE, it looks wrong.
 CRect rect;
 GetClientRect(&rect);
 if (rect.PtInRect(point))
 {
  if (!m_bHaveCapture)
  {
   SetCapture();
   m_bHaveCapture = true;
  }
 }
 else
 {
  if (m_bHaveCapture)
  {
   ReleaseCapture();
   m_bHaveCapture = false;
  }
 }
 CWnd::OnMouseMove(nFlags, point);

Quote:


> > I have an obect that is the child of a CView.  If this object had a
caption,
> > the user could move it by grabing the caption with the mouse.  I don't
want
> > my window object to have a caption.  The user should be allowed to move
the
> > window.  This should be simple as detecting mouse button down and mouse
move
> > messages where the button is down.

> > void CMyWindowObject::OnLButtonDown(UINT nFlags, CPoint point)
> > {
> >  m_bHaveMouse = true;
> >  CWnd::OnLButtonUp(nFlags, point);
> > }

> > void CMyWindowObject::OnLButtonUp(UINT nFlags, CPoint point)
> > {
> >  m_bHaveMouse = false;
> >  CWnd::OnLButtonUp(nFlags, point);
> > }

> > void CMyWindowObject::OnMouseMove(UINT nFlags, CPoint point)
> > {
> >  if (MK_LBUTTON & nFlags)
> >  {
> >   static int count = 0;
> >   count++;
> >   TRACE("Mouse Move %d, From (%d, %d) To (%d, %d)\n", count,
> >    m_pointLastMouse.x, m_pointLastMouse.y, point.x, point.y);

> >   m_bMoving = true;

> >   LPARAM lParam;
> >   CRect rect;
> >  // CWnd::Default();
> >   GetWindowRect(&rect);
> >   GetParent()->ScreenToClient(&rect);
> >   rect.OffsetRect(point.x - m_pointLastMouse.x, point.y -

m_pointLastMouse.y);
Quote:
> >   MoveWindow(&rect);
> >   lParam = MAKELPARAM(rect.left, rect.top);
> >  // SendMessage(WM_MOVE, NULL, lParam);
> >  // PostMessage(WM_MOVE);
> >   m_pointLastMouse = point;

> >  }
> >  CWnd::OnMouseMove(nFlags, point);
> > }

> > I get mouse move messages fast enough for this method to work.  Some

thing is wrong with the MoveWindow(....) function.  (There must be a
function that will move the window without generating mouse move messages.)
Quote:

> > An alternative solution to this problem is to not have a

CMyWindowObject.  Simply do all drawing in the view and repaint the view
when the object is moved by the mouse.
Quote:

> > I would like to have self contained objects.  There must be a way to

move the object as if it is the caption.

- Show quoted text -



Mon, 19 Nov 2001 03:00:00 GMT  
 Moving Windows Without Captions

Quote:

>     How do I get the WM_NCHITTEST message with out using SetCapture()?

    WM_NCHITTEST is just like any normal windows message.  You might not find it
in the class wizard, but you can get it by right clicking your window class,
choosing "Add Windows Message Handler", selecting "Window" in the bottom right
corner, then choosing WM_NCHITTEST in the left pane, and adding a handler into
the class.

Quote:

>     Question 2:  What it the proper way to use SetCapture and
> ReleaseCapture?

    You don't need SetCapture()/ReleaseCapture() to do this.

John



Mon, 19 Nov 2001 03:00:00 GMT  
 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/


Mon, 19 Nov 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Moving windows without visible caption

2. Help: How to move windows with no caption?

3. Window without caption bar

4. Getting window handle without matching window caption text

5. How to implement a Property Sheet without caption bar

6. Creating A Window Without A Caption Bar.....

7. MFC without Caption bar

8. SDI As Child without Caption but with Menu

9. Problem creating PropertySheet objects without a caption

10. How to implement a Property Sheet without caption bar

11. Making a main(frame)window without a border and caption (just white)

12. How to drag a form without caption answer.

 

 
Powered by phpBB® Forum Software