gpt4 book ai didi

c# - 如何限制表单的水平移动?

转载 作者:行者123 更新时间:2023-12-02 15:20:31 25 4
gpt4 key购买 nike

我有一个带有标准标题栏的标准表单,用户可以抓取并移动表单。在某些情况下,我想将此移动限制为仅水平移动,因此无论鼠标实际如何移动,表单都保持在相同的 Y 坐标上。

为此,我捕获移动事件,当我检测到与 Y 的偏差时,我将表单移回原始 Y。就像这样:

private void TemplateSlide_Move(object sender, EventArgs e)
{
int y = SlideSettings.LastLocation.Y;
if (y != this.Location.Y)
{
SlideSettings.LastLocation = new Point(this.Location.X, y);
this.Location=Settings.LastLocation;
}
}

但这会导致很多闪烁。另外,由于表单实际上会暂时偏离所需的 Y,这会导致我的程序特有的其他问题。

有没有办法防止表单偏离所需的 Y 坐标?

最佳答案

捕获WM_MOVING并相应修改LPARAM中的RECT结构。

类似于:

public partial class Form1 : Form
{

public const int WM_MOVING = 0x216;

public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

private int OriginalY = 0;
private int OriginalHeight = 0;
private bool HorizontalMovementOnly = true;

public Form1()
{
InitializeComponent();
this.Shown += new EventHandler(Form1_Shown);
this.SizeChanged += new EventHandler(Form1_SizeChanged);
this.Move += new EventHandler(Form1_Move);
}

void Form1_Move(object sender, EventArgs e)
{
this.SaveValues();
}

void Form1_SizeChanged(object sender, EventArgs e)
{
this.SaveValues();
}

void Form1_Shown(object sender, EventArgs e)
{
this.SaveValues();
}

private void SaveValues()
{
this.OriginalY = this.Location.Y;
this.OriginalHeight = this.Size.Height;
}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_MOVING:
if (this.HorizontalMovementOnly)
{
RECT rect = (RECT)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(RECT));
rect.Top = this.OriginalY;
rect.Bottom = rect.Top + this.OriginalHeight;
System.Runtime.InteropServices.Marshal.StructureToPtr(rect, m.LParam, false);
}
break;
}
base.WndProc(ref m);
}
}

关于c# - 如何限制表单的水平移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com