Tuesday, December 16, 2008

[vb6] Modify the styles for a window

Some style and extended style bits have no counterpart in VB properties for forms and control windows and hence cannot be set in Visual Basic, but it may be possible to change the style at run-time after the window has been created. Use the ModifyStyle and ModifyStyleEx functions to remove and / or add style bits to the window style or window extended style, respectively. Note that changing these bits will often have no effect in Visual Basic - you have to experiment to find out which modifications work and which do not.
    Private Const GWL_EXSTYLE& = (-20)
Private Const GWL_STYLE& = (-16)
Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long)
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)

Public Sub ModifyStyle(ByVal hWindow As Long, _
ByVal Remove As Long, ByVal Add As Long)

Dim style As Long, OldStyle As Long
OldStyle = GetWindowLong(hWindow, GWL_STYLE)
style = (OldStyle And (Not Remove)) Or Add
SetWindowLong hWindow, GWL_STYLE, style

End Sub

Public Sub ModifyStyleEx(ByVal hWindow As Long, _
ByVal Remove As Long, ByVal Add As Long)

Dim xstyle As Long, OldStyle As Long
OldStyle = GetWindowLong(hWindow, GWL_EXSTYLE)
xstyle = (OldStyle And (Not Remove)) Or Add
SetWindowLong hWindow, GWL_EXSTYLE, xstyle

End Sub

For example, to remove the maximize and minimize buttons from a form and make it transparent, you will use the above functions like this:
    ModifyStyle hwnd, WS_MAXIMIZEBOX Or WS_MINIMIZEBOX, 0
ModifyStyleEx hwnd, 0, WS_EX_TRANSPARENT

No comments:

Post a Comment