DrawHelper.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:3k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.Windows.Forms;
  6. namespace WeifenLuo.WinFormsUI.Docking
  7. {
  8. internal static class DrawHelper
  9. {
  10.         public static Point RtlTransform(Control control, Point point)
  11.         {
  12.             if (control.RightToLeft != RightToLeft.Yes)
  13.                 return point;
  14.             else
  15.                 return new Point(control.Right - point.X, point.Y);
  16.         }
  17.         public static Rectangle RtlTransform(Control control, Rectangle rectangle)
  18.         {
  19.             if (control.RightToLeft != RightToLeft.Yes)
  20.                 return rectangle;
  21.             else
  22.                 return new Rectangle(control.ClientRectangle.Right - rectangle.Right, rectangle.Y, rectangle.Width, rectangle.Height);
  23.         }
  24.         public static GraphicsPath GetRoundedCornerTab(GraphicsPath graphicsPath, Rectangle rect, bool upCorner)
  25.         {
  26.             if (graphicsPath == null)
  27.                 graphicsPath = new GraphicsPath();
  28.             else
  29.                 graphicsPath.Reset();
  30.             
  31.             int curveSize = 6;
  32.             if (upCorner)
  33.             {
  34.                 graphicsPath.AddLine(rect.Left, rect.Bottom, rect.Left, rect.Top + curveSize / 2);
  35.                 graphicsPath.AddArc(new Rectangle(rect.Left, rect.Top, curveSize, curveSize), 180, 90);
  36.                 graphicsPath.AddLine(rect.Left + curveSize / 2, rect.Top, rect.Right - curveSize / 2, rect.Top);
  37.                 graphicsPath.AddArc(new Rectangle(rect.Right - curveSize, rect.Top, curveSize, curveSize), -90, 90);
  38.                 graphicsPath.AddLine(rect.Right, rect.Top + curveSize / 2, rect.Right, rect.Bottom);
  39.             }
  40.             else
  41.             {
  42.                 graphicsPath.AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom - curveSize / 2);
  43.                 graphicsPath.AddArc(new Rectangle(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize), 0, 90);
  44.                 graphicsPath.AddLine(rect.Right - curveSize / 2, rect.Bottom, rect.Left + curveSize / 2, rect.Bottom);
  45.                 graphicsPath.AddArc(new Rectangle(rect.Left, rect.Bottom - curveSize, curveSize, curveSize), 90, 90);
  46.                 graphicsPath.AddLine(rect.Left, rect.Bottom - curveSize / 2, rect.Left, rect.Top);
  47.             }
  48.             return graphicsPath;
  49.         }
  50. public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap)
  51. {
  52. return CalculateGraphicsPathFromBitmap(bitmap, Color.Empty);
  53. }
  54. // From http://edu.cnzz.cn/show_3281.html
  55. public static GraphicsPath CalculateGraphicsPathFromBitmap(Bitmap bitmap, Color colorTransparent) 
  56. GraphicsPath graphicsPath = new GraphicsPath(); 
  57. if (colorTransparent == Color.Empty)
  58. colorTransparent = bitmap.GetPixel(0, 0); 
  59. for(int row = 0; row < bitmap.Height; row ++) 
  60. int colOpaquePixel = 0;
  61. for(int col = 0; col < bitmap.Width; col ++) 
  62. if(bitmap.GetPixel(col, row) != colorTransparent) 
  63. colOpaquePixel = col; 
  64. int colNext = col; 
  65. for(colNext = colOpaquePixel; colNext < bitmap.Width; colNext ++) 
  66. if(bitmap.GetPixel(colNext, row) == colorTransparent) 
  67. break;
  68.  
  69. graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1)); 
  70. col = colNext; 
  71. return graphicsPath; 
  72. }
  73. }