ZsUI - 第一个例子
第一步,我们找个容器.
在VB里,Form或者PictureBox又或者其它那些有句柄的可视控件都能胜任.
接着,在脑子想象一个控件应该有的样子. 位置和尺寸(x.y.width.height).各种属性和状态.(image.status).
我们先用一个Type来描述它.
Type Zs_UI_Button '想象中的简陋按钮.
X As Integer
Y As Integer
Width As Integer
Height As Integer
Image As Long
Status As Long
End Type
第二部,给它来点实际的东西.
Dim ZButton1 As Zs_UI_Button
With ZButton1
.X = 30
.Y = 30
.Width = 92
.Height = 40
Call GdipLoadImageFromFile(StrPtr(App.Path & "/res/button.png"), pImage)
.Image = pImage
.Status = C_Status_Leave
End With
第三部,我们为它弄点"事件"并把它显示出来.
"事件"不可能凭空出现,这事得靠依附的那个容器了.
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '容器的MouseMove事件
With ZButton1
If X > .X And X < .X + .Width And Y > .Y And Y < .Y + .Height Then '看看是不是轮到我出场了.MouseEnter和MouseLeave都是靠这句了.
If .Status And C_Status_Leave Then '为了防止一直重画,设置个状态标志.
Me.Cls '因为我们要做的控件是透明的,所以先要把容器背景重画一次.
Call GdipDrawImageRectRect(pGraphics, .Image, .X, .Y, .Width, .Height, 92, 0, 92, 40, UnitPixel) '根据一个状态画出对应的图.
.Status = C_Status_Hover '标记一下
End If
Else
If .Status = C_Status_Hover Then
Me.Cls
Call GdipDrawImageRectRect(pGraphics, .Image, .X, .Y, .Width, .Height, 0, 0, 92, 40, UnitPixel)
.Status = C_Status_Leave
End If
End If
End With
End Sub
Private Sub Form_Click() '处理容器的单击事件.
If ZButton1.Status = C_Status_Hover Then '如果鼠标还在虚拟控件的范围内,就吱一声.
MsgBox "ZButton_Click"
End If
End Sub
写完这些,运行来张效果图看看.

嘿,可爱的"控件"出现了.
不过这样的代码明显是各种BUG,糟糕透顶了.
一两个"控件"还好,多了就是一团糟.
下期我们开始把这些封装起来.让它更像"控件"一点.
本期代码下载地址:
http://pan.baidu.com/netdisk/singlepublic?fid=295000_4071038647