VB 窗口处理技巧大全

VB提供了API函数SetWindowLong和GetWindowLong,可以让我们很容易取得对窗口的操作;通过对窗口属性的操作,可以更改窗口的显示风格。有些看来是正常情况下无法实现的窗口,现在你可以很容易的实现。只要你想到,更多希奇古怪的你也能做到。快试试下面的例子吧。

'以下例子中可能用到的API声明和常量、变量声明
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 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 Const WS_DLGFRAME = &H400000
Private Const WS_POPUP = &H80000000
Private Const WS_CAPTION = &HC00000
Private Const WS_SYSMENU = &H80000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZE = &H20000000
Private Const WS_MAXIMIZE = &H1000000


--------------------------------------------------------------------------------

'例子一:任何一个控件(只要有窗口,这是我们的前提,下同),你可以在运行时随便更改它的大小。
Private Sub ControlSize(ControlName As Control, SetTrue As Boolean )
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_THICKFRAME
Else
dwStyle = dwStyle - WS_THICKFRAME
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd,
0 , 0 , 0 , 0 , SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
'用法:ControlSize picture1,true;设置第二个参数为False取消这种设置,下同


--------------------------------------------------------------------------------

'例子二:任何一个控件,我们都可以控制其显示风格为对话框的风格。
Private Sub ControlDialog(ControlName As Control, SetTrue As Boolean )
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_DLGFRAME
Else
dwStyle = dwStyle - WS_DLGFRAME
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd,
0 , 0 , 0 , 0 , SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
'用法:ControlSize picture1,true


--------------------------------------------------------------------------------

'例子三:任何一个控件,我们都可以控制其显示风格为模式对话框的风格
Private Sub ControlModal(ControlName As Control, SetTrue As Boolean )
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_POPUP
Else
dwStyle = dwStyle - WS_POPUP
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd,
0 , 0 , 0 , 0 , SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
'用法:ControlModal Picture1,true


--------------------------------------------------------------------------------

'例子四:任何一个控件,我们都可以给它加上标题栏,通过拖动标题栏,可以实现控件的运行时移动。
Private Sub ControlCaption(ControlName As Control, SetTrue As Boolean ) Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_CAPTION
Else
dwStyle = dwStyle - WS_CAPTION
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd,
0 , 0 , 0 , 0 , SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
'用法:ControlCaption picture1,true


--------------------------------------------------------------------------------

'例子五:任何一个控件,我们都可以给它加上ControlBox(所谓ControlBox,就是窗体的图标+最小化+最大化+关闭按钮)。
Private Sub ControlSysMenu(ControlName As Control, SetTrue As Boolean )
Dim dwStyle As Long
dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
If SetTrue Then
dwStyle = dwStyle Or WS_SYSMENU
Else
dwStyle = dwStyle - WS_SYSMENU
End If
dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd,
0 , 0 , 0 , 0 , SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
End Sub
'用法:ControlCaption picture1,true:ControlSysmenu picture1,true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值