gpt4 book ai didi

c# - 使用菜单条移动无边界表单

转载 作者:太空狗 更新时间:2023-10-30 01:24:15 24 4
gpt4 key购买 nike

我正在寻找一种使用菜单条移动表单的方法。

虽然周围有一些解决方案,但它们有一个我不喜欢的特殊问题。为了使这些方法起作用,需要在拖动菜单条之前已经聚焦表单。

是否有解决该特定问题的方法,以便菜单条实际上表现得像一个适当的窗口标题栏?

最佳答案

最好的办法是使用 pinvoke。将“mousedown”事件绑定(bind)到您想要拖动的控件。

using System.Runtime.InteropServices;

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

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

public Form1()
{
InitializeComponent();
}

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

这仍然需要聚焦表单,但您可以使用鼠标悬停来解决。它不是那么优雅,但它确实有效。

private void menuStrip1_MouseHover(object sender, EventArgs e)
{
Focus();
}

更新:悬停有轻微延迟,mousemove 响应更快

private void menuStrip1_MouseMove(object sender, MouseEventArgs e)
{
if (!Focused)
{
Focus();
}
}

关于c# - 使用菜单条移动无边界表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9935977/

24 4 0