Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CToolBar basic questions

CToolBar basic questions

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestionloungeworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PatrykDabrowski
    wrote on last edited by
    #1

    Hi:) I have some (probably) basic questions about CToolbar class, just point me somewhere (article, code or just general idea) How to change button's position? - I would like to move them closer, without a single pixel of free space between. (or just 2 pixels right and bottom for 'press' effect) How to disable this square box around button's bitmap when I hover/press the button? And finally what message/base class member function should I override to make my bitmaps transparent? I have already handled WM_ERASEBKGND message but now my buttons have plain background instead of a bitmap. I've seen some articles about it but the solutions are not so simple (many lines of code, message handlers ect.) Is there a simple way how to draw CBitmap 24bit bitmap on a CToolBar control? Assuming some transparency RGB value in my input bitmap?? (Let's assume Windows 2000+ environment) Thanks in advance!

    M 1 Reply Last reply
    0
    • P PatrykDabrowski

      Hi:) I have some (probably) basic questions about CToolbar class, just point me somewhere (article, code or just general idea) How to change button's position? - I would like to move them closer, without a single pixel of free space between. (or just 2 pixels right and bottom for 'press' effect) How to disable this square box around button's bitmap when I hover/press the button? And finally what message/base class member function should I override to make my bitmaps transparent? I have already handled WM_ERASEBKGND message but now my buttons have plain background instead of a bitmap. I've seen some articles about it but the solutions are not so simple (many lines of code, message handlers ect.) Is there a simple way how to draw CBitmap 24bit bitmap on a CToolBar control? Assuming some transparency RGB value in my input bitmap?? (Let's assume Windows 2000+ environment) Thanks in advance!

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      First - Toolbar Controls Overview[^]

      PatrykDabrowski wrote:

      How to change button's position? - I would like to move them closer, without a single pixel of free space between. (or just 2 pixels right and bottom for 'press' effect)

      A combination of TB_SETMETRICS message to set button spacing and using separators between buttons will help for custiomizing spacing.

      PatrykDabrowski wrote:

      How to disable this square box around button's bitmap when I hover/press the button?

      Use image lists to have control over button images.

      PatrykDabrowski wrote:

      And finally what message/base class member function should I override to make my bitmaps transparent?

      This has nothing to do with WM_ERASEBKGND (actually toolbars have the TBSTYLE_TRANSPARENT style so you don't have to do that). For the button images you need to use a masked image list with the mask created using a certain color in the bitmap that represents transparent. For example, this code takes a 24bit source bitmap resource and creates a masked image list from it, using bright green as the transparent color...

      // Load source bitmap resource (24bit, 64x32 (Two 32x32 images))
      CBitmap bitmap;
      bitmap.LoadBitmap(IDB_TOOLBARBITMAP);

      // Create image list - Each image 32x32, initially with 2 images, growby 1
      m_ToolBarImageList.Create(32, 32, ILC_COLOR24|ILC_MASK, 2, 1);

      // Add the bitap to the image list - mask will be created automagically
      m_ToolBarImageList.Add(&bitmap, RGB(0x00,0xFF,0x00));

      When the toolbar is created set its image list to the image list created above:

      static TBBUTTON ToolBarButtonDescs[] =
      {
      {0, ID_BUTTON1, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
      {1, ID_BUTTON2, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0}
      };

      m_ToolBar.CreateEx(this, TBSTYLE_FLAT);
      m_ToolBar.SetSizes(CSize(39,38), CSize(32,32));
      m_ToolBar.GetToolBarCtrl().SetImageList(&m_ToolBarImageList);
      m_ToolBar.GetToolBarCtrl().AddButtons(2, ToolBarButtonDescs);

      P 1 Reply Last reply
      0
      • M Mark Salsbery

        First - Toolbar Controls Overview[^]

        PatrykDabrowski wrote:

        How to change button's position? - I would like to move them closer, without a single pixel of free space between. (or just 2 pixels right and bottom for 'press' effect)

        A combination of TB_SETMETRICS message to set button spacing and using separators between buttons will help for custiomizing spacing.

        PatrykDabrowski wrote:

        How to disable this square box around button's bitmap when I hover/press the button?

        Use image lists to have control over button images.

        PatrykDabrowski wrote:

        And finally what message/base class member function should I override to make my bitmaps transparent?

        This has nothing to do with WM_ERASEBKGND (actually toolbars have the TBSTYLE_TRANSPARENT style so you don't have to do that). For the button images you need to use a masked image list with the mask created using a certain color in the bitmap that represents transparent. For example, this code takes a 24bit source bitmap resource and creates a masked image list from it, using bright green as the transparent color...

        // Load source bitmap resource (24bit, 64x32 (Two 32x32 images))
        CBitmap bitmap;
        bitmap.LoadBitmap(IDB_TOOLBARBITMAP);

        // Create image list - Each image 32x32, initially with 2 images, growby 1
        m_ToolBarImageList.Create(32, 32, ILC_COLOR24|ILC_MASK, 2, 1);

        // Add the bitap to the image list - mask will be created automagically
        m_ToolBarImageList.Add(&bitmap, RGB(0x00,0xFF,0x00));

        When the toolbar is created set its image list to the image list created above:

        static TBBUTTON ToolBarButtonDescs[] =
        {
        {0, ID_BUTTON1, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
        {1, ID_BUTTON2, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0}
        };

        m_ToolBar.CreateEx(this, TBSTYLE_FLAT);
        m_ToolBar.SetSizes(CSize(39,38), CSize(32,32));
        m_ToolBar.GetToolBarCtrl().SetImageList(&m_ToolBarImageList);
        m_ToolBar.GetToolBarCtrl().AddButtons(2, ToolBarButtonDescs);

        P Offline
        P Offline
        PatrykDabrowski
        wrote on last edited by
        #3

        Thanks for help but it seems not working for me... But first, my second question. I was asking how to disable this square "3d-like" frame around my buttons. Image lists do not solve this problem because they are 32x32 pixel and this box is bigger, about 38x38 pixels. I have partialy solved this problem by this code: void CToolBarEx::OnTbnHotItemChange(NMHDR *pNMHDR, LRESULT *pResult) { *pResult = (1-m_bHoverMouse); //0=no change;1=no box around buttons } and now, when I move the mouse over buttons, this box is not drawn but when I press the button it is still there...How to disable it completly?? To my last question, I have almost copy-pasted Your code to my app but the buttons are still opaque. I have a transparent color RGB(255,0,255) in my bitmap containing 6 buttons (192x32 24bit bitmap). Now the transparent color is not directly drawn but replaced by the plain gray (background) color. That would be fine, but I have a background bitmap?!? Maybe the way I'm drawing my background bitmap is wrong??? The drawing code is: BOOL CToolBarEx::OnEraseBkgnd(CDC* pDC) { CRect rect; GetClientRect(&rect); //toolbar rect CBrush *m_brOld = NULL; //Old brush m_brOld = pDC->SelectObject(&m_bgBrush); //load my background brush pDC->FillRect(&rect,&m_bgBrush); //fill the whole toolbar pDC->SelectObject(m_brOld); //restore original brush return TRUE; } Maybe I should save somehow this brush into the toolbar's DC for proper background drawing when the control draw its buttons?? How to "tell" the control to use bitmap/brush to draw its background?? Thanks again:) I'm waiting for Your response...

        M 1 Reply Last reply
        0
        • P PatrykDabrowski

          Thanks for help but it seems not working for me... But first, my second question. I was asking how to disable this square "3d-like" frame around my buttons. Image lists do not solve this problem because they are 32x32 pixel and this box is bigger, about 38x38 pixels. I have partialy solved this problem by this code: void CToolBarEx::OnTbnHotItemChange(NMHDR *pNMHDR, LRESULT *pResult) { *pResult = (1-m_bHoverMouse); //0=no change;1=no box around buttons } and now, when I move the mouse over buttons, this box is not drawn but when I press the button it is still there...How to disable it completly?? To my last question, I have almost copy-pasted Your code to my app but the buttons are still opaque. I have a transparent color RGB(255,0,255) in my bitmap containing 6 buttons (192x32 24bit bitmap). Now the transparent color is not directly drawn but replaced by the plain gray (background) color. That would be fine, but I have a background bitmap?!? Maybe the way I'm drawing my background bitmap is wrong??? The drawing code is: BOOL CToolBarEx::OnEraseBkgnd(CDC* pDC) { CRect rect; GetClientRect(&rect); //toolbar rect CBrush *m_brOld = NULL; //Old brush m_brOld = pDC->SelectObject(&m_bgBrush); //load my background brush pDC->FillRect(&rect,&m_bgBrush); //fill the whole toolbar pDC->SelectObject(m_brOld); //restore original brush return TRUE; } Maybe I should save somehow this brush into the toolbar's DC for proper background drawing when the control draw its buttons?? How to "tell" the control to use bitmap/brush to draw its background?? Thanks again:) I'm waiting for Your response...

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          PatrykDabrowski wrote:

          I was asking how to disable this square "3d-like" frame around my buttons. Image lists do not solve this problem because they are 32x32 pixel and this box is bigger, about 38x38 pixels.

          The closest I think you'll get is using the TBSTYLE_FLAT style. But you'll still see the hot- track, 3d button outline indication. The buttons are 7 pixels wider and 6 pixels higher than the bitmaps. I tested to see if this could be changed but the system defaulted them. OnEraseBkgnd is fine but the buttons are not drawn transparently. The images on the buttons are. This is the behavior of the toolbar control. The 3d box around buttons indicates to the user that it's a button. You could use the TBSTYLE_CUSTOMERASE style and respond to NM_CUSTOMDRAW messages. In the passed NMTBCUSTOMDRAW struct there's a NMCUSTOMDRAW struct member that you can look at to see if a button is about to be drawn hot and if so, change the state to normal so it gets drawn normally. You could also draw the buttons yourself. It sounds like you may just need to transparently draw images on a background and respond to clicks within the images. Maybe that's a viable alternative route as well(?) Mark

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups