开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 9636|回复: 11
收起左侧

[C#源码] C#控件自绘

[复制链接]
发表于 2012-8-18 11:05:10 | 显示全部楼层 |阅读模式   四川省成都市
本帖最后由 Im922 于 2013-2-4 18:38 编辑

当我看到C#这个功能的时候,我简直是疯了!!!
易语言还易语言!!!
下面教大家如何用C#自己画个按钮!
第一步,看效果:

效果图

效果图

第二步:确保你有面向对象基础!(最重要)必须学会继承与重写基方法
第三步:准备图片!在右边有一个“Properties”文件夹,找到他的子文件夹“Resources”上传图片,再此写一个:
  1. using (你的解决方案名称).Properties;
复制代码
第五步,源代码:
  1. class mybutton : Button//自绘按钮,继承自Button
  2.     {
  3.         //注意override关键字意思为重写基方法
  4.         protected override void OnMouseDown(MouseEventArgs mevent)//鼠标按下的特效
  5.         {
  6.             //base.OnMouseDown(mevent);//这个是为了屏蔽按下事件
  7.             Graphics g = this.CreateGraphics();//声明一个Graphics类型的g为这个时间的Graphics
  8.             g.DrawImage(Resources.untitled2, new Rectangle(0, 0, Width, Height));/*第一个参数为调用Resources里的图片参数2指定大小,new Rectangle里面的参数为(我用易语言的说)(int 左边,int 顶边,int 宽度,int 高度) Width, Height是获取控件高度,前面两个零是让图片就画在按钮里*/
  9.             g.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width / 4, Height / 3); //画控件的文本
  10.            //参数一指定画什么文本,参数2是字体,参数三是颜色,参数4是左边,参数5是顶边(易语言表达)
  11.            //注:, Width / 4, Height / 3这两个是我计算过的,可以画在按钮中心!
  12.           //下同!
  13.         }
  14.         protected override void OnMouseUp(MouseEventArgs mevent)
  15.         {
  16.             //base.OnMouseUp(mevent);
  17.             CreateGraphics().DrawImage(Resources.untitled1, new Rectangle(0, 0, Width, Height));
  18.             Graphics g = this.CreateGraphics();
  19.             g.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width / 4, Height / 3);
  20.             EventArgs e = new EventArgs();//留神!鼠标抬起后必须执行按钮按下事件
  21.             base.OnClick(e);
  22.         }
  23.         protected override void OnMouseEnter(EventArgs e)//下面几个根据英文翻译应该就知道了
  24.         {
  25.             //base.OnMouseEnter(e);
  26.             CreateGraphics().DrawImage(Resources.untitled1, new Rectangle(0, 0, Width, Height));
  27.             Graphics g = this.CreateGraphics();
  28.             g.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width / 4, Height / 3);
  29.         }
  30.         protected override void OnMouseClick(MouseEventArgs e)
  31.         {
  32.             //base.OnMouseClick(e);
  33.             CreateGraphics().DrawImage(Resources.untitled2, new Rectangle(0, 0, Width, Height));
  34.             Graphics g = this.CreateGraphics();
  35.             g.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width / 4, Height / 3);      
  36.         }
  37.         protected override void OnPaint(PaintEventArgs pevent)//把初始状态画下来
  38.         {
  39.             //base.OnPaint(pevent);
  40.             pevent.Graphics.DrawImage(Resources.untitled, new Rectangle(0, 0, Width, Height));
  41.             pevent.Graphics.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width / 4, Height / 3);
  42.         }
  43.         protected override void OnMouseLeave(EventArgs e)
  44.         {
  45.             //base.OnMouseLeave(e);
  46.             CreateGraphics().DrawImage(Resources.untitled, new Rectangle(0, 0, Width, Height));
  47.             Graphics g = this.CreateGraphics();
  48.             g.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width / 4, Height / 3);
  49.         }
  50.     }
  51.     class mytextbox : TextBox//这个无视,没研究出来………………
  52.     {
  53.         protected override void OnPaint(PaintEventArgs e)
  54.         {
  55.             //base.OnPaint(e);
  56.             e.Graphics.DrawImage(Resources.untitled2, new Rectangle(0, 0, Width, Height));
  57.         }
  58.     }
  59.     class mylabel : Label//制作一个阴影特效按钮(在原来文字偏移一下画一个灰色的文字)
  60.     {
  61.         protected override void OnPaint(PaintEventArgs e)
  62.         {
  63.             //base.OnPaint(e);
  64.             int heightint = Convert.ToInt32(Height *0.4);
  65.             int widthint = Convert.ToInt32(Width /4.5);//获取高与宽(我打了草稿)
  66.             e.Graphics.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Black), Width /4, Height / 3);
  67.             e.Graphics.DrawString(Text, new Font("Arial", 10), new SolidBrush(Color.Gray), widthint,heightint);//画阴影(为此我打了很多草稿才算出来的在哪里画阴影合适)
  68.         }
  69.     }
复制代码
第五步:运行一下,正常的话工具箱里会出现你的控件
第六部,检验:你可以修改Text的属性看是不是正常显示,然后写一个事件来看看
  1. private void mybutton1_Click(object sender, EventArgs e)
  2.         {
  3.             MessageBox.Show("做到这里说明你成功了!!!");
  4.         }
复制代码
注:每个程序都有BUG,我也不能确保我的程序就没有BUG。如果没看懂,就请进精易论坛C#编程交流群:217229525
学好以上方法,就可以在C#完美自绘了(与其他方法操作差不多)

控件自绘源码.zip (125.85 KB, 下载次数: 80)

评分

参与人数 1好评 +1 精币 +20 收起 理由
啄啄 + 1 + 20 精彩文章希望继续努力

查看全部评分

结帖率:50% (5/10)
发表于 2013-5-6 17:39:31 | 显示全部楼层   湖南省岳阳市
看看 支持下
回复 支持 反对

使用道具 举报

结帖率:0% (0/2)
发表于 2013-1-2 01:01:38 | 显示全部楼层   四川省成都市
果断没看懂的说
回复 支持 反对

使用道具 举报

发表于 2012-11-22 20:22:45 | 显示全部楼层   湖北省襄阳市
牛人啊 ,需要学习这个
回复 支持 反对

使用道具 举报

发表于 2012-10-22 18:26:37 | 显示全部楼层   浙江省宁波市
Textbox 这个的确有点难弄,呵呵
回复 支持 反对

使用道具 举报

结帖率:100% (3/3)
发表于 2012-8-18 18:37:42 | 显示全部楼层   广东省中山市
前排支持
回复 支持 反对

使用道具 举报

结帖率:100% (19/19)
发表于 2012-8-18 11:26:09 | 显示全部楼层   山东省潍坊市
支持呀 ~~牛人~
回复 支持 反对

使用道具 举报

结帖率:67% (16/24)
发表于 2012-8-18 11:23:39 | 显示全部楼层   广东省广州市
真是好东西,以后发多些
回复 支持 反对

使用道具 举报

结帖率:0% (0/1)
发表于 2012-8-18 11:14:05 | 显示全部楼层   甘肃省定西市
牛啊,正在学习中
回复 支持 反对

使用道具 举报

发表于 2012-8-18 11:09:22 | 显示全部楼层   湖南省郴州市
正好在学习  看看
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表