成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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-12 11:24:25

C# String對象

2009-08-31 09:37:09

C# Employee

2009-08-25 10:08:39

C# MyData對象

2009-08-19 17:12:18

C# Connecti

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-28 10:14:45

C#內存泄露

2009-09-03 16:58:49

C#內存管理

2009-08-20 11:01:51

C#操作內存

2009-09-02 16:02:52

C#引用托管對象

2009-08-21 17:45:40

C#調用COM對象

2009-08-26 17:24:49

C# Mutex對象

2009-09-01 10:58:46

C#匿名類型對象

2009-08-03 11:32:49

C#調用COM對象

2009-09-03 17:21:51

C# VSProjec

2009-08-10 13:40:46

創建C# COM對象
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本精品久久 | 亚洲成人福利视频 | 91 在线| 亚洲国产欧美日韩 | 青青草原综合久久大伊人精品 | 91精品在线观看入口 | 日日干综合 | 久久久久久免费免费 | 久久久国产一区二区三区 | 欧美一级免费观看 | 久久久久久久一区二区三区 | 成人免费观看男女羞羞视频 | 成人精品一区二区三区中文字幕 | 国产精品视频一区二区三区, | 精品视频在线观看 | 6996成人影院网在线播放 | 在线成人www免费观看视频 | 久久精品成人 | 久久大陆| 国产真实精品久久二三区 | 欧美久久一级 | 久久久在线视频 | 亚洲精品一区在线观看 | 日日夜夜精品视频 | 中文字幕在线精品 | 成人免费xxxxx在线视频 | 久久久在线视频 | 亚洲综合中文字幕在线观看 | 精品国产伦一区二区三区观看说明 | 香蕉久久网 | 国产一区不卡在线观看 | 久久久亚洲 | 999免费观看视频 | 国产精品成人一区二区三区夜夜夜 | 成人国产一区二区三区精品麻豆 | 国产美女精品 | 日韩欧美在线观看视频 | 欧美一区二区三区精品免费 | 亚洲欧洲激情 | 久久精品a级毛片 | 自拍第一页 |