0

我有一些包含在图像中的自定义控件,如果它们被拖出屏幕并重新打开,则图像无法正确重新绘制。我已经为这些不同的控件覆盖了油漆,它们似乎工作正常,除了如果反复拖出屏幕,它们就不能正确绘制。任何人都知道为什么会发生这种情况和/或解决方案?

编辑:即使对话框只是移动调整大小太快,其中一些似乎也存在问题,而不仅仅是将其移出屏幕。他们开始看起来像是被吸引到了自己身上。哪种是有道理的,但我不知道如何治愈它。

编辑 2:这些是具有四种状态的自定义按钮(悬停、单击、正常、禁用),所以我不认为容器的问题是我不认为的问题..?OnPaint 代码是:



private void CQPButton_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(BackColor);

    if (BackgroundImage != null)
        e.Graphics.DrawImage(BackgroundImage, e.ClipRectangle);

    if (Image != null)
    {
        RectangleF rect = new RectangleF();
        rect.X = (float)((e.ClipRectangle.Width - Image.Size.Width) / 2.0);
        rect.Y = (float)((e.ClipRectangle.Height - Image.Size.Height) / 2.0);
        rect.Width = Image.Width;
        rect.Height = Image.Height;
        e.Graphics.DrawImage(Image, rect);
    }

    if (Text != null)
    {
        SizeF size = e.Graphics.MeasureString(this.Text, this.Font);

        // Center the text inside the client area of the PictureButton.
        e.Graphics.DrawString(this.Text,
            this.Font,
            new SolidBrush(this.ForeColor),
            (this.ClientSize.Width - size.Width) / 2,
            (this.ClientSize.Height - size.Height) / 2);
    }

}

我尝试强制重绘各种事件,LocationChanged 和 Move 尝试处理调整大小问题,ClientSizeChanged 尝试在它们离开屏幕时处理,并且没有任何问题。我不知道我错过了什么...

4

1 回答 1

3

看到代码片段后,我完全改变了我的答案。它有一个错误:

    RectangleF rect = new RectangleF();
    rect.X = (float)((e.ClipRectangle.Width - Image.Size.Width) / 2.0);
    rect.Y = (float)((e.ClipRectangle.Height - Image.Size.Height) / 2.0);

在这里使用 e.ClipRectangle 是不正确的,它是一个始终在变化的值,具体取决于控件的哪个部分需要重新绘制。是的,当您调整控件大小或将其部分拖出屏幕时,它的变化最大。您需要使用控件的实际大小:

    rect.X = (float)((this.ClientSize.Width - Image.Size.Width) / 2.0);
    rect.Y = (float)((this.ClientSize.Height - Image.Size.Height) / 2.0);
于 2012-05-05T09:18:18.543 回答