C#创建不规则窗体的4种方式详解

2019-12-30 11:01:57丽君
  • // Set bitmap as the background image   //将位图设置成窗体背景图片  
  • form.BackgroundImage = bitmap;   // Calculate the graphics path based on the bitmap supplied  
  • //计算位图中不透明部分的边界   GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);  
  • // Apply new region   //应用新的区域  
  • form.Region = new Region(graphicsPath);   }  
  • // Check if we are dealing with Button here   //当控件是button时  
  • else if (control is System.Windows.Forms.Button)   {  
  • // Cast to a button object   //强制转换为 button  
  • Button button = (Button)control;   // Do not show button text  
  • //不显示button text   button.Text = "";  
  •   // Change cursor to hand when over button  
  • //改变 cursor的style   button.Cursor = Cursors.Hand;  
  • // Set background image of button   //设置button的背景图片  
  • button.BackgroundImage = bitmap;    
  • // Calculate the graphics path based on the bitmap supplied   //计算位图中不透明部分的边界  
  • GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);   // Apply new region  
  • //应用新的区域   button.Region = new Region(graphicsPath);  
  • }   }  
  • /// <summary>   /// Calculate the graphics path that representing the figure in the bitmap  
  • /// excluding the transparent color which is the top left pixel.   /// //计算位图中不透明部分的边界  
  • /// </summary>   /// <param name="bitmap">The Bitmap object to calculate our graphics path from</param>