z3nth10n
z3nth10n

Reputation: 2441

Drag & drop event for WinForms Form not Control

What I'm trying to do.

I have searching if there exists a drag&drop event for a System.Windows.Forms.Form class without luck...

I was wondering if there is an optimized way of doing this...


What I have done so far.

I have been doing the following thing:

    private Rectangle WinBar
    {
        get
        {
            return new Rectangle(Location.X, Location.Y, Width, 31);
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && WinBar.Contains(e.Location))
            Console.WriteLine("Being dragged!");
    }

My purpouse is that if the IF-statment is true I will call an EventHandler.


My main questions

But, for now, I can't figure this out, because of the following reasons:


Why I used 31 pixels for the WinBar height

I have checked that WinForms when FormBorderStyle property is set to FormBorderStyle.FixedSingle or Fixed3D or FixedSingle or Sizable is 31 pixels, as this image show us:

evidence

The evidence is on the size of the selection on MSPaint.

But I not use how to measure it for example for FixedToolWindow or SizableToolWindow. (Maybe I should use a ternary condition)


So, there are several uncertain things, and I don't know if this is the better way to do it. So, any help would be great!

Thanks in advance!

Upvotes: 0

Views: 476

Answers (1)

C. Gonzalez
C. Gonzalez

Reputation: 724

If I understand you correctly, you are trying to sense when the whole form is moved around the screen, right?

If so, use the Form´s LocationChanged event and examine the Form´s Location property to see where you are...

Edit to add example:

using System;
using System.Windows.Forms;

namespace WindowsFormsTestMove
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_LocationChanged(object sender, EventArgs e)
        {
            var loc = this.Location;
            this.Text = loc.X + " " + loc.Y;
        }
    }
}

Upvotes: 1

Related Questions