• 载入条LoadingBar


      这个控件太傻瓜了,只搁在博客里算了。日前需要用到一个载入条,

    但不想找GIF图片,.NET里面没有提供这个控件,只有ProgressBar。自己写吧!要写也不难,就是周期性绘制一个长方形,让那个长方形不停地向右移动。这个周期性的操作可以开一条线程Thread。我就用了一个WinForm的控件Timer

      用到了GDI+,重写OnPaint方法是免不了的。

     1         protected override void OnPaint(PaintEventArgs e)
     2         {
     3             base.OnPaint(e);
     4             Rectangle rec=new Rectangle((int)(curLen - this.Width * barLength), 1, (int)(this.Width * barLength), this.Height - 2);
     5             if (Application.RenderWithVisualStyles)
     6             {
     7                 VisualStyleRenderer glyphRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);
     8                 glyphRenderer.DrawBackground(e.Graphics, rec);
     9             }
    10             else
    11                 e.Graphics.FillRectangle(Brushes.Green, rec);
    12 
    13             e.Graphics.DrawRectangle(Pens.Black, 0, 0, this.Width-1, this.Height-1);
    14 
    15 
    16         }

    自从上次写了那个可分租的GroupGridView之后,学多了一样东西,利用VisualStyleRenderer这个类的就可以使用上系统上的三维效果。

    在Timer控件的Tick事件绑定以下方法,

    1         private void timer1_Tick(object sender, EventArgs e)
    2         {
    3             if (!this.DesignMode)
    4             {
    5                 curLen += 10;
    6                 if (curLen >= this.Width * (1 + barLength)) curLen = 0;
    7                 this.Refresh();
    8             }
    9         }

    那个DesignMode就是判断是否在视图设计器上显示,如果不加那个判断,编译控件之后,拉到窗体里面,那Loading的效果也能看出来,这属性找了很久都没找到,感谢匡哥告诉我。其他也没什么好说的,上图上代码

    由于是Win8的,看不到什么三维效果了。

     1     class LoadingBar:Control
     2     {
     3 
     4         private System.Windows.Forms.Timer timer1;
     5         private System.ComponentModel.IContainer components;
     6 
     7         private void InitializeComponent()
     8         {
     9             this.components = new System.ComponentModel.Container();
    10             this.timer1 = new System.Windows.Forms.Timer(this.components);
    11             this.SuspendLayout();
    12             // 
    13             // timer1
    14             // 
    15             this.timer1.Enabled = true;
    16             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    17             this.ResumeLayout(false);
    18 
    19             curLen = 0;
    20             barLength = 0.5f;      
    21         }
    22 
    23         internal float curLen;
    24         internal float barLength;
    25 
    26         public LoadingBar()
    27         {
    28             InitializeComponent();
    29         }
    30 
    31         private void timer1_Tick(object sender, EventArgs e)
    32         {
    33             if (!this.DesignMode)
    34             {
    35                 curLen += 10;
    36                 if (curLen >= this.Width * (1 + barLength)) curLen = 0;
    37                 this.Refresh();
    38             }
    39         }
    40 
    41         protected override void OnPaint(PaintEventArgs e)
    42         {
    43             base.OnPaint(e);
    44 
    45             Rectangle rec=new Rectangle((int)(curLen - this.Width * barLength), 1, (int)(this.Width * barLength), this.Height - 2);
    46             if (Application.RenderWithVisualStyles)
    47             {
    48                 VisualStyleRenderer glyphRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);
    49                 glyphRenderer.DrawBackground(e.Graphics, rec);
    50             }
    51             else
    52                 e.Graphics.FillRectangle(Brushes.Green, rec);
    53 
    54             e.Graphics.DrawRectangle(Pens.Black, 0, 0, this.Width-1, this.Height-1);
    55 
    56 
    57         }
    58     }
    LoadingBar
  • 相关阅读:
    java:字符串的split方法,使用多个分隔符,分割一个字符串
    mysql 导入txt数据到数据表【原创】
    配置SSH无密码登录【原著】
    springboot 控制台程序读取配置文件(原创)
    Idea开发环境中,开发springboot类型的项目,如果只引入parent节点,不添加依赖节点,maven是不会加载springboot的任何依赖的
    Windows版的OpenJDK下载(Red Hat 提供)
    谈谈php里的IOC控制反转,DI依赖注入(转)
    高质量的工程代码为什么难写 (转)
    系统权限管理设计 (转)
    php mqtt client
  • 原文地址:https://www.cnblogs.com/HopeGi/p/3369044.html
Copyright © 2020-2023  润新知