回答:
CodeProjectに関するこの記事では、手法について詳しく説明しています。基本的に要約すると:
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);
    }
}
これは基本的に、ウィンドウマネージャーの観点から、ウィンドウのタイトルバーを取得することとまったく同じです。
Form1_MouseDown、実際のMouseDownイベントに割り当てられていないため、機能しないコードをコピーしましたForm1。
                    this.MouseDown += ...のMain()関数に追加する必要があります
                    物事を必要以上に難しくしないようにしましょう。フォーム(または別のコントロール)をドラッグできるコードスニペットをたくさん見つけました。そしてそれらの多くは、独自の欠点/副作用があります。特に、Windowsをだましてフォーム上のコントロールを実際のフォームであると思わせるようなものです。
そうは言っても、これが私のスニペットです。いつも使っています。また、this.Invalidate();は使用しないでください。フォームがちらつく場合があるので、他の人がやりたいように。そして、場合によってはこれも行います。this.Updateを使用して、ちらつきの問題は発生していません。
private bool mouseDown;
private Point lastLocation;
    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDown = true;
        lastLocation = e.Location;
    }
    private void Form1_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 Form1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;
    }同じことを行う別の簡単な方法。
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // set this.FormBorderStyle to None here if needed
        // if set to none, make sure you have a way to close the form!
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_NCHITTEST)
            m.Result = (IntPtr)(HT_CAPTION);
    }
    private const int WM_NCHITTEST = 0x84;
    private const int HT_CLIENT = 0x1;
    private const int HT_CAPTION = 0x2;
}MouseDown、MouseMove、MouseUpを使用します。そのための変数フラグを設定できます。サンプルがありますが、修正する必要があると思います。
マウスのアクションをパネルにコーディングしています。パネルをクリックすると、フォームも一緒に移動します。
//Global variables;
private bool _dragging = false;
private Point _offset;
private Point _start_point=new Point(0,0);
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
   _dragging = true;  // _dragging is your variable flag
   _start_point = new Point(e.X, e.Y);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
   _dragging = false; 
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
  if(_dragging)
  {
     Point p = PointToScreen(e.Location);
     Location = new Point(p.X - this._start_point.X,p.Y - this._start_point.Y);     
  }
}WPFのみ
正確なコードはありませんが、最近のプロジェクトでは、MouseDownイベントを使用して、これを単純に記述しました。
frmBorderless.DragMove();これはテスト済みで、理解しやすいです。
protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case 0x84:
            base.WndProc(ref m);
            if((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;
            return;
    }
    base.WndProc(ref m);
}WM_NCHITTEST偽装です。
                    これを魔法のように起こすために反転できるプロパティはありません。フォームのイベントを見て、それが設定することで、これを実装するために些細ななりthis.Topとthis.Left。具体的にはMouseDown、MouseUpとを確認する必要がありますMouseMove。
public Point mouseLocation;
private void frmInstallDevice_MouseDown(object sender, MouseEventArgs e)
{
  mouseLocation = new Point(-e.X, -e.Y);
}
private void frmInstallDevice_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left)
  {
    Point mousePos = Control.MousePosition;
    mousePos.Offset(mouseLocation.X, mouseLocation.Y);
    Location = mousePos;
  }
}これはあなたの問題を解決することができます...
上記のリンクからのこのコードは、私の場合はうまくいきました:)
protected override void OnMouseDown(MouseEventArgs e)  
{
      base.OnMouseDown(e);
      if (e.Button == MouseButtons.Left)
      {
        this.Capture = false;
        Message msg = Message.Create(this.Handle, 0XA1, new IntPtr(2), IntPtr.Zero);
        this.WndProc(ref msg);
      }
}私が見つけた最良の方法(もちろん修正済み)
// 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; }
    }
}コントロールにドラッグを適用するには、これをInitializeComponent()の後に挿入するだけです
AddDrag(NameOfControl);それは私のために働いた。
    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);
        }
    }.NET Framework 4の場合
ドラッグに使用しているコンポーネント(この例ではmainLayout)this.DragMove()のMouseDownイベントに使用できます。
private void mainLayout_MouseDown(object sender, MouseButtonEventArgs e)
{
    this.DragMove();
}最も簡単な方法は次のとおりです。
まず、label1という名前のラベルを作成します。label1のイベント>マウスイベント> Label1_Mouse Moveに移動して、次のように記述します。
if (e.Button == MouseButtons.Left){
    Left += e.X;
    Top += e.Y;`
}WPF Element HostコントロールとWPF Userコントロールを含むボーダーレスウィンドウフォームを移動可能にしようとしていました。
結局、WPFユーザーコントロールにStackPanelというスタックパネルがあり、クリックして移動するのは論理的なことのように思えました。マウスをゆっくり動かすとjunmatsのコードを試すことができましたが、マウスを速く動かすと、マウスがフォームから外れ、フォームが移動の途中で動かなくなります。
これにより、CaptureMouseとReleaseCaptureMouseを使用した私の状況に対する彼の回答が改善され、マウスをすばやく動かしても、フォームの移動中にマウスがフォームから移動しなくなりました。
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
    _start_point = e.GetPosition(this);
    StackPanel.CaptureMouse();
}
private void StackPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
    StackPanel.ReleaseMouseCapture();
}
private void StackPanel_MouseMove(object sender, MouseEventArgs e)
{
    if (StackPanel.IsMouseCaptured)
    {
        var p = _form.GetMousePositionWindowsForms();
        _form.Location = new System.Drawing.Point((int)(p.X - this._start_point.X), (int)(p.Y - this._start_point.Y));
    }
}
    //Global variables;
    private Point _start_point = new Point(0, 0);いくつかの回答では、子コントロールをドラッグできないようにするため、小さなヘルパークラスを作成しました。トップレベルのフォームを渡す必要があります。必要に応じて、より汎用的にすることができます。
class MouseDragger
{
    private readonly Form _form;
    private Point _mouseDown;
    protected void OnMouseDown(object sender, MouseEventArgs e)
    {
        _mouseDown = e.Location;
    }
    protected void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int dx = e.Location.X - _mouseDown.X;
            int dy = e.Location.Y - _mouseDown.Y;
            _form.Location = new Point(_form.Location.X + dx, _form.Location.Y + dy);
        }
    }
    public MouseDragger(Form form)
    {
        _form = form;
        MakeDraggable(_form);            
    }
    private void MakeDraggable(Control control)
    {
        var type = control.GetType();
        if (typeof(Button).IsAssignableFrom(type))
        {
            return;
        }
        control.MouseDown += OnMouseDown;
        control.MouseMove += OnMouseMove;
        foreach (Control child in control.Controls)
        {
            MakeDraggable(child);
        }
    }
}MainWindowにMouseLeftButtonDownイベントハンドラーを追加すると、うまくいきました。
自動生成されるイベント関数に、以下のコードを追加します。
base.OnMouseLeftButtonDown(e);
this.DragMove();ToolStrip1_MouseLeaveマウスがすばやく移動して領域を離れるイベントを処理するもう1つのメソッドを使用して、jay_t55からソリューションを拡張しています。
private bool mouseDown;
private Point lastLocation;
private void ToolStrip1_MouseDown(object sender, MouseEventArgs e) {
    mouseDown = true;
    lastLocation = e.Location;
}
private void ToolStrip1_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 ToolStrip1_MouseUp(object sender, MouseEventArgs e) {
    mouseDown = false;
}
private void ToolStrip1_MouseLeave(object sender, EventArgs e) {
    mouseDown = false;
}私は以下を試してみて、changeoを試してみました。透明なウィンドウはもう固定されていませんが、移動できました!! (上記の他の複雑なソリューションはすべて破棄してください...)
   private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);
        // Begin dragging the window
        this.DragMove();
    }Form1(): new Moveable(control1, control2, control3);
クラス:
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; }
                }
            };
        }
    }
}   [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);
    }