4 Attachment(s)
[VB6] CC6 Graphical Buttons
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:
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.
Re: [VB6] CC6 Graphical Buttons
Re: [VB6] CC6 Graphical Buttons
Quote:
Originally Posted by
xxdoc123
win7 test is ok.
Thanks, good to know.
Re: [VB6] CC6 Graphical Buttons
In XP SP3 the icons don't show up.
Re: [VB6] CC6 Graphical Buttons
Thanks, more useful info.
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.
2 Attachment(s)
Re: [VB6] CC6 Graphical Buttons
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.
This was very helpful, though hardly a cookbook:
Custom Controls in Win32 API: Visual Styles
1 Attachment(s)
Re: [VB6] CC6 Graphical Buttons
On XP, the effect of CC6 Graphical Buttons is as follows:
Re: [VB6] CC6 Graphical Buttons
For those still using XP, if someone could verify that the button has the proper style bits set, that may or may not be helpful.
For example, in dilettante's code...
Code:
If (NewStyle And &H40) Then Debug.Print "Button has BS_ICON style bit"
If (NewStyle And &H80) Then Debug.Print "Button has BS_BITMAP style bit"
Oh, and another thing... If the button has no text, I'm guessing the icon will show in XP?
Re: [VB6] CC6 Graphical Buttons
Good point.
See the table under Remarks at BM_SETIMAGE message that shows the result of different combinations.
Edit:
That seems odd too, because getting only the text doesn't seem possible according to that table if BM_SETIMAGE was sent.
Or maybe XP is just not described there, since MSDN says:
Quote:
Minimum supported client: Windows Vista [desktop apps only]
Re: [VB6] CC6 Graphical Buttons
Quote:
Originally Posted by
LaVolpe
For those still using XP, if someone could verify that the button has the proper style bits set, that may or may not be helpful.
For example, in dilettante's code...
Code:
If (NewStyle And &H40) Then Debug.Print "Button has BS_ICON style bit"
If (NewStyle And &H80) Then Debug.Print "Button has BS_BITMAP style bit"
Oh, and another thing... If the button has no text, I'm guessing the icon will show in XP?
LaVolpe, I've tested it in dilattante's IconizeButton procedure on XP, and the results are as follows:
(NewStyle And & H40) = False
(NewStyle And & H80) = False
Re: [VB6] CC6 Graphical Buttons
I am not sure whether this is relevant... has it got to do with comctl32.dll v6.1 or higher is required for picture and caption?
Re: [VB6] CC6 Graphical Buttons
Quote:
Originally Posted by
dreammanor
LaVolpe, I've tested it in dilattante's IconizeButton procedure on XP, and the results are as follows:
(NewStyle And & H40) = False
(NewStyle And & H80) = False
Easy enough to test then for XP users: Ensure NewStyle includes &H40, i.e.,
Code:
NewStyle = GetWindowLong(.hWnd, GWL_STYLE) And Not [_BS_TEXTPOSMASK] Or TextAlignment or &H40
However, setting that style after the button is created may not take effect
Edited: I assume you used a common controls v6 manifest?
Re: [VB6] CC6 Graphical Buttons
I believe comctl32.dll ver6.1 is not available in WinXP.
Re: [VB6] CC6 Graphical Buttons
If you check older versions of MSDN:
Quote:
BM_SETIMAGE
Requirements :
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.
Re: [VB6] CC6 Graphical Buttons
Tested. Looks fine on Vista.
Re: [VB6] CC6 Graphical Buttons
Quote:
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.