基本信息
源码名称:C# 我的世界进度条控件
源码大小:2.81M
文件格式:.zip
开发语言:C#
更新时间:2017-12-24
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
也没什么,也就随便做做,新手参考用(里面有一些乱写的代码忘了删了)
进度条代码在核心代码那里了
using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;
namespace 控件库
{
public class 我的世界经验进度条 : ProgressBar
{
public 我的世界经验进度条()
{
base.SetStyle(ControlStyles.UserPaint, true);//使控件可由用户自由重绘
}
/// <summary>
/// 使用资源中的字体(无释放,无安装)
/// </summary>
/// <param name="path">
/// 字体在项目中的路径
/// 如提取WindowsFormsApplication1中的font.ttf:WindowsFormsApplication1.font.ttf
/// </param>
/// <param name="fontsize">字体大小</param>
/// <param name="fontstyle">字体样式[默认为FontStyle.Regular(常规)]</param>
/// <returns>返回已赋值的字体</returns>
public Font GetResoruceFont(string path, int fontsize, FontStyle fontstyle = FontStyle.Regular)
{
/* 提取文件字节到bytes变量中 */
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, int.Parse(stream.Length.ToString()));
/* 开始将文件的字节转成字体 */
PrivateFontCollection pfc = new PrivateFontCollection();//声明字体组数变量
IntPtr MeAdd = Marshal.AllocHGlobal(bytes.Length);//字节转句柄
Marshal.Copy(bytes, 0, MeAdd, bytes.Length);//托管字节
pfc.AddMemoryFont(MeAdd, bytes.Length);//从字节添加字体
/* 转换完成,返回值 */
return new Font(pfc.Families[0], fontsize, fontstyle);
}
/// <summary>
/// 当控件被绘制时
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
//双缓冲(避免闪烁)
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
/* 开始绘图 */
//绘制背景
e.Graphics.DrawImageUnscaledAndClipped(
Windows窗体测试.Properties.Resources.ProgressBar_BackImage,//背景图片
new Rectangle(//绘制区域
new Point(0, 0), //坐标
new Size(this.Width, 20)));//大小
//绘制前景
e.Graphics.DrawImageUnscaledAndClipped(
Windows窗体测试.Properties.Resources.ProgressBar_ForeImage,//前景图片
new Rectangle(//绘制区域
new Point(0, 0), //坐标
new Size(this.Width * Value / Maximum, this.Height)));//大小
//绘制字符
e.Graphics.DrawString(
this.Value.ToString(),//文字
this.GetResoruceFont("Windows窗体测试.minecraft.ttf", 10),//字体
new SolidBrush(this.ForeColor), //颜色
this.Width / 2, //坐标x
this.Height / 3 - 3);//坐标y
}
}
}