如何移动没有边框的窗口。应用程序上没有空白空间,可用的只有一个网络浏览器和一个菜单条。我希望用户能够通过拖动菜单条来移动窗口。我该如何编码?我尝试了一些我在网上找到的代码块,但没有一个有效。
问问题
27924 次
8 回答
32
这篇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 事件。
于 2011-01-02T22:25:05.543 回答
19
这是.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;
}
就是这样。
于 2013-10-14T09:25:25.797 回答
2
只需将起点放入 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);
}
}
}
于 2017-02-22T12:52:09.380 回答
0
您可以伪造您的菜单条,例如使用带有标签的面板。然后你可以手动处理:当用户点击标签时,会打开一个弹出菜单,当用户拖动标签时,窗口会移动。但我建议不要使用这种变通方法,因为它不是标准的 GUI 行为,而且您可能会让您的用户感到困惑。
于 2011-01-02T05:31:32.427 回答
0
我没试过,但如果你能处理菜单栏上的“OnMouseDown”和“onMouseUp”事件:
- On mouse down - 根据鼠标移动移动窗口
- 在鼠标向上或鼠标移出时停止跟踪鼠标移动
于 2011-01-02T22:13:32.130 回答
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);
}
}
于 2013-07-26T07:18:11.103 回答
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;
}
于 2013-12-11T18:23:20.217 回答
0
我不得不使用System.Runtime.InteropServices.DllImportAttribute
-只是想我会发表评论并让大家知道。
于 2017-01-07T02:20:46.153 回答