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. 排序算法大汇总 Java实现

    一.插入类算法 排序算法的稳定性:两个大小相等的元素排序前后的相对位置不变.{31,32,2} 排序后{2,31,32},则称排序算法稳定 通用类: public class Common { pub ...

  2. #undef取消宏定义

    如果你想定义这个宏那就#define X 如果你不想让你已经#define X的宏在其他地方由于引入这个包含宏定义的.h文件而引起一些编译问题,那你就#undef X掉,就这么简单. 举个简单的例子: ...

  3. ci框架读取上传的excel文件数据

    原文链接: https://blog.csdn.net/qq_38148394/article/details/87921373 此功能实现使用到PHPExcel类库,PHPExcel是一个PHP类库 ...

  4. 16-Perl 文件操作

    1.Perl 文件操作Perl 使用一种叫做文件句柄类型的变量来操作文件.从文件读取或者写入数据到文件需要使用文件句柄.文件句柄(file handle)是一个I/O连接的名称.Perl提供了三种文件 ...

  5. Java Web ClassLoader工作机制

    一.ClassLoader的作用: 1.类加载机制:父优先的等级加载机制 2.类加载过程 3.将Class字节码重新解析成JVM统一要求的对象格式 二.ClassLoader常用方法 1.define ...

  6. 客户端相关知识学习(六)之deeplink技术

    Deeplink应用描述 Deeplink,简单讲,就是你在手机上点击一个链接之后,可以直接链接到app内部的某个页面,而不是app正常打开时显示的首页.不似web,一个链接就可以直接打开web的内页 ...

  7. shiro学习(一)

    基础依赖: shiro-core,junit(因为在单元测试中) test.class public class AuthenticationTest { SimpleAccountRealm rea ...

  8. Vue访问权限

    设置权限 <script> export default { created(){ if(this.$store.state.userStore.role !== 'manager'){ ...

  9. Qt5配置winpCap

    在网上查了很多资料,搞了差不多一天总算解决Qt5使用winPcap配置的问题了!记录一下 以便后续忘记 1.下载winpcap4.1.3,百度即可搜索到 2.下载winpCap开发者工具包http:/ ...

  10. 由于MTU设置不当导致的访问超时

    现象 工作中遇到一件怪事:搭建好服务器后(VPN服务器,创建了虚拟网卡),服务器和客户端之间响应正常且很稳定,客户端也能正常通过服务器访问外网.但是访问个别网站时可以打开文字,但是部分图片打不开(也不 ...