65.9K
CodeProject 正在变化。 阅读更多。
Home

C# StatusBarProgressPanel 控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.69/5 (20投票s)

2005年4月19日

1分钟阅读

viewsIcon

106620

downloadIcon

1365

一个在状态栏中显示标准进度条控件的面板。

引言

我想要一个状态栏的进度条,但对网上找到的任何代码都不满意。我的解决方案是创建一个从 StatusBarPanel 类继承的用户控件,并将一个 ProgressBar 控件作为成员对象添加。

演示项目中的源代码很简单,这里不需要做过多的解释。我将简要概述 StatusBarProgressPanel 类以及如何在自己的项目中使用的步骤。

类基础

StatusBarPanel 和我的类之间的唯一区别是添加了 ProgressBar 属性。

private ProgressBar progressBar = new ProgressBar();
[Category("Progress")]
public ProgressBar ProgressBar
{  
  get { return progressBar; }
}

以及状态栏的 DrawItem 事件的处理程序。

public void ParentDrawItemHandler(object sender, 
                       StatusBarDrawItemEventArgs sbdevent)
{
  // Only add this once to the parent's control container

  if (isAdded == false)
  {
    this.Parent.Controls.Add(this.progressBar);
    this.isAdded = true;
  }
    
  // Get the bounds of this panel and copy to the progress bar's bounds

  if (sbdevent.Panel == this)
    progressBar.Bounds = sbdevent.Bounds;
}

我知道这可能不是最高效的方法,但它有效且简单。如果有人想发布任何改进,我将很乐意添加并给予您署名。

如何使用

使用进度面板非常简单。涉及的步骤如下:

  1. 创建您的 StatusBar 并将其添加到窗体中。
  2. 从属性窗口打开 StatusBarPanel 集合编辑器。单击“添加”按钮以添加一个新的面板,并将面板的 Style 设置为 OwnerDraw
  3. 关闭 StatusBarPanel 集合编辑器窗口。
  4. 查看包含 StatusBar 的窗体的源代码。
  5. 将进度面板的类从 System.Windows.Form.StatusBarPanel 更改为 MarkHarmon.Controls.StatusBarProgressPanel
  6. 将状态栏的 DrawItem 事件处理程序设置为 StatusBarProgressPanelParentDrawItemHandler 方法。
  7. 保存并重新生成项目。
  8. 再次打开 StatusBarPanel 集合编辑器,找到 ProgressBar 属性。根据您的喜好设置进度条的属性。

以下是如何设置 DrawItem 事件处理程序的示例:

// Set the DrawItem event handler 

  statusBar1.DrawItem += 
    new StatusBarDrawItemEventHandler(progressPanel.ParentDrawItemHandler);

要以编程方式设置进度条的位置或其他属性,只需使用面板的 ProgressBar 属性即可。

© . All rights reserved.