c#

C#中子窗口的拖拽与缩放功能如何实现

小樊
105
2024-09-04 16:10:05
栏目: 编程语言

在C#中,要实现子窗口的拖拽和缩放功能,可以使用Windows Forms或WPF。这里分别给出两种方法的实现方法:

  1. Windows Forms:

拖拽功能:

首先,需要在子窗口上添加一个Panel控件作为标题栏。然后,为该Panel添加MouseDown、MouseMove和MouseUp事件。

public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
    }

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

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

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

缩放功能:

在子窗口的边缘添加一个透明的Panel控件作为缩放区域。然后,为该Panel添加MouseDown、MouseMove和MouseUp事件。

private bool resizing = false;
private Point resizeCursorPoint;
private Point resizeFormSize;

private void panelResize_MouseDown(object sender, MouseEventArgs e)
{
    resizing = true;
    resizeCursorPoint = Cursor.Position;
    resizeFormSize = this.Size;
}

private void panelResize_MouseMove(object sender, MouseEventArgs e)
{
    if (resizing)
    {
        Point dif = Point.Subtract(Cursor.Position, new Size(resizeCursorPoint));
        this.Size = new Size(resizeFormSize.Width + dif.X, resizeFormSize.Height + dif.Y);
    }
}

private void panelResize_MouseUp(object sender, MouseEventArgs e)
{
    resizing = false;
}
  1. WPF:

拖拽功能:

在子窗口的标题栏上添加鼠标事件。

    <!-- 标题栏内容 -->
</Border>
private bool dragging = false;
private Point dragStartPosition;

private void titleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    dragging = true;
    dragStartPosition = e.GetPosition(this);
    this.CaptureMouse();
}

private void titleBar_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging)
    {
        Point currentPosition = e.GetPosition(this);
        double offsetX = currentPosition.X - dragStartPosition.X;
        double offsetY = currentPosition.Y - dragStartPosition.Y;
        this.Left += offsetX;
        this.Top += offsetY;
    }
}

protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
    base.OnMouseLeftButtonUp(e);
    dragging = false;
    this.ReleaseMouseCapture();
}

缩放功能:

在子窗口的边缘添加一个透明的Border控件作为缩放区域。然后,为该Border添加鼠标事件。

private bool resizing = false;
private Point resizeStartPosition;

private void resizeGrip_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    resizing = true;
    resizeStartPosition = e.GetPosition(this);
    this.CaptureMouse();
}

private void resizeGrip_MouseMove(object sender, MouseEventArgs e)
{
    if (resizing)
    {
        Point currentPosition = e.GetPosition(this);
        double offsetX = currentPosition.X - resizeStartPosition.X;
        double offsetY = currentPosition.Y - resizeStartPosition.Y;
        this.Width += offsetX;
        this.Height += offsetY;
    }
}

protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
    base.OnMouseLeftButtonUp(e);
    resizing = false;
    this.ReleaseMouseCapture();
}

这样就实现了子窗口的拖拽和缩放功能。

0
看了该问题的人还看了