Click here to Skip to main content
13,189,506 members (64,259 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 25 Sep 2007

Creating Movable Child Window Controls

, 25 Sep 2007
Rate this:
Please Sign up or sign in to vote.
A walkthrough on building a Windows control that can be moved by the user at runtime.


I have come across many queries on 'how to make a control on a dialog movable' and 'how to make a group of controls movable at runtime'. Here is an attempt to achieve the same, in its simplest form; however, this can be molded as per requirements.

Screenshot - Position1.jpg

Screenshot - Position2.jpg


As per Windows predefined behavior, we can move the dialog/frame windows at runtime using their caption/title bar. There can be instances when you want to move a control/a group of controls which do not have a title/caption bar. The sample code here addresses that.

Using the code

The code is written and compiled using Microsoft Visual Studio 6.0.

Use the Dialog Layout Editor of Windows to place the controls on a dialog.

Screenshot - LayoutEditor.jpg

You can associate the member variables as per your requirement, with these controls:

// MovableTrialDlg.h
CStatic m_staticMovable;
CEdit   m_editMovable;

Define additional member variables in the class CMovableTrialDlg.

// MovableTrialDlg.h
bool m_bMoving;

Please include PreTranslateMessage() in your code.

// MovableTrialDlg.h
virtual BOOL PreTranslateMessage(MSG* pMsg);

PreTranslateMessage() performs the required action for the controls which are required to be moved.

BOOL CMovableTrialDlg::PreTranslateMessage(MSG* pMsg) 
 // TODO: Add your specialized code here and/or call the base class
 if(pMsg->hwnd == m_editMovable.m_hWnd)
  if(pMsg->message == WM_LBUTTONUP)
  if(pMsg->message == WM_LBUTTONDOWN)
  if(pMsg->message == WM_MOUSEMOVE)
 return CDialog::PreTranslateMessage(pMsg);

While the mouse moves and the status for moving the windows, m_bMoving, is true, perform the movement by changing the window placement for the controls.

void CMovableTrialDlg::OnMovableStaticMouseMove(MSG* pMsg)
 if(m_bMoving == false)
 ::GetWindowPlacement(m_editMovable.m_hWnd, &wp);
 int nXDiff = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
 int nYDiff = wp.rcNormalPosition.bottom -;
 POINT pt = pMsg->pt;
 wp.rcNormalPosition.left  = pt.x - 5;   = pt.y - 5;
 wp.rcNormalPosition.right = wp.rcNormalPosition.left + nXDiff;
 wp.rcNormalPosition.bottom= + nYDiff;
 ::SetWindowPlacement(m_editMovable.m_hWnd, &wp);
 ::GetWindowPlacement(m_staticMovable.m_hWnd, &wp);
 nXDiff = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
 nYDiff = wp.rcNormalPosition.bottom -;
 pt = pMsg->pt;
 wp.rcNormalPosition.left  = pt.x - 5;   = pt.y - nYDiff - 5;
 wp.rcNormalPosition.right = wp.rcNormalPosition.left + nXDiff;
 wp.rcNormalPosition.bottom= + nYDiff;
 ::SetWindowPlacement(m_staticMovable.m_hWnd, &wp);

As soon as the mouse left button down event occurs on the edit control, set the moving status, m_bMoving, to true.

void CMovableTrialDlg::OnMovableStaticLButtonDown(MSG* pMsg)
 m_bMoving = true;

Similarly when the mouse left button is up, change the status for moving, m_bMoving, to false.

void CMovableTrialDlg::OnMovableStaticLButtonUp(MSG* pMsg)
 m_bMoving = false;


I'd supply the code so that the group of controls look like a child container with a non-MDI frame/dialog and can be moved using the caption/title bar. The caption/title bar will be simulated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171016.2 | Last Updated 25 Sep 2007
Article Copyright 2007 by saud_ad
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid