【发布时间】:2022-05-14 20:47:26
【问题描述】:
我有我想要启用的 System.Windows.Forms.Panel,这样如果用户单击并拖动鼠标,就会将窗口拖到周围。
我可以这样做吗?我必须实现多个事件吗?
【问题讨论】:
-
使用面板的 MouseMove 事件。
我有我想要启用的 System.Windows.Forms.Panel,这样如果用户单击并拖动鼠标,就会将窗口拖到周围。
我可以这样做吗?我必须实现多个事件吗?
【问题讨论】:
最适合我的解决方案是使用非托管代码,与 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;
可以通过面板的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);
}
}
【讨论】:
【讨论】:
当前设置为面板。 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;
}
}
【讨论】:
嘿希望这对你有用
panel 并将其停靠在顶部
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);
}
panel 上创建MouseDown 事件并添加以下代码: private void Top_PNL_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessge(this.Handle, 0x112, 0xf012, 0);
}
【讨论】:
Bravo 的代码工作得非常好,但是直到我在我想要移动的面板的->properties->事件部分中明确启用 MouseMove 事件后,我才能让它工作。
【讨论】: