Click here to Skip to main content
Click here to Skip to main content
Go to top

DragExtender - An extenderprovider for dragging functionality

, 28 Oct 2004
Rate this:
Please Sign up or sign in to vote.
An extenderprovider for dragging functionality.

Sample Image - DragExtender.jpg

Introduction

This is a piece of code that I wrote to simplify creating borderless forms.

Background

Have you ever used a borderless form? You have to write your own move handlers. If you include (e.g.) a panel on that form, you have to write move handlers for that as well. So, I wrote a little IExtenderProvider implementation to provide this functionality. You can use this code in other ways (with a little tweaking perhaps), e.g., to move controls around a form.

Using the code

I have yet to integrate this code into the Forms Designer to make it a no-brainer to use. Using it in its current form is very easy, however:

Add the extender as a private variable to the form.

private DragExtender dragExtender1;

In the constructor, add the following after the call to InitializeComponent():

this.dragExtender1 = new DragExtender();

You should also specify which component should be the drag target (this leaves a lot of room for experiments):

this.dragExtender1.Form = this;

The only code left is to assign draggable controls:

// make the form draggable
this.dragExtender1.SetDraggable(this, true);
// make panel1 draggable
this.dragExtender1.SetDraggable(this.panel1, true);

The DragExtender then captures the OnMouseDown event of that control and handles the dragging code:

private void control_MouseDown(object sender, MouseEventArgs e)
{
    if (!DesignMode && m_form!=null)
    {
        Control control = sender as Control;
        ReleaseCapture(control.Handle);
        int nul =0;
        SendMessage(m_form.Handle, WM_SYSCOMMAND, MOUSE_MOVE, ref nul);
    }
}

Points of Interest

I will try to integrate Windows Forms designer functionality for the next update.

History

  • Version 1.0, 28-10-2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Guido_d
Web Developer
Netherlands Netherlands
I am a .NET developer from Gouda, the Netherlands, developing software since mid eighties.

Comments and Discussions

 
Questionwhat about resize... Pinmembertyounsi28-Oct-05 10:07 
Generalgreat code Pinmembermarcel2464-Mar-05 3:41 
Great code but i would change one thing. The code doesn't check if the form is maximized before trying to move it, so i would just change this:
------------------------------------------------------------------------
private void control_MouseDown(object sender, MouseEventArgs e)
{
   if (!DesignMode && m_form!=null)
   {
      Control control = sender as Control;
      ReleaseCapture(control.Handle);
 
      int nul =0;
      SendMessage(m_form.Handle, WM_SYSCOMMAND, MOUSE_MOVE, ref nul);
   }
}
-------------------------------------------------------------------------
to this:
-------------------------------------------------------------------------
private void control_MouseDown(object sender, MouseEventArgs e)
{
   if (!DesignMode && m_form!=null)
   {
      if (m_form.WindowState != System.Windows.Forms.FormWindowState.Maximized )
      {
         Control control = sender as Control;
         ReleaseCapture(control.Handle);
 
         int nul =0;
         SendMessage(m_form.Handle, WM_SYSCOMMAND, MOUSE_MOVE, ref nul);
      }
   }
}
--------------------------------------------------------------------------
GeneralRe: great code PinmemberGuido_d13-Apr-05 23:59 
GeneralVery simple but it's wonderful Pinmembertaithien2-Nov-04 17:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 28 Oct 2004
Article Copyright 2004 by Guido_d
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid