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. How to change a single toolbar icon at runtime?

How to change a single toolbar icon at runtime?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
2 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.
  • M Offline
    M Offline
    madmax0001
    wrote on last edited by
    #1

    Hello, I have a toolbar with several icons in it and there is one icon that I must change at runtime. How can I manage this? I have tried someting like this, but the code crashes...

    CToolBarCtrl& tb = m\_wndToolBar.GetToolBarCtrl();
    int iButtonCount = tb.GetButtonCount();
    
    CDC dcMemToolbar; 
    CDC dcMemSrc;
    CBitmap SrcBmp;
    CBitmap NewBmp;
    CBitmap\* pOldSrcBmp = NULL;
    CDC\* pDc = GetDC();
    dcMemToolbar.CreateCompatibleDC(pDc);
    dcMemSrc.CreateCompatibleDC(pDc);
    
    SrcBmp.LoadBitmap(IDB\_TOOLBAR);
    pOldSrcBmp = dcMemSrc.SelectObject(&SrcBmp);
    dcMemToolbar.BitBlt(0, 0, iButtonCount\*16, 16, &dcMemSrc, 0, 0, SRCCOPY);	
    
    NewBmp.LoadBitmap(IDB\_INVALID\_ICON);
    dcMemSrc.SelectObject(&NewBmp);
    dcMemToolbar.BitBlt(0, 0, 16, 16, &dcMemSrc, 0, 0, SRCCOPY);	
    
    dcMemSrc.SelectObject(pOldSrcBmp);
    
    m\_wndToolBar.SetBitmap(SrcBmp);
    

    Thank you

    S 1 Reply Last reply
    0
    • M madmax0001

      Hello, I have a toolbar with several icons in it and there is one icon that I must change at runtime. How can I manage this? I have tried someting like this, but the code crashes...

      CToolBarCtrl& tb = m\_wndToolBar.GetToolBarCtrl();
      int iButtonCount = tb.GetButtonCount();
      
      CDC dcMemToolbar; 
      CDC dcMemSrc;
      CBitmap SrcBmp;
      CBitmap NewBmp;
      CBitmap\* pOldSrcBmp = NULL;
      CDC\* pDc = GetDC();
      dcMemToolbar.CreateCompatibleDC(pDc);
      dcMemSrc.CreateCompatibleDC(pDc);
      
      SrcBmp.LoadBitmap(IDB\_TOOLBAR);
      pOldSrcBmp = dcMemSrc.SelectObject(&SrcBmp);
      dcMemToolbar.BitBlt(0, 0, iButtonCount\*16, 16, &dcMemSrc, 0, 0, SRCCOPY);	
      
      NewBmp.LoadBitmap(IDB\_INVALID\_ICON);
      dcMemSrc.SelectObject(&NewBmp);
      dcMemToolbar.BitBlt(0, 0, 16, 16, &dcMemSrc, 0, 0, SRCCOPY);	
      
      dcMemSrc.SelectObject(pOldSrcBmp);
      
      m\_wndToolBar.SetBitmap(SrcBmp);
      

      Thank you

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      Better to handle using CImageList it will be easy and convenient. Please try in this way.

      -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

      My blog - Sharing My Thoughts, An Article - Understanding Statepattern

      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