您的位置:首页 > 其它

创建图表托盘图标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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: