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

2019-12-30 11:01:57丽君

实现步骤

如下:

1. 建立winform应用程序

2. 找到窗体的Load事件,双击进行编辑

3. 编写方法,主要的代码如下:

 

 
  1. class BitmapRegion   {  
  2. public BitmapRegion()   { }  
  3.    
  4. /// <summary>   /// Create and apply the region on the supplied control  
  5. /// 创建支持位图区域的控件(目前有button和form)   /// </summary>  
  6. /// <param name="control">The Control object to apply the region to控件</param>   /// <param name="bitmap">The Bitmap object to create the region from位图</param>  
  7. public static void CreateControlRegion(Control control, Bitmap bitmap)   {  
  8. // Return if control and bitmap are null   //判断是否存在控件和位图  
  9. if (control == null || bitmap == null)   return;  
  10.   // Set our control''s size to be the same as the bitmap  
  11. //设置控件大小为位图大小   control.Width = bitmap.Width;  
  12. control.Height = bitmap.Height;   // Check if we are dealing with Form here  
  13. //当控件是form时   if (control is System.Windows.Forms.Form)  
  14. {   // Cast to a Form object  
  15. //强制转换为FORM   Form form = (Form)control;  
  16. // Set our form''s size to be a little larger that the bitmap just   // in case the form''s border style is not set to none in the first place  
  17. //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点   form.Width = control.Width;  
  18. form.Height = control.Height;   // No border  
  19. //没有边界   form.FormBorderStyle = FormBorderStyle.None;