我们在winform中给按钮设置个背景图片超级简单,是不?可是在wince下面就没那么简单了,下面我来介绍一种方式来实现ImageButton。

实现思路是重新写一个usercontrol就ok。具体的实现方式如下:

/IotButton是我们新控件的名字,具体的可以自己定义
public partial class IotButton : UserControl
{
public IotButton()
{
InitializeComponent();
} Image backgroundImage;
bool pressed = false; // Property for the background image to be drawn behind the button text.
public Image BackgroundImage
{
get
{
return this.backgroundImage;
}
set
{
this.backgroundImage = value;
}
}
// When the mouse button is pressed, set the "pressed" flag to true
// and invalidate the form to cause a repaint. The .NET Compact Framework
// sets the mouse capture automatically.
protected override void OnMouseDown(MouseEventArgs e)
{
this.pressed = true;
this.Invalidate();
base.OnMouseDown(e);
} // When the mouse is released, reset the "pressed" flag
// and invalidate to redraw the button in the unpressed state.
protected override void OnMouseUp(MouseEventArgs e)
{
this.pressed = false;
this.Invalidate();
base.OnMouseUp(e);
} // Override the OnPaint method to draw the background image and the text.
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle); if (this.backgroundImage != null)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(Color.Magenta, Color.Magenta); if (this.pressed)
e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
else
e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr); } base.OnPaint(e);
}
}

这里为什么要实现一个新的UserControl而不是基于Button呢?具体原因是这样的,因为Button中没有Paint事件,没有MouseUP MouseDown MouseMove等事件因此不能很好的实现,有图有证据哦?

看看我们自定义的按钮的事件和属性吧:

总结

上面的方式实现了在wince下自定义一个可以放image的控件,具体的不同我们可以对比上面的两图得到。希望对大家有帮助哦。

Wince下实现ImageButton的更多相关文章

  1. WinCE下读取注册表获得SD路径

    WinCE下读取注册表获得SD路径 [要点]WinCE注册表中[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\SDMemory\] 下键Folde ...

  2. WinCE下GPRS自动拨号软件(GPRS AutoDial)

    之前在WinCE下调试USB的3G Modem时,写过一个拨号助手RASManager,基本能用.后来车机卖到俄罗斯去,客户老M提供了一个更好的GPRS自动拨号软件GPRS AutoDial,功能完善 ...

  3. Wince下sqlce数据库开发(二)

    上次写到使用数据绑定的方法测试本地sqlce数据库,这次使用访问SQL Server的方法访问sqlce,你会发现他们是如此的相似... 参考资料:http://www.cnblogs.com/rai ...

  4. Wince下sqlce数据库开发(一)

    对于Wince下的sqlce数据库虽然很多人在用,但在我查找资料时,却发现资料是多么的匮乏,在此对自己这几天的了解做个简单介绍,希望对大家能有所帮助! 本文的最后附有所使用到的sqlce在wince下 ...

  5. 在Window Embedded CE(Wince)下使用OpenNETCF进行路由表的开发

    点击打开链接 背景 在开发3G项目的是时候,发现尽管3G网络连接已经建立成功了,但是数据不能发送成功,查明原因,由于路由表的问题,导致数据往ActiveSync连接的对端,也就是PC发送,而不是发送到 ...

  6. WinCE下使用C#获得带毫秒的DateTime.Now

    在WinCE下,使用DateTime.Now获取的系统时间是不带毫秒的,如果想要它带毫秒,需要耍点手段.话不多说,直接上代码: public static DateTimePrecisely { // ...

  7. wince下sources\sources.cmn\Makefile.def的相关作用

    1:首先是Makefile.def: ---------------------------------------- 在所有驱动的makefile中有!INCLUDE $(_MAKEENVROOT) ...

  8. SD card技术了解并WINCE下SDHC驱动开发(updated)

    Suumary: 简单介绍了一下SD卡的历史和发展,同时结合MX31 ADS上的WINCE 下SDHC驱动更深入的了解该硬件的一些行为特点. 了解SD card SD是Secure Digital C ...

  9. 如何捕获Wince下form程序的全局异常

    前言 上两篇文章我们总结了在winform程序下如何捕获全局的异常.那么同样的问题,在wince下我们如何来处理呢?用相同的代码来处理可以吗? 答案是否定的,上面的方案1完全不能解决wince下的情况 ...

随机推荐

  1. 如何在UILable上添加点击事件?

    最近开始学习iOS开发,今天上来写第一个iOS笔记 昨天碰到一个需求,在UILable上添加点击事件,网上找了写资料,有人建议用透明的UIButton覆盖,有人建议写一个集成自UILable的类,扩展 ...

  2. JavaSE学习总结第05天_Java语言基础1

      05.01 方法概述和格式说明 简单的说:方法就是完成特定功能的代码块 在很多语言里面都有函数的定义,函数在Java中被称为方法 格式: 修饰符返回值类型方法名(参数类型参数名1,参数类型参数名2 ...

  3. Django问题汇总

    Q1.运行python manage.py runserver后,出现如下错误(CentOS6.5x86,Python2.7.12,Django1.10): django.core.exception ...

  4. PHP学习笔记14-操作session

    PHP会话管理图: 创建index: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/7/2 * Time ...

  5. Java多线程实现......(1,继承Thread类)

    MyThread.java 中的代码: public class MyThread extends Thread{ private int startPrint,printCount; private ...

  6. QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址

    获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...

  7. php返回的json格式

    public function search_ip(){ $where['ip'] = $_GET['ip']; $Machine = M('Machine_info'); $arr = $Machi ...

  8. 基于物品的协同过滤推荐算法——读“Item-Based Collaborative Filtering Recommendation Algorithms” .

    ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3 基于物品的协同过滤推荐算法--读"Item-Based ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

  10. android一个弹出菜单的动画(二)

    假设做一个弹出的控件,我们能够进行加入view: 写class SatelliteMenu extends FrameLayout private void init(Context context, ...