博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转:不规则按钮实现
阅读量:4543 次
发布时间:2019-06-08

本文共 3201 字,大约阅读时间需要 10 分钟。

按钮控件在windows forms总是我们使用最多的控件之一了,可是你真的了解它吗?除了修改它的text属性以及该name的名字和增加事件处理的代码你还了解其他吗?

假如有这样一个需求,我们要做一个不规则的按钮,我们该具体如何实现呢?

好吧,别的先不多说了,我直接上实例代码。

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;namespace Borwer{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        ///                  /// 返回指定图片中的非透明区域;                 ///                  /// 位图                 /// alpha 小于等于该值的为透明                 /// 
public static GraphicsPath GetNoneTransparentRegion(Bitmap img, byte alpha) { int height = img.Height; int width = img.Width; int xStart, xEnd; GraphicsPath grpPath = new GraphicsPath(); for (int y = 0; y < height; y++) { //逐行扫描 for (int x = 0; x < width; x++) { //略过连续透明的部分 while (x < width && img.GetPixel(x, y).A <= alpha) { x++; } //不透明部分 xStart = x; while (x < width && img.GetPixel(x, y).A > alpha) { x++; } xEnd = x; if (img.GetPixel(x - 1, y).A > alpha) { grpPath.AddRectangle(new Rectangle(xStart, y, xEnd - xStart, 1)); } } } return grpPath; } private void Form1_Load(object sender, EventArgs e) { pictbox.Visible = false; } private void button1_Paint(object sender, PaintEventArgs e) { Bitmap img = (Bitmap)pictbox.Image; GraphicsPath grapth = GetNoneTransparentRegion(img, 10); button1.Region = new Region(grapth); //要显示的图片设置为窗体背景; button1.BackgroundImage = pictbox.Image; button1.BackgroundImageLayout = ImageLayout.Zoom; //在修改窗体尺寸之前设置窗体为无边框样式; button1.Width = pictbox.Image.Width; button1.Height = pictbox.Image.Height; } private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show("我是心状不规则按钮"); } private void button2_Click(object sender, System.EventArgs e) { MessageBox.Show("我是苹果不规则按钮"); } private void button2_Paint(object sender, PaintEventArgs e) { Bitmap img = (Bitmap)pictbox.Image; GraphicsPath grapth = GetNoneTransparentRegion(img, 10); button2.Region = new Region(grapth); //要显示的图片设置为窗体背景; button2.BackgroundImage = pictbox.Image; button2.BackgroundImageLayout = ImageLayout.Zoom; //在修改窗体尺寸之前设置窗体为无边框样式; button2.Width = pictbox.Image.Width; button2.Height = pictbox.Image.Height; } }}

运行结果:

不规则按钮效果

上面的弹出框是点击心状按钮的回应,怎么样效果实现了吧,你可以选择一些更多的图片去玩玩看看,很爽的效果。

 除非注明,木杉博客文章均为原创并采用协议进行授权原创文章,转载请注明: 转载自

转载链接地址:

转载于:https://www.cnblogs.com/lusunqing/p/3449610.html

你可能感兴趣的文章
E20170624-ts
查看>>
linux shell实现随机数多种方法(date,random,uuid)
查看>>
页面上有tab,如何点击加载更多?
查看>>
bash&nbsp;shell笔记1&nbsp;脚本基础知识
查看>>
html dl dt dd标签元素语法结构与使用
查看>>
OC单元测试框架-----Google开源单元测试框架Google Test(gtest)
查看>>
QLineEdit IP地址校验
查看>>
Using Bing Search Service over SOAP Protocol in an ASP.NET Web Application
查看>>
2016年秋季-《UML大战需求分析》-个人阅读计划
查看>>
cocos2d-x多线程解析域名
查看>>
LeetCode:Verify Preorder Serialization of a Binary Tree
查看>>
4_something
查看>>
ACM数论之旅12---康托展开((*゚▽゚*)装甲展开,主推进器启动,倒计时3,2,1......)...
查看>>
使用WebHelper调用Asp.net WebAPI
查看>>
磁盘创建
查看>>
linux发行版
查看>>
如何求解灰度共生矩阵
查看>>
Vue Router的官方示例改造
查看>>
算法第5章上机实践报告
查看>>
css 断行省略号,隐藏,fixed定位
查看>>