f4946b6473382bad61d59df8dd5a44ce67e809ed.svn-base 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. namespace Core.LZMes.Client.UIM.comm
  9. {
  10. public class BitmapRegion
  11. {
  12. public BitmapRegion() { }
  13. /// <summary>
  14. /// 创建支持位图区域的控件
  15. /// </summary>
  16. /// <param name="control">控件</param>
  17. /// <param name="bitmap">位图</param>
  18. public static void CreateControlRegion(Control control, Bitmap bitmap)
  19. {
  20. //判断是否存在控件和位图
  21. if (control == null || bitmap == null)
  22. return;
  23. //设置控件大小为位图大小
  24. control.Width = bitmap.Width;
  25. control.Height = bitmap.Height;
  26. //当控件是FormByCode时
  27. if (control is System.Windows.Forms.Form)
  28. {
  29. //强制转换为FORM
  30. Form form = (Form)control;
  31. //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点
  32. form.Width = control.Width + 20;
  33. form.Height = control.Height + 35;
  34. //没有边界
  35. form.FormBorderStyle = FormBorderStyle.None;
  36. //将位图设置成窗体背景图片
  37. form.BackgroundImage = bitmap;
  38. //计算位图中不透明部分的边界
  39. GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
  40. //应用新的区域
  41. form.Region = new Region(graphicsPath);
  42. }
  43. //当控件是Button时
  44. else if (control is System.Windows.Forms.Button)
  45. {
  46. //强制转换为Button
  47. Button button = (Button)control;
  48. //不显示button Text
  49. button.Text = "";
  50. //改变cursor 的 style
  51. button.Cursor = Cursors.Hand;
  52. //设置Button的背景图片
  53. button.BackgroundImage = bitmap;
  54. //计算位图中不透明部分的边界
  55. GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
  56. //应用新的区域
  57. button.Region = new Region(graphicsPath);
  58. }
  59. //当控件是Picture时
  60. else if (control is System.Windows.Forms.PictureBox)
  61. {
  62. //强制转换为Button
  63. PictureBox picBox = (PictureBox)control;
  64. //设置Button的背景图片
  65. picBox.BackgroundImage = bitmap;
  66. //计算位图中不透明部分的边界
  67. GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
  68. //应用新的区域
  69. picBox.Region = new Region(graphicsPath);
  70. }
  71. }
  72. /// <summary>
  73. /// 计算位图中不透明部分的边界
  74. /// </summary>
  75. /// <param name="bitmap"></param>
  76. /// <returns>GraphicsPath</returns>
  77. private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
  78. {
  79. GraphicsPath graphicsPath = new GraphicsPath();
  80. //使用左上角的一点颜色来作为我们透明的颜色
  81. Color colorTransparent = bitmap.GetPixel(0, 0);
  82. //第一个找到点的x
  83. int colOpaquePixel = 0;
  84. //遍历所有行(Y方向)
  85. for (int row = 0; row < bitmap.Height; row++)
  86. {
  87. //重设
  88. colOpaquePixel = 0;
  89. //遍历所有列(X方向)
  90. for (int col = 0; col < bitmap.Width; col++)
  91. {
  92. //如果是不需要透明处理的点则标记,然后继续偏历
  93. if (bitmap.GetPixel(col, row) != colorTransparent)
  94. {
  95. //记录当前
  96. colOpaquePixel = col;
  97. //建立新变量来记录当前点
  98. int colNext = col;
  99. //从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度
  100. for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
  101. {
  102. if (bitmap.GetPixel(colNext, row) == colorTransparent)
  103. break;
  104. }
  105. //将不透明点加到graphics path
  106. graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
  107. col = colNext;
  108. }
  109. }
  110. }
  111. return graphicsPath;
  112. }
  113. }
  114. }