日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#內(nèi)存Graphics對象

Windos窗體有很多值得學習的地方,這里我們主要介紹C#內(nèi)存Graphics對象,包括介紹SetBackgroundBitmap函數(shù)。

想必大部分網(wǎng)友都使用過QQ、MSN等聊天程序,它們的界面都相當華麗,尤其是當網(wǎng)友上線以及消息提示時會有一個浮動的窗體從屏幕的右下方緩慢升起,既美觀又人性化,作為程序員在享受的同時我們也不禁要問:這到底是怎么實現(xiàn)的呢?本文就利用C#內(nèi)存Graphics對象

SetBackgroundBitmap函數(shù)首先將窗體背景圖像保存到BackgroundBitmap變量中,然后根據(jù)該位圖圖像輪廓和透明色創(chuàng)建Region,BitmapToRegion就用于完成Bitmap到Region的轉(zhuǎn)換,程序再將這個Region付值給窗體的Region屬性以完成不規(guī)則窗體的創(chuàng)建。

 
 
 
  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方法中完成,而且利用了雙重緩沖區(qū)技術(shù)來進行繪制操作,代碼如下:

 
 
 
  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中,然后創(chuàng)建一個C#內(nèi)存Graphics對象offScreenGraphics和內(nèi)存位圖對象offscreenBitmap,將內(nèi)存位圖對象的引用付值給offScreenGraphics,這樣所有對offScreenGraphics的繪制操作也都同時作用于offscreenBitmap,這時就將需要繪制到通知窗體表面的背景圖像BackgroundBitmap繪制到C#內(nèi)存Graphics對象上,DrawText函數(shù)根據(jù)需要顯示文字的大小和范圍調(diào)用Graphics.DrawString將文字顯示在窗體的特定區(qū)域。***,調(diào)用Graphics.DrawImage將內(nèi)存中已經(jīng)繪制完成的圖像顯示到通知窗體表面。

我們還需要捕獲窗體的鼠標操作,有三個操作在這里進行,
1、處理拖動窗體操作
2、處理通知窗體的關(guān)閉操作
3、內(nèi)容區(qū)域的單擊操作。
三個操作都需要檢測鼠標的當前位置與每個Rectangle區(qū)域的包含關(guān)系,只要單擊落在特定區(qū)域我們就進行相應(yīng)的處理,代碼如下:

 
 
 
  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. //發(fā)送左鍵點擊的消息至該窗體(標題欄)  
  10. }  
  11. if (CloseBtnRectangle.Contains(e.Location)) //單擊Close按鈕關(guān)閉  
  12. {  
  13. this.Hide();  
  14. currentTop = 1;  
  15. }  
  16. if (ContentRectangle.Contains(e.Location )) //單擊內(nèi)容區(qū)域  
  17. {  
  18. System.Diagnostics.Process.Start("http://www.Rithia.com");  
  19. }  
  20. }  

該程序可以很好的進行通知窗體的顯示、停留和隱藏操作,并且具備簡單的換膚機制,在利用了雙重緩沖區(qū)繪圖技術(shù)后,可以保證窗體的繪制平滑且沒有閃爍。


網(wǎng)站標題:C#內(nèi)存Graphics對象
本文來源:http://m.5511xx.com/article/cdpdehg.html