1

I want to make my mainform draggable like smartphone touchscreen.

So i put up a bunifugradientpanel in it and dock it in mainform. also put a bunifudragcontrol and set targetcontrol property = 'bunifugradientpanel' and vertical property to 'false' also fixed property to 'false'. however, whenever i drag my panel to the right in rumtime, the portion of the mainform is showing which is the white part in the picture.

here's what's happening when i drag him to the right.

The white part of the screen is the mainform. what i want is to stop the dragging activity if bunifugradientpanel location on mainform is = (x=0,y=0) so the the portion of the mainform wont appear. thanks for your help guys.

Héctor M.
  • 2,302
  • 4
  • 17
  • 35

1 Answers1

0

BunifuGradientPanel is a Third Party control and that's why it can present drawing and flickering problems, my recommendation is that you use a common Panel System.Windows.Forms.Panel

That said, I created this code for you, makes a control draggable only from right to left and from left to right while the mouse button is pressed:

using System;
using System.Windows.Forms;

namespace JeremyHelp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.UserPaint |
            ControlStyles.DoubleBuffer,
            true);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.SetDraggable(this.bunifuGradientPanel1, vertical: false);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams handleParam = base.CreateParams;
                handleParam.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED       
                return handleParam;
            }
        }

        private void SetDraggable(Control target, bool horizontal = true, bool vertical = true)
        {
            bool IsDraggable = false;
            int
            X = 0, Y = 0,
            A = 0, B = 0;

            target.MouseUp += (s, e) =>
            {
                IsDraggable = false;
                X = 0; Y = 0;
                A = 0; B = 0;
            };

            target.MouseDown += (s, e) =>
            {
                IsDraggable = true;
                A = Control.MousePosition.X - target.Left;
                B = Control.MousePosition.Y - target.Top;
            };

            target.MouseMove += (s, e) =>
            {
                X = Control.MousePosition.X;
                Y = Control.MousePosition.Y;

                if (IsDraggable)
                {
                    if (horizontal) target.Left = X - A;
                    if (vertical) target.Top = Y - B;
                }
            };
        }
    }
}
Héctor M.
  • 2,302
  • 4
  • 17
  • 35