【实例说明】 动态改变控件尺寸,类似于控件大小随窗体的改变而改变。
【编程思路】 用API函数来动态改变控件尺寸。
【设计步骤】
1.新建一个标准工程,创建一个新窗体,默认名为Form1。
2.在窗体上放置一个CommandButton控件和一个PictureBox控件。
3.源程序 [素材源程序下载]
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOMOVE = &H2
Private Const SWP_DRAWFRAME = &H20
Private Const GWL_STYLE = (-16)
Private Const WS_THICKFRAME = &H40000
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim mousemove As Boolean
mousemove = (X > 0) And (X < Picture1.Width) And (Y > 0) And (Y < Picture1.Height)
If mousemove Then
Dim NewStyle As Long
NewStyle = GetWindowLong(Picture1.hwnd, GWL_STYLE)
NewStyle = NewStyle Or WS_THICKFRAME
'调整控件大小
NewStyle = SetWindowLong(Picture1.hwnd, GWL_STYLE, NewStyle)
End If
End Sub
'退出
Private Sub Command1_Click()
End
End Sub
|