调整控件大小时的奇怪效果

本文关键字:控件 小时 调整 | 更新日期: 2023-09-27 17:50:22

我创建了两个自定义控件。一个矩形和一个椭圆。我可以通过鼠标拖动来移动它们,但我也想调整它们的大小。矩形调整大小工作良好,但调整椭圆的大小会产生奇怪的效果。当我在调整大小后单击椭圆并再次拖动它时,椭圆看起来又正常了。这里的链接与gif显示我的意思是"奇怪"的影响http://gyazo.com/319adb7347ed20fe28b6b93ced8744eb。如何修复这种影响?而且这个椭圆的边角有一些空白因为它是长方形的,也许有办法解决这个问题?

Control.cs

class Ellipse : Control
{
    Point mDown { get; set; }
    public Ellipse()
    {
        MouseDown += shape_MouseDown;
        MouseMove += shape_MouseMove;
    }
    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);
        //Set transparent background
        SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);
        this.BackColor = Color.Transparent;
    }  
    public void shape_MouseDown(object sender, MouseEventArgs e)
    {
        mDown = e.Location;
    }
    public void shape_MouseMove(object sender, MouseEventArgs 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)
        {
            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);
    }
  } 
}

Form1.cs

通过这种方式,我创建了Ellipse,这段代码来自panel_MouseUp (object sender, MouseEventArgs e)方法。

case Item.Ellipse:
var el = new Ellipse();
panel.Controls.Add(el);
el.Location = new Point(x, y);
el.Width = (xe - x);
el.Height = (ye - y);
break;

调整控件大小时的奇怪效果

您需要告诉控件在调整大小时完全重新绘制自身。将它的ResizeRedraw属性设置为true。您还需要注意在Paint事件处理程序中所做的事情,它不应该有全局状态的副作用。如前所述,当我尝试时,你的控件立即使设计器崩溃。

从OnPaint中删除最后两行,并使你的构造函数看起来像这样:

public Ellipse() {
    MouseDown += shape_MouseDown;
    MouseMove += shape_MouseMove;
    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    this.BackColor = Color.Transparent;
    this.DoubleBuffered = true;
    this.ResizeRedraw = true;
}

通过重写OnMouseDown/Move()而不是使用事件来进一步改进这一点。然后查看ControlPaint.DrawGrabHandle(),使调整大小更直观一些。