加载表单时未设置表单图形 [英] Form graphics not set when form loads

查看:62
本文介绍了加载表单时未设置表单图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My form has a group box which contains two overlapping rectangles. The form's other controls are two sets of four numeric up down controls to set the rectangles' colors. (nudF1,2,3 and 4 set the rectangle that's in front, and nudB1,2,3 and 4 set the rectangle that's behind.) Everything works fine, except that the rectangles do not display the colors set in the numeric up downs when the form first loads. The numeric up down controls' ChangeValue events all call the ShowColors() method. The form's Load event calls the csColorsForm_Load() method. Any suggestions?

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();
        }

    }
}

推荐答案

当Windows向其发送WM_PAINT消息时,就像任何控件一样,一个组框也会自动绘制.您确实可以使用Control.CreateGraphics()来绘制自己,而无需遵循常规绘制逻辑.但这不会持续下去,当Windows决定控件需要再次重新绘制自身时,它会随机消失.当您最小化窗体并还原它时,这是显而易见的.在启用Aero的Vista和Win7上不太明显,当您的窗体与另一个窗口重叠时,不需要重画.但是在XP或禁用Aero的情况下,这将是显而易见的.

A group box, like any control, will paint itself when Windows sends it the WM_PAINT message. You can indeed use Control.CreateGraphics() to draw yourself, bypassing the normal paint logic. But that's not going to last and will randomly disappear when Windows decides that the control needs to repaint itself again. Which is obvious when you minimize the form and restore it. Less obvious on Vista and Win7 with Aero enabled, repaints are not necessary when your form is overlapped by another window. But it will be quite obvious on XP or with Aero disabled.

您无法可靠地完成此工作,您必须使用Paint事件.不是您的表单,而是由组框控制的.调用其Invalidate()方法,以在颜色更改时强制其重新绘制.

You cannot make this work reliably, you have to use the Paint event. Not your form's, the group box controls'. Call its Invalidate() method to force it to repaint when the color changes.

这篇关于加载表单时未设置表单图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆