开发环境:vs2005 beta2
测试环境:多普达696 Windows Mobile 2003 SE
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Drawing;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
public class ImageButton : System.Windows.Forms.Control
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
enum ButtonState
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
Normal,Mousedown
9
}
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
private ButtonState m_ButtonState;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public ImageButton()
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
Text = "";
16
Size = new Size(72, 25);
17
m_ButtonState = ButtonState.Normal;
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
private Image m_NormalImage;
21
public Image NormalImage
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_NormalImage; }
24
set
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
m_NormalImage = value;
27
if (value != null)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
Size = new Size(value.Width, value.Height);
30
}
31
else
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
Size = new Size(72, 25);
34
}
35
}
36
}
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
private Image m_MouseDownImage;
39
public Image MouseDownImage
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_MouseDownImage; }
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_MouseDownImage = value; }
43
}
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
if (m_ButtonState == ButtonState.Normal)
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
if (NormalImage != null)
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
e.Graphics.DrawImage(NormalImage, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, NormalImage.Width, NormalImage.Height),GraphicsUnit.Pixel);
52
}
53
}
54
else if (m_ButtonState == ButtonState.Mousedown)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
if (MouseDownImage != null)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
e.Graphics.DrawImage(MouseDownImage, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, MouseDownImage.Width, MouseDownImage.Height), GraphicsUnit.Pixel);
59
}
60
else if (NormalImage != null)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
e.Graphics.DrawImage(NormalImage, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, NormalImage.Width, NormalImage.Height), GraphicsUnit.Pixel);
63
}
64
}
65
StringFormat sf = new StringFormat();
66
sf.Alignment = StringAlignment.Center;
67
sf.LineAlignment = StringAlignment.Center;
68
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(0, 0, Width, Height), sf);
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
m_ButtonState = ButtonState.Mousedown;
74
Refresh();
75
}
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
m_ButtonState = ButtonState.Normal;
80
Refresh();
81
}
82
}
83![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
84![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
设计效果图
![](https://www.cnblogs.com/images/cnblogs_com/mobile/ImageButtonDesigh.JPG)
另外问一句都谁把696升级成了WM5.0了?我也想升一下试试,指教一下