2009-08-06 65 views
14

使用C#。
我正在尝试移动没有标题栏的Form
我发现关于它的文章:http://www.codeproject.com/KB/cs/csharpmovewindow.aspx如何将FormBorderStyle属性设置为None时移动Windows窗体?

它的工作原理,只要我不设置FormBorderStyleNone

有没有办法让它使用这个属性设置为None

+0

它适用于我与FormBorderStyle设置为无。这是在Server 2008上,VS2008在.NET 3.5上。你的.NET版本和操作系统是什么? – 2009-08-06 22:28:26

+0

@Michael McCloskey - 我使用windows 7 rc,vs2008和.net 3.5。 – Moon 2009-08-06 22:35:39

+0

@Michael McClosKey - 不要错过!它现在有效。我不知道发生了什么。 – Moon 2009-08-06 22:36:38

回答

36

我知道这个问题已经过了一年多了,但我一直在寻找记住过去我是如何做到的。所以对于其他人的参考,最快和最简单的方法就是重写WndProc函数。

/* 
Constants in Windows API 
0x84 = WM_NCHITTEST - Mouse Capture Test 
0x1 = HTCLIENT - Application Client Area 
0x2 = HTCAPTION - Application Title Bar 

This function intercepts all the commands sent to the application. 
It checks to see of the message is a mouse click in the application. 
It passes the action to the base action by default. It reassigns 
the action to the title bar if it occured in the client area 
to allow the drag and move behavior. 
*/ 

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

这将允许通过在客户区域内单击并拖动来移动任何表单。

+0

你能解释一下这个 – masfenix 2010-08-04 21:14:59

+0

增加了一些细节,它是什么 – LizB 2010-08-04 21:28:48

+0

为什么我不能拖动窗体,当mousedown事件是在menuStrip上? – CMA 2012-08-17 07:02:07

35

这里是我找到的最好的方法。这是一个“.NET方式”,而不使用WndProc。您只需处理要拖动的曲面的MouseDown,MouseMove和MouseUp事件。

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

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

private void FormMain_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 FormMain_MouseUp(object sender, MouseEventArgs e) 
{ 
    dragging = false; 
} 
0

首先,我们将不得不使用命名空间作为

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

,最后我们将编写代码来发送每当用户按下鼠标按键的消息。如果用户按住鼠标按钮,表单将根据鼠标移动重新定位。

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    ReleaseCapture(); 
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
} 

请参考以下链接Dragable form

rahul-rajat-singh

1

我前一阵子有同样的问题,同时寻找答案,我发现下面的代码(不记得网站)和这是我做的:

Point mousedownpoint = Point.Empty; 

    private void Form_MouseDown(object sender, MouseEventArgs e) 
    { 
     mousedownpoint = new Point(e.X, e.Y); 
    } 

    private void Form_MouseMove(object sender, MouseEventArgs e) 
    { 

     if (mousedownpoint.IsEmpty) 
      return; 
     Form f = sender as Form; 
     f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y)); 

    } 

    private void Form_MouseUp(object sender, MouseEventArgs e) 
    { 
     mousedownpoint = Point.Empty; 
    } 
相关问题