有些时候我们做的程序需要进度条,而vs提供的控件不是我们想要的。先看效果图:

      进度条闪烁动画,当然背景可设为Transparent

之前想手绘进度条线条的,结果控件运行时会闪烁,所以直接用了panel控件

源码:

    [DefaultEvent("ProgressClick")]
[ToolboxBitmap(typeof(TrackBar))]
public partial class ProcessBar : UserControl
{
public ProcessBar()
{
//InitializeComponent();
//this.SetStyle(ControlStyles.UserPaint, true);
//this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//this.SetStyle(ControlStyles.DoubleBuffer, true);
} private int locationX=;
[Description("单击时X的坐标")]
public int LocationX
{
get { return locationX; }
} private int current = ;
[Description("当前进度")]
public int Current
{
get { return current; }
set
{
if (value > || value < )
return;
current = value;
panelCurrent.Size = new Size(value, );
picture.Location = new Point(value - , -);
Invalidate();
}
} private bool isPlay = false;
[Description("是否播放")]
public bool IsPlay
{
get { return isPlay; }
set { isPlay = value; tmrCurrent.Enabled = isPlay; Invalidate(); }
} public delegate void MouseHandle(object sender,EventArgs e);
[Description("点下鼠标")]
public event MouseHandle BarMouseDown; int picturetype = ;
private void tmrCurrent_Tick(object sender, EventArgs e)
{
if (picturetype == )
{ picture.Image = Properties.Resources.play_slider_thumb; picturetype = ; }
else
{ picture.Image = Properties.Resources.play_slider_thumb_animate; picturetype = ; }
GraphicsPath g = subGraphicsPath(picture.Image);
if (g == null) return;
picture.Region = new Region(g);
} private unsafe static GraphicsPath subGraphicsPath(Image img)
{
if (img == null) return null;
// 建立GraphicsPath, 给我们的位图路径计算使用
GraphicsPath g = new GraphicsPath(FillMode.Alternate);
Bitmap bitmap = new Bitmap(img);
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData bmData = bitmap.LockBits(new Rectangle(, , width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* p = (byte*)bmData.Scan0;
int offset = bmData.Stride - width * ;
int p0, p1, p2; // 记录左上角0,0座标的颜色值
p0 = p[];
p1 = p[];
p2 = p[]; int start = -;
// 行座标 ( Y col )
for (int Y = ; Y < height; Y++)
{
// 列座标 ( X row )
for (int X = ; X < width; X++)
{
if (start == - && (p[] != p0 || p[] != p1 || p[] != p2)) //如果 之前的点没有不透明 且 不透明
{
start = X; //记录这个点
}
else if (start > - && (p[] == p0 && p[] == p1 && p[] == p2)) //如果 之前的点是不透明 且 透明
{
g.AddRectangle(new Rectangle(start, Y, X - start, )); //添加之前的矩形到
start = -;
}
if (X == width - && start > -) //如果 之前的点是不透明 且 是最后一个点
{
g.AddRectangle(new Rectangle(start, Y, X - start + , )); //添加之前的矩形到
start = -;
}
p += ; //下一个内存地址
}
p += offset;
} bitmap.UnlockBits(bmData);
bitmap.Dispose();
// 返回计算出来的不透明图片路径
return g;
} private void panelTotal_MouseDown(object sender, MouseEventArgs e)
{
Current = e.Location.X;
locationX = e.Location.X;
if (BarMouseDown != null)
{
BarMouseDown.Invoke(sender, e);
}
} private void panelCurrent_MouseDown(object sender, MouseEventArgs e)
{
Current = e.Location.X;
locationX = e.Location.X;
if (BarMouseDown != null)
{
BarMouseDown.Invoke(sender, e);
}
}
}

用到的素材:

直接右键另存为图片,之所以用黑色背景是因为图片是白色的看不见,不用多说了。

提示:这里用到了unsafe关键字,需要设置项目的属性-----允许运行不安全的代码,没有设置的同学不要以为程序错了

c#自定义进度条的更多相关文章

  1. android 自定义进度条颜色

    android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程!   这个没法了只能看源码了,还好下载了源码, sources\b ...

  2. Qt之模型/视图(自定义进度条)

    简述 在之前的章节中分享过关于QHeaderView表头排序.添加复选框等内容,相信大家模型/视图.自定义风格有了一定的了解,下面我们来分享一个更常用的内容-自定义进度条. 实现方式: 从QAbstr ...

  3. android113 自定义进度条

    MainActivity: package com.itheima.monitor; import android.os.Bundle; import android.app.Activity; im ...

  4. 自定义进度条PictureProgressBar——从开发到开源发布全过程

    自定义进度条PictureProgressBar——从开发到开源发布全过程 出处: 炎之铠邮箱:yanzhikai_yjk@qq.com 本文原创,转载请注明本出处! 本项目JCenter地址:htt ...

  5. C# 根据BackgroundWoker异步模型和ProgressBar控件,自定义进度条控件

    前言 程序开发过程中,难免会有的业务逻辑,或者算法之类产生让人能够感知的耗时操作,例如循环中对复杂逻辑处理;获取数据库百万乃至千万级数据;http请求的时候等...... 用户在使用UI操作并不知道程 ...

  6. BitBlt()函数实现带数字百分比进度条控件、静态文本(STATIC)控件实现的位图进度条、自定义进度条控件实现七彩虹颜色带数字百分比

    Windows API BitBlt()函数实现带数字百分比进度条控件. 有两个例子:一用定时器实现,二用多线程实现. 带有详细注解. 此例是本人原创,绝对是网上稀缺资源(本源码用Windows AP ...

  7. 最简单的android自定义进度条样式

    一.自定义圆形进度条样式 1.在安卓项目drawable目录下新建一个xml文件如下:<?xml version="1.0" encoding="utf-8&quo ...

  8. iOS 自定义进度条

    自定义条形进度条(iOS) ViewController.m文件 #import "ViewController.h" @interface ViewController () @ ...

  9. Android_自定义进度条

    转载:http://blog.csdn.net/lmj623565791/article/details/43371299 ,本文出自:[张鸿洋的博客] 1.概述 最近需要用进度条,秉着不重复造轮子的 ...

  10. jquery自定义进度条与h5原生进度条

      介绍一款自定义的进度条 <div class="box-nine"> <div class="progress"> <!--一 ...

随机推荐

  1. SQL 优化原则

    一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...

  2. spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX

    spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...

  3. node在安装完成后,出现node不是内部或外部命令

    node在安装完成后,查看node版本 node -v出现"node不是内部或外部命令"郁闷. 各种搜索之后,处理好了问题了. 一张图解决问题.

  4. AngularJs创建服务

    在开发中我们总是需要向服务器请求同样的数据,那么我们如何来把他们提取出来进行封装一下呢,这就需要用到服务了. 需要用到关键字factory了. <!DOCTYPE html> <ht ...

  5. Kinect测量人体身高的程序

    对着书上敲得,从中体会kinect骨骼识别与深度识别的原理.大体原理是懂了,但有些细节还没有完全弄明白. using System; using System.Collections.Generic; ...

  6. [SQL]获取所有数据库名、获取数据库中表名、获取表中的字段名

    --()获取所有数据库名: Select Name FROM Master..SysDatabases order by Name --()获取所有表名 --XType=''U'':表示所有用户表; ...

  7. 转载cocos2dx的各种动作用法

    以下内容来源于:http://www.cnblogs.com/linux-ios/archive/2013/04/06/3001946.html 转载时请保留以上链接. bool HelloWorld ...

  8. task中的一些属性

    1.android:allowTaskReparenting 这个属性用来标记一个Activity实例在当前应用退居后台后,是否能从启动它的那个task移动到有共同affinity的task,“tru ...

  9. Hadoop 2.6.0动态添加节点

    文章出自:http://my.oschina.net/leoleong/blog/477508 本文主要从基础准备,添加DataNode和添加NodeManager三个部分详细说明在Hadoop2.6 ...

  10. document.write('<script type=\"text/javascript\"><\/script>')

    document.write('<script type=\"text/javascript\"><\/script>')