Thursday, April 23, 2020

How to restrict user to drag a form outside the screen bounds



This code shows how we can restrict our winform within screen bounds in our windows form application using c#.



 public partial class frmLogin_restrictWithin_ScreenBounds : Form
    {
        private bool mouseDown;
        private Point lastLocation;
        public frmLogin_restrictWithin_ScreenBounds()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            IntPtr ptr = NativeMethods.CreateRoundRectRgn(15, 15, this.Width, this.Height, 40, 40); // _BoarderRaduis can be adjusted to your needs, try 15 to start.
            this.Region = System.Drawing.Region.FromHrgn(ptr);
            NativeMethods.DeleteObject(ptr);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDown = true;
                lastLocation = e.Location;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                this.Location = new Point(
                    (this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);

                this.Update();
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                var currHandleScreen = Screen.FromControl(this);
                if (this.Bounds.Top < currHandleScreen.Bounds.Top)
                    this.Location = new Point(this.Location.X, currHandleScreen.WorkingArea.Top);
                else if (this.Bounds.Right > currHandleScreen.Bounds.Right)
                    this.Location = new Point(currHandleScreen.WorkingArea.Right - this.Width, currHandleScreen.WorkingArea.Top);
                else if (this.Bounds.Left < currHandleScreen.Bounds.Left)
                    this.Location = new Point(currHandleScreen.WorkingArea.Left, currHandleScreen.WorkingArea.Top);
                else if (this.Bounds.Bottom > currHandleScreen.Bounds.Bottom)
                    this.Location = new Point(this.Location.X, currHandleScreen.WorkingArea.Top);
            }
            mouseDown = false;
        }

    }

    public class NativeMethods
    {
        [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        public static extern System.IntPtr CreateRoundRectRgn
         (
          int nLeftRect, // x-coordinate of upper-left corner
          int nTopRect, // y-coordinate of upper-left corner
          int nRightRect, // x-coordinate of lower-right corner
          int nBottomRect, // y-coordinate of lower-right corner
          int nWidthEllipse, // height of ellipse
          int nHeightEllipse // width of ellipse
         );

        [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
        public static extern bool DeleteObject(System.IntPtr hObject);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    }



No comments:

Post a Comment

How to use combobox using c#

This code shows how we can use combobox in our winforms application using c#.     public partial class frmComboBox_Demo : Form     {...