【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年,遇到一本电子书 <女友十年精华> 觉得很美,想 私藏 这些文章: >网络搜索文章 —— 没有找到: >反编译程序 —— 所有文字 都是图片格式(部分文章 ...
随机推荐
- Java MyBatis逆向工程,自动生成pojo,mapper
生成xml文件,文件名generator.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYP ...
- python 小数据池,代码块, is == 深入剖析
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了, ...
- SQLSERVER2008 内存占用高的处理方式
原文:SQLSERVER2008 内存占用高的处理方式 方法一: 方法二: 使用以下语句查找出什么语句占内存最高,针对占内存高的语句进行优化SELECT SS.SUM_EXECUTION_COUNT, ...
- c++学习笔记之函数重载和模板理解
1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...
- OpenVZ平台 Google BBR加速
前言 一直以来用的都是搬瓦工的VPS,不得不说比国内那些大厂的性价比高得不知道哪里去了. 当做梯子来用的话搬瓦工年付19.9美元的方案就够用了,网上还有一些官方优惠码(折扣6%: BWH1ZBPV ...
- 关于php性能优化
php性能优化 1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方 ...
- 第2章:Python生态工具
1.Python内置小工具 1).1秒钟启动一个下载服务器: python -m SimpleHTTPServer python3 -m http.server 会在当前目录下启动一个文件下载服务器, ...
- 【spring boot】3.spring boot项目,绑定资源文件为bean并使用
整个例子的结构目录如下: 1.自定义一个资源文件 com.sxd.name = 申九日木 com.sxd.secret = ${random.value} com.sxd.intValue = ${r ...
- Pycharm有必要改的几个默认设置项以及快捷键
最近在用Pycharm学习Python的时候,总有两个地方感觉不是很舒服,比如调用方法的时候区分大小写(thread就不会出现Thread,string就不会出现String)等,这让我稍稍有点不舒服 ...
- 使用WSAIoctl获取AcceptEx,Connectex,Getacceptexsockaddrs函数指针
运行WinNT和Win2000的系统上,这些APIs在Microsoft提供的DLL(mswsock.dll)里实现,可以通过链接mswsock.lib或者通过WSAioctl的SIO_GET_EXT ...