我有一个自定义用户进度条控件,并在显示文本的过程中覆盖了其Font属性。
当我将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();
}
}
}
以下是'font'属性更改后保存在设计文件中的内容:
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;
}
}
正如您所看到的,没有为progressBarWithText1为font属性设置参数。有没有办法强制保存它们而不必手动设置它们或者是否有什么我做错了需要进一步指导?
感谢您抽出宝贵时间。上帝保佑,
克雷格
聚苯乙烯。此控件尚未完全完成。我现在正在写它而且还挂了这个。
答案 0 :(得分:1)
以下是问题的答案:
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();
}
}
}
如果您想了解有关虚拟,新增和覆盖的更多信息,可以在Understanding C#网站上找到它们。
感谢您的时间。
上帝保佑,
克雷格