请问如何把ProgressBar加到StatusBar的Panel中???????????? http://www.syncfusion.com/FAQ/WinForms/FAQ_c41c.asp#q894q 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我的一个继承自 StatusBarPanel 的组件。我并不赞同硬生生的把一个 ProgressBar 控件放到状态条上去,因为把(过分)花哨的进度条控件放到状态条上,处理不好的话会破坏界面的简洁美观性。尽管此组件没有专业的第三方 ProgressBar 控件的强或花哨,但其效果已经可以与 VS.NET 上的效果相比美了。使用时此组件不能用 IDE 加入,但用代码插入到 StatusBar 的集合中就可以了。因为 StatusBar 的设计器不认识这个组件,尽管它继承自 StatusBarPanel。另外,为了保持界面的一致性,其他 StatusBarPanel 最好也要重画,使得它们有一致的边框样式,就象 VS.NET IDE 一样。需要其他 StatusBarPanel 重画的示例代码的话,请留言。using System;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;namespace YourNameSpace{ /// <summary> /// ProgressStatusBarPanel 的摘要说明。 /// </summary> public class ProgressStatusBarPanel : System.Windows.Forms.StatusBarPanel { public static readonly int MaxValue = 100; public static readonly int MinValue = 0; private int indentRight = 1; private float _value = 0; private Color borderColor = SystemColors.ControlDark; private Color progressColor = SystemColors.ActiveCaption; private StatusBarDrawItemEventHandler drawHandler; public ProgressStatusBarPanel() { //此方法判断是不是 XP 样式,可能你没有此方法的代码。但不重要,仅仅是修正一个值。 if (WindowsAPI.IsCommonCtrl6()) { indentRight = 2; } base.Style = StatusBarPanelStyle.OwnerDraw; this.drawHandler = new StatusBarDrawItemEventHandler(this.ParentStatusBar_DrawItem); } /// <summary> /// 用 new 操作符将样式固定为 OwnerDraw。 /// 不能进行设置 /// </summary> public new StatusBarPanelStyle Style { get { return base.Style; } } public Color BorderColor { get { return this.borderColor; } set { this.borderColor = value; } } public Color ProgressColor { get { return this.progressColor; } set { this.progressColor = value; } } /// <summary> /// 进度值,此处使用最小值为 0,最大值为 100。 /// 可以自行增加 Min,Max的定义 。 /// </summary> public float Value { get { return this._value; } set { if (this._value == value) return; if (value < MinValue) this._value = MinValue; else if (value > MaxValue) this._value = MaxValue; else this._value = value; this.ToolTipText = (this._value/MaxValue).ToString("p2"); if (this.Parent != null) { // 通过查询父状态条的 Panels 集合来确定当前进度板所在的实际矩形 // 在 Invalidate 时将此矩形传入。可以减少不必要的闪烁并提高性能。 int index = this.Parent.Panels.IndexOf(this); int startLeft = 0; for (int i = 0; i < index; i++) { startLeft += this.Parent.Panels[i].Width; } Rectangle rect = new Rectangle(startLeft, 0, this.Width, this.Parent.Height); // 以下会状态条的特定区域 this.Parent.Invalidate(rect); } } } // 能过此方法的增强和修改可以达到你所想要的效果 // 譬如: // 可以准备一图形来描述进度值, // 可以使用渐变刷来增强进度颜色效果, // 可以画出进度条的分隔条, // 可以在背景上填充一图形, // 可以画出进度百分比值, // 可以调整进度颜色条矩形的尺寸和位置 // 可以起始处绘一个图标 // 可以用一个 Timer 来绘制自定义的动画图标 // ... // 等等,充分发挥你的想像力吧。 private void ParentStatusBar_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent) { if (sbdevent.Panel == this) { Graphics g = sbdevent.Graphics; Rectangle rectBorder = new Rectangle( sbdevent.Bounds.Left, sbdevent.Bounds.Top, sbdevent.Bounds.Width - indentRight, sbdevent.Bounds.Height - 1); using(Pen pen = new Pen(this.borderColor)) { g.DrawRectangle(pen, rectBorder); } if (this.Value == 0) return; Rectangle rc = new Rectangle(rectBorder.Left + 2 , rectBorder.Top + 2, (int)((rectBorder.Width - 3) * this._value / MaxValue), rectBorder.Height - 3); using(Brush br = new SolidBrush(this.progressColor)) { g.FillRectangle(br, rc); } } } // 绑定绘图事件处理到父状态条。 // 此方法在进度状态面板加入到状态条后执行,否则无效。 // 如果从父状态条中移除时,记得 public void BindDrawHandlerToParent() { if (this.Parent != null) { this.Parent.DrawItem += drawHandler; } } // 在应用时,应注意在更改其父状态条,将已绑定绘图事件处理委托移除。 // 此方法从父状态条移除事件处理委托 public void RemoveDrawHandlerFromParent() { if (this.Parent != null) { this.Parent.DrawItem -= drawHandler; } } }} 补充一个注释:// 如果从父状态条中移除时,记得代码中调用 RemoveDrawHandlerFromParent。可惜的是,Panels 集合没有相应的 Removed 事件可供使用,否则不用这么烦的。我想想办法,能不能增强。即使能增强 Panels ,可能设计器就不管用了,其他的操作都只能直接用代码了。 ProgressBar加到StatusBar其实直接把那个ProgressBar 设置好大小,拖动到StatusBar你认为合适的位置就行具体:拖动后将ProgressBar 的Anchor属性重设为Bottom, Right即可! 那如果我在运行后,改变窗体的大小呢?progressBar会不会跟着变啊?? 不会,你试试,注意Anchor属性 取消Top, Left , 变成Bottom, Right 谢谢了,OK了!不过不知道你有没有发现,你这样的做存在一个异常要处理的, 就是你改变窗体的宽度的时候,如果小于一定的宽度就会出现异常.老兄,我还想问你一个问题,为什么我下面的代码会通不过啊?Directory dir=new Directory(".");这可是书里面的啊? 说什么没获取"1"参数啊?我把它改成Directory dir=new Directory(1,".");后,它又说没有获取"2"参数 老兄,请问怎样使用StringList类啊?好像调用不了啊?找不到名字空间啊? http://www.syncfusion.com/FAQ/WinForms/FAQ_c41c.asp#q894q SQlserver触发器原理 急 在线等!!!!!!!! C#数据库刷新 webbrowser控件, 如何做到提供html源码就可以显示网页 遇到一个问题解决不了,请各位帮帮忙! 各位大大们帮帮忙呀,简单问题,小弟在线等 get set属性疑问? DATAGRID 更新问题!! 为什么我的SQL语句只能更新一列 调用 Next() 方法能得到什么? 如何使用反射 ASP.net中动态加载控件怎么加载到页面的指定位置?
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;namespace YourNameSpace
{
/// <summary>
/// ProgressStatusBarPanel 的摘要说明。
/// </summary>
public class ProgressStatusBarPanel : System.Windows.Forms.StatusBarPanel
{
public static readonly int MaxValue = 100;
public static readonly int MinValue = 0;
private int indentRight = 1; private float _value = 0;
private Color borderColor = SystemColors.ControlDark;
private Color progressColor = SystemColors.ActiveCaption;
private StatusBarDrawItemEventHandler drawHandler;
public ProgressStatusBarPanel()
{
//此方法判断是不是 XP 样式,可能你没有此方法的代码。但不重要,仅仅是修正一个值。
if (WindowsAPI.IsCommonCtrl6())
{
indentRight = 2;
}
base.Style = StatusBarPanelStyle.OwnerDraw;
this.drawHandler =
new StatusBarDrawItemEventHandler(this.ParentStatusBar_DrawItem);
} /// <summary>
/// 用 new 操作符将样式固定为 OwnerDraw。
/// 不能进行设置
/// </summary>
public new StatusBarPanelStyle Style
{
get
{
return base.Style;
}
} public Color BorderColor
{
get
{
return this.borderColor;
}
set
{
this.borderColor = value;
}
} public Color ProgressColor
{
get
{
return this.progressColor;
}
set
{
this.progressColor = value;
}
} /// <summary>
/// 进度值,此处使用最小值为 0,最大值为 100。
/// 可以自行增加 Min,Max的定义 。
/// </summary>
public float Value
{
get
{
return this._value;
}
set
{
if (this._value == value)
return;
if (value < MinValue)
this._value = MinValue;
else if (value > MaxValue)
this._value = MaxValue;
else
this._value = value;
this.ToolTipText = (this._value/MaxValue).ToString("p2"); if (this.Parent != null)
{
// 通过查询父状态条的 Panels 集合来确定当前进度板所在的实际矩形
// 在 Invalidate 时将此矩形传入。可以减少不必要的闪烁并提高性能。
int index = this.Parent.Panels.IndexOf(this);
int startLeft = 0;
for (int i = 0; i < index; i++)
{
startLeft += this.Parent.Panels[i].Width;
}
Rectangle rect = new Rectangle(startLeft, 0, this.Width, this.Parent.Height);
// 以下会状态条的特定区域
this.Parent.Invalidate(rect);
}
}
}
// 能过此方法的增强和修改可以达到你所想要的效果
// 譬如:
// 可以准备一图形来描述进度值,
// 可以使用渐变刷来增强进度颜色效果,
// 可以画出进度条的分隔条,
// 可以在背景上填充一图形,
// 可以画出进度百分比值,
// 可以调整进度颜色条矩形的尺寸和位置
// 可以起始处绘一个图标
// 可以用一个 Timer 来绘制自定义的动画图标
// ...
// 等等,充分发挥你的想像力吧。
private void ParentStatusBar_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
if (sbdevent.Panel == this)
{
Graphics g = sbdevent.Graphics;
Rectangle rectBorder = new Rectangle(
sbdevent.Bounds.Left,
sbdevent.Bounds.Top,
sbdevent.Bounds.Width - indentRight,
sbdevent.Bounds.Height - 1);
using(Pen pen = new Pen(this.borderColor))
{
g.DrawRectangle(pen, rectBorder);
}
if (this.Value == 0)
return;
Rectangle rc = new Rectangle(rectBorder.Left + 2 , rectBorder.Top + 2,
(int)((rectBorder.Width - 3) * this._value / MaxValue), rectBorder.Height - 3);
using(Brush br = new SolidBrush(this.progressColor))
{
g.FillRectangle(br, rc);
}
}
} // 绑定绘图事件处理到父状态条。
// 此方法在进度状态面板加入到状态条后执行,否则无效。
// 如果从父状态条中移除时,记得
public void BindDrawHandlerToParent()
{
if (this.Parent != null)
{
this.Parent.DrawItem += drawHandler;
}
} // 在应用时,应注意在更改其父状态条,将已绑定绘图事件处理委托移除。
// 此方法从父状态条移除事件处理委托
public void RemoveDrawHandlerFromParent()
{
if (this.Parent != null)
{
this.Parent.DrawItem -= drawHandler;
}
}
}
}
其实直接把那个ProgressBar 设置好大小,拖动到StatusBar你认为合适的位置就行具体:拖动后将ProgressBar 的Anchor属性重设为Bottom, Right即可!
老兄,我还想问你一个问题,为什么我下面的代码会通不过啊?
Directory dir=new Directory(".");
这可是书里面的啊?
我把它改成
Directory dir=new Directory(1,".");
后,它又说没有获取"2"参数