6.15 Make a Borderless Form Movable


Problem

You need to create a borderless form that can be moved. This might be the case if you are creating a custom window that has a unique look (for example, for a visually rich application such as a game or a media player).

Solution

Create another control that responds to the MouseDown , MouseUp , and MouseMove events and programmatically moves the form.

Discussion

Borderless forms omit a title bar, which makes it impossible for a user to move them. You can compensate for this shortcoming by adding a control to the form that serves the same purpose. For example, Figure 6.9 shows a form that includes a label to support dragging. The user can click this label, and then drag the form to a new location on the screen while holding down the mouse button. As the user moves the mouse, the form is automatically moved correspondingly, as though it's "attached" to the mouse pointer.


Figure 6.9: A movable borderless form.

To implement this solution, you need take the following steps:

  1. Create a form-level Boolean variable that tracks whether or not the form is currently being dragged.

  2. When the label is clicked, the code sets the flag to indicate that the form is in drag mode. At the same time, the current mouse position is recorded. You add this logic to the event handler for the Label.MouseDown event.

  3. When the user moves the mouse over the label, the form is moved correspondingly so that the position of the mouse over the label is unchanged. You add this logic to the event handler for the Label.MouseMove event.

  4. When the user releases the mouse button, the dragging mode is switched off. You add this logic to the event handler for the Label.MouseUp event.

Here's the complete form code:

 using System; using System.Windows.Forms; using System.Drawing; public class DragForm : System.Windows.Forms.Form {     // (Designer code omitted.)     // Tracks whether the form is in drag mode. If it is, mouse movements     // over the label will be translated into form movements.     private bool dragging;     // Stores the offset where the label is clicked.     private Point pointClicked;     private void lblDrag_MouseDown(object sender,       System.Windows.Forms.MouseEventArgs e) {              if (e.Button == MouseButtons.Left) {                      dragging = true;             pointClicked = new Point(e.X, e.Y);         }         else {                      dragging = false;         }     }     private void lblDrag_MouseMove(object sender,       System.Windows.Forms.MouseEventArgs e) {              if (dragging) {                      Point pointMoveTo;             // Find the current mouse position in screen coordinates.             pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));             // Compensate for the position the control was clicked.             pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);             // Move the form.             this.Location = pointMoveTo;         }        }     private void lblDrag_MouseUp(object sender,       System.Windows.Forms.MouseEventArgs e) {              dragging = false;     }     private void cmdClose_Click(object sender, System.EventArgs e) {              this.Close();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net