图片按钮:
思路:很简单,就是在一个 picturebox 控件上放置一个 button 控件,然后将这个 button 添加进 picturebox 上(确保先拖拽 picturebox, 后拖拽 button ) , 设置这个 button 的背景色(这个时候是相对于 picturebox )为透明。
Imports System.ComponentModel
Public Class picturebutton
Inherits System.Windows.Forms.UserControl
Region " Windows 窗体设计器生成的代码 "
'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose( ByVal disposing As Boolean )
If disposing Then
If Not (components Is Nothing ) Then
components.Dispose()
End If
End If
MyBase .Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
' 注意:以下过程是 Windows 窗体设计器所必需的
' 可以使用 Windows 窗体设计器修改此过程。
' 不要使用代码编辑器修改它。
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2
3Me .PictureBox1 = New System.Windows.Forms.PictureBox()
4
5Me .Button1 = New System.Windows.Forms.Button()
6
7Me .SuspendLayout()
8
9'
10
11'PictureBox1
12
13'
14
15Me .PictureBox1.Name = "PictureBox1"
16
17Me .PictureBox1.Size = New System.Drawing.Size(136, 40)
18
19Me .PictureBox1.TabIndex = 0
20
21Me .PictureBox1.TabStop = False
22
23'
24
25'Button1
26
27'
28
29Me .Button1.Name = "Button1"
30
31Me .Button1.TabIndex = 1
32
33Me .Button1.Text = "Button1"
34
35'
36
37'picturebutton
38
39'
40
41Me .Controls.AddRange( New System.Windows.Forms.Control() { Me .Button1, Me .PictureBox1})
42
43Me .Name = "picturebutton"
44
45Me .ResumeLayout( False )
46
47End Sub
48
49# End Region
50
51Public Sub New ()
52
53MyBase .New()
54
55' 该调用是 Windows 窗体设计器所必需的。
56
57InitializeComponent()
58
59' 在 InitializeComponent() 调用之后添加任何初始化
60
61Me .Button1.Width = 100 ‘设置按钮的初始大小
62
63Me .Button1.Height = 23
64
65Me .Button1.BackColor = Color.Transparent ‘ 背景色透明
66
67Me .Button1.ForeColor = Color.Black
68
69Me .PictureBox1.Controls.Add( Me .Button1)
70
71End Sub
72
73Private m_text As String ‘设置按钮标题
74
75Private a As Integer
76
77'Private m_image As Image
78
79<description("picturebox ")="" 图片。=""> _
80
81Public Property image() As image
82
83Get
84
85Return Me .PictureBox1.Image
86
87End Get
88
89Set ( ByVal Value As image)
90
91Me .PictureBox1.Image = Value
92
93Invalidate()
94
95End Set
96
97End Property
98
99Shadows Property forecolor() As Color
100
101Get
102
103Return Me .Button1.ForeColor
104
105End Get
106
107Set ( ByVal Value As Color)
108
109Me .Button1.ForeColor = Value
110
111Invalidate()
112
113End Set
114
115End Property
116
117Shadows Sub ResetForeColor()
118
119Me .Button1.ForeColor = SystemColors.ControlText
120
121End Sub
122
123'////
124
125' 按钮的单击事件
126
127Event BtnClick( ByVal sender As Object , ByVal e As System.EventArgs)
128
129Private Sub Button1_Click( ByVal sender As Object , ByVal e As System.EventArgs) Handles Button1.Click
130
131RaiseEvent BtnClick( Me , e)
132
133End Sub
134
135'////
136
137' 控件改变大小时,需重绘控件,以使子控件排位美观
138
139Private Sub FileTextBox_Resize( ByVal sender As Object , ByVal e As System.EventArgs) Handles MyBase .Resize
140
141RedrawControls()
142
143End Sub
144
145' 子控件会自动继续容器的 Font 属性,所以改变容器的 Font 属性时也要重绘控件
146
147Protected Overrides Sub OnFontChanged( ByVal e As System.EventArgs)
148
149' 让基控件更新文本框
150
151MyBase .OnFontChanged(e)
152
153' 重绘控件
154
155RedrawControls()
156
157End Sub
158
159' 重绘控件
160
161Private Sub RedrawControls()
162
163' 控件宽度
164
165Dim width As Integer = Me .ClientRectangle.Width ' 获得工作区宽
166
167' 以按钮的高度来确定控件高度
168
169Dim btnSide As Integer = Button1.Height
170
171Dim btnwidth As Integer = Button1.Width
172
173If Me .ClientRectangle.Height <> btnSide Then
174
175' 设置控件工作区的大小
176
177'Me.SetClientSizeCore(btnwidth, btnSide)
178
179Me .SetClientSizeCore(width, btnSide) ' 这里使用工作区的宽是因为:按钮和 picturebox 可以调整宽度
180
181' 上面的语句激发了嵌套的 Resize 事件,因此需要立即退出,如果不退出 , 就会反复调用进入死循环
182
183Exit Sub
184
185End If
186
187' 调整子控件的大小
188
189'Txt.SetBounds(0, 0, width, btnSide)
190
191'Btn.SetBounds(width - 19, 2, 17, btnSide - 4)
192
193Me .PictureBox1.SetBounds(0, 0, width, btnSide)
194
195Me .PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
196
197Me .Button1.SetBounds(0, 0, width, btnSide)
198
199End Sub
200
201<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN:</description("picturebox></system.diagnostics.debuggerstepthrough()>