Properties 自定义usercontrol中重写的字体属性未保存在设计器文件中

Properties 自定义usercontrol中重写的字体属性未保存在设计器文件中,properties,fonts,show,Properties,Fonts,Show,我有一个自定义的用户进度条控件,并在显示文本的过程中覆盖了它的字体属性 当我将usercontrol的副本放到表单上时,我可以很好地设置Font属性,但在表单的设计器文件中看不到为“Font”设置的值。当我编译/运行我的应用程序时,我输入的值丢失 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using

我有一个自定义的用户进度条控件,并在显示文本的过程中覆盖了它的字体属性

当我将usercontrol的副本放到表单上时,我可以很好地设置Font属性,但在表单的设计器文件中看不到为“Font”设置的值。当我编译/运行我的应用程序时,我输入的值丢失

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ProgressBarWithText
{
    /// <summary>
    /// Control that extends the System.Windows.Forms.ProgressBar with
    /// the ability to overlay the percentage or a text message.
    /// </summary>
    [Description(
        "Control that extends the System.Windows.Forms.ProgressBar with the ability to overlay the Text."),
    DefaultProperty("TextVisible"),
    DefaultEvent("TextChanged")]
    public class ProgressBarWithText : ProgressBar
    {
        private const int WM_PAINT = 0x0F;

        /// <summary>
        /// Raised when the visibility of the percentage text is changed.
        /// </summary>
        [Description("Raised when the visibility of the percentage text is changed."),
        Category("Property Changed")]
        public event EventHandler TextVisibleChanged;

        /// <summary>
        /// Raised when the text has changed.
        /// </summary>
        [Description("Raised when the text has changed."),
        Category("Property Changed")]
        public event EventHandler TextChanged;

        /// <summary>
        /// Raised when the font has changed.
        /// </summary>
        [Description("Raised when the font has changed."),
        Category("Property Changed")]
        public event EventHandler FontChanged;

        //private ContentAlignment m_p_align;
        private Font m_Font;
        private Color m_overlayColor;
        private StringFormat m_stringFormat;
        private string m_Text;
        private bool m_TextVisible;

        /// <summary>
        /// Create a new instance of a ProgressbarWithPercentage.
        /// </summary>
        public ProgressBarWithText()
        {
            InitializeComponent();

            m_overlayColor = Color.White;
            m_stringFormat = new StringFormat();
            m_TextVisible = true;
            m_stringFormat.Alignment = StringAlignment.Center;
            m_stringFormat.LineAlignment = StringAlignment.Center;
            if (m_Font == null)
                m_Font = SystemFonts.DialogFont;
        }

        #region Properties

        /// <summary>
        /// Get or Sets the Font of the Text being displayed.
        /// </summary>
        [Bindable(true),
        Browsable(true),
        Category("Appearance"),
        Description("Get or Sets the Font of the Text being displayed."),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorBrowsable(EditorBrowsableState.Always)]
        public override Font Font
        {
            get
            {
                return m_Font;
            }
            set
            {
                if (m_Font != value)
                {
                    m_Font = value;
                    Invalidate();
                    OnFontChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Gets or sets the Color that is used to draw the text over a filled section of the progress bar.
        /// </summary>
        [Browsable(true),
        Description("The Color that is used to draw the text over a filled section of the progress bar."),
        Category("Appearance"),
        DefaultValue(typeof(Color), "White")]
        public Color OverlayColor
        {
            get { return m_overlayColor; }
            set
            {
                if (m_overlayColor != value)
                    m_overlayColor = value;
            }
        }

        /// <summary>
        /// Gets or Sets the Text being displayed.
        /// </summary>
        [Browsable(true),
        Category("Appearance"),
        Description("The Text to be displayed or if left null will display a percentage"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorBrowsable(EditorBrowsableState.Always)]
        public override string Text
        {
            get
            {
                if (m_Text != "") return m_Text;
                return Value.ToString() + "%";
            }

            set
            {
                if (m_Text != value)
                {
                    m_Text = value;
                    Invalidate();
                    OnTextChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Gets or sets a value that indicates whether the percentage will be displayed.
        /// </summary>
        [Browsable(true),
        Description("Indicates whether the percentage will be displayed on the progress bar."),
        Category("Appearance"),
        DefaultValue(true)]
        public bool TextVisible
        {
            get { return m_TextVisible; }
            set
            {
                if (m_TextVisible != value)
                {
                    m_TextVisible = value;
                    OnTextVisibleChanged(EventArgs.Empty);
                }
            }
        }

        public new int Value
        {
            get { return base.Value; }
            set
            {
                if (base.Value != value)
                {
                    base.Value = value;

                    /* Needed for XP. Downside is control will be drawn twice
                     * when value coincides with one that the system uses for
                     * repaint. Could maybe use Environment.OSVersion to check? */
                    if (m_TextVisible)
                        Invalidate();
                }
            }
        }

        #endregion Properties

        #region Event Handlers

        protected virtual void OnTextVisibleChanged(EventArgs e)
        {
            EventHandler eh = TextVisibleChanged;
            if (eh != null)
                eh(this, e);
        }

        protected virtual void OnTextChanged(EventArgs e)
        {
            EventHandler eh = TextChanged;
            if (eh != null)
                eh(this, e);
        }

        protected virtual void OnFontChanged(EventArgs e)
        {
            EventHandler eh = FontChanged;
            if (eh != null)
                eh(this, e);
        }

        #endregion Event Handlers

        private void ShowText()
        {
            using (Graphics graphics = CreateGraphics())
            {
                // Draw left side
                Region regionLeft = new Region(new RectangleF(
                    ClientRectangle.X,
                    ClientRectangle.Y,
                    (ClientRectangle.Width * base.Value) / 100,
                    ClientRectangle.Height));
                using (Brush brush = new SolidBrush(m_overlayColor))
                {
                    graphics.Clip = regionLeft;
                    graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                }
                // Draw right side
                Region regionRight = new Region(ClientRectangle);
                regionRight.Exclude(regionLeft);
                using (Brush brush = new SolidBrush(ForeColor))
                {
                    graphics.Clip = regionRight;
                    graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m_TextVisible && m.Msg == WM_PAINT)
                ShowText();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Windows.Forms;
命名空间ProgressBarWithText
{
/// 
///控件,该控件使用扩展System.Windows.Forms.ProgressBar
///覆盖百分比或文本消息的功能。
/// 
[说明](
“扩展System.Windows.Forms.ProgressBar并能够覆盖文本的控件。”),
DefaultProperty(“TextVisible”),
DefaultEvent(“TextChanged”)]
公共类ProgressBarWithText:ProgressBar
{
私有常量int WM_PAINT=0x0F;
/// 
///百分比文本的可见性更改时引发。
/// 
[说明(“百分比文本的可见性更改时引发”),
类别(“财产变更”)]
公共事件事件处理程序TextVisibleChanged;
/// 
///文本更改时引发。
/// 
[说明(“文本更改时引发”),
类别(“财产变更”)]
公共事件事件处理程序文本已更改;
/// 
///字体更改时引发。
/// 
[说明(“字体更改时引发”),
类别(“财产变更”)]
公共事件事件处理程序已更改;
//私有内容对齐m_p_align;
专用字体m_字体;
私用彩色m_套色;
私有StringFormat m_StringFormat;
私有字符串m_文本;
私有布尔m_TextVisible;
/// 
///创建ProgressbarWithPercentage的新实例。
/// 
PublicProgressBarWithText()
{
初始化组件();
m_overlycolor=颜色。白色;
m_stringFormat=新stringFormat();
m_TextVisible=真;
m_stringFormat.Alignment=StringAlignment.Center;
m_stringFormat.LineAlignment=StringAlignment.Center;
如果(m_Font==null)
m_Font=SystemFonts.DialogFont;
}
#区域属性
/// 
///获取或设置正在显示的文本的字体。
/// 
[可装订(真实),
可浏览(真),
类别(“外观”),
描述(“获取或设置正在显示的文本的字体。”),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always)]
公共覆盖字体
{
得到
{
返回m_字体;
}
设置
{
如果(m_Font!=值)
{
m_Font=值;
使无效();
OnFontChanged(EventArgs.Empty);
}
}
}
/// 
///获取或设置用于在进度条的填充部分上绘制文本的颜色。
/// 
[可浏览(正确),
描述(“用于在进度条的填充部分上绘制文本的颜色。”),
类别(“外观”),
默认值(类型(颜色),“白色”)]
公共彩色套色
{
获取{return m_overlycolor;}
设置
{
if(m_叠加颜色!=值)
m_overlycolor=值;
}
}
/// 
///获取或设置要显示的文本。
/// 
[可浏览(正确),
类别(“外观”),
说明(“待显示的文本或如果保留为空,将显示百分比”),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always)]
公共重写字符串文本
{
得到
{
如果(m_Text!=“”)返回m_Text;
返回值.ToString()+“%”;
}
设置
{
如果(m_Text!=值)
{
m_Text=值;
使无效();
OnTextChanged(EventArgs.Empty);
}
}
}
/// 
///获取或设置一个值,该值指示是否显示百分比。
/// 
[可浏览(正确),
说明(“指示进度条上是否显示百分比。”),
类别(“外观”),
默认值(真)]
公共布尔文本可见
{
获取{return m_TextVisible;}
设置
{
如果(m_TextVisible!=值)
{
m_TextVisible=值;
OnTextVisibleChanged(EventArgs.Empty);
}
}
}
公共新整数值
{
获取{return base.Value;}
设置
{
如果(base.Value!=值)
{
基本价值=价值;
/*XP需要。缺点是控件将被绘制两次
*当值与系统用于的值一致时
*重新绘制。是否可以使用Environment.OSVersion进行检查*/
如果(m_TextVisible)
使无效();
}
}
}
#端域属性
#区域事件处理程序
namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.progressBarWithText1 = new ProgressBarWithText.ProgressBarWithText();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // trackBar1
            // 
            this.trackBar1.Location = new System.Drawing.Point(13, 104);
            this.trackBar1.Maximum = 100;
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(596, 45);
            this.trackBar1.TabIndex = 1;
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
            // 
            // progressBarWithText1
            // 
            this.progressBarWithText1.Location = new System.Drawing.Point(13, 13);
            this.progressBarWithText1.Name = "progressBarWithText1";
            this.progressBarWithText1.Size = new System.Drawing.Size(596, 85);
            this.progressBarWithText1.TabIndex = 2;
            this.progressBarWithText1.Text = "foo";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(621, 262);
            this.Controls.Add(this.progressBarWithText1);
            this.Controls.Add(this.trackBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TrackBar trackBar1;
        private ProgressBarWithText.ProgressBarWithText progressBarWithText1;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ProgressBarWithText
{
    /// <summary>
    /// Control that extends the System.Windows.Forms.ProgressBar with
    /// the ability to overlay the percentage or a text message.
    /// </summary>
    [Description(
        "Control that extends the System.Windows.Forms.ProgressBar with the ability to overlay the Text."),
    DefaultProperty("TextVisible"),
    DefaultEvent("TextChanged")]

    public class ProgressBarWithText : ProgressBar
    {
        private const int WM_PAINT = 0x0F;

        /// <summary>
        /// Raised when the visibility of the percentage text is changed.
        /// </summary>
        [Description("Raised when the visibility of the percentage text is changed."),
        Category("Property Changed")]
        public event EventHandler TextVisibleChanged;

        /// <summary>
        /// Raised when the text has changed.
        /// </summary>
        [Browsable(true),
        Description("Raised when the text has changed."),
        Category("Property Changed")]
        public new event EventHandler TextChanged;

        /// <summary>
        /// Raised when the font has changed.
        /// </summary>
        [Browsable(true),
        Description("Raised when the font has changed."),
        Category("Property Changed")]
        public new event EventHandler FontChanged;

        //private ContentAlignment m_p_align;
        private Color m_overlayColor;
        private StringFormat m_stringFormat;
        private string m_Text;
        private bool m_TextVisible;

        /// <summary>
        /// Create a new instance of a ProgressbarWithPercentage.
        /// </summary>
        public ProgressBarWithText()
        {
            m_overlayColor = Color.White;
            m_stringFormat = new StringFormat();
            m_TextVisible = true;
            m_stringFormat.Alignment = StringAlignment.Center;
            m_stringFormat.LineAlignment = StringAlignment.Center;
        }

        #region Properties

        /// <summary>
        /// Get or Sets the Font of the Text being displayed.
        /// </summary>
        [Bindable(true),
        Browsable(true),
        Category("Appearance"),
        Description("Get or Sets the Font of the Text being displayed."),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorBrowsable(EditorBrowsableState.Always)]
        public override Font  Font
        {
            get 
            { 
                return base.Font;
            }
            set 
            { 
                base.Font = value;
                Invalidate();
                OnFontChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// Gets or sets the Color that is used to draw the text over a filled section of the progress bar.
        /// </summary>
        [Browsable(true),
        Description("The Color that is used to draw the text over a filled section of the progress bar."),
        Category("Appearance"),
        DefaultValue(typeof(Color), "White")]
        public Color OverlayColor
        {
            get { return m_overlayColor; }
            set
            {
                if (m_overlayColor != value)
                    m_overlayColor = value;
            }
        }

        /// <summary>
        /// Gets or Sets the Text being displayed.
        /// </summary>
        [Browsable(true),
        Category("Appearance"),
        Description("The Text to be displayed or if left null will display a percentage"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorBrowsable(EditorBrowsableState.Always)]
        public override string Text
        {
            get
            {
                if (m_Text != "") return m_Text;
                return Value.ToString() + "%";
            }

            set
            {
                if (m_Text != value)
                {
                    m_Text = value;
                    Invalidate();
                    OnTextChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Gets or sets a value that indicates whether the percentage will be displayed.
        /// </summary>
        [Browsable(true),
        Description("Indicates whether the Text will be displayed on the progress bar."),
        Category("Appearance"),
        DefaultValue(true)]
        public bool TextVisible
        {
            get { return m_TextVisible; }
            set
            {
                if (m_TextVisible != value)
                {
                    m_TextVisible = value;
                    OnTextVisibleChanged(EventArgs.Empty);
                }
            }
        }

        public new int Value
        {
            get { return base.Value; }
            set
            {
                if (base.Value != value)
                {
                    base.Value = value;

                    /* Needed for XP. Downside is control will be drawn twice
                        * when value coincides with one that the system uses for
                        * repaint. Could maybe use Environment.OSVersion to check? */
                    if (m_TextVisible)
                        Invalidate();
                }
            }
        }

        #endregion Properties

        #region Event Handlers

        protected virtual void OnTextVisibleChanged(EventArgs e)
        {
            EventHandler eh = TextVisibleChanged;
            if (eh != null)
                eh(this, e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            EventHandler eh = TextChanged;
            if (eh != null)
                eh(this, e);
        }

        protected override void OnFontChanged(EventArgs e)
        {
            EventHandler eh = FontChanged;
            if (eh != null)
                eh(this, e);
        }

        #endregion Event Handlers

        private void ShowText()
        {
            using (Graphics graphics = CreateGraphics())
            {
                // Draw left side
                Region regionLeft = new Region(new RectangleF(
                    ClientRectangle.X,
                    ClientRectangle.Y,
                    (ClientRectangle.Width * base.Value) / 100,
                    ClientRectangle.Height));
                using (Brush brush = new SolidBrush(m_overlayColor))
                {
                    graphics.Clip = regionLeft;
                    graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                }
                // Draw right side
                Region regionRight = new Region(ClientRectangle);
                regionRight.Exclude(regionLeft);
                using (Brush brush = new SolidBrush(ForeColor))
                {
                    graphics.Clip = regionRight;
                    graphics.DrawString(Text, Font, brush, ClientRectangle, m_stringFormat);
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m_TextVisible && m.Msg == WM_PAINT)
                ShowText();
        }
    }
}