How to drag forms by using the client area with Visual C++ 2005 or with Visual C++ .NET
This step-by-step article describes how to permit a user to move a form by dragging the client area of the form. This article describes how to override the WndProc method of the form to enable the client area dragging.
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
- Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET 2003
- Microsoft .NET Framework 1.1 or later
This article assumes that you are familiar with the following topics:
- Microsoft Visual C++ 2005 or Microsoft Visual C++ .NET 2003
Introduction
When you design an application, you may want to permit the user to move the form of an application by dragging the client area of the form. For example, you may want to do this if the form does not have a title bar or if the form has an irregular (that is, non-rectangular) shape.
An example of an application that provides this type of functionality is the Microsoft Windows Media Player. The Media Player permits you to select a “skin” to provide a customized look for the player. When you select a skin, the title bar is hidden, and you can move the window by dragging the form itself.
When Microsoft Windows handles mouse activity on a form, Windows sends the WM_NCHITTEST message to the form to determine where mouse events occur. For example, if you click the title bar of a form, the form returns an HTCAPTION value that indicates that the mouse event occurs on the title bar. If you click the client area of the form, the form returns an HTCLIENT value that indicates that the mouse event occurs in the client area of the form.
When you override the WndProc method of the form, you can intercept the WM_NCHITTEST message and determine where mouse activity on the form occurs. If the activity occurs in the client area, you can return an HTCAPTION value that causes Windows to handle the activity as if the activity actually occurs in the title bar.
The following sample code overrides the WndProc method of the form. The code intercepts the WM_NCHITTEST message and determines whether the mouse activity occurs in the client area of the form. If the mouse activity occurs in the client area, the procedure returns an HTCAPTION value that causes Windows to handle the event as if it occurs in the title bar.
Note If you override the WndProc method of the form in this manner, the mouse events for the form are also overridden, and any code that those event handlers contain does not run.
Create the Sample
- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET 2003.
- On the File menu, point to New, and then click Project.
- Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.
Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.
- In the Name box, type DragForm, and then click OK.
By default, the Form1 form is created and is opened in design mode.
- Right-click Form1, and then click View Code.
- Add the following code before the Form1 class constructor code.
private: static const int WM_NCHITTEST = 0x84; static const int HTCLIENT = 0x1; static const int HTCAPTION = 0x2;
- Add the following method to the Form1 class.
protected: void WndProc(Message * m) { switch(m->Msg) { case WM_NCHITTEST: __super::WndProc(m); if ((int)m->Result == HTCLIENT) m->Result = (IntPtr)HTCAPTION; break; default: __super::WndProc(m); break; } return; }Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project.
- Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
/clr (Common Language Runtime Compilation)
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx (http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx) - Click Project, and then click <ProjectName> Properties.
- Press the CTRL+SHIFT+S key combination to save the project.
- Press the CTRL+SHIFT+B key combination to build the solution.
- Press the CTRL+F5 key combination to run the project.
- Click anywhere in the client area of the form, and then try to drag the form. Notice that you can successfully move the form by dragging it from anywhere in the client area or title bar of the form.