Results 1 to 29 of 29

Thread: VB6 - Change your Toolbar Background (Color or Picture)

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    VB6 - Change your Toolbar Background (Color or Picture)



    With this code you can change a Toolbar backcolor (Windows Common Controls 5 or 6).
    You can also use a picture (bitmap) as background, here I'll do both things.

    I recommend downloading the example attached, but I'll also show the code here, I used Common Controls 6 toolbars for this example, minor changes are needed if you want to use Common Controls 5 toolbars.

    IN THE FORM
    - Add 2 Toolbars, change Toolbar1 style to FLAT, Toolbar2 style must remain STANDARD
    - Add a Picture box and add a picture to it.
    - Paste this code:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        ApplyChanges
    End Sub
    
    Private Sub ApplyChanges()
    '=========================
        Dim LngNew As Long
        'Use a picture with the FLAT TB (Toolbar1)
        LngNew = CreatePatternBrush(Picture1.Picture.Handle) 'Creates the background from a Picture Handle
        ChangeTBBack Toolbar1, LngNew, enuTB_FLAT    
        'Change Backcolor of the STANDARD TB (Toolbar2)
        LngNew = CreateSolidBrush(RGB(240, 120, 120))        'Creates the background from a Color (Long)
        ChangeTBBack Toolbar2, LngNew, enuTB_STANDARD
        'Refresh Screen to see changes
        InvalidateRect 0&, 0&, False
    End Sub
    IN A MODULE
    Code:
    Option Explicit
    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
        ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
        ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" ( _
                    ByVal hwnd As Long, ByVal nindex As Long, ByVal dwnewlong As Long) As Long
    Public Declare Function InvalidateRect Lib "user32" _
                    (ByVal hwnd As Long, lpRect As Long, ByVal bErase As Long) As Long
    Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Public Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
    
    Public Enum enuTBType
        enuTB_FLAT = 1
        enuTB_STANDARD = 2
    End Enum
    
    Private Const GCL_HBRBACKGROUND = (-10)
    
    Public Sub ChangeTBBack(TB As Object, PNewBack As Long, pType As enuTBType)
    Dim lTBWnd      As Long
        Select Case pType
            Case enuTB_FLAT     'FLAT Button Style Toolbar
                'Apply directly to TB Hwnd
                DeleteObject SetClassLong(TB.hwnd, GCL_HBRBACKGROUND, PNewBack)        
            Case enuTB_STANDARD 'STANDARD Button Style Toolbar
                lTBWnd = FindWindowEx(TB.hwnd, 0, "msvb_lib_toolbar", vbNullString) 'Find Hwnd first
                DeleteObject SetClassLong(lTBWnd, GCL_HBRBACKGROUND, PNewBack)      'Set new Back
        End Select
    End Sub
    '===========================================================================================
    ' If you want to use Win Common Control 5 Toolbars, use "ToolbarWindow32" instead of
    ' "msvb_lib_toolbar". Win Common Control 5 Toolbars can't be FLAT, they are always STANDARD,
    ' so use enuTB_STANDARD when you call this
    '===========================================================================================
    More info about this
    - This will also work with xp styles (manifest file).
    - If the picture is smaller than the toolbar the pattern will be repeated, if its too big, you will see just part of it, so I recomend to make your bmp or jpg picture the same size of the toolbar. You can also let it repeat the pattern as I did in the project attached.
    - Changes will remain visible in design mode until you close VB IDE.
    - If you apply this to a toolbar in your app with a given style (i.e: Flat) all the flat toolbars and the controls that contain flat toolbars with the same Comon Controls version will also be modified, (but JUST inside your app).
    Example: Common Dialog Control uses a vertical toolbar (WCC version 5).

    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by jcis; Aug 1st, 2012 at 06:56 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    I used your code in the project I'm working on at the moment, and one of the other programmers happen to be in my cube while I was running a test, and wanted to know if I was using a third party menu control.

  3. #3

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    And I was wrong when I said it doesn't work with XP styles (manifest file), now I tryied again and it works, with flat or standard toolbars, also with color or bitmaps.

  4. #4
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    jcis
    Hi! I just try your code, it's good idea!
    But I found one big bug!
    When I run project, it look great. But when I stop application and return to IDE, toolbar have picture! Look attached screenshot.
    How resolve this problem?
    I just want to make an checkbox on the form and enable or disable picture on toolbar.

    PS: sorry for very bad english.
    Attached Images Attached Images   

  5. #5
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    I test your sample on VB6 SP6, W2k SP4

  6. #6

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Quote Originally Posted by pantalone
    jcis
    Hi! I just try your code, it's good idea!
    But I found one big bug!
    When I run project, it look great. But when I stop application and return to IDE, toolbar have picture!
    How resolve this problem?
    Hi Patalone, As I said in my first post..
    - Changes will remain visible in design mode until you close VB IDE.

    That's not a bug, nor a problem, isn't it better to have the Toolbar Background in design mode skinned as at runtime? I think so. Anyway, you can change this by restoring original TB color before closing your App, see the following example.

    Ok, you want to enable or disable picture at runtime with a checkbox,
    I attached a project for that, so take a look.
    For this you just have to select another brush, apply changes and call invalidateRect aPI again. Example..
    In this example I used GetSysColorBrush API, so I can change the Toolbar backcolor back to original control color (vbButtonFace)
    VB Code:
    1. Option Explicit
    2.  
    3. 'New API add this one with the rest, in the module, declare it Public there
    4. Private Declare Function GetSysColorBrush Lib "user32" (ByVal nIndex As Long) As Long
    5.  
    6. Dim LngNew As Long
    7.  
    8. Private Sub Check1_Click()
    9.     If Check1.Value = vbChecked Then
    10.         'Apply Picture
    11.         LngNew = CreatePatternBrush(Picture1.Picture.Handle)
    12.     Else
    13.         'Restore normal TB Background
    14.         LngNew = GetSysColorBrush(vbButtonFace)
    15.     End If
    16.     ApplyChanges
    17. End Sub
    18.  
    19. Private Sub ApplyChanges()
    20.     ChangeTBBack Toolbar1, LngNew, enuTB_FLAT
    21.     InvalidateRect 0&, 0&, False 'Refresh Screen to see changes
    22. End Sub
    23.  
    24. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    25.     'Restore normal TB Background
    26.     LngNew = GetSysColorBrush(vbButtonFace)
    27.     ApplyChanges
    28. End Sub
    Check out the project attached also.
    Attached Files Attached Files
    Last edited by jcis; Feb 18th, 2006 at 12:29 AM.

  7. #7
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Thank you very much!

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    FYI - if you type in "change backcolor" toolbar vb6 into the Google search box, this is the very first link it returns.

    Very cool!

  9. #9
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Great work. Have you any idea how to get toolbarbuttons to be transparent as well as the toolbar background ?

    Also not sure why the call to DeleteObject is needed ? According to the docs SetClassLong returns the previous value for the target offset (in this case GCL_HBRBACKGROUND) not a handle as required by DeleteObject. In my case the SetClassLong call returns 16 so DeleteObject fails.

    Jon

  10. #10
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Is somebody have method how to change menu background like toolbar in this sample?

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Quote Originally Posted by pantalone
    Is somebody have method how to change menu background like toolbar in this sample?
    Do you mean change the menu backcolor?

  12. #12
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    No! I know how to change the menu background color. I need use a picture as background.

  13. #13
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    I found the bug in the sample with toolbar: if form have one more toolbar same type then this toolbar change background too! How we can select toolbar for apply background?

    PS: sorry for bad english.

  14. #14

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Quote Originally Posted by pantalone
    I found the bug in the sample with toolbar: if form have one more toolbar same type then this toolbar change background too! How we can select toolbar for apply background?
    PS: sorry for bad english.
    I know, no solution for that, I already said in my first post:
    - If you apply this to a toolbar in your app with a given style (i.e: Flat) all the flat toolbars and the controls that contain flat toolbars with the same Comon Controls version will also be modified, (but JUST inside your app).
    Quote Originally Posted by jonrmorgan
    Have you any idea how to get toolbarbuttons to be transparent as well as the toolbar background ?
    Just one way, use a flat toolbar (windows common controls 6, not 5), and change style to flat.

  15. #15
    Addicted Member
    Join Date
    Feb 2006
    Posts
    133

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Quote Originally Posted by jcis
    I know, no solution for that, I already said in my first post
    Sorry, I very unattentive

  16. #16
    Junior Member
    Join Date
    Feb 2006
    Posts
    20

    Post Re: VB6 - Change your Toolbar Background (Color or Picture)

    jcis, you are the man and the one

    i am searching for such a code from along time ago so thanx man..

    there is a small prpblem i have herem i hope its sol. will be easy..
    when i tried to apply this code on a MDI form, i doesn't work at all, so please test it on it, and all developers here please, and tell me if the code may be developed to work with such forms..

    best wishes

  17. #17

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    there is a small problem i have herem i hope its sol. will be easy..
    when i tried to apply this code on a MDI form, i doesn't work at all, so please test it on it, and all developers here please, and tell me if the code may be developed to work with such forms..
    There is no difference, you can change the picture or backcolor of Toolbars in MDIForms and MDIChilds the same way. See the example attached, in this example I used the same picturebox in both forms, it's in the MDI Form.
    Attached Files Attached Files
    Last edited by jcis; May 3rd, 2006 at 01:54 PM.

  18. #18
    New Member
    Join Date
    Jul 2006
    Posts
    2

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    jcis,

    I'm hoping you may be able to help me out. I'm using VB6, and the Common Controls 5 toolbar since the standard v6 one doesn't give XP styles with a manifest. Unfortunately, although I've changed "msvb_lib_toolbar" to "ToolbarWindow32", I can't get the code above to change the background colour of the v5 toolbar.

    I took your example program, added a v5 toolbar, duplicated the ChangeTBBack subroutine to ChangeTBBack5 with "ToolbarWindow32" changed. It's giving the two example v6 toolbars in pre-XP style with the background picture and colour, and the v5 toolbar in XP style with its fixed standard colour (which itself isn't quite as light as the standard XP background).

    Am I missing something obvious or does it not work on Common Controls v5, and if not, since you've mentioned it working with a manifest file, how else (other than using v5) do you get a toolbar to show in XP style?

  19. #19

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    @[CF]Gareth

    You're right, so it doesn't work when the XP style is applied to the toolbar, I had tested just using a Manifest file but I never tested changing the toolbar style also like for ex. is explained in This Article, I think that is what you're doing, and it seems the backcolor or picture can't be changed when doing that.

  20. #20
    New Member
    Join Date
    Jul 2006
    Posts
    2

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    That article you've linked to is exactly the one I found when trying to work out how to use XP styles on a toolbar. Dammit, I hoped I'd missed something.

    Thanks for the reply.

  21. #21
    New Member
    Join Date
    Dec 2007
    Posts
    1

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    jcis,

    I just found your post and it's fantastic! Is there a way to change the color of the toolbar from the blue you have to another color?

    Thanks!

  22. #22

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Quote Originally Posted by zong
    jcis,

    I just found your post and it's fantastic! Is there a way to change the color of the toolbar from the blue you have to another color?

    Thanks!
    There is a PictureBox in the form with a picture inside, that's what the toolbar will have as background, you can add any Picture there.

    In the main Post see where it saids More info about this, 2nd paragraph.

  23. #23
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    I need to change the red color to another color but the color does not seem to go away. how do it do this ?

  24. #24

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Assign Red - Green - Blue in this line:
    Code:
    LngNew = CreateSolidBrush(RGB(240, 120, 120))
    Examples:
    Yellow = RGB(255, 255, 0)
    Green = RGB(0, 255, 0)

  25. #25
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    Hi

    Can you write similar thing for Visual Basic .NET (1.framework)??

    I'm currently working on a soft that cannot be updated to 2. framework and therefore im stuck with ToolBar control.

  26. #26
    New Member nikaka's Avatar
    Join Date
    Jul 2008
    Posts
    1

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    hey
    i've tried to use this in my project. The main toolbar is perfect. the only problem is that in this same project i have others forms with toolbars and these ones have to have a different image. How can i do this? all the toolbars get the image of the first one, or can i change the color of the back like you did with the standard way but in the flat style in the same project?
    please help!






  27. #27
    Lively Member
    Join Date
    Jan 2007
    Posts
    76

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    unable to make it work with Common Controls 5

  28. #28
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: VB6 - Change your Toolbar Background (Color or Picture)

    how can i make the tool bar transparent

  29. #29
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Question Re: VB6 - Change your Toolbar Background (Color or Picture)

    Quote Originally Posted by jcis View Post
    And I was wrong when I said it doesn't work with XP styles (manifest file), now I tryied again and it works, with flat or standard toolbars, also with color or bitmaps.
    can i make the toolbar have gradient just like the coolbar with xp style applied? this could make it look transparent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width