C# 메모리 Graphics 객체

Windos 창은 배울 점이 많습니다. 여기서 우리는 주로 C#메모리Graphics 대상을 소개하는데 SetBackgroundBitmap 함수를 소개합니다.
틀림없이 대부분의 네티즌들이 QQ, MSN 등 채팅 프로그램을 사용한 적이 있을 것이다. 그들의 인터페이스는 상당히 화려하다. 특히 네티즌들이 인터넷에 접속하고 메시지 알림을 할 때 화면의 오른쪽 아래에서 떠다니는 창이 천천히 올라온다. 아름다우면서도 인성화된 것이다. 프로그래머로서 즐길 때 우리는 이것이 도대체 어떻게 실현된 것이냐고 묻지 않을 수 없다.본고는 C# 메모리Graphics 대상을 이용하여
Background Bitmap 함수는 먼저 창 배경 이미지를 Background Bitmap 변수에 저장한 다음 이 비트맵 이미지의 윤곽과 투명색에 따라 Region을 만듭니다. BitmapTo Region은 Bitmap에서 Region으로 전환하는 데 사용되며, 프로그램은 이 Region을 창의 Region 속성에 값을 지불하여 불규칙한 창을 만듭니다.
 
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 메소드에서 수행되며 다음과 같은 이중 버퍼 기술을 사용하여 그려집니다.
    
      
      
      
      
    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에 동시에 작용한다.알림 창의 표면에 그려야 하는 배경 그림 Background Bitmap을 C# 메모리 Graphics 대상에 그립니다. DrawText 함수는 텍스트의 크기와 범위에 따라 Graphics를 호출합니다.DrawString은 창의 특정 영역에 텍스트를 표시합니다.마지막으로Graphics를 호출합니다.DrawImage는 메모리에 그려진 그림을 알림 창 표면에 표시합니다.
    우리는 창의 마우스 조작을 포획해야 한다. 세 가지 조작이 여기서 진행된다. 첫째, 드래그 창 조작 처리, 둘째, 알림 창의 닫기 조작 처리, 내용 영역의 클릭 조작이다.세 가지 작업 모두 마우스의 현재 위치와 각 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. }  
    이 프로그램은 알림 창의 표시, 머무르기, 숨기기 작업을 잘 할 수 있고 간단한 스킨케어 메커니즘을 갖추고 있으며 이중 버퍼 그리기 기술을 사용한 후 창의 그리기가 매끄럽고 깜빡거리지 않도록 할 수 있다.

    좋은 웹페이지 즐겨찾기