我们在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. 修改SQL Server登录密码(使用SQL Server身份登录)

    修改登录密码: http://blog.sina.com.cn/s/blog_631611220100iqao.html

  2. JBPM6工作台使用Mysql数据库

    前面已经安装好jbpm了.现在进行数据库的替换.很简单: 1,修改 build.properties 原: # default is H2 H2.version=1.3.168 db.name=h2 ...

  3. 织梦dedecms|图片模型内容页标签

    图片列表开始:{dede:productimagelist}图片列表结束:{/dede:productimagelist}图片显示:     [field:imgsrc/]图集缩略图:  {dede: ...

  4. nginx 引入外部文件

    http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $ ...

  5. 「数据结构」:模拟指针(simulated pointer)

    模拟指针,也就是清华严老师<数据结构-C语言描述>中的静态链表,静态链表的引用是使用一段连续的存储区还模拟指针的功能,可以有效的利用一段连续内存进行一定范围内可变的子链表的空间分配,此数据 ...

  6. BZOJ 网站镜像

    网站镜像大小:15Mb 镜像保存时间:2012年2月[有绝大多数题目] 镜像下载地址:http://download.csdn.net/detail/wnjxyk/7913125

  7. mmc一维下料例子

    数据来自维基, 长度 = 5600; l切割长度= {1380, 1520, 1560, 1710, 1820, 1880, 1930, 2000, 2050, 2100, 2140,     215 ...

  8. Effective C++:条款37:绝不又一次定义继承而来的缺省參数值

    因为又一次定义继承而来的non-virtual函数是不对的(见上一个条款),所以这个条款就将问题局限于:绝不又一次定义继承一个带有缺省參数值的virtual函数. (一) virtual函数是动态绑定 ...

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

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

  10. ThinkPHP - 模板引擎

    1.导入css/js文件 - CSS文件 <!--<link rel="stylesheet" type="text/css" href=" ...