C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Move a window in MFC Posted by CocaCola on 13 Apr 2001 at 1:13 PM
I want to have a window that has no title bar so you can't move it around. I've tried this but something is wrong because it disappears when using it:

void CMoveagainDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if((nFlags & MK_LBUTTON) == MK_LBUTTON)
{
cpPos.x = point.x;
cpPos.y = point.y;

MoveWindow(cpPos.x,cpPos.y,0,0,TRUE);
}

CDialog::OnMouseMove(nFlags, point);
}

Do I have to handle a WM_LBUTTONDON message to? if so, for what!


<br>CocaCola
ICQ: 50302279
E-mail: nikado@pc.nu

Report
Re: Move a window in MFC Posted by Eric Tetz on 13 Apr 2001 at 3:22 PM
: I want to have a window that has no title bar so you can't move it around. I've tried this but something is wrong because it disappears when using it:
:
: void CMoveagainDlg::OnMouseMove(UINT nFlags, CPoint point)
: {
: if((nFlags & MK_LBUTTON) == MK_LBUTTON)
: {
: cpPos.x = point.x;
: cpPos.y = point.y;
:
: MoveWindow(cpPos.x,cpPos.y,0,0,TRUE);
: }
:
: CDialog::OnMouseMove(nFlags, point);
: }
:
: Do I have to handle a WM_LBUTTONDON message to? if so, for what!

I wrote a little routine for that a long time ago...

  //------------------------------------------------------------------------------
  // HandleClientAreaDragMsg() - Eric Tetz 7/13/99
  //
  // If you pass WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE messages to this
  // function, it will enable "client area drag" for the window.
  //------------------------------------------------------------------------------
  static void HandleClientAreaDragMsg (HWND hwnd, UINT msg, int mouseX, int mouseY)
  {
    static int captureX = 0;
    static int captureY = 0;

    switch (msg)
    {
    case WM_LBUTTONDOWN:
      captureX = mouseX;
      captureY = mouseY;
      SetCapture (hwnd);
      break;

    case WM_LBUTTONUP:
      ReleaseCapture();
      break;

    case WM_MOUSEMOVE:
      if (GetCapture() == hwnd)
      {
        RECT rc;
        GetWindowRect (hwnd, &rc);
        int  newX   = rc.left + mouseX - captureX;
        int  newY   = rc.top  + mouseY - captureY;
        int  width  = rc.right - rc.left;
        int  height = rc.bottom - rc.top;
        UINT flags  = SWP_NOZORDER | SWP_NOACTIVATE;
        SetWindowPos (hwnd, NULL, newX, newY, width, height, flags);
      }
      break;
    }
  }
It's written using the raw API, but you can easily use it in your MFC app like this:

  void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point) 
  {
    HandleClientAreaDrag(m_hWnd, WM_LBUTTONDOWN, point.x, point.y);
    CDialog::OnLButtonDown(nFlags, point);
  }

  void CMoveagainDlg::OnLButtonUp(UINT nFlags, CPoint point) 
  {
    HandleClientAreaDrag(m_hWnd, WM_LBUTTONUP, point.x, point.y);
    CDialog::OnLButtonUp(nFlags, point);
  }

  void CMoveagainDlg::OnMouseMove(UINT nFlags, CPoint point) 
  {
    HandleClientAreaDrag(m_hWnd, WM_MOUSEMOVE, point.x, point.y);
    CDialog::OnMouseMove(nFlags, point);
  }
Cheers,
Eric

Report
Re: Move a window in MFC Posted by Sephiroth2 on 13 Apr 2001 at 4:13 PM
If you just want to move the window using the mouse, this is all you have to do:
if(msg==WM_LBUTTONDOWN) {
DefWindowProc(hWnd,SC_SYSCOMMAND,SC_MOVE+1,lparam);
}

Using MFC, it would be something like this...
void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
DefWindowProc(m_hWnd, SC_SYSCOMMAND, SC_MOVE+1,MAKELPARAM(point.x,point.y));
}

Report
Re: Move a window in MFC Posted by Eric Tetz on 13 Apr 2001 at 5:32 PM
: If you just want to move the window using the mouse, this is all you have to do:
: if(msg==WM_LBUTTONDOWN) {
: DefWindowProc(hWnd,SC_SYSCOMMAND,SC_MOVE+1,lparam);
: }
:
: Using MFC, it would be something like this...
: void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
: {
: DefWindowProc(m_hWnd, SC_SYSCOMMAND, SC_MOVE+1,MAKELPARAM(point.x,point.y));
: }

Ahhh!! Cool! Much easier than my method. For CocaCola's sake, I should point out a typo: it's WM_SYSCOMMAND, not SC_SYSCOMMAND.

Anyway, I can still see a situation where my approach would be useful... ;) Newer version of Windows have a "show window contents while dragging" option. If you want your window to show it's contents while dragging regardless of the Windows version and/or the setting of that option, you can use my EnableClientAreaDrag routine.

This can be really helpful if you have non-rectangular window. It looks cheesy if you have a window shaped like a pear, but when you drag it you see a rectangle - it kinda spoils the illusion.

Cheers,
Eric


Report
Re: Move a window in MFC Posted by Sephiroth2 on 14 Apr 2001 at 6:36 AM
: : If you just want to move the window using the mouse, this is all you have to do:
: : if(msg==WM_LBUTTONDOWN) {
: : DefWindowProc(hWnd,SC_SYSCOMMAND,SC_MOVE+1,lparam);
: : }
: :
: : Using MFC, it would be something like this...
: : void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
: : {
: : DefWindowProc(m_hWnd, SC_SYSCOMMAND, SC_MOVE+1,MAKELPARAM(point.x,point.y));
: : }
:
: Ahhh!! Cool! Much easier than my method. For CocaCola's sake, I should point out a typo: it's WM_SYSCOMMAND, not SC_SYSCOMMAND.
:
: Anyway, I can still see a situation where my approach would be useful... ;) Newer version of Windows have a "show window contents while dragging" option. If you want your window to show it's contents while dragging regardless of the Windows version and/or the setting of that option, you can use my EnableClientAreaDrag routine.
:
: This can be really helpful if you have non-rectangular window. It looks cheesy if you have a window shaped like a pear, but when you drag it you see a rectangle - it kinda spoils the illusion.
:
: Cheers,
: Eric
:
:
Right.
I gotta stop typing these things at 1AM... =)




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.