使无边框形式可移动?

时间:2009-10-20 06:53:52

标签: c# winforms border movable

有没有办法让一个没有边框的表单(FormBorderStyle设置为“none”)可以在窗体上单击鼠标时移动,就好像有一个边框一样?

20 个答案:

答案 0 :(得分:218)

关于CodeProject的

This文章详述了一项技术。基本归结为:

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

[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.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);
    }
}

从窗口管理器的角度来看,这基本上完全与抓取窗口的标题栏相同。

答案 1 :(得分:40)

让我们不要让事情变得比他们需要的更困难。我遇到了很多代码片段,允许你拖动一个表单(或另一个控件)。其中许多都有自己的缺点/副作用。特别是那些他们欺骗Windows认为表单上的控件是实际形式的那些。

话虽如此,这是我的片段。我用它所有的时间。我还要注意你不应该使用this.Invalidate();正如其他人喜欢这样做,因为它会导致表格在某些情况下闪烁。在某些情况下,这样做.Refresh。使用this.Update,我没有任何闪烁问题:

private bool mouseDown;
private Point lastLocation;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDown = true;
        lastLocation = e.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if(mouseDown)
        {
            this.Location = new Point(
                (this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);

            this.Update();
        }
    }

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

答案 2 :(得分:29)

另一种更简单的方法来做同样的事情。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // set this.FormBorderStyle to None here if needed
        // if set to none, make sure you have a way to close the form!
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_NCHITTEST)
            m.Result = (IntPtr)(HT_CAPTION);
    }

    private const int WM_NCHITTEST = 0x84;
    private const int HT_CLIENT = 0x1;
    private const int HT_CAPTION = 0x2;
}

答案 3 :(得分:18)

使用MouseDown,MouseMove和MouseUp。您可以为其设置变量标志。我有一个样本,但我认为你需要修改。

我正在将鼠标操作编码到面板。单击面板后,表单将随之移动。

//Global variables;
private bool _dragging = false;
private Point _offset;
private Point _start_point=new Point(0,0);


private void panel1_MouseDown(object sender, MouseEventArgs e)
{
   _dragging = true;  // _dragging is your variable flag
   _start_point = new Point(e.X, e.Y);
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
   _dragging = false; 
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
  if(_dragging)
  {
     Point p = PointToScreen(e.Location);
     Location = new Point(p.X - this._start_point.X,p.Y - this._start_point.Y);     
  }
}

答案 4 :(得分:11)

仅限WPF


没有准确的代码,但在最近的一个项目中,我认为我使用了MouseDown事件,只是简单地说:

frmBorderless.DragMove();

Window.DragMove Method (MSDN)

答案 5 :(得分:8)

Ref. video Link

经过测试且易于理解。

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case 0x84:
            base.WndProc(ref m);
            if((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;
            return;
    }

    base.WndProc(ref m);
}

答案 6 :(得分:4)

public Point mouseLocation;
private void frmInstallDevice_MouseDown(object sender, MouseEventArgs e)
{
  mouseLocation = new Point(-e.X, -e.Y);
}

private void frmInstallDevice_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left)
  {
    Point mousePos = Control.MousePosition;
    mousePos.Offset(mouseLocation.X, mouseLocation.Y);
    Location = mousePos;
  }
}

这可以解决你的问题......

答案 7 :(得分:4)

没有任何属性你可以翻转使这只是神奇地发生。查看表单的事件,通过设置this.Topthis.Left来实现这一点变得非常简单。具体来说,您需要查看MouseDownMouseUpMouseMove

答案 8 :(得分:3)

我发现的最佳方式(当然已修改)

// This adds the event handler for the control
private void AddDrag(Control Control) { Control.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DragForm_MouseDown); }
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void DragForm_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        // Checks if Y = 0, if so maximize the form
        if (this.Location.Y == 0) { this.WindowState = FormWindowState.Maximized; }
    }
}

要将拖动应用于控件,只需在InitializeComponent()

之后插入即可
AddDrag(NameOfControl);

答案 9 :(得分:2)

https://social.msdn.microsoft.com/Forums/vstudio/en-US/d803d869-68e6-46ff-9ff1-fabf78d6393c/how-to-make-a-borderless-form-in-c?forum=csharpgeneral

以上链接中的这段代码在我的情况下起了作用:)

protected override void OnMouseDown(MouseEventArgs e)  

{
      base.OnMouseDown(e);
      if (e.Button == MouseButtons.Left)
      {
        this.Capture = false;
        Message msg = Message.Create(this.Handle, 0XA1, new IntPtr(2), IntPtr.Zero);
        this.WndProc(ref msg);
      }
}

答案 10 :(得分:1)

我试图制作一个可移动的无边框窗体,其中包含一个WPF元素主机控件和一个WPF用户控件。

我最终在我的WPF用户控件中使用了一个名为StackPanel的堆栈面板,这似乎是尝试点击移动的合理逻辑。当我慢慢移动鼠标时尝试junmats的代码工作,但是如果我更快地移动鼠标,鼠标将移离窗体并且窗体将被移动到移动中间的某处。

这改善了他使用CaptureMouse和ReleaseCaptureMouse对我的情况的回答,现在即使我快速移动它,鼠标也不会移动它移动它。

private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
    _start_point = e.GetPosition(this);
    StackPanel.CaptureMouse();
}

private void StackPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
    StackPanel.ReleaseMouseCapture();
}

private void StackPanel_MouseMove(object sender, MouseEventArgs e)
{
    if (StackPanel.IsMouseCaptured)
    {
        var p = _form.GetMousePositionWindowsForms();
        _form.Location = new System.Drawing.Point((int)(p.X - this._start_point.X), (int)(p.Y - this._start_point.Y));
    }
}

    //Global variables;
    private Point _start_point = new Point(0, 0);

答案 11 :(得分:1)

此外,如果您需要DoubleClick并使您的表格更大/更小,您可以使用第一个答案,创建一个全局int变量,每次用户点击您用于拖动的组件时添加1。如果variable == 2,则表示您的表单更大/更小。还要每隔半秒钟或秒钟使用一个计时器来制作variable = 0;

答案 12 :(得分:1)

最简单的方法是:

首先创建一个名为label1的标签。 转到label1&#39; s事件&gt; <鼠标事件> Label1_Mouse移动并写下这些:

if (e.Button == MouseButtons.Left){
    Left += e.X;
    Top += e.Y;`
}

答案 13 :(得分:1)

对于.NET Framework 4,

您可以将this.DragMove()用于您用来拖动的组件的MouseDown事件(本例中为mainLayout)。

private void mainLayout_MouseDown(object sender, MouseButtonEventArgs e)
{
    this.DragMove();
}

答案 14 :(得分:1)

由于某些答案不允许子控件可拖动,因此我创建了一个小帮助程序类。 应该通过顶层表格。如果需要,可以使其更通用。

class MouseDragger
{
    private readonly Form _form;
    private Point _mouseDown;

    protected void OnMouseDown(object sender, MouseEventArgs e)
    {
        _mouseDown = e.Location;
    }

    protected void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int dx = e.Location.X - _mouseDown.X;
            int dy = e.Location.Y - _mouseDown.Y;
            _form.Location = new Point(_form.Location.X + dx, _form.Location.Y + dy);
        }
    }
    public MouseDragger(Form form)
    {
        _form = form;

        MakeDraggable(_form);            
    }

    private void MakeDraggable(Control control)
    {
        var type = control.GetType();
        if (typeof(Button).IsAssignableFrom(type))
        {
            return;
        }

        control.MouseDown += OnMouseDown;
        control.MouseMove += OnMouseMove;

        foreach (Control child in control.Controls)
        {
            MakeDraggable(child);
        }
    }
}

答案 15 :(得分:1)

对我有用。

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        _mouseLoc = e.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int dx = e.Location.X - _mouseLoc.X;
            int dy = e.Location.Y - _mouseLoc.Y;
            this.Location = new Point(this.Location.X + dx, this.Location.Y + dy);
        }
    }

答案 16 :(得分:0)

向MainWindow添加MouseLeftButtonDown事件处理程序对我有用。

在自动生成的事件函数中,添加以下代码:

base.OnMouseLeftButtonDown(e);
this.DragMove();

答案 17 :(得分:0)

我尝试了以下操作,然后进行了更改,我的透明窗口不再冻结到位,而是可以移动的!! (丢弃上面所有其他复杂的解决方案...)

const arr = ['my string is awesome ', getDataById('010203'), ' and super cool ', getDataById('090807')]

答案 18 :(得分:0)

我正在使用另一种方法ToolStrip1_MouseLeave从jay_t55扩展解决方案,该方法可以处理鼠标快速移动并离开区域的事件。

private bool mouseDown;
private Point lastLocation;

private void ToolStrip1_MouseDown(object sender, MouseEventArgs e) {
    mouseDown = true;
    lastLocation = e.Location;
}

private void ToolStrip1_MouseMove(object sender, MouseEventArgs e) {
    if (mouseDown) {
        this.Location = new Point(
            (this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);

        this.Update();
    }
}

private void ToolStrip1_MouseUp(object sender, MouseEventArgs e) {
    mouseDown = false;
}

private void ToolStrip1_MouseLeave(object sender, EventArgs e) {
    mouseDown = false;
}

答案 19 :(得分:0)

Form1():new Moveable(control1, control2, control3);

班级:

using System;
using System.Windows.Forms;

class Moveable
{
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();
    public Moveable(params Control[] controls)
    {
        foreach (var ctrl in controls)
        {
            ctrl.MouseDown += (s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(ctrl.FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                    // Checks if Y = 0, if so maximize the form
                    if (ctrl.FindForm().Location.Y == 0) { ctrl.FindForm().WindowState = FormWindowState.Maximized; }
                }
            };
        }
    }
}