如何在 winforms C# 中 maximized/un-maximized 表单时触发 Control.MouseMove 事件
How to trigger Control.MouseMove event when maximized/un-maximized form in winforms C#
在我的表单中,我有几个控件,每个控件都有一个 MouseMove 事件处理程序。当表单为 maximized/un-maximized 时,如何触发这些事件 (Control.MouseMove)?下面的代码演示了我如何将事件处理程序分配给每个控件。感谢您的帮助和建议。
control.MouseMove += delegate(object sender, MouseEventArgs e)
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
}
};
事件只能由每个定义的实施 class 触发。
但是您可以将 MouseMove、Maximize 和 Minimize 事件定位到在与原始 MouseMove 委托相同的范围内创建的同一个非匿名委托,以保留局部变量的使用。
您需要自己在表单中创建最大化和最小化事件,因为 winforms 中没有提供它们(参见 Event when a window gets maximized/un-maximized)
public event Action<object> Maximized;
public event Action<object> Minimized;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x0112) { // WM_SYSCOMMAND
// Check your window state here
if (m.WParam == new IntPtr(0xF030) && Maximized != null) Maximized(this);// Maximize event - SC_MAXIMIZE from Winuser.h
if (m.WParam == new IntPtr(0XF020) && Minimized != null) Minimized(this);// Minimize event - SC_MINIMIZE from Winuser.h
}
base.WndProc(ref m);
}
您之前的代码可以改写成这样:
var MMove = new Action<Point>(mousePosition =>
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, mousePosition.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, mousePosition.Y + container.Top - DragStart.Y);
}
});
this.MouseMove += (sender,e) => MMove(e.Location);
this.Maximized += (sender) => MMove(MousePosition);
this.Minimized += (sender) => MMove(MousePosition);
在我的表单中,我有几个控件,每个控件都有一个 MouseMove 事件处理程序。当表单为 maximized/un-maximized 时,如何触发这些事件 (Control.MouseMove)?下面的代码演示了我如何将事件处理程序分配给每个控件。感谢您的帮助和建议。
control.MouseMove += delegate(object sender, MouseEventArgs e)
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
}
};
事件只能由每个定义的实施 class 触发。 但是您可以将 MouseMove、Maximize 和 Minimize 事件定位到在与原始 MouseMove 委托相同的范围内创建的同一个非匿名委托,以保留局部变量的使用。
您需要自己在表单中创建最大化和最小化事件,因为 winforms 中没有提供它们(参见 Event when a window gets maximized/un-maximized)
public event Action<object> Maximized;
public event Action<object> Minimized;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x0112) { // WM_SYSCOMMAND
// Check your window state here
if (m.WParam == new IntPtr(0xF030) && Maximized != null) Maximized(this);// Maximize event - SC_MAXIMIZE from Winuser.h
if (m.WParam == new IntPtr(0XF020) && Minimized != null) Minimized(this);// Minimize event - SC_MINIMIZE from Winuser.h
}
base.WndProc(ref m);
}
您之前的代码可以改写成这样:
var MMove = new Action<Point>(mousePosition =>
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, mousePosition.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, mousePosition.Y + container.Top - DragStart.Y);
}
});
this.MouseMove += (sender,e) => MMove(e.Location);
this.Maximized += (sender) => MMove(MousePosition);
this.Minimized += (sender) => MMove(MousePosition);