自定义控件检查是否已启动调整大小

本文关键字:调整 启动 检查 是否 自定义控件 | 更新日期: 2023-09-27 17:51:19

我已经创建了一个名为Ellipse的自定义Control。我能够调整大小,移动,并绘制这个Ellipse。现在我正试图为调整大小添加撤销/重做功能。用户可以在右下角调整控件的大小。此时,只要光标位于Control的右下角,控件就会打印hello。但是我想要的是,当用户开始调整大小(因此鼠标左键按下,光标在右下角)时,打印hello(仅一次)。如何做到这一点,或者是否有另一种(更好的)方法来做到这一点?

Ellipse.cs

class Ellipse : Control
{
    private Point mDown { get; set; }
    public Ellipse()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        // Draw a black ellipse in the rectangle represented by the control.
        e.Graphics.FillEllipse(Brushes.Black, 0, 0, Width, Height);
    }  
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        mDown = e.Location;
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        // Call MyBase.OnMouseMove to activate the delegate. 
        base.OnMouseMove(e);

        if (e.Button == MouseButtons.Left)
        {
            Location = new Point(e.X + Left - mDown.X, e.Y + Top - mDown.Y);
        }
    }
    /* Allow resizing at the bottom right corner */
    protected override void WndProc(ref Message m)
    {
        const int wmNcHitTest = 0x84;
        const int htBottomLeft = 16;
        const int htBottomRight = 17;
        if (m.Msg == wmNcHitTest)
        {
            Console.WriteLine("Hello");
            int x = (int)(m.LParam.ToInt64() & 0xFFFF);
            int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16);
            Point pt = PointToClient(new Point(x, y));
            Size clientSize = ClientSize;
            if (pt.X >= clientSize.Width - 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(IsMirrored ? htBottomLeft : htBottomRight);
                return;
            }
        }
        base.WndProc(ref m);
    } 

自定义控件检查是否已启动调整大小

我会尝试添加更多消息来检查鼠标是否在非客户端区域下降,然后另一个消息用于确定大小以完成事务:

private bool userResizing = false;
protected override void WndProc(ref Message m) {
  const int wmNcHitTest = 0x84;
  const int htBottomLeft = 16;
  const int htBottomRight = 17;
  const int WM_EXITSIZEMOVE = 0x232;
  const int WM_NCLBUTTONDWN = 0xA1;
  if (m.Msg == WM_NCLBUTTONDWN) {
    if (!userResizing) {
      userResizing = true;
      Console.WriteLine("Start Resizing");
    }
  } else if (m.Msg == WM_EXITSIZEMOVE) {
    if (userResizing) {
      userResizing = false;
      Console.WriteLine("Finish Resizing");
    }
  } else if (m.Msg == wmNcHitTest) {
    int x = (int)(m.LParam.ToInt64() & 0xFFFF);
    int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16);
    Point pt = PointToClient(new Point(x, y));
    Size clientSize = ClientSize;
    if (pt.X >= clientSize.Width - 16 && 
        pt.Y >= clientSize.Height - 16 &&
        clientSize.Height >= 16) {
      m.Result = (IntPtr)(IsMirrored ? htBottomLeft : htBottomRight);
      return;
    }
  }
  base.WndProc(ref m);
}