C#:生成基于WinForm的不规则窗体或控件
2011-10-31 18:10
507 查看
/* ---------------------------------------------------------- 文件名称:ImageControl.cs 作者:秦建辉 MSN:splashcn@msn.com QQ:36748897 博客:http://blog.csdn.net/jhqin 开发环境: Visual Studio V2010 .NET Framework 4 Client Profile 版本历史: V1.1 2011年11月01日 接口优化 V1.0 2010年08月02日 生成基于WinForm的不规则窗体或控件 ------------------------------------------------------------ */ using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace Splash.Drawing { /// <summary> /// 生成基于WinForm的不规则窗体或控件 /// </summary> public static class ImageControl { /// <summary> /// 设置控件区域 /// </summary> /// <param name="control">要设置的控件</param> /// <param name="bitmap">要使用的图像</param> /// <remarks>Control的扩展方法</remarks> public static void SetRegion(this Control control, Bitmap bitmap) { // 判断是否存在控件和位图 if (control == null || bitmap == null) return; // 设置控件大小为位图大小 control.Size = bitmap.Size; if (control is Form) { // 当控件是Form时 // 强制转换为Form Form ImageForm = control as Form; // 当Form的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点 ImageForm.Size = control.Size; // 去掉边界 ImageForm.FormBorderStyle = FormBorderStyle.None; // 将位图设置成窗体背景图片 ImageForm.BackgroundImage = bitmap; // 计算位图中不透明部分的边界 GraphicsPath graphicsPath = CreateGraphicsPath(bitmap); // 应用新的区域 ImageForm.Region = new Region(graphicsPath); } else if (control is Button) { // 当控件是Button时 // 强制转换为Button Button ImageButton = control as Button; // 不显示Button Text ImageButton.Text = System.String.Empty; // 设置Button的背景图片 ImageButton.BackgroundImage = bitmap; // 计算位图中不透明部分的边界 GraphicsPath graphicsPath = CreateGraphicsPath(bitmap); // 应用新的区域 ImageButton.Region = new Region(graphicsPath); } } /// <summary> /// 创建图层路径 /// </summary> /// <param name="bitmap">用于创建图层路径的位图</param> /// <returns>图层路径</returns> /// <remarks>Bitmap的扩展方法</remarks> public static GraphicsPath CreateGraphicsPath(this Bitmap bitmap) { return CreateGraphicsPath(bitmap, Color.Empty); } /// <summary> /// 创建图层路径 /// </summary> /// <param name="bitmap">用于创建图层路径的位图</param> /// <param name="colorTransparent">透明色,如果为Color.Empty,则使用第一点作为透明色</param> /// <returns>图层路径</returns> /// <remarks>Bitmap的扩展方法</remarks> public static GraphicsPath CreateGraphicsPath(this Bitmap bitmap, Color colorTransparent) { // 创建GraphicsPath GraphicsPath graphicsPath = new GraphicsPath(); if (colorTransparent == Color.Empty) { // 使用左上角第一点的颜色作为透明色 colorTransparent = bitmap.GetPixel(0, 0); } // 遍历所有行(Y方向) for (int y = 0; y < bitmap.Height; y++) { // 遍历所有列(X方向) for (int x = 0; x < bitmap.Width; x++) { // 如果是不需要透明处理的点则标记,然后继续偏历 if (bitmap.GetPixel(x, y) != colorTransparent) { // 记录当前 int xBegin = x; int xEnd = x + 1; // 从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度 for (; xEnd < bitmap.Width; xEnd++) { if (bitmap.GetPixel(xEnd, y) == colorTransparent) { break; } } // 将不透明点加到GraphicsPath graphicsPath.AddRectangle(new Rectangle(xBegin, y, xEnd - xBegin, 1)); x = xEnd; } } } return graphicsPath; } } }
相关文章推荐
- c# winform动态生成窗体及控件,并使用控件事件
- 【C#基础】之WinForm窗体常用控件的属性、方法及事件简介
- C#WinForm制作异形窗体/控件
- C#创建不规则窗体和控件
- c# winform 打印 窗体 及 窗体控件内容 的 初级尝试
- 改进C# WinForm窗体及其控件的自适应
- C# Winform 左侧菜单右边显示、子窗体显示在主窗体(子窗体当控件添加到Panel)
- C# winform中不规则窗体制作的解决方案
- C# WinForm控件随窗体大小改变自动适应改变
- 报表源代码 winform c# -基于DexExpressXtraReport的打印报表,后台生成报表,winform版本,采用C#语言封装,比金质通更强大,现在开放源代码 .
- C#WinForm制作异形窗体/控件
- c#中跨线程调用windows窗体控件 .我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。
- ZedGraph:一个同时支持WinForm和WebForm的开源图表控件(基于LGPL协议,.NET 2.0 C#源代码)
- C#中winform窗体的美化控件 iriskin(一)
- 基于C#的WinForm中DataGridView控件操作汇总
- C# winform中一个类中如何调用另一个窗体的控件或方法
- C# Winform MDI窗体,父窗体控件覆盖子窗体的解决办法
- C# WinForm 控件在窗体中动态居中 创建、删除控件及对其赋值
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变