移动没有边框的窗口

时间:2011-01-02 04:29:28

标签: c# move menustrip borderless

如何移动没有边框的窗口。应用程序上没有空白区域,所有可用的内容都是webbrowser和menustrip。我希望用户能够通过拖动菜单条来移动窗口。我该如何编码呢?我尝试了一些我在网上找到的代码块,但都没有。

8 个答案:

答案 0 :(得分:15)

This Code Project文章应该可以帮助您实现这一目标。我自己也没用过这个问题。这是它的主旨:

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事件。

答案 1 :(得分:10)

这是.Net Way

    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;
    }

就是这样。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我没有尝试过,但如果您可以在菜单栏上处理“OnMouseDown”和“onMouseUp”事件:

  • 鼠标按下 - 根据鼠标移动移动窗口
  • 停止跟踪鼠标向上移动或鼠标移动

答案 4 :(得分:0)

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

中添加

<强> 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);
            }
        }

答案 5 :(得分: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;
    }

答案 6 :(得分:0)

我不得不使用;; QUESTION SECTION: ;127.0.0.5._ip.test.com._ehlo.foo.com._spf.moat.email. IN TXT ;; ANSWER SECTION: foo.com. 600 IN TXT "v=spf1 include:test.com -all" - 只是想我会发表评论并让大家都知道。

答案 7 :(得分:0)

将起点放入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);

        }

    }
}