首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

利用 C# 实现任务栏通知窗口

public void SetBackgroundBitmap(Image image, Color transparencyColor) 
   {
   BackgroundBitmap = new Bitmap(image);
   Width = BackgroundBitmap.Width;
   Height = BackgroundBitmap.Height;
   Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
   }
  
   public Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
   {
   if (bitmap == null)
   throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
  
   int height = bitmap.Height;
   int width = bitmap.Width;
   GraphicsPath path = new GraphicsPath();
   for (int j = 0; j < height; j++)
   for (int i = 0; i < width; i++)
   {
   if (bitmap.GetPixel(i, j) == transparencyColor)
   continue;
   int x0 = i;
   while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))
   i++;
   path.AddRectangle(new Rectangle(x0, j, i - x0, 1));
   }
   Region region = new Region(path);
   path.Dispose();
   return region;
  }

  通知窗体背景以及文字的绘制在重载的OnPaintBackground方法中完成,而且利用了双重缓冲区技术来进行绘制操作,代码如下:

protected override void OnPaintBackground(PaintEventArgs e) 
   {
   Graphics grfx = e.Graphics;
   grfx.PageUnit = GraphicsUnit.Pixel;
   Graphics offScreenGraphics;
   Bitmap offscreenBitmap;
   offscreenBitmap = new Bitmap(BackgroundBitmap.Width,
BackgroundBitmap.Height);
   offScreenGraphics = Graphics.FromImage(offscreenBitmap);
   if (BackgroundBitmap != null)
   {
   offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0,
BackgroundBitmap.Width,
BackgroundBitmap.Height);
   }
   DrawText(offScreenGraphics);
   grfx.DrawImage(offscreenBitmap, 0, 0);
   }

  上述代码首先返回窗体绘制表面的Graphics并保存在变量grfx中,然后创建一个内存Graphics对象offScreenGraphics和内存位图对象offscreenBitmap,将内存位图对象的引用付值给offScreenGraphics,这样所有对offScreenGraphics的绘制操作也都同时作用于offscreenBitmap,这时就将需要绘制到通知窗体表面的背景图像BackgroundBitmap绘制到内存的Graphics对象上,DrawText函数根据需要显示文字的大小和范围调用Graphics.DrawString将文字显示在窗体的特定区域。最后,调用Graphics.DrawImage将内存中已经绘制完成的图像显示到通知窗体表面。

  我们还需要捕获窗体的鼠标操作,有三个操作在这里进行,1、处理拖动窗体操作,2、处理通知窗体的关闭操作,3、内容区域的单击操作。三个操作都需要检测鼠标的当前位置与每个Rectangle区域的包含关系,只要单击落在特定区域我们就进行相应的处理,代码如下:

 private void TaskbarForm_MouseDown(object sender, MouseEventArgs e) 
   {
   if (e.Button == MouseButtons.Left)
   {
   if (TitlebarRectangle.Contains(e.Location)) //单击标题栏时拖动
   {
   ReleaseCapture(); //释放鼠标捕捉
   SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
//发送左键点击的消息至该窗体(标题栏)
   }
   if (CloseBtnRectangle.Contains(e.Location)) //单击Close按钮关闭
   {
   this.Hide();
   currentTop = 1;
   }
   if (ContentRectangle.Contains(e.Location )) //单击内容区域
   {
   System.Diagnostics.Process.Start("http://www.Rithia.com");
   }
   }
  }

  结论

  该程序可以很好的进行通知窗体的显示、停留和隐藏操作,并且具备简单的换肤机制,在利用了双重缓冲区绘图技术后,可以保证窗体的绘制平滑且没有闪烁。

上一页  [1] [2] [3] 

【责编:John】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航