窗体边框样式设为NONE

 

 
  
  1. private void Form_Load(object sender, EventArgs e) 
  2.     TransparencyKey = Color.White; 
  3.     BackgroundImage = Bitmap.FromFile("p_w_picpaths/bg.bmp"); 
  4.     BitmapRegion BitmapRegion = new BitmapRegion();//此为生成不规则窗体和控件的类 
  5.     BitmapRegion.CreateControlRegion(thisnew Bitmap("p_w_picpaths/bg.bmp"));  

 

BitmapRegion.cs:

 

 
  
  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.  
  9. namespace MyWinForm 
  10.     public class BitmapRegion 
  11.     { 
  12.         public BitmapRegion() 
  13.         { } 
  14.  
  15.         /// <summary>  
  16.  
  17.         /// Create and apply the region on the supplied control 
  18.  
  19.         /// 创建支持位图区域的控件(目前有button和form) 
  20.  
  21.         /// </summary>  
  22.  
  23.         /// <param name="control">The Control object to apply the region to控件</param>  
  24.  
  25.         /// <param name="bitmap">The Bitmap object to create the region from位图</param>  
  26.  
  27.         public static void CreateControlRegion(Control control, Bitmap bitmap) 
  28.         { 
  29.             // Return if control and bitmap are null 
  30.  
  31.             //判断是否存在控件和位图 
  32.  
  33.             if (control == null || bitmap == null
  34.                 return
  35.  
  36.             // Set our control''s size to be the same as the bitmap 
  37.  
  38.             //设置控件大小为位图大小 
  39.  
  40.             control.Width = bitmap.Width; 
  41.             control.Height = bitmap.Height; 
  42.             // Check if we are dealing with Form here  
  43.  
  44.             //当控件是form时 
  45.  
  46.             if (control is System.Windows.Forms.Form) 
  47.             { 
  48.                 // Cast to a Form object 
  49.  
  50.                 //强制转换为FORM 
  51.  
  52.                 Form form = (Form)control; 
  53.                 // Set our form''s size to be a little larger that the  bitmap just  
  54.  
  55.                 // in case the form''s border style is not set to none in the first place  
  56.  
  57.                 //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点 
  58.  
  59.                 form.Width = control.Width; 
  60.                 form.Height = control.Height; 
  61.                 // No border  
  62.  
  63.                 //没有边界 
  64.  
  65.                 form.FormBorderStyle = FormBorderStyle.None; 
  66.                 // Set bitmap as the background p_w_picpath  
  67.  
  68.                 //将位图设置成窗体背景图片 
  69.  
  70.                 form.BackgroundImage = bitmap; 
  71.                 // Calculate the graphics path based on the bitmap supplied  
  72.  
  73.                 //计算位图中不透明部分的边界 
  74.  
  75.                 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); 
  76.                 // Apply new region  
  77.  
  78.                 //应用新的区域 
  79.  
  80.                 form.Region = new Region(graphicsPath); 
  81.             } 
  82.             // Check if we are dealing with Button here  
  83.  
  84.             //当控件是button时 
  85.  
  86.             else if (control is System.Windows.Forms.Button) 
  87.             { 
  88.                 // Cast to a button object  
  89.  
  90.                 //强制转换为 button 
  91.  
  92.                 Button button = (Button)control; 
  93.                 // Do not show button text  
  94.  
  95.                 //不显示button text 
  96.  
  97.                 button.Text = ""
  98.  
  99.                 // Change cursor to hand when over button  
  100.  
  101.                 //改变 cursor的style 
  102.  
  103.                 button.Cursor = Cursors.Hand; 
  104.                 // Set background p_w_picpath of button  
  105.  
  106.                 //设置button的背景图片 
  107.  
  108.                 button.BackgroundImage = bitmap; 
  109.  
  110.                 // Calculate the graphics path based on the bitmap supplied  
  111.  
  112.                 //计算位图中不透明部分的边界 
  113.  
  114.                 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); 
  115.                 // Apply new region  
  116.  
  117.                 //应用新的区域 
  118.  
  119.                 button.Region = new Region(graphicsPath); 
  120.             } 
  121.         } 
  122.         /// <summary>  
  123.  
  124.         /// Calculate the graphics path that representing the figure in the bitmap  
  125.  
  126.         /// excluding the transparent color which is the top left pixel.  
  127.  
  128.         /// //计算位图中不透明部分的边界 
  129.  
  130.         /// </summary>  
  131.  
  132.         /// <param name="bitmap">The Bitmap object to calculate our graphics path from</param>  
  133.  
  134.         /// <returns>Calculated graphics path</returns>  
  135.  
  136.         private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap) 
  137.         { 
  138.             // Create GraphicsPath for our bitmap calculation  
  139.  
  140.             //创建 GraphicsPath 
  141.  
  142.             GraphicsPath graphicsPath = new GraphicsPath(); 
  143.             // Use the top left pixel as our transparent color  
  144.  
  145.             //使用左上角的一点的颜色作为我们透明色 
  146.  
  147.             Color colorTransparent = bitmap.GetPixel(0, 0); 
  148.             // This is to store the column value where an opaque pixel is first found.  
  149.  
  150.             // This value will determine where we start scanning for trailing opaque pixels. 
  151.  
  152.             //第一个找到点的X 
  153.  
  154.             int colOpaquePixel = 0; 
  155.             // Go through all rows (Y axis)  
  156.  
  157.             // 偏历所有行(Y方向) 
  158.  
  159.             for (int row = 0; row < bitmap.Height; row++) 
  160.             { 
  161.                 // Reset value  
  162.  
  163.                 //重设 
  164.  
  165.                 colOpaquePixel = 0; 
  166.                 // Go through all columns (X axis)  
  167.  
  168.                 //偏历所有列(X方向) 
  169.  
  170.                 for (int col = 0; col < bitmap.Width; col++) 
  171.                 { 
  172.                     // If this is an opaque pixel, mark it and search for anymore trailing behind  
  173.  
  174.                     //如果是不需要透明处理的点则标记,然后继续偏历 
  175.  
  176.                     if (bitmap.GetPixel(col, row) != colorTransparent) 
  177.                     { 
  178.                         // Opaque pixel found, mark current position 
  179.  
  180.                         //记录当前 
  181.  
  182.                         colOpaquePixel = col; 
  183.                         // Create another variable to set the current pixel position  
  184.  
  185.                         //建立新变量来记录当前点 
  186.  
  187.                         int colNext = col; 
  188.                         // Starting from current found opaque pixel, search for anymore opaque pixels  
  189.  
  190.                         // trailing behind, until a transparent   pixel is found or minimum width is reached  
  191.  
  192.                         ///从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度  
  193.  
  194.                         for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++) 
  195.                             if (bitmap.GetPixel(colNext, row) == colorTransparent) 
  196.                                 break
  197.                         // Form a rectangle for line of opaque   pixels found and add it to our graphics path  
  198.  
  199.                         //将不透明点加到graphics path 
  200.  
  201.                         graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1)); 
  202.                         // No need to scan the line of opaque pixels just found  
  203.  
  204.                         col = colNext; 
  205.                     } 
  206.                 } 
  207.             } 
  208.             // Return calculated graphics path  
  209.  
  210.             return graphicsPath; 
  211.         } 
  212.     }