开发环境:vs2005 beta2
测试环境:多普达696 Windows Mobile 2003 SE


1
using System;
2
using System.Drawing;
3
4
public class ImageButton : System.Windows.Forms.Control
5

{
6
enum ButtonState
7
{
8
Normal,Mousedown
9
}
10
11
private ButtonState m_ButtonState;
12
13
public ImageButton()
14
{
15
Text = "";
16
Size = new Size(72, 25);
17
m_ButtonState = ButtonState.Normal;
18
}
19
20
private Image m_NormalImage;
21
public Image NormalImage
22
{
23
get
{ return m_NormalImage; }
24
set
25
{
26
m_NormalImage = value;
27
if (value != null)
28
{
29
Size = new Size(value.Width, value.Height);
30
}
31
else
32
{
33
Size = new Size(72, 25);
34
}
35
}
36
}
37
38
private Image m_MouseDownImage;
39
public Image MouseDownImage
40
{
41
get
{ return m_MouseDownImage; }
42
set
{ m_MouseDownImage = value; }
43
}
44
45
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
46
{
47
if (m_ButtonState == ButtonState.Normal)
48
{
49
if (NormalImage != null)
50
{
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
{
56
if (MouseDownImage != null)
57
{
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
{
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
71
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
72
{
73
m_ButtonState = ButtonState.Mousedown;
74
Refresh();
75
}
76
77
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
78
{
79
m_ButtonState = ButtonState.Normal;
80
Refresh();
81
}
82
}
83
84
设计效果图

另外问一句都谁把696升级成了WM5.0了?我也想升一下试试,指教一下