Jalaj

February 3, 2007

Modifying Minimize, Maximize buttons during runtime

Filed under: Application, Visual Basic, WinAPI — Jalaj @ 5:38 am

Previous post WinAPI : Making Form Transparent described how to make a form transparent. A similar problem that is often asked is how to modify the minimize and maximize button during runtime. While design time of cource you can change the MinButton and MaxButton properties to achieve this but during runtime the above properties cannot be changed by directly accessing them.

The GetWindowLong and SetWindowLong function comes to the rescue here. You can modify the avaibility of the System menu also by the same way. As already described in the previous post you just need to get the current Styles (not extended) and include the ammendments before setting the new styles.

Create a new project and paste the code to the code section of the form. The form while appears with minimize, maximize button when executed but as soon as the form is clicked the styles are ammended which causes the minimize, maximize button to disappear. The System Menu stops functioning too.. Form1 is then successively made invisible and then visible ensuring that the form is repainted to accomodate the changes.

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

' Window Indexes
Const GWL_STYLE = (-16)     ' Get Window's Style
Const GWL_EXSTYLE = (-20)   ' Get Window's Extended Style
Const GWL_WNDPROC = (-4)    ' Get handle to address of window procedure
Const GWL_HINSTANCE = (-6)  ' Get handle to Application Instance
Const GWL_HWNDPARENT = (- 8) ‘ Get handle to parent window
Const GWL_ID As Long = (-12) ‘ Get identifier of the window
Const GWL_USERDATA = (-21)  ‘ Get user data associated witht he window.

‘Styles
Const WS_BORDER = &H800000
Const WS_CAPTION = &HC00000
Const WS_CHILD = &H40000000
Const WS_CHILDWINDOW = WS_CHILD
Const WS_CLIPCHILDREN = &H2000000
Const WS_CLIPSIBLINGS = &H4000000
Const WS_DISABLED = &H8000000
Const WS_DLGFRAME = &H400000
Const WS_GROUP = &H20000
Const WS_MINIMIZE = &H20000000
Const WS_ICONIC = WS_MINIMIZE
Const WS_MAXIMIZE = &H1000000
Const WS_MAXIMIZEBOX = &H10000
Const WS_MINIMIZEBOX = &H20000
Const WS_POPUP = &H80000000
Const WS_SYSMENU = &H80000
Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Const WS_THICKFRAME = &H40000
Const WS_SIZEBOX = WS_THICKFRAME
Const WS_TABSTOP = &H10000
Const WS_VISIBLE = &H10000000

‘Extended Styles
Const WS_EX_ACCEPTFILES = &H10&
Const WS_EX_APPWINDOW = &H40000
Const WS_EX_CLIENTEDGE = &H200
Const WS_EX_COMPOSITED = &H2000000
Const WS_EX_CONTEXTHELP = &H400
Const WS_EX_CONTROLPARENT = &H10000
Const WS_EX_DLGMODALFRAME = &H1
Const WS_EX_LAYERED = &H80000
Const WS_EX_LAYOUTRTL = &H400000
Const WS_EX_LEFT = &H0
Const WS_EX_MDICHILD = &H40
Const WS_EX_NOACTIVATE = &H8000000
Const WS_EX_NOINHERITLAYOUT = &H100000
Const WS_EX_NOPARENTNOTIFY = &H4
Const WS_EX_TOPMOST = &H8
Const WS_EX_TOOLWINDOW = &H80
Const WS_EX_WINDOWEDGE = &H100
Const WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
Const WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
Const WS_EX_RIGHT = &H1000
Const WS_EX_RIGHTSCROLLBAR = &H0
Const WS_EX_RTLREADING = &H2000
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20
Const WS_EX_LEFTSCROLLBAR = &H4000
Const WS_EX_LTRREADING = &H0

Private Sub Form_Click()

    Dim lngCurStyle As Long
    Dim lngNewStyle As Long

    lngCurStyle = GetWindowLong(Form1.hWnd, GWL_STYLE)
    lngNewStyle = lngCurStyle And Not (WS_MINIMIZEBOX Or WS_MAXIMIZEBOX Or WS_SYSMENU)
    SetWindowLong Form1.hWnd, GWL_STYLE, lngNewStyle

    Form1.Visible = False
    Form1.Visible = True

End Sub

1 Comment »

  1. [...] include WS_EX_LAYERED using GetWindowLong and SetWindowLong as already descibed in pervious posts Modifying Minimize, Maximize buttons during runtime and WinAPI : Making Form [...]

    Pingback by WinAPI : Making Forms Translucent « Jalaj — February 13, 2007 @ 10:58 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.