Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21243

Problem With Changing Form Style

$
0
0
Having a problem changing window style using below code. First, I would like to mention it only works if I set the Form.BorderStyle = 2. Here's what it does: it changes the Form to have no titlebar, no maxbutton, and not sizable(borderless). When the app is minimized (using custom buttons) the app goes to the taskbar and it allows the app to be toggled from Min/Restore and also show the sysmenu. This is exactly what I want but there is a slight side affect which I don't like. I need the app icon in the taskbarbar to show some info in it and the only way I have been able to do this is to put that info in the Form.Caption. When I do this the whole app jerks which is a very undesirable appearance.

The Sub SetFormStyle is called at Form Load one time only. This sub calls the other subs to set the desired results.

Is there anything I can do to change the below code or some other approach or method to get the same end results and still be able to initialize Form.Caption without the jerking of the app?


Code:

Private Sub SetFormStyle()
 'No Titlebar
 FlipBit WS_CAPTION, False
 
 'Not Sizable
 EnableMenuItem SysMenuItems.smsize, False
 FlipBit WS_THICKFRAME, False

 'No Max Button
 FlipBit WS_MAXIMIZEBOX, False
 EnableMenuItem SysMenuItems.smmaximize, False
End Sub

Private Function FlipBit(ByVal Bit As Long, ByVal Value As Boolean) As Boolean
 Dim nStyle As Long
 
 ' Retrieve current style bits.
 nStyle = GetWindowLong(Me.hWND, GWL_STYLE)
 
 ' Attempt to set requested bit On or Off,
 ' and redraw
 If Value Then
  nStyle = nStyle Or Bit
 Else
  nStyle = nStyle And Not Bit
 End If
 
 Call SetWindowLong(Me.hWND, GWL_STYLE, nStyle)
 Call Redraw
 
 ' Return success code.
 FlipBit = (nStyle = GetWindowLong(Me.hWND, GWL_STYLE))
End Function

Private Sub EnableMenuItem(ByVal MenuItem As SysMenuItems, Optional ByVal Enabled As Boolean = True)
 Dim hMenu As Long
 Dim nPosition As Long
 Dim uFlags As Long
 Dim mii As MENUITEMINFO
 
 Const HighBit As Long = &H8000&
 
 ' Retrieve handle to system menu.
 hMenu = GetSystemMenu(Me.hWND, False)
 
 ' Translate ID to position.
 nPosition = GetMenuItemPosition(MenuItem)
 
 If nPosition >= 0 Then
  ' Initialize structure.
  mii.cbSize = Len(mii)
  mii.fMask = MIIM_STATE Or MIIM_ID Or MIIM_DATA Or MIIM_TYPE
  mii.dwTypeData = String$(80, 0)
  mii.cch = Len(mii.dwTypeData)
  Call GetMenuItemInfo(hMenu, nPosition, MF_BYPOSITION, mii)
     
  ' Set appropriate state.
  If Enabled Then
    mii.fState = MF_ENABLED
  Else
    mii.fState = MF_GRAYED
  End If
 
  ' New ID uses highbit to signify that
  ' the menu item is enabled.
  If Enabled Then
    mii.wID = MenuItem
  Else
    mii.wID = MenuItem And Not HighBit
  End If
 
  ' Modify the menu!
  mii.fMask = MIIM_STATE Or MIIM_ID
  Call SetMenuItemInfo(hMenu, nPosition, MF_BYPOSITION, mii)
 End If
End Sub

Private Function GetMenuItemPosition(ByVal MenuItem As SysMenuItems) As Long
 Dim hMenu As Long
 Dim ID As Long
 Dim i As Long
 
 Const HighBit As Long = &H8000&
 
 ' Default to returning -1 in case of
 ' failure, since menu is 0-based.
 GetMenuItemPosition = -1
 
 ' Retrieve handle to system menu.
 hMenu = GetSystemMenu(Me.hWND, False)

 ' Loop through system menu, scanning
 ' for requested standard menu item.
 For i = 0 To GetMenuItemCount(hMenu) - 1
  ID = GetMenuItemID(hMenu, i)
     
  If ID = MenuItem Then
    ' Return position of normal
    ' enabled menu item.
    GetMenuItemPosition = i
    Exit For
  ElseIf ID = (MenuItem And Not HighBit) Then
    ' This item is disabled.
    ' Return position and alter
    ' MenuItem with new ID.
    MenuItem = ID
    GetMenuItemPosition = i
    Exit For
  End If
 Next i
End Function

Private Sub Redraw()
 ' Redraw window with new style.
 Const swpFlags As Long = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE
 SetWindowPos Me.hWND, 0, 0, 0, 0, 0, swpFlags
End Sub


Viewing all articles
Browse latest Browse all 21243

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>