حرکت دادن فرم با موس در سی شارپ #C

کد با پلتفرم بوی - کد سی شارپ
کد با پلتفرم بوی – کد سی شارپ

روش اول

اگر نیاز دارید که فرم بدون Titlebar خود را حرکت دهید می توانید از قطعه کد زیر استفاده کنید :

پلتفرم بوی اجرا شدن خروجی صحیح قطعه کد زیر را تایید می کند

قطعه کدی که در زیر مشاهده می کنید توسط برنامه کامپایلر به جهت تست آزمایش شده و خروجی صحیح مدنظر را بدست آورد.

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

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

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{     
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

روش دوم

[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
   private extern static void ReleaseCapture();

   [DllImport("user32.DLL", EntryPoint = "SendMessage")]
   private extern static void SendMessage(System.IntPtr hWnd, int Msg, int wParam, int lParam);
   private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
   {
       ReleaseCapture();
       SendMessage(this.Handle, 0x112, 0xf012, 0);
   }

روش سوم

می توانید از کلاس زیر استفاده کنید :

using System;
using System.Windows.Forms;

class Moveable
{
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();
    public Moveable(params Control[] controls)
    {
        foreach (var ctrl in controls)
        {
            ctrl.MouseDown += (s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(ctrl.FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                    // Checks if Y = 0, if so maximize the form
                    if (ctrl.FindForm().Location.Y == 0) { ctrl.FindForm().WindowState = FormWindowState.Maximized; }
                }
            };
        }
    }
}

روش چهارم

private void Form1_MouseDown(object sender, MouseEventArgs e)
   {
       _mouseLoc = e.Location;
   }

   private void Form1_MouseMove(object sender, MouseEventArgs e)
   {
       if (e.Button == MouseButtons.Left)
       {
           int dx = e.Location.X - _mouseLoc.X;
           int dy = e.Location.Y - _mouseLoc.Y;
           this.Location = new Point(this.Location.X + dx, this.Location.Y + dy);
       }
   }

روش پنجم

// This adds the event handler for the control
private void AddDrag(Control Control) { Control.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DragForm_MouseDown); }
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void DragForm_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        // Checks if Y = 0, if so maximize the form
        if (this.Location.Y == 0) { this.WindowState = FormWindowState.Maximized; }
    }
}

کدهای بیشتر - سی شارپ

آیا این مطلب برای شما مفید بود؟

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *