I;m not a great fan of title bars on windows forms, so I've removed it. However I'm looking for a way for the end user to drag it to wherever they choose.

Is there a way in the .Net framework? All my searches resulted in importing user32.dll, but the majority of answers were from a few years ago.

Was looking for an Allow Drag attribute for the form itself or even a control without luck.

As alway, I appreciate any advice warmly.

Check here: http://www.codeproject.com/KB/cs/csharpmovewindow.aspx

Two ways. First:

//API functions to move the form
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

//And in your mouse down event of the form:

public void Form1_MouseDown(object sender, MouseEventArgs e)
{
//If the left mouse is pressed, release form for movement
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}

 

//Second:

protected override void WndProc(ref Message m)
{
            base.WndProc(ref m);

            int WM_NCHITTEST = 0x84;
            if (m.Msg == WM_NCHITTEST)
            {
                int HTCLIENT = 1;
                int HTCAPTION = 2;
                if (m.Result.ToInt32() == HTCLIENT)
                    m.Result = (IntPtr)HTCAPTION;
            }
}

I use the second. Its much simplier and doesn't use any API's. Though the first method is the one that I was using during my VB6 days...

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Panel panel1 = new Panel();
        private PictureBox box = new PictureBox();
        private Panel closeButton = new Panel();
        private bool moveable;
        private Point currentPosition;
        private Image img;
        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            closeButton.Dock = DockStyle.Right;
            panel1.Controls.Add(closeButton);
            closeButton.Width = 35;
            closeButton.Click += new EventHandler(closeButton_Click);
            img = box.ErrorImage;
            box.Dispose();
            closeButton.Paint += new PaintEventHandler(closeButton_Paint);
            panel1.BackColor = Color.Blue;
            panel1.Dock = DockStyle.Top;
            panel1.Height = 35;
            this.Controls.Add(panel1);
            panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
            panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
            panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
        }

        void closeButton_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(img, 0, 0, closeButton.Width, closeButton.Height);
        }

        void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (moveable)
            {
                Point newPosition = Control.MousePosition;
                newPosition.X = newPosition.X - currentPosition.X; // .Offset(mouseOffset.X, mouseOffset.Y);
                newPosition.Y = newPosition.Y - currentPosition.Y;
                this.Location = newPosition;
            }
        }

        void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            moveable = false;
        }

        void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            moveable = true;
            currentPosition.X = e.X;
            currentPosition.Y = e.Y;
        }

        
    }
}

Hey thanks guys, I'll definately have a try at those ASAP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.