1.看图

可以实现MouseDown改变背景颜色或背景图片。

遗憾是没有实现键盘触发按钮事件。

2.选择继承自Control基类

  public class ImageButton : Control

3.创建几个枚举类型

        public enum TextAlignStyle
{
TopCenter,
MiddleCenter,
BottonCenter,
} public enum PictureLocationStyle
{
TopCenter,
MiddleCenter,
BottonCenter,
} public enum BorderStyle
{
None,
Single,
}

4.设置字段及其默认值

        Image backgroundImage;//背景图片
Image pressedImage;//鼠标按下ImageButton时的图片
bool pressed = false;//ImageButton是否按下,true表示ImageButton按下
Color pressBackColor = SystemColors.Control;//鼠标按下时背景颜色
Color preBackColor = SystemColors.Control;//原来的背景颜色
TextAlignStyle textAlign = TextAlignStyle.BottonCenter;//文本的位置
PictureLocationStyle pictureLocation = PictureLocationStyle.TopCenter;//图片的位置
BorderStyle buttonBorder = BorderStyle.Single;//默认单线条边框
Color borderColor = SystemColors.Control;//默认边框颜色

5.设置属性

        //背景图片属性
public Image BackgroundImage
{
get { return backgroundImage; }
set { backgroundImage = value; this.Invalidate(); }
} // 鼠标按下ImageButton时的图片
public Image PressedImage
{
get { return pressedImage; }
set { pressedImage = value; this.Invalidate(); }
} //鼠标按下时背景颜色
public Color PressBackColor
{
get { return pressBackColor; }
set { pressBackColor = value; this.Invalidate(); }
} //鼠标起来时背景颜色
public Color PreBackColor
{
get { return preBackColor; }
set { preBackColor = value; this.Invalidate(); }
} //文本的位置
public TextAlignStyle TextAlign
{
get { return textAlign; }
set { textAlign = value; this.Invalidate(); }
} //图片的位置
public PictureLocationStyle PictureLocation
{
get { return pictureLocation; }
set { pictureLocation = value; this.Invalidate(); }
} //边框样式
public BorderStyle ButtonBorder
{
get { return buttonBorder; }
set { buttonBorder = value; this.Invalidate(); }
}

6.重写MouseDown&MouseUp事件

        protected override void OnMouseDown(MouseEventArgs e)
{
this.pressed = true;//鼠标按下,设置pressed=true
this.Invalidate();
base.OnMouseDown(e);
} protected override void OnMouseUp(MouseEventArgs e)
{
this.pressed = false;//鼠标释放,设置pressed=false
this.Invalidate();
base.OnMouseUp(e);
}

7. 重点:重绘控件

        // 重绘背景图片和文本
protected override void OnPaint(PaintEventArgs e)
{
//画背景图片
DrawBackGroundImage(e);
//改变背景颜色
ChangeBackColor();
//画文字
DrawForeText(e);
//画边框
DrawBorder(e); // Calling the base class OnPaint
base.OnPaint(e);
} //改变背景颜色
private void ChangeBackColor()
{
//按下鼠标时改变背景颜色
if (this.pressed)
{
this.BackColor = this.pressBackColor;
}
else
{
this.BackColor = this.preBackColor;
}
} //画边框
private void DrawBorder(PaintEventArgs e)
{
switch (buttonBorder)
{
case BorderStyle.None:
break;
case BorderStyle.Single:
//画边框
e.Graphics.DrawRectangle(new Pen(borderColor), , , this.ClientSize.Width - , this.ClientSize.Height - );
break;
}
} //画文本
private void DrawForeText(PaintEventArgs e)
{
//如果Text不为空,就显示文本
if (this.Text.Length > )
{
//获取文本的Size
SizeF size = e.Graphics.MeasureString(this.Text, this.Font);
//设置画文本的初始位置
float textX = ;
float textY = ;
switch (textAlign)
{
case TextAlignStyle.TopCenter:
textX = (this.ClientSize.Width - size.Width) / ;
textY = ;
break;
case TextAlignStyle.MiddleCenter:
textX = (this.ClientSize.Width - size.Width) / ;
textY = (this.ClientSize.Height - size.Height) / ;
break;
case TextAlignStyle.BottonCenter:
textX = (this.ClientSize.Width - size.Width) / ;
textY = this.ClientSize.Height - size.Height;
break;
}
//画文本
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textX, textY);
}
} //画背景图片
private void DrawBackGroundImage(PaintEventArgs e)
{
switch (pictureLocation)
{
case PictureLocationStyle.TopCenter:
//当鼠标按下且按下的图片为不为空,此时显示鼠标按下时图片
if (this.pressed && this.pressedImage != null)
{
e.Graphics.DrawImage(this.pressedImage, (this.ClientSize.Width - this.pressedImage.Width) / , );
}
//其他情况显示背景图片
else if (this.backgroundImage != null)
{
e.Graphics.DrawImage(this.backgroundImage, (this.ClientSize.Width - this.backgroundImage.Width) / , );
}
break;
case PictureLocationStyle.MiddleCenter:
//当鼠标按下且按下的图片为不为空,此时显示鼠标按下时图片
if (this.pressed && this.pressedImage != null)
{
e.Graphics.DrawImage(this.pressedImage, (this.ClientSize.Width - this.pressedImage.Width) / , (this.ClientSize.Width - this.pressedImage.Width) / );
}
//其他情况显示背景图片
else if (this.backgroundImage != null)
{
e.Graphics.DrawImage(this.backgroundImage, (this.ClientSize.Width - this.backgroundImage.Width) / , (this.ClientSize.Width - this.backgroundImage.Width) / );
}
break;
case PictureLocationStyle.BottonCenter:
//当鼠标按下且按下的图片为不为空,此时显示鼠标按下时图片
if (this.pressed && this.pressedImage != null)
{
e.Graphics.DrawImage(this.pressedImage, (this.ClientSize.Width - this.pressedImage.Width) / , this.ClientSize.Width - this.pressedImage.Width);
}
//其他情况显示背景图片
else if (this.backgroundImage != null)
{
e.Graphics.DrawImage(this.backgroundImage, (this.ClientSize.Width - this.backgroundImage.Width) / , this.ClientSize.Width - this.backgroundImage.Width);
}
break;
}
}

所有代码如下:

using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging; namespace Demo
{
public class ImageButton : Control
{
public ImageButton() { } public enum TextAlignStyle
{
TopCenter,
MiddleCenter,
BottonCenter,
} public enum PictureLocationStyle
{
TopCenter,
MiddleCenter,
BottonCenter,
} public enum BorderStyle
{
None,
Single,
} Image backgroundImage;//背景图片
Image pressedImage;//鼠标按下ImageButton时的图片
bool pressed = false;//ImageButton是否按下,true表示ImageButton按下
Color pressBackColor = SystemColors.Control;//鼠标按下时背景颜色
Color preBackColor = SystemColors.Control;//原来的背景颜色
TextAlignStyle textAlign = TextAlignStyle.BottonCenter;//文本的位置
PictureLocationStyle pictureLocation = PictureLocationStyle.TopCenter;//图片的位置
BorderStyle buttonBorder = BorderStyle.Single;//默认单线条边框
Color borderColor = SystemColors.Control;//默认边框颜色 //背景图片属性
public Image BackgroundImage
{
get { return backgroundImage; }
set { backgroundImage = value; this.Invalidate(); }
} // 鼠标按下ImageButton时的图片
public Image PressedImage
{
get { return pressedImage; }
set { pressedImage = value; this.Invalidate(); }
} //鼠标按下时背景颜色
public Color PressBackColor
{
get { return pressBackColor; }
set { pressBackColor = value; this.Invalidate(); }
} //鼠标起来时背景颜色
public Color PreBackColor
{
get { return preBackColor; }
set { preBackColor = value; this.Invalidate(); }
} //文本的位置
public TextAlignStyle TextAlign
{
get { return textAlign; }
set { textAlign = value; this.Invalidate(); }
} //图片的位置
public PictureLocationStyle PictureLocation
{
get { return pictureLocation; }
set { pictureLocation = value; this.Invalidate(); }
} //边框样式
public BorderStyle ButtonBorder
{
get { return buttonBorder; }
set { buttonBorder = value; this.Invalidate(); }
} protected override void OnMouseDown(MouseEventArgs e)
{
this.pressed = true;//鼠标按下,设置pressed=true
this.Invalidate();
base.OnMouseDown(e);
} protected override void OnMouseUp(MouseEventArgs e)
{
this.pressed = false;//鼠标释放,设置pressed=false
this.Invalidate();
base.OnMouseUp(e);
} // 重绘背景图片和文本
protected override void OnPaint(PaintEventArgs e)
{
//画背景图片
DrawBackGroundImage(e);
//改变背景颜色
ChangeBackColor();
//画文字
DrawForeText(e);
//画边框
DrawBorder(e); // Calling the base class OnPaint
base.OnPaint(e);
} //改变背景颜色
private void ChangeBackColor()
{
//按下鼠标时改变背景颜色
if (this.pressed)
{
this.BackColor = this.pressBackColor;
}
else
{
this.BackColor = this.preBackColor;
}
} //画边框
private void DrawBorder(PaintEventArgs e)
{
switch (buttonBorder)
{
case BorderStyle.None:
break;
case BorderStyle.Single:
//画边框
e.Graphics.DrawRectangle(new Pen(borderColor), , , this.ClientSize.Width - , this.ClientSize.Height - );
break;
}
} //画文本
private void DrawForeText(PaintEventArgs e)
{
//如果Text不为空,就显示文本
if (this.Text.Length > )
{
//获取文本的Size
SizeF size = e.Graphics.MeasureString(this.Text, this.Font);
//设置画文本的初始位置
float textX = ;
float textY = ;
switch (textAlign)
{
case TextAlignStyle.TopCenter:
textX = (this.ClientSize.Width - size.Width) / ;
textY = ;
break;
case TextAlignStyle.MiddleCenter:
textX = (this.ClientSize.Width - size.Width) / ;
textY = (this.ClientSize.Height - size.Height) / ;
break;
case TextAlignStyle.BottonCenter:
textX = (this.ClientSize.Width - size.Width) / ;
textY = this.ClientSize.Height - size.Height;
break;
}
//画文本
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textX, textY);
}
} //画背景图片
private void DrawBackGroundImage(PaintEventArgs e)
{
switch (pictureLocation)
{
case PictureLocationStyle.TopCenter:
//当鼠标按下且按下的图片为不为空,此时显示鼠标按下时图片
if (this.pressed && this.pressedImage != null)
{
e.Graphics.DrawImage(this.pressedImage, (this.ClientSize.Width - this.pressedImage.Width) / , );
}
//其他情况显示背景图片
else if (this.backgroundImage != null)
{
e.Graphics.DrawImage(this.backgroundImage, (this.ClientSize.Width - this.backgroundImage.Width) / , );
}
break;
case PictureLocationStyle.MiddleCenter:
//当鼠标按下且按下的图片为不为空,此时显示鼠标按下时图片
if (this.pressed && this.pressedImage != null)
{
e.Graphics.DrawImage(this.pressedImage, (this.ClientSize.Width - this.pressedImage.Width) / , (this.ClientSize.Width - this.pressedImage.Width) / );
}
//其他情况显示背景图片
else if (this.backgroundImage != null)
{
e.Graphics.DrawImage(this.backgroundImage, (this.ClientSize.Width - this.backgroundImage.Width) / , (this.ClientSize.Width - this.backgroundImage.Width) / );
}
break;
case PictureLocationStyle.BottonCenter:
//当鼠标按下且按下的图片为不为空,此时显示鼠标按下时图片
if (this.pressed && this.pressedImage != null)
{
e.Graphics.DrawImage(this.pressedImage, (this.ClientSize.Width - this.pressedImage.Width) / , this.ClientSize.Width - this.pressedImage.Width);
}
//其他情况显示背景图片
else if (this.backgroundImage != null)
{
e.Graphics.DrawImage(this.backgroundImage, (this.ClientSize.Width - this.backgroundImage.Width) / , this.ClientSize.Width - this.backgroundImage.Width);
}
break;
}
}
}
}

【Wince-自定义控件】ImageButton 带图片、文字的更多相关文章

  1. H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报

    摘要:初略算了算大概有20天没有写博客了,原本是打算1月1号元旦那天写一个年终总结的,博客园里大佬们都在总结过去,迎接将来,看得我热血沸腾,想想自己也工作快2年了,去年都没有去总结一下,今年势必要总结 ...

  2. 自定义带图片和文字的ImageTextButton

    今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学 ...

  3. 基于bootstrap的轮播广告页,带图片和文字

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  4. 026 Android 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+读取内存空间、手机进程信息+常驻悬浮框

    1.目标效果 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+常驻悬浮框 2.页面布局文件 (1)activity_process_manager.xml <?xml ...

  5. C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏

    1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...

  6. Qrcode生成二维码支持中文,带图片,带文字

    1.下载Qrcode库源码, 下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library2.打开源码时, 部分类 ...

  7. C#Qrcode生成二维码支持中文,带图片,带文字

    C#Qrcode生成二维码支持中文带图片的操作请看二楼的帖子,当然开始需要下载一下C#Qrcode的源码 下载地址 : http://www.codeproject.com/Articles/2057 ...

  8. JAVA实现带图片的列表——JList

    JList:显示对象列表并且允许用户选择一个或多个项的组件. JList的构造方法: 1.根据数组创建列表: JList(Object[] listData) 构造一个 JList,使其显示指定数组中 ...

  9. 『方案』《女友十年精华》 ORC 图片 文字识别 详解

    目的需求: 2008年,遇到一本电子书 <女友十年精华> 觉得很美,想 私藏 这些文章: >网络搜索文章 —— 没有找到: >反编译程序 —— 所有文字 都是图片格式(部分文章 ...

随机推荐

  1. selenium爬取斗鱼所有直播房间信息

    还是分析一下大体的流程: 首先还是Chrome浏览器抓包分析元素,这是网址:https://www.douyu.com/directory/all 发现所有房间的信息都是保存在一个无序列表中的li中, ...

  2. C#面向对象22 委托事件反射

    1.委托的定义:声明委托类型(返回值和参数,命名空间中):定义委托对象 (把委托想象成函数中的占位符~因为你并不确定调用哪个函数~) using System; using System.Collec ...

  3. 题解 CF670C 【Cinema】

    题目链接: https://www.luogu.org/problemnew/show/CF670C 思路: step-1: 语言的数据范围是10^9,所以我们采取用map离散化,这样就能方便且不ML ...

  4. C++ const关键字以及static关键字

    const可以用来修饰类中的成员函数以及成员变量以及类的对象 1.const修饰成员函数: 该函数是只读函数,不允许修改任何成员变量,但是可以使用类中的任何成员变量: 不允许修改任何非static的类 ...

  5. 深入理解hive之事务处理

    事务的四个特性 1.automicity:原子性 2.consistency:一致性 3. isolation:独立性 4.durability:持久性 5.支持事务有几个条件需要满足:1.所有的事务 ...

  6. dva中的一些备忘

    dva/router就是react-router-dom dva/router里的routerRedux就是react-router-redux 一个react的单页面应用: 编写一个基础框架,包含单 ...

  7. JS和JS是IE上JavaScript或JScript的缩写。

    JS和JS是IE上JavaScript或JScript的缩写.javascript是所有浏览器的开放式标准脚本语言JScript是微软自己的开放式脚本语言标准,只有微软的IE浏览器遵循.JScript ...

  8. 小程序API接口调用

    1.在config.js中写入api接口及appkey   2.在HTTP.js中引入config.js,然后新建HTTP.js,在里进行wx.request的封装. 定义一个HTTP的类,来类里定义 ...

  9. flex整页布局

    使用flex进行整页的三列布局,flex:1下的子元素无法铺满父级.给flex:1元素,添加stretch拉伸 display: flex; align-content: stretch; align ...

  10. qt tableview中如何添加右键菜单且不可编辑单元格

    QTableView是一个比较实用的类,下面教给大家如何在QTableView中添加右键菜单. #include <QMenu>#include <QAction> QTabl ...