We have "graphical style" command buttons, but they won't accept Common Controls 6 UxTheme adjustments. However I have found a way around this:
Code:
Option Explicit
'Demonstrates using CommandButtons where Style = vbButtonStandard and
'Picture = <some icon> along with a Common Controls 6 manifest to get
'"graphical" buttons with CC6 styling and effects.
'
'Why do this?
'
'It can come in handy when you want to make a "toolbar" without the
'need to deploy any toolbar OCX. Just put them into a PictureBox,
'UserControl, etc.
'
'And some people just like to have icons on buttons here and there.
Private Const BM_SETIMAGE As Long = &HF7&
Private Const IMAGE_ICON As Long = 1
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const GWL_STYLE As Long = -16&
Private Enum BS_TEXTALIGN_STYLES
[_BS_TEXTPOSMASK] = &HF00&
'Note: the next two values seems to be swapped in winuser.h, or at least
'have the opposite meaning when used with an icon, so I have swapped them:
BS_RIGHT = &H100&
BS_LEFT = &H200&
'Note: the next two values seems to be swapped in winuser.h, or at least
'have the opposite meaning when used with an icon, so I have swapped them:
BS_BOTTOM = &H400&
BS_TOP = &H800&
BS_CENTER = &H300&
End Enum
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Sub IconizeButton( _
ByVal CommandButton As CommandButton, _
Optional ByVal TextAlignment As BS_TEXTALIGN_STYLES = BS_BOTTOM)
'Assumes the CommandButton has an icon as its Picture property.
Dim NewStyle As Long
With CommandButton
NewStyle = GetWindowLong(.hWnd, GWL_STYLE) And Not [_BS_TEXTPOSMASK] Or TextAlignment
SetWindowLong .hWnd, GWL_STYLE, NewStyle
SendMessage .hWnd, BM_SETIMAGE, IMAGE_ICON, .Picture.Handle
End With
End Sub
Private Sub Form_Load()
IconizeButton cmdBurn
IconizeButton cmdSave
IconizeButton cmdSmiley
IconizeButton cmdSomeButton, BS_CENTER
IconizeButton cmdSubmit, BS_LEFT
End Sub
These buttons are larger than they need to be, and a normal toolbar would probably use smaller icons anyway:
As it begins
Tabbed to move focus, hovering mouse
Just hovering
I put my "toolbar buttons" within a PictureBox, but I haven't figured out how to theme that to look like a proper toolbar. Maybe the BackColor at least ought to be a different color? There are no borders but a Line control at the "bottom" of the PictureBox is standing in for one here.
Requirements
Should work on Windows XP or later. Reports say no on XP though.
Tested, working on Windows 10, Windows 7, Windows Server 2008 R2.
IDE runs of course look clunky and the icons don't show, but if you apply a CC6 manifest to the IDE that might not happen. I haven't tried this since I don't use an IDE manifest for theming.
Last edited by dilettante; Jun 17th, 2017 at 03:53 PM.
I wonder if somebody can get them to work? There were a lot of things that got fixed and improved in the Longhorn development process though, so I'd expect Vista to work ok.
I took another run at this and found a way to get at theme colors. Not as important as the icons, but i raised the point so I figured I should follow through after I found an answer.
Quite a bit more code, though not a ton, but I won't post it inline. If anyone really cares they can download the source.
Clunky enough to be worth moving to a separate class or static module, and for a toolbar it makes more sense to create a UserControl instead of using a PictureBox. I didn't do any of that here though, it is all inline within the Form module.
Windows NT/2000/XP: Included in Windows NT 4.0 and later.
Windows 95/98/Me: Included in Windows 95 and later.
So that suggests there is something about VB6 Command buttons that is incompatible with BM_SETIMAGE on XP, since the message itself is valid there.
If you look at the current article (link in post #9 above that nobody seems to have read) in Remarks there you will see that you DO NOT want those style bits set.
What may be making this work for Vista+ is that a lot of changes were made to UxTheme.dll and the Win32 controls that make use of it (which of course goes far beyond comctl32.dll itself). Win32 controls defined in other system DLLs check the version of comctl32.dll that is loaded to decide how to behave themselves.
In any case this works on Windows 10, Windows 7, and Windows Server 2008 R2.
I don't have any Vista machines running and Vista died last month anyway. XP has been dead for over 3 years, leave it buried.
If you look at the current article (link in post #9 above that nobody seems to have read) in Remarks there you will see that you DO NOT want those style bits set.
Depends on intent, no? If only icon is wanted, no text, then BS_ICON is required per that article. But point taken if text + icon/bitmap is desired.
Insomnia is just a byproduct of, "It can't be done"