gpt4 book ai didi

c# - 调整 winform 大小后的绘画问题

转载 作者:太空宇宙 更新时间:2023-11-03 17:02:06 24 4
gpt4 key购买 nike

我在调整 winform 大小时遇到​​问题。我想为我的所有表单提供特定的背景颜色,所以我重写了 OnPaintBackground 方法,如下所示

protected override void OnPaintBackground(PaintEventArgs e)
{
DoubleBuffered = true;
ResizeRedraw = true;

using (var brush = new LinearGradientBrush(ClientRectangle, Color.LightBlue, Color.CornflowerBlue, 90F))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
}
}

在表单上,​​我还有一个包含文本框、标签等的 GroupBox,其背景颜色设置为透明。当第一次执行 winform 时工作正常,但是当我最小化 winform 然后最大化时,一个红十字会出现在组框中。

我审阅过很多案例,但大多数都在谈论闪烁,但事实并非如此。我也读过这幅画是如何工作的,显然当首先显示 winform 时告诉子控件自己画画,但是由于父亲还没有被画,所以控件不知道要画什么作为背景的透明方面的控制。

这是调整大小后的样子示例。

Resize showing RED CROSS

非常感谢您的帮助!

PS:我使用的是 Visual Studio 2012,C#。

最佳答案

我在这里找到了一篇文章:Calling OnPaintBackground

问题的答案是如何覆盖 OnPaintBackground,为了确保它正常工作,他说你应该将表单样式设置为以下

this.SetStyle(ControlStyles.AllPaintingInWmPaint 
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.DoubleBuffer
| ControlStyles.UserPaint
, true);

.UserPaint 只是告诉它您正在自己处理这幅画,以确保它没有被其他任何东西处理,以便它被正确覆盖。

希望对你有帮助!

关于c# - 调整 winform 大小后的绘画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24088860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com