首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义控件检查是否已启动调整大小

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

Stack Overflow用户
提问于 2015-06-01 13:57:38
回答 1查看 67关注 0票数 1

我创建了一个名为Control的自定义Ellipse。我可以调整大小,移动,并画这个Ellipse。现在,我正在尝试为调整大小添加撤销/重做功能。用户可以在右下角调整控件的大小。此时,只要光标位于Control的右下角,控件就会打印hello。但是我想要的是,当用户开始调整大小时(所以左键向下,光标在右下角),hello就会被打印出来(只有一次)。如何做到这一点,还是有另一种(更好的)方式来做它?

Ellipse.cs

代码语言:javascript
运行
复制
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);
    } 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-01 16:04:48

我会尝试再添加几条消息,以检查鼠标是否在非客户端区域下降,然后再添加另一条消息,以检查大小调整完成后是否完成事务:

代码语言:javascript
运行
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30575472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档