创建图表托盘图标NotifyIconChart
2011-08-13 11:04
519 查看
转载请注明:敏捷学院-技术资源库原文链接:http://dev.mjxy.cn/a-Create-a-chart-tray-icon-NotifyIconChart.aspx
介绍
本文介绍如何创建一个带图标显示的系统托盘图标。动态的托盘图标有时候很有用,例如,可以实时显示CPU的占用情况,硬盘空间的使用情况等。我们开发的组件NotifyIconChart可以提供一列柱形图,两列柱形图和饼图。具体示例参考代码下载。
NotifyIconChart
NotifyIconChart是自定的组件,继承自Component. 封装了NotifyIcon控件。NotifyIcon的密封类(sealed class),无法被继承和重写。
创建托盘图标
private void CreateIcon()
{
// Draw different charts
if(chartType == ChartTypeEnum.singleBar)
DrawBars(value1);
else if(chartType == ChartTypeEnum.twoBars)
DrawBars(value1, value2);
else if(chartType == ChartTypeEnum.pie)
DrawPie(value1);
}
绘制单条柱形图
// Draw one bar
private void DrawBars(int bar1)
{
// Draw background
DrawBG(BGType.rect);
// Set GDI+ objects
Pen p = new Pen(frameColor, 1);
SolidBrush b = new SolidBrush(color1);
// Do some math
int width = (size * bar1) / 100;
// Draw bar
g.FillRectangle(b, 3, size - width, 10, width-1);
g.DrawRectangle(p, 3, size - width, 10, width-1);
// Create and display icon
ShowIcon();
}
绘制双条柱形图
// Draw two bars
private void DrawBars(int bar1, int bar2)
{
// Draw background
DrawBG(BGType.rect);
// Set GDI+ objects
Pen p = new Pen(frameColor, 1);
SolidBrush b = new SolidBrush(color1);
// Do some math for the first bar
int width1 = (size * bar1) / 100;
// Draw the first bar
g.FillRectangle(b, 1, size - width1, 5, width1-1);
g.DrawRectangle(p, 1, size - width1, 5, width1-1);
// Set GDI+ brush for the second bar
b = new SolidBrush(Color2);
// Do some math for the second bar
int width2 = (size * bar2) / 100;
// Draw the second bar
g.FillRectangle(b, 9, size - width2, 5, width2-1);
g.DrawRectangle(p, 9, size - width2, 5, width2-1);
// Create and display icon
ShowIcon();
}
绘制饼图
// Draw pie chart
private void DrawPie(int pie1)
{
// Draw background
DrawBG(BGType.pie);
// Set GDI+ objects
Pen p = new Pen(frameColor, 1);
SolidBrush b = new SolidBrush(color1);
// Draw pie
g.FillPie(b,0,0,size-1,size-1,270,(int)(pie1 * 3.6));
g.DrawPie(p,0,0,size-1,size-1,270,(int)(pie1 * 3.6));
// Create and display icon
ShowIcon();
}
绘制背景
private void DrawBG(BGType t)
{
// Clear Graphics object
g.Clear(Color.Transparent);
SolidBrush b = new SolidBrush(backgroundColor);
if(t == BGType.rect)
g.FillRectangle(b,0,0,size,size);
else if(t == BGType.pie)
g.FillEllipse(b,0,0,size,size);
}
ICON
NofityIconChart类引用一个NofityIcon对象来显示图表。它使用GDI+ Graphics类的方法FillRectangle和FillPiel绘制一个(16x16)大小的柱形图或饼图。最后,将图片转换成一个Icon传递给NotifyIcon对象。
private void ShowIcon()
{
// Convert bitmap to an icon
icon = Icon.FromHandle(bitmap.GetHicon());
// if none NotifyIcon object was selected, do not draw icon.
if(ni != null)
ni.Icon = icon;
}
图表类型
定义了3个枚举值来表示图表的类型:
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.singleBar; // Draw one only bar
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.twoBars; // Draw two bars
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.pie; // Draw pie chart
颜色
可以通过下面属性来改变图表或背景的颜色:
Color1 和 Color2 :绘制柱形图的颜色
FrameColor :柱图或饼图的边框色
BackgroundColor:背景颜色。
还可以使用Color.Transparent来设置透明色。
如何使用NotifyIconChart
增加NotifyIconChart.cs代码文件到你的项目中。NofityIconChart的命名空间定义在System.Windows.Forms,这样可以避免在你的项目中重新命名空间的麻烦。
为你的Form窗体正常添加NotifyIcon对象。因为NotifyIcon是sealed class。所以我们只能通过NotifyIconChart的成员来引用实际的NotifyIcon的对象了。
在Form的构造函数或Load事件中设置NotifyIconChart对NotifyIcon的引用:
private NotifyIconChart notifyChart = new NotifyIconChart();
NotifyIcon notifyIcon = new NotifyIcon();
notifyChart.NotifyIconObject = notifyIcon;
接下来,设置两个属性的值:Value1和Value2。分别表示两个柱形图的大小。值的范围在0-100之间。
notifyChart.Value1 = 75;
notifyChart.Value2 = 43;
代码下载
NotifyIconChart_demo.zip
参考资料
托盘图标 NotifyIcon http://dev.mjxy.cn/a-Tray-icon-NotifyIcon.aspx
翻译参考 http://www.codeproject.com/KB/miscctrl/notifyiconchart.aspx NotifyIcon类 http://msdn.microsoft.com/zh-cn/library/system.windows.forms.notifyicon(VS.80).aspx创建图表托盘图标NotifyIconChart
介绍
本文介绍如何创建一个带图标显示的系统托盘图标。动态的托盘图标有时候很有用,例如,可以实时显示CPU的占用情况,硬盘空间的使用情况等。我们开发的组件NotifyIconChart可以提供一列柱形图,两列柱形图和饼图。具体示例参考代码下载。
NotifyIconChart
NotifyIconChart是自定的组件,继承自Component. 封装了NotifyIcon控件。NotifyIcon的密封类(sealed class),无法被继承和重写。
创建托盘图标
private void CreateIcon()
{
// Draw different charts
if(chartType == ChartTypeEnum.singleBar)
DrawBars(value1);
else if(chartType == ChartTypeEnum.twoBars)
DrawBars(value1, value2);
else if(chartType == ChartTypeEnum.pie)
DrawPie(value1);
}
绘制单条柱形图
// Draw one bar
private void DrawBars(int bar1)
{
// Draw background
DrawBG(BGType.rect);
// Set GDI+ objects
Pen p = new Pen(frameColor, 1);
SolidBrush b = new SolidBrush(color1);
// Do some math
int width = (size * bar1) / 100;
// Draw bar
g.FillRectangle(b, 3, size - width, 10, width-1);
g.DrawRectangle(p, 3, size - width, 10, width-1);
// Create and display icon
ShowIcon();
}
绘制双条柱形图
// Draw two bars
private void DrawBars(int bar1, int bar2)
{
// Draw background
DrawBG(BGType.rect);
// Set GDI+ objects
Pen p = new Pen(frameColor, 1);
SolidBrush b = new SolidBrush(color1);
// Do some math for the first bar
int width1 = (size * bar1) / 100;
// Draw the first bar
g.FillRectangle(b, 1, size - width1, 5, width1-1);
g.DrawRectangle(p, 1, size - width1, 5, width1-1);
// Set GDI+ brush for the second bar
b = new SolidBrush(Color2);
// Do some math for the second bar
int width2 = (size * bar2) / 100;
// Draw the second bar
g.FillRectangle(b, 9, size - width2, 5, width2-1);
g.DrawRectangle(p, 9, size - width2, 5, width2-1);
// Create and display icon
ShowIcon();
}
绘制饼图
// Draw pie chart
private void DrawPie(int pie1)
{
// Draw background
DrawBG(BGType.pie);
// Set GDI+ objects
Pen p = new Pen(frameColor, 1);
SolidBrush b = new SolidBrush(color1);
// Draw pie
g.FillPie(b,0,0,size-1,size-1,270,(int)(pie1 * 3.6));
g.DrawPie(p,0,0,size-1,size-1,270,(int)(pie1 * 3.6));
// Create and display icon
ShowIcon();
}
绘制背景
private void DrawBG(BGType t)
{
// Clear Graphics object
g.Clear(Color.Transparent);
SolidBrush b = new SolidBrush(backgroundColor);
if(t == BGType.rect)
g.FillRectangle(b,0,0,size,size);
else if(t == BGType.pie)
g.FillEllipse(b,0,0,size,size);
}
ICON
NofityIconChart类引用一个NofityIcon对象来显示图表。它使用GDI+ Graphics类的方法FillRectangle和FillPiel绘制一个(16x16)大小的柱形图或饼图。最后,将图片转换成一个Icon传递给NotifyIcon对象。
private void ShowIcon()
{
// Convert bitmap to an icon
icon = Icon.FromHandle(bitmap.GetHicon());
// if none NotifyIcon object was selected, do not draw icon.
if(ni != null)
ni.Icon = icon;
}
图表类型
定义了3个枚举值来表示图表的类型:
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.singleBar; // Draw one only bar
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.twoBars; // Draw two bars
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.pie; // Draw pie chart
颜色
可以通过下面属性来改变图表或背景的颜色:
Color1 和 Color2 :绘制柱形图的颜色
FrameColor :柱图或饼图的边框色
BackgroundColor:背景颜色。
还可以使用Color.Transparent来设置透明色。
如何使用NotifyIconChart
增加NotifyIconChart.cs代码文件到你的项目中。NofityIconChart的命名空间定义在System.Windows.Forms,这样可以避免在你的项目中重新命名空间的麻烦。
为你的Form窗体正常添加NotifyIcon对象。因为NotifyIcon是sealed class。所以我们只能通过NotifyIconChart的成员来引用实际的NotifyIcon的对象了。
在Form的构造函数或Load事件中设置NotifyIconChart对NotifyIcon的引用:
private NotifyIconChart notifyChart = new NotifyIconChart();
NotifyIcon notifyIcon = new NotifyIcon();
notifyChart.NotifyIconObject = notifyIcon;
接下来,设置两个属性的值:Value1和Value2。分别表示两个柱形图的大小。值的范围在0-100之间。
notifyChart.Value1 = 75;
notifyChart.Value2 = 43;
代码下载
NotifyIconChart_demo.zip
参考资料
托盘图标 NotifyIcon http://dev.mjxy.cn/a-Tray-icon-NotifyIcon.aspx
翻译参考 http://www.codeproject.com/KB/miscctrl/notifyiconchart.aspx NotifyIcon类 http://msdn.microsoft.com/zh-cn/library/system.windows.forms.notifyicon(VS.80).aspx创建图表托盘图标NotifyIconChart
相关文章推荐
- wince 6.0或5.0 windows mobile 6.5 trayicon 如何创建 系统托盘 图标
- Flex中如何创建放射线状填充列图(ColumnChart)图表的例子
- windows程序托盘图标以及消息提示--基本知识(Shell_NotifyIcon)
- Java桌面应用程序创建系统托盘图标
- Shell_NotifyIcon实现任意托盘图标的隐藏与再现
- C#创建基本图表(Chart Controls)
- 在VS里创建Windows窗体,让其最小化在系统托盘,并显示一个Icon图标
- C#创建基本图表(Chart Controls)
- C# 托盘图标. notifyIcon 用法
- 转载:JFrame 创建系统托盘中的图标
- 创建QT系统托盘图标
- QT 创建托盘图标和右键菜单
- 用 chart.js 创建漂亮图表 (HTML绘制工具库)
- WPF for Chart FX创建Bullet图表
- [转]WPF 学习笔记-在WPF下创建托盘图标
- 托盘图标(NotifyIcon)详解
- windows API 创建系统托盘图标
- c# winform notifyIcon 系统托盘图标闪动 气泡提示
- WPF 学习笔记-在WPF下创建托盘图标
- 用Win32API Shell_NotifyIcon进行系统托盘图标编程