GDIUtil.cs
上传用户:nnpulika
上传日期:2013-02-15
资源大小:597k
文件大小:6k
源码类别:

状态条

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using UtilityLibrary.Win32;
  5. namespace UtilityLibrary.General
  6. {
  7. /// <summary>
  8. /// Summary description for GDIUtil.
  9. /// </summary>
  10. public class GDIUtil
  11. {
  12. #region Constructors
  13. // We won't instatiate any object
  14. private GDIUtil()
  15. {
  16. }
  17. #endregion
  18. #region Methods
  19. static  public void Draw3DRect(Graphics g, Rectangle rc, Color topLeft, Color bottomRight)
  20. {
  21. Draw3DRect(g, rc.Left, rc.Top, rc.Width, rc.Height,  topLeft, bottomRight);
  22. }
  23. static  public void Draw3DRect(Graphics g, int x, int y, int width, int height, Color topLeft, Color bottomRight)
  24. {
  25. g.FillRectangle(new SolidBrush(topLeft), x, y, width - 1, 1);
  26. g.FillRectangle(new SolidBrush(topLeft), x, y, 1, height - 1);
  27. g.FillRectangle(new SolidBrush(bottomRight), x + width, y, -1, height);
  28. g.FillRectangle(new SolidBrush(bottomRight), x, y + height, width, -1);
  29. }
  30. static public void StrechBitmap(Graphics gDest, Rectangle rcDest, Bitmap bitmap)
  31. {
  32. // Draw From bitmap
  33. IntPtr hDCTo = gDest.GetHdc();
  34. WindowsAPI.SetStretchBltMode(hDCTo, StrechModeFlags.HALFTONE);
  35. IntPtr hDCFrom = WindowsAPI.CreateCompatibleDC(hDCTo);
  36. IntPtr hOldFromBitmap = WindowsAPI.SelectObject(hDCFrom, bitmap.GetHbitmap());
  37. WindowsAPI.StretchBlt(hDCTo, rcDest.Left , rcDest.Top, rcDest.Width, rcDest.Height, hDCFrom, 
  38. 0 , 0, bitmap.Width, bitmap.Height, PatBltTypes.SRCCOPY);
  39.                 
  40. // Cleanup
  41. WindowsAPI.SelectObject(hDCFrom, hOldFromBitmap);
  42. gDest.ReleaseHdc(hDCTo);
  43. }
  44. static public void StrechBitmap(Graphics gDest, Rectangle rcDest, Rectangle rcSource, Bitmap bitmap)
  45. {
  46. // Draw From bitmap
  47. IntPtr hDCTo = gDest.GetHdc();
  48. WindowsAPI.SetStretchBltMode(hDCTo, StrechModeFlags.COLORONCOLOR);
  49. IntPtr hDCFrom = WindowsAPI.CreateCompatibleDC(hDCTo);
  50. IntPtr hOldFromBitmap = WindowsAPI.SelectObject(hDCFrom, bitmap.GetHbitmap());
  51. WindowsAPI.StretchBlt(hDCTo, rcDest.Left , rcDest.Top, rcDest.Width, rcDest.Height, hDCFrom, 
  52. rcSource.Left , rcSource.Top, rcSource.Width, rcSource.Height, PatBltTypes.SRCCOPY);
  53.                 
  54. // Cleanup
  55. WindowsAPI.SelectObject(hDCFrom, hOldFromBitmap);
  56. WindowsAPI.DeleteDC(hDCFrom);
  57. gDest.ReleaseHdc(hDCTo);
  58. }
  59. static public Bitmap GetStrechedBitmap(Graphics gDest, Rectangle rcDest, Bitmap bitmap)
  60. {
  61. // Draw To bitmap
  62. Bitmap newBitmap = new Bitmap(rcDest.Width, rcDest.Height);
  63. Graphics gBitmap = Graphics.FromImage(newBitmap); 
  64. IntPtr hDCTo = gBitmap.GetHdc();
  65. WindowsAPI.SetStretchBltMode(hDCTo, StrechModeFlags.COLORONCOLOR);
  66. IntPtr hDCFrom = WindowsAPI.CreateCompatibleDC(hDCTo);
  67. IntPtr hOldFromBitmap = WindowsAPI.SelectObject(hDCFrom, bitmap.GetHbitmap());
  68. WindowsAPI.StretchBlt(hDCTo, rcDest.Left , rcDest.Top, rcDest.Width, rcDest.Height, hDCFrom, 
  69. 0 , 0, bitmap.Width, bitmap.Height, PatBltTypes.SRCCOPY);
  70.                 
  71. // Cleanup
  72. WindowsAPI.SelectObject(hDCFrom, hOldFromBitmap);
  73. WindowsAPI.DeleteDC(hDCFrom);
  74. gBitmap.ReleaseHdc(hDCTo);
  75. return newBitmap;
  76. }
  77. static public Bitmap GetTileBitmap(Rectangle rcDest, Bitmap bitmap)
  78. {
  79. Bitmap tiledBitmap = new Bitmap(rcDest.Width, rcDest.Height);
  80. using ( Graphics g = Graphics.FromImage(tiledBitmap) )
  81. {
  82. for ( int i = 0; i < tiledBitmap.Width; i += bitmap.Width )
  83. {
  84. for ( int j = 0; j < tiledBitmap.Height; j += bitmap.Height )
  85. {
  86. g.DrawImage(bitmap, new Point(i, j));
  87. }
  88. }
  89. }
  90. return tiledBitmap;
  91. }
  92. static public void BlitBitmap(Graphics gDest, Rectangle rcDest, int xSrc, int ySrc, IntPtr handleBitmap)
  93. {
  94. // Use this function when you want faster painting of a image. Specially
  95. // if that image is being painting many time in a short period of time
  96. // --like when dragging the thumb of a scrollbar and painting an image in 
  97. // real time-- Having the handle of the bitmap saved somewhere avoids
  98. // recreating the handle every time the image is drawn as GDI+ does it 
  99. // every time it paints the image
  100. IntPtr hDCTo = gDest.GetHdc();
  101. IntPtr hDCSrc = WindowsAPI.CreateCompatibleDC(hDCTo);
  102. IntPtr hOldFromBitmap = WindowsAPI.SelectObject(hDCSrc, handleBitmap);
  103. WindowsAPI.BitBlt(hDCTo, rcDest.Left, rcDest.Top, rcDest.Width, rcDest.Height, hDCSrc, xSrc, ySrc, PatBltTypes.SRCCOPY);
  104. // Cleanup
  105. WindowsAPI.SelectObject(hDCSrc, hOldFromBitmap);
  106. WindowsAPI.DeleteDC(hDCSrc);
  107. gDest.ReleaseHdc(hDCTo);
  108. }
  109. static public void DrawArrowGlyph(Graphics g, Rectangle rc, ArrowGlyph arrowGlyph, Brush brush)
  110. {
  111. // Draw arrow glyph with the default 
  112. // size of 5 pixel wide and 3 pixel high
  113. DrawArrowGlyph(g, rc, 5, 3, arrowGlyph, brush);
  114. }
  115. static public void DrawArrowGlyph(Graphics g, Rectangle rc, int arrowWidth, int arrowHeight, ArrowGlyph arrowGlyph, Brush brush)
  116. {
  117. // Tip: use an odd number for the arrowWidth and 
  118. // arrowWidth/2+1 for the arrowHeight 
  119. // so that the arrow gets the same pixel number
  120. // on the left and on the right and get symetrically painted
  121. Point[] pts = new Point[3];
  122. int yMiddle = rc.Top + rc.Height/2-arrowHeight/2+1;
  123. int xMiddle = rc.Left + rc.Width/2;
  124. if ( arrowGlyph == ArrowGlyph.Up )
  125. {
  126. pts[0] = new Point(xMiddle, yMiddle-2);
  127. pts[1] = new Point(xMiddle-arrowWidth/2-1, yMiddle+arrowHeight-1);
  128. pts[2] = new Point(xMiddle+arrowWidth/2+1,  yMiddle+arrowHeight-1);
  129. }
  130. else if ( arrowGlyph == ArrowGlyph.Down )
  131. {
  132. //yMiddle -= 1;
  133. pts[0] = new Point(xMiddle-arrowWidth/2, yMiddle);
  134. pts[1] = new Point(xMiddle+arrowWidth/2+1,  yMiddle);
  135. pts[2] = new Point(xMiddle, yMiddle+arrowHeight);
  136. }
  137. else if ( arrowGlyph == ArrowGlyph.Left )
  138. {
  139. yMiddle = rc.Top + rc.Height/2;
  140. pts[0] = new Point(xMiddle-arrowHeight/2,  yMiddle);
  141. pts[1] = new Point(pts[0].X+arrowHeight, yMiddle-arrowWidth/2-1);
  142. pts[2] = new Point(pts[0].X+arrowHeight,  yMiddle+arrowWidth/2+1);
  143. }
  144. else if ( arrowGlyph == ArrowGlyph.Right )
  145. {
  146. yMiddle = rc.Top + rc.Height/2;
  147. pts[0] = new Point(xMiddle+arrowHeight/2+1,  yMiddle);
  148. pts[1] = new Point(pts[0].X-arrowHeight, yMiddle-arrowWidth/2-1);
  149. pts[2] = new Point(pts[0].X-arrowHeight,  yMiddle+arrowWidth/2+1);
  150. }
  151. g.FillPolygon(brush, pts);
  152. }
  153. #endregion
  154. }
  155. }