您的位置:首页 > 编程语言 > VB

窗口处理技巧大全

2009-08-15 17:52 211 查看
'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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  function user api vb