C# 仅垂直移动窗体
如何创建只通过标题栏垂直移动的WinForms表单?这样就可以了(但并不漂亮):C# 仅垂直移动窗体,c#,.net,winforms,C#,.net,Winforms,如何创建只通过标题栏垂直移动的WinForms表单?这样就可以了(但并不漂亮): 通过将窗体的位置重置为移动的初始X值和Y值,可以缩短移动操作。这个解决方案很简单,但会闪烁一点 protected Point StartPosition { get; set; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); StartPosition = this.Location; } protected
通过将窗体的位置重置为移动的初始X值和Y值,可以缩短移动操作。这个解决方案很简单,但会闪烁一点
protected Point StartPosition { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
StartPosition = this.Location;
}
protected override void OnMove(EventArgs e)
{
if (StartPosition == new Point())
return;
var currentLocation = Location;
Location = new Point(StartPosition.X, currentLocation.Y);
base.OnMove(e);
}
您必须截获Windows发送的WM_移动通知消息。代码如下:
using System.Runtime.InteropServices;
...
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private struct RECT {
public int left, top, right, bottom;
}
protected override void WndProc(ref Message m) {
if (m.Msg == 0x216) { // Trap WM_MOVING
var rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
int w = rc.right - rc.left;
rc.left = this.Left;
rc.right = rc.left + w;
Marshal.StructureToPtr(rc, m.LParam, false);
}
base.WndProc(ref m);
}
}
自动移动??你所说的“移动方式”是什么意思?我的意思是当用户移动表单时,它不应该水平移动-只有垂直移动-这是最好的解决方案,而且抖动是不可避免的。这就是说,这种行为让最终用户感到困惑,他们有理由认为自己可以拖动窗口。你想解决的问题是什么?
using System.Runtime.InteropServices;
...
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private struct RECT {
public int left, top, right, bottom;
}
protected override void WndProc(ref Message m) {
if (m.Msg == 0x216) { // Trap WM_MOVING
var rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
int w = rc.right - rc.left;
rc.left = this.Left;
rc.right = rc.left + w;
Marshal.StructureToPtr(rc, m.LParam, false);
}
base.WndProc(ref m);
}
}