【问题标题】:How do I make mousedrag inside Panel move form window?如何使面板内的鼠标拖动从窗口移动?
【发布时间】:2022-05-14 20:47:26
【问题描述】:

我有我想要启用的 System.Windows.Forms.Panel,这样如果用户单击并拖动鼠标,就会将窗口拖到周围。

我可以这样做吗?我必须实现多个事件吗?

【问题讨论】:

  • 使用面板的 MouseMove 事件。

标签: c# winforms


【解决方案1】:

最适合我的解决方案是使用非托管代码,与 HatSoft 发布的答案不同,它可以为您提供平滑的窗口移动。

3 个小步骤在 Panel 上拖动窗口

using System.Runtime.InteropServices;

在你的类中添加这六行

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

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

Panel 上的 MouseMove 事件应如下所示

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

发布有点晚了 :) ,谁知道我们将来可能会再次需要它。

【讨论】:

  • 例如const int WM_NCLBUTTONDOWN = 0x00A1;
  • 哦,是的,我错过了,感谢您指出这一点。 :) 更新了我的答案
  • 这应该被选为接受的答案。奇迹般有效。当前接受的答案存在问题,但对解决此问题时要采取的方法有所帮助。
  • 接受的答案在使用多个屏幕时会导致问题。这应该是公认的答案。易于实施且非常流畅
  • 这真是完美的解决方案。其他被接受的解决方案正在闪烁。这很顺利。您也可以将此功能赋予面板上的标签和其他控件。就我而言,面板上没有空间可以拖动。所以我在“事件”选项卡中为标签提供相同的功能。
【解决方案2】:

可以通过面板的MouseMove事件来实现

示例应该是这样的(抱歉没有测试过)

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.Location = new Point(Cursor.Position.X + e.X , Cursor.Position.Y + e.Y);
    }
}

【讨论】:

    【解决方案3】:

    您可能想看看我在这里粘贴的这个组件:

    http://pastebin.com/5ufJmuay

    它是一个组件,您可以将其拖放到表单上,然后通过在其中拖动来拖动表单。

    【讨论】:

      【解决方案4】:

      当前设置为面板。 VS C# Just Messing About 似乎对我有用 按下左键时将应用程序的左上角设置为鼠标位置。

       public form1()
          {
              InitializeComponent();
              this.panel2.MouseMove += new MouseEventHandler(panel2_MouseMove);
          }
          
          public const int WM_NCLBUTTONDOWN = 0xA1;
          public const int HT_CAPTION = 0x2;
      
          [DllImportAttribute("user32.dll")]
          public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
          
          private void panel2_MouseMove(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
              {
                  
                  Point loc1 = MousePosition;
                  this.Location = loc1;
              }
          }
      

      【讨论】:

        【解决方案5】:

        嘿希望这对你有用

        1. 首先,您必须在表单上添加 panel 并将其停靠在顶部
        2. 然后添加这几行代码
        
        using System.Runtime.InteropServices;
        
        
        public partial class Main_FM : Form
        {
        
          [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
            private extern static void ReleaseCapture();
        
          [DllImport("user32.dll", EntryPoint = "SendMessage")]
            private extern static void SendMessge(System.IntPtr hwnd, int wmsg, int wparam, int lparam);
        
        }
        
        1. panel 上创建MouseDown 事件并添加以下代码:
         private void Top_PNL_MouseDown(object sender, MouseEventArgs e)
          {
              ReleaseCapture();
              SendMessge(this.Handle, 0x112, 0xf012, 0);
          }
        

        【讨论】:

          【解决方案6】:

          Bravo 的代码工作得非常好,但是直到我在我想要移动的面板的->properties->事件部分中明确启用 MouseMove 事件后,我才能让它工作。

          【讨论】:

            猜你喜欢
            • 2021-09-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多