C#内存Graphics对象

开发 后端
这里介绍C#内存Graphics对象,以及介绍DrawText函数根据需要显示文字的大小和范围调用Graphics.DrawString将文字显示在窗体的特定区域。

Windos窗体有很多值得学习的地方,这里我们主要介绍C#内存Graphics对象,包括介绍SetBackgroundBitmap函数。

想必大部分网友都使用过QQ、MSN等聊天程序,它们的界面都相当华丽,尤其是当网友上线以及消息提示时会有一个浮动的窗体从屏幕的右下方缓慢升起,既美观又人性化,作为程序员在享受的同时我们也不禁要问:这到底是怎么实现的呢?本文就利用C#内存Graphics对象

SetBackgroundBitmap函数首先将窗体背景图像保存到BackgroundBitmap变量中,然后根据该位图图像轮廓和透明色创建Region,BitmapToRegion就用于完成Bitmap到Region的转换,程序再将这个Region付值给窗体的Region属性以完成不规则窗体的创建。

  1. public void SetBackgroundBitmap(Image image, Color transparencyColor)  
  2. {  
  3. BackgroundBitmap = new Bitmap(image);  
  4. Width = BackgroundBitmap.Width;  
  5. Height = BackgroundBitmap.Height;  
  6. Region = BitmapToRegion(BackgroundBitmap, transparencyColor);  
  7. }  
  8.  
  9. public Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)  
  10. {  
  11. if (bitmap == null)  
  12. throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");  
  13.  
  14. int height = bitmap.Height;  
  15. int width = bitmap.Width;  
  16. GraphicsPath path = new GraphicsPath();  
  17. for (int j = 0; j < height; j++)  
  18. for (int i = 0; i < width; i++)  
  19. {  
  20. if (bitmap.GetPixel(i, j) == transparencyColor)  
  21. continue;  
  22. int x0 = i;  
  23. while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))  
  24. i++;  
  25. path.AddRectangle(new Rectangle(x0, j, i - x0, 1));  
  26. }  
  27. Region region = new Region(path);  
  28. path.Dispose();  
  29. return region;  

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

  1. protected override void OnPaintBackground(PaintEventArgs e)  
  2. {  
  3. Graphics grfx = e.Graphics;  
  4. grfx.PageUnit = GraphicsUnit.Pixel;  
  5. Graphics offScreenGraphics;  
  6. Bitmap offscreenBitmap;  
  7. offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);  
  8. offScreenGraphics = Graphics.FromImage(offscreenBitmap);  
  9. if (BackgroundBitmap != null)  
  10. {  
  11. offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, 
    BackgroundBitmap.Width, BackgroundBitmap.Height);  
  12. }  
  13. DrawText(offScreenGraphics);  
  14. grfx.DrawImage(offscreenBitmap, 0, 0);  

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

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

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

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

【编辑推荐】

  1. C#与VB7比较详解
  2. C#连接Access浅析
  3. C#创建XML Web services学习经验
  4. C# Windows应用程序概述
  5. C# SmartPhone程序学习笔记
责任编辑:佚名 来源: 赛迪网
相关推荐

2009-08-20 17:13:37

C# FileSyst

2009-08-28 16:50:25

C# PromptPo

2009-08-25 16:03:51

C# SQLDMO对象

2009-08-31 09:44:23

C# Employee

2009-08-26 10:34:59

C# Hashtabl

2009-09-02 15:41:21

C# HTTPWebR

2009-08-19 17:12:18

C# Connecti

2009-08-12 11:24:25

C# String对象

2009-08-25 10:08:39

C# MyData对象

2009-08-31 09:37:09

C# Employee

2009-08-28 10:14:45

C#内存泄露

2009-09-03 16:58:49

C#内存管理

2009-08-20 11:01:51

C#操作内存

2009-08-18 09:06:41

C#对象和集合

2009-09-02 16:36:37

C#调用Excel对象

2009-08-13 13:31:13

锁定对象C# Monitor类

2009-08-18 11:32:24

C# FTP WebR

2009-08-10 13:40:46

创建C# COM对象

2009-09-04 14:01:30

C#存储BLOB对象

2009-08-20 17:22:45

C# FileSyst
点赞
收藏

51CTO技术栈公众号