【Wince-自定义控件】ImageButton 带图片、文字
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 带图片、文字的更多相关文章
- H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报
摘要:初略算了算大概有20天没有写博客了,原本是打算1月1号元旦那天写一个年终总结的,博客园里大佬们都在总结过去,迎接将来,看得我热血沸腾,想想自己也工作快2年了,去年都没有去总结一下,今年势必要总结 ...
- 自定义带图片和文字的ImageTextButton
今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学 ...
- 基于bootstrap的轮播广告页,带图片和文字
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- 026 Android 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+读取内存空间、手机进程信息+常驻悬浮框
1.目标效果 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+常驻悬浮框 2.页面布局文件 (1)activity_process_manager.xml <?xml ...
- C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏
1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...
- Qrcode生成二维码支持中文,带图片,带文字
1.下载Qrcode库源码, 下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library2.打开源码时, 部分类 ...
- C#Qrcode生成二维码支持中文,带图片,带文字
C#Qrcode生成二维码支持中文带图片的操作请看二楼的帖子,当然开始需要下载一下C#Qrcode的源码 下载地址 : http://www.codeproject.com/Articles/2057 ...
- JAVA实现带图片的列表——JList
JList:显示对象列表并且允许用户选择一个或多个项的组件. JList的构造方法: 1.根据数组创建列表: JList(Object[] listData) 构造一个 JList,使其显示指定数组中 ...
- 『方案』《女友十年精华》 ORC 图片 文字识别 详解
目的需求: 2008年,遇到一本电子书 <女友十年精华> 觉得很美,想 私藏 这些文章: >网络搜索文章 —— 没有找到: >反编译程序 —— 所有文字 都是图片格式(部分文章 ...
随机推荐
- DOM、BOM
DOM DOM 是 W3C(万维网联盟)的标准. DOM 定义了访问 HTML 和 XML 文档的标准: “W3C 文档对象模型 (DOM) 是中立于平台和语言的接口,它允许程序和脚本动态地访问和更新 ...
- 查询集 QuerySet
1 概念 Django的ORM中存在查询集的概念. 查询集,也称查询结果集.QuerySet,表示从数据库中获取的对象集合. 当调用如下过滤器方法时,Django会返回查询集(而不是简单的列表): a ...
- 美团2017年CodeM大赛-初赛A轮 C合并回文子串
区间dp一直写的是递归版本的, 竟然超时了, 学了一下非递归的写法. #include <iostream> #include <sstream> #include <a ...
- 怎样单独遍历NodeList的键、值和键值对
1. 单独遍历键: NodeList.prototype.keys(); 2. 单独遍历值: NodeList.prototype.values(); 3. 遍历键值对: NodeList.proto ...
- java——ArrayList中contains()方法中的疑问
问题引子: ist<Student> students=new ArrayList<Student>(); students.add(new Student("201 ...
- sql lesson21homework
2017-08-15 18:03:17 mysql> show databases;+--------------------+| Database |+---------- ...
- GET方法和POST方法的区别,Get方法到底可传递的字符串的最大长度是多少?
GET方法和POST方法的区别,Get方法到底可传递的字符串的最大长度是多少?曾经人介绍,如果使用GET方式传输参数,URL的最大长度是256个字节,对此深信不疑. 但是最近看到一些超长的url,能够 ...
- A Pythonic Card Deck: __len__ & __getitem__ & for 循环的嵌套
1. 列表生成式的嵌套 for 循环: 示例如下: li1 = range(1,6) li2 = list("ABC") # list("ABC") 的结果为 ...
- maven:无效的目标发行版:11
maven:无效的目标发行版:11 我之前在博客里是不记录bug和error的处理的,昨天听了一个资深程序员的视频,决定要改习惯了,记录一些自己平时遇到的问题 这个是我在mvn clean insta ...
- input type 为 number 时去掉上下小箭头
<input type="number" ...> <style> input::-webkit-outer-spin-button, input::-we ...