日期:2014-05-20  浏览次数:21047 次

关于在C#winform中pictureBox上显示文字的简单问题,求教!
一个winform的小程序,拖了一个pictureBox在form上,然后希望在pictureBox上调用drawString方法写几个字,代码如下:
 Graphics g = pictureBox.CreateGraphics();
 g.DrawString("第一幕", boldTimesFont, greenBrush, 10, 40);
问题是这两行代码放在OnPaint()方法中是可以正确显示文字,可是我放在Form1()中InitializeComponent()之后,或者Form1_Load()中都不能显示文字。
求教!不甚感激!
ps:有没有推荐的图像编程的资料,刚入门,探索中。。。。。

------解决方案--------------------
g.DrawString("第一幕", boldTimesFont, greenBrush, 10, 40);
放在 OnPaint 里面 因为你在显示器上看到的东西 都是代码不停地的绘制

如果只是初始化窗体的时候绘制一遍 则 第二次绘制的时候会把之前的东西覆盖掉

所以要想改变外观必须在 onpaint里面做

要特别理解一下OnPaint 事件 或者 可视化控件里的OnPaint 方法 该方法/事件只在外观发生破坏(比如控件大小改变,控件被其他控件遮住 等)的时候执行 
也可以使用 Invalidate 方法 破坏掉外观 强制调用OnPaint 方法进行重绘 给你一个例子
这是我重绘的 Tab 目前只支持标签在上方 你参考一下 我的建议是你重写该控件 
C# code

  /// <summary>
    /// 重写标签控件
    /// </summary>
    public class TabControlFx : TabControl
    {
        #region  属性

        private int Round = 8;    // 圆角弧度

        #region 控件边框颜色

        private Color colorBorder = Color.FromArgb(78, 160, 209);

        /// <summary>
        /// 控件边框颜色
        /// </summary>
        [Category("颜色设置")]
        [Description("控件边框颜色")]
        public Color ColorBorder
        {
            get
            {
                return this.colorBorder;
            }
            set
            {
                this.colorBorder = value;
                this.penBorder.Dispose();
                this.penBorder = new Pen(colorBorder);
            }
        }

        private Pen penBorder;

        #endregion

        #region 控件背景色

        private Color colorControlBack = Color.White;

        /// <summary>
        /// 控件背景色
        /// </summary>
        [Category("颜色设置")]
        [Description("控件背景色")]
        public Color ControlBack
        {
            get
            {
                return this.colorControlBack;
            }
            set
            {
                this.colorControlBack = value;
                this.brushControlBack.Dispose();
                this.brushControlBack = new SolidBrush(colorControlBack);
            }
        }

        private Brush brushControlBack;

        #endregion

        #region 标签未被选中时背景色

        private Color colorTabNormal = Color.LightGray;

        /// <summary>
        /// 标签未被选中时背景色
        /// </summary>
        [Category("颜色设置")]
        [Description("标签未被选中时背景色")]
        public Color ColorTabNormal
        {
            get
            {
                return this.colorTabNormal;
            }
            set
            {
                this.colorTabNormal = value;
                this.brushTabNormal.Dispose();
                this.brushTabNormal = new SolidBrush(colorTabNormal);
            }
        }

        private Brush brushTabNormal;

        #endregion

        #region 标签被选中时背景色

        private Color colorTabActive = Color.White;

        /// <summary>
        /// 标签被选中时背景色
        /// </summary>

        [Category("颜色设置")]
        [Description("标签被选中时背景色")]
        public Color ColorTabActive
        {
            get
            {
                return this.colorTabActive;
            }
            set
            {
                this.colorTabActive = value;
                this.brushTabActive.Dispose();
                this.brushTabActive = new SolidBrush(colorTabActive);
            }
        }
        private Brush brushTabActive;

        #endregion

        #region 标签集合

        /// <summary>
        /// 标签集合
        /// </summary>
        [Editor(typeof(TabpageExCollectionEditor), typeof(UITypeEditor))]
        public new TabPageCollection TabPages
        {
            get
            {
                return base.TabPages;
            }
        }

        #endregion

        #region 标签停靠位置

        /// <summary>
        /// 标签停靠位置 现在只有 Top 和Bottom两种
        /// </summary>
        public new TabAlignment Alignment
        {
            get
            {
                return base.Alignment;
            }
            set
            {
                if (value != TabAlignment.Top)

                    throw new Exception("只支持标签在顶部显示");

                base.Alignment = value;
            }
        }

        #endregion

        #region 设置外观模式

        /// <summary>
        /// 设置外观模式只能是正常模式
        /// </summary>
        public new TabAppearance Appearance
        {
            get { return base.Appearance; }
            set { base.Appearance = TabAppearance.Normal; }
        }

        #endregion

        #endregion

        public TabControlFx()
        {
            this.brushControlBack = new SolidBrush(colorControlBack);
            this.penBorder = new Pen(colorBorder);
            this.brushTabNormal = new SolidBrush(colorTabNormal);
            this.brushTabActive = new SolidBrush(colorTabActive);

            // 由用户绘制控件
            this.SetStyle(ControlStyles.UserPaint, true);

            // 防止闪烁
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            // 启动缓冲
            this.SetStyle(ControlStyles.DoubleBuffer, true);

            // 调整空间大小时重绘
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        /// <summary>
        /// 重绘标签控件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (!this.Visible)
            {
                return;
            }

            // 控件区域
            Rectangle ControlArea = this.ClientRectangle;

            // 标签页区域
            Rectangle TabsArea = this.DisplayRectangle;

            // 绘制控件背景
            e.Graphics.FillRectangle(brushControlBack, ControlArea);

            // 绘制控件边框
            int nDelta = SystemInformation.Border3DSize.Width;

            // 扩大标签范围 排除3D边框造成的缝隙
            TabsArea.Inflate(nDelta, nDelta);

            // 绘制控件边框
            e.Graphics.DrawRectangle(penBorder, TabsArea);

            // 绘制标签
            Rectangle rectangle = new Rectangle(TabsArea.Left, ControlArea.Top, TabsArea.Width, ControlArea.Height);

            e.Graphics.SetClip(rectangle);

            for (int i = 0; i < this.TabCount; i++)
            {
                DrawTab(e.Graphics, this.TabPages[i], i);
            }
        }

        /// <summary>
        /// 绘制标签
        /// </summary>
        /// <param name="g"></param>
        /// <param name="tabPage"></param>
        /// <param name="nIndex"></param>
        internal void DrawTab(Graphics g, TabPage tabPage, int nIndex)
        {
            Rectangle recBounds = this.GetTabRect(nIndex);
            RectangleF tabTextArea = (RectangleF)this.GetTabRect(nIndex);


            using (GraphicsPath FormTopAreaPath = new GraphicsPath())
            {
                if (this.Alignment == TabAlignment.Top)
                {
                    // 绘制窗体上部两个圆角
                    FormTopAreaPath.AddArc(recBounds.Left, recBounds.Top, Round, Round, 180, 90);
                    FormTopAreaPath.AddArc(recBounds.Right - Round, recBounds.Top, Round, Round, 270, 90);
                    FormTopAreaPath.AddLine(recBounds.Right, recBounds.Top + Round, recBounds.Right, recBounds.Bottom - Round);
                    FormTopAreaPath.AddLine(recBounds.Right, recBounds.Bottom, recBounds.Left, recBounds.Bottom);
                    FormTopAreaPath.AddLine(recBounds.Left, recBounds.Top + Round, recBounds.Left, recBounds.Bottom - Round);

                    FormTopAreaPath.CloseAllFigures();

                    // 绘制标签背景色
                    if (this.SelectedIndex == nIndex)
                    {
                        using (Brush brush = new SolidBrush(Color.White))
                        {
                            g.FillPath(brush, FormTopAreaPath);
                        }
                    }
                    else
                    {
                        // 填充背景图
                        using (TextureBrush textureBrush = new TextureBrush(Resources.TabWhite))
                        {
                            textureBrush.TranslateTransform(4, 0);

                            g.FillPath(textureBrush, FormTopAreaPath);
                        }
                    }

                    // 绘制标签边框                    
                    g.DrawPath(penBorder, FormTopAreaPath);

                }

                // 是选中标签和标签页联通
                if (this.SelectedIndex == nIndex)
                {
                    switch (this.Alignment)
                    {
                        case TabAlignment.Top:
                            g.FillRectangle(brushTabActive, recBounds.Left + 1, recBounds.Bottom, recBounds.Width - 1, 2);

                            break;

                        case TabAlignment.Bottom:
                            g.FillRectangle(brushTabActive, recBounds.Left + 1, recBounds.Top - 2, recBounds.Width - 1, 3);

                            break;
                    }
                }

                // 重绘字体
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                g.DrawString(tabPage.Text, this.Font, new SolidBrush(tabPage.ForeColor), tabTextArea, stringFormat);
            }
        }

        #region 提供用户编辑界面

        internal class TabpageExCollectionEditor : CollectionEditor
        {
            public TabpageExCollectionEditor(System.Type type)
                : base(type)
            {
            }

            protected override Type CreateCollectionItemType()
            {
                return typeof(TabPage);
            }
        }

        #endregion