我如何移动一个没有边框的窗口。应用程序上没有空白空间,所有可用的都是webbrowser和menustrip。我希望用户能够通过拖动菜单条来移动窗口。我该如何编码?我尝试了一些在线发现的代码块,但它们都没有用。

有帮助吗?

解决方案

这个 代码项目文章应帮助您实现这一目标。我自己没有任何问题。这是它的jist:

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();

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

这基本上将“欺骗”窗口经理以为它正在抓住Winform的标题栏。

要将其应用于您的项目,只需使用Menustrip的Mousedown活动即可。

其他提示

这是.net方式

    private bool dragging = false;
    private Point dragCursorPoint;
    private Point dragFormPoint;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;
        dragCursorPoint = Cursor.Position;
        dragFormPoint = this.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
            this.Location = Point.Add(dragFormPoint, new Size(dif));
        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }

而已。

您可以伪造您的menustrip,例如使用带有标签的面板。然后,您可以手动处理此操作:当用户单击标签时,将打开弹出菜单,当用户拖动标签时,窗口将移动。但是我建议不要这样的解决方法,因为它不是标准的GUI行为,您可能会使您的用户感到困惑。

我没有尝试过,但是如果您可以处理菜单栏上的“ onmousedown”和“ onmouseup”事件:

  • 在鼠标向下 - 根据鼠标运动移动窗口
  • 停止跟踪鼠标上的鼠标运动或鼠标向外移动

如果您使用的是面板,则必须在

yourform.designer.cs

this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);

这在

yourform.cs

 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();

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

Mbithi Kioko在正确的轨道上,但我会这样做。

    bool dragging = false;
    int xOffset = 0;
    int yOffset = 0;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;

        xOffset = Cursor.Position.X - this.Location.X;
        yOffset = Cursor.Position.Y - this.Location.Y;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            this.Location = new Point(Cursor.Position.X - xOffset, Cursor.Position.Y - yOffset);
            this.Update();
        }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }

我不得不使用 System.Runtime.InteropServices.DllImportAttribute - 只是想我会发表评论,让大家知道。

只需将起点放入这样的2D阵列中:

public partial class mainForm : Form
{
    //Global variables for Moving a Borderless Form
    private bool dragging = false;
    private Point startPoint = new Point(0, 0); 


    public mainForm()
    {
        InitializeComponent();
    }

    private void mainForm_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;
        startPoint = new Point(e.X, e.Y);

    }

    private void mainForm_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }

    private void mainForm_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            Point p = PointToScreen(e.Location);
            Location = new Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);

        }

    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top