C#异形窗体

简介:

 窗体边框样式设为NONE

 

 
  1. private void Form_Load(object sender, EventArgs e) 
  2.     TransparencyKey = Color.White; 
  3.     BackgroundImage = Bitmap.FromFile("images/bg.bmp"); 
  4.     BitmapRegion BitmapRegion = new BitmapRegion();//此为生成不规则窗体和控件的类 
  5.     BitmapRegion.CreateControlRegion(thisnew Bitmap("images/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 image  
  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 image 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.     } 

 




 本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/435591,如需转载请自行联系原作者

相关文章
|
4月前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
|
11月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
60 0
|
2月前
|
开发框架 数据可视化 C#
|
4月前
|
C#
C#如何实现窗体最小化到托盘
C#如何实现窗体最小化到托盘
42 0
|
4月前
|
JavaScript Linux C#
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
125 0
|
4月前
|
C# Windows
C#安装“Windows 窗体应用(.NET Framework)”
C#安装“Windows 窗体应用(.NET Framework)”
128 0
|
9月前
|
C# 数据安全/隐私保护
C# 窗体之间参数互相传递的两种方法与使用
C# 窗体之间参数互相传递的两种方法与使用
|
9月前
|
C# Kotlin
C#is、as关键字及获取当前活动窗体的实例
这篇日志记录一下C#中is关键字及as关键字的用法。 Is :判断检查对象是否与给定类型兼容 As :将对象转换为指定类型(强转),就跟(int )这样的用法是一样的。 获取当前窗体的活动子窗体。
48 0
|
C# 数据安全/隐私保护
ApeForms | C# WinForm窗体控件平滑减速运动
在桌面软件开发中,有时会需要控制窗体或控件移动以实现某些界面效果,比如幻灯片换页、侧面的展开栏等。 通常情况下我们会使用Timer以每隔一段时间修改一下坐标位置的方式来实现目标对象的位移效果,但通过这个方式实现的动效存在几个问题: 匀速运动效果生硬; 运动过程中不便灵活改变运动状态(如侧栏展开一半时令其收起); 动效多时需要创建多个Timer对象,不易管理且占用资源; ApeForms中为控件和窗体提供了平滑运动的扩展方法,很好的解决了这些问题。不仅是坐标的平滑运动,还有控件\窗体尺寸的平滑变化、透明度的平滑变化。允许在变化的中途随时更改目标坐标\尺寸\透明度,且使用共享的Timer
11348 1
ApeForms | C# WinForm窗体控件平滑减速运动
|
编译器 C# Windows
C# 编写 WinForm 窗体应用程序(第一期)
WinForm 是 Windows Form 的简称,是基于 .NET Framework 平台的客户端(PC软件)开发技术,一般使用 C# 编程。
C# 编写 WinForm 窗体应用程序(第一期)