【问题标题】:Form graphics not set when form loads表单加载时未设置表单图形
【发布时间】:2011-02-10 15:52:19
【问题描述】:

我的表单有一个包含两个重叠矩形的组框。窗体的其他控件是两组四个数字上下控件,用于设置矩形的颜色。 (nudF1,2,3 和 4 设置前面的矩形,nudB1,2,3 和 4 设置后面的矩形。)一切正常,除了矩形不显示在数字上下设置时设置的颜色表单首先加载。数字上下控件的 ChangeValue 事件都调用 ShowColors() 方法。表单的 Load 事件调用 csColorsForm_Load() 方法。有什么建议吗?

namespace csColors
{
    public partial class csColorsForm : Form
    {
        public csColorsForm()
        {
            InitializeComponent();
        }

        private void csColorsForm_Load(object sender, EventArgs e)
        {
            this.BackColor = System.Drawing.Color.DarkBlue;
            SetColors(sender, e);
        }

        private void SetColors(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            String ctrlName = control.Name;
            Graphics objGraphics;
            Rectangle rect1, rect2;
            int colorBack, colorFore;
            objGraphics = this.grpColor.CreateGraphics();
            // If calling control is not a forecolor control, paint backcolor rectangle
            if (ctrlName.Substring(0,4)!="nudF")
            {
                colorBack = int.Parse(SetColorsB("nudB"), NumberStyles.HexNumber);
                SolidBrush BrushB = new SolidBrush(Color.FromArgb(colorBack));
                rect1 = new Rectangle(this.grpColor.Left, this.grpColor.Top,
                    this.grpColor.Width, this.grpColor.Height);
                objGraphics.FillRectangle(BrushB, rect1);
            }
            // Always paint forecolor rectangle
            colorFore = int.Parse(SetColorsB("nudF"), NumberStyles.HexNumber);
            SolidBrush BrushF = new SolidBrush(Color.FromArgb(colorFore));
            rect2 = new Rectangle(this.grpColor.Left, this.grpColor.Top,
                this.grpColor.Width, this.grpColor.Height);
            objGraphics.FillRectangle(BrushF, rect2);
            objGraphics.Dispose();
        }

        private string SetColorsB(string nam)
        {
            string txt="";
            for (int n = 1; n <= 4; ++n)
            {
                var ud = Controls[nam + n] as NumericUpDown;
                int hex = (int)ud.Value;
                txt += hex.ToString("X2");
            }
            return txt;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

【问题讨论】:

  • 你想做什么?隐藏控件?
  • 不,我没有隐藏任何控件。我正在使用表格来显示混合两种颜色的结果。

标签: c# winforms graphics forms


【解决方案1】:

当 Windows 向其发送 WM_PAINT 消息时,组框与任何控件一样,将自行绘制。您确实可以使用 Control.CreateGraphics() 来绘制自己,绕过正常的绘制逻辑。但这不会持续,当 Windows 决定控件需要重新绘制自身时,它会随机消失。当您最小化表单并恢复它时,这一点很明显。在启用 Aero 的 Vista 和 Win7 上不太明显,当您的表单与另一个窗口重叠时,不需要重新绘制。但在 XP 或禁用 Aero 时会很明显。

您无法使这项工作可靠地进行,您必须使用 Paint 事件。不是您的表单,而是组框控件。调用它的 Invalidate() 方法来强制它在颜色改变时重新绘制。

【讨论】:

  • 汉斯,你这个男人!将 This.Invalidate() 放在 groupbox 的 paint 方法中对我不起作用。但是,在 groupbox 的paint 方法中调用 SetColors() 就可以了。非常感谢。
【解决方案2】:

我怀疑在Load 事件中绘画有点为时过早。尝试在Paint 活动中绘画。

或者:

您能否只使用几个面板控件并设置它们的背景颜色?

【讨论】:

  • 感谢您的回复。使用 Paint 事件而不是 Load 事件没有帮助。
猜你喜欢
  • 2013-03-04
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 2021-01-04
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多