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

垂直进度条

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.48/5 (37投票s)

2004年9月30日

1分钟阅读

viewsIcon

211898

downloadIcon

11060

一个从 UserControl 继承的垂直进度条。

Sample Image - verticalprogressbar.jpg

引言

这是一个从 System.Windows.Forms.UserControl 继承的非常简单的进度条。System.Windows.Forms.ProgressBar 只能水平显示。而且我找不到垂直进度条的版本。我实现了这个垂直进度条,使其行为与水平进度条相同。为了我的个人用途,我为这个垂直进度条提供了一些增强功能。首先,条的颜色可以通过 Color 属性更改。其次,可以通过将 Style 属性设置为 Styles.Solid 来将条设置为实心。最后一个功能是,可以通过将 BorderStyle 属性设置为 BorderStyles.None 来使垂直进度条无边框。

代码

代码非常简单。StylesBorderStyles enum 被定义为

public enum Styles
{
    Classic, // same as ProgressBar

    Solid
}
public enum BorderStyles
{
    Classic, // same as ProgressBar

    None
}

VerticalProgressBar 类是从 UserControl 类继承的。您可以使用 ToolboxBitmapAttribute 类指定特定的图像。在这里,我使用了与 ProgressBar 相同的图像。

[Description("Vertical Progress Bar")]
[ToolboxBitmap(typeof(ProgressBar))]
public sealed class VerticalProgressBar : System.Windows.Forms.UserControl
{
    ...    ...
}

为了方便起见,所有垂直进度条属性都组织到 Visual Studio .NET 的属性窗口中的 VerticalProgressBar 类别中,使用 Category 属性。

[Description( "VerticalProgressBar Maximum Value")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Maximum
{
    get
    {
    return m_Maximum;
    }
    set
    {
    m_Maximum = value;
    if(m_Maximum < m_Minimum)
            m_Minimum=m_Maximum;
        if(m_Maximum < m_Value)
                m_Value = m_Maximum;
        Invalidate();
    }
}

... ...

OnPaint 函数被重写,以根据需要绘制条和边框。

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    Graphics dc = e.Graphics;
    
    //Draw Bar

    drawBar(dc);

    //Draw Border

    drawBorder(dc);

    base.OnPaint(e);
}

Using the Code

要使用此垂直进度条,请将 VerticalProgressBar.dll 复制到您的项目目录中。然后在工具箱面板的上下文菜单上选择“添加/删除项..”,并选择此文件以将此控件添加到工具箱。它将显示与 ProgressBar 相同的图标。然后您可以像使用 ProgressBar 一样使用它。此垂直进度条的默认属性设置为与 ProgressBar 相同。您可以根据需要更改 ColorStyleBorderStyle 属性。

© . All rights reserved.