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. Trying to Create a Toolbar using the class ToolBar

Trying to Create a Toolbar using the class ToolBar

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebugginghelp
5 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.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    I am trying to create a tool bar using the class CToolBar. Maybe, I should be using the class CToolbarCtrl. However, I want to understand both so I can make an informed decision what is best for my application. I wrote the following code: BOOL status1 = toolBar.Create( this ); BOOL status2 = toolBar.LoadToolBar( IDR_TOOLBAR ); toolBar.SetButtonStyle( 0, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 1, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 2, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 3, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 4, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 5, TBBS_CHECKBOX ); const UINT idArray[] = { IDM_LINES, IDM_RECTANGLES, IDM_ELLIPSES, IDM_ENLARGE, IDM_ORG, IDM_RESET }; BOOL status3 = toolBar.SetButtons( idArray, 6 ); toolBar.UpdateWindow(); BOOL status4 = toolBar.ShowWindow( SW_SHOW ); toolBar.Invalidate(); this->Invalidate(); This code runs as part of the function that creates (OnCreate) the main window of the application. The return values as stored in status1, status2, status3 and status4 are 1, 1, 1, and 4. However no tool bar is being displayed. Please tell me what I am missing or how I should go about trying to debug this problem? Thanks Bob

    B 1 Reply Last reply
    0
    • B BobInNJ

      I am trying to create a tool bar using the class CToolBar. Maybe, I should be using the class CToolbarCtrl. However, I want to understand both so I can make an informed decision what is best for my application. I wrote the following code: BOOL status1 = toolBar.Create( this ); BOOL status2 = toolBar.LoadToolBar( IDR_TOOLBAR ); toolBar.SetButtonStyle( 0, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 1, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 2, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 3, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 4, TBBS_CHECKBOX ); toolBar.SetButtonStyle( 5, TBBS_CHECKBOX ); const UINT idArray[] = { IDM_LINES, IDM_RECTANGLES, IDM_ELLIPSES, IDM_ENLARGE, IDM_ORG, IDM_RESET }; BOOL status3 = toolBar.SetButtons( idArray, 6 ); toolBar.UpdateWindow(); BOOL status4 = toolBar.ShowWindow( SW_SHOW ); toolBar.Invalidate(); this->Invalidate(); This code runs as part of the function that creates (OnCreate) the main window of the application. The return values as stored in status1, status2, status3 and status4 are 1, 1, 1, and 4. However no tool bar is being displayed. Please tell me what I am missing or how I should go about trying to debug this problem? Thanks Bob

      B Offline
      B Offline
      bolivar123
      wrote on last edited by
      #2

      Where is the variable toolBar declared? Just an off the cuff guess, did you declare toolBar locally in the OnCreate method? If so, it would get destroyed when OnCreate returns.

      B 1 Reply Last reply
      0
      • B bolivar123

        Where is the variable toolBar declared? Just an off the cuff guess, did you declare toolBar locally in the OnCreate method? If so, it would get destroyed when OnCreate returns.

        B Offline
        B Offline
        BobInNJ
        wrote on last edited by
        #3

        Thanks for the response. The tool bar is declared/defined in the class of the main window of the application. Here is how it is defined:

        CToolBar toolBar;
        

        I am wondering if the OnPaint routine of the main window needs to do something special to get the tool bar drawn. Bob

        B 1 Reply Last reply
        0
        • B BobInNJ

          Thanks for the response. The tool bar is declared/defined in the class of the main window of the application. Here is how it is defined:

          CToolBar toolBar;
          

          I am wondering if the OnPaint routine of the main window needs to do something special to get the tool bar drawn. Bob

          B Offline
          B Offline
          bolivar123
          wrote on last edited by
          #4

          You should not have to do anything special in the OnPaint. Give this a try:

          if(!toolBar.Create(
          	NULL,
          	this,
          	IDR\_TOOLBAR,
          	WS\_CHILD | WS\_VISIBLE | CBRS\_TOP |  CBRS\_FLYBY | CBRS\_SIZE\_DYNAMIC
          	| CBRS\_TOOLTIPS
          	| CBRS\_HIDE\_INPLACE | CBRS\_GRIPPER
          	) ||
          	!toolBar.LoadToolBar( IDR\_TOOLBAR )
          	)
          {
          	TRACE0("Failed to create toolbar\\n");
          	return -1;      // fail to create
          }
          

          toolBar.SetButtonStyle( 0, TBBS_CHECKBOX );
          toolBar.SetButtonStyle( 1, TBBS_CHECKBOX );
          toolBar.SetButtonStyle( 2, TBBS_CHECKBOX );
          toolBar.SetButtonStyle( 3, TBBS_CHECKBOX );
          toolBar.SetButtonStyle( 4, TBBS_CHECKBOX );
          toolBar.SetButtonStyle( 5, TBBS_CHECKBOX );

          const UINT idArray[] = {
          IDM_LINES, IDM_RECTANGLES, IDM_ELLIPSES,
          IDM_ENLARGE, IDM_ORG, IDM_RESET
          };

          BOOL status3 = toolBar.SetButtons( idArray, 6 );
          toolBar.UpdateWindow();
          BOOL status4 = toolBar.ShowWindow( SW_SHOW );
          toolBar.Invalidate();
          this->Invalidate();

          B 1 Reply Last reply
          0
          • B bolivar123

            You should not have to do anything special in the OnPaint. Give this a try:

            if(!toolBar.Create(
            	NULL,
            	this,
            	IDR\_TOOLBAR,
            	WS\_CHILD | WS\_VISIBLE | CBRS\_TOP |  CBRS\_FLYBY | CBRS\_SIZE\_DYNAMIC
            	| CBRS\_TOOLTIPS
            	| CBRS\_HIDE\_INPLACE | CBRS\_GRIPPER
            	) ||
            	!toolBar.LoadToolBar( IDR\_TOOLBAR )
            	)
            {
            	TRACE0("Failed to create toolbar\\n");
            	return -1;      // fail to create
            }
            

            toolBar.SetButtonStyle( 0, TBBS_CHECKBOX );
            toolBar.SetButtonStyle( 1, TBBS_CHECKBOX );
            toolBar.SetButtonStyle( 2, TBBS_CHECKBOX );
            toolBar.SetButtonStyle( 3, TBBS_CHECKBOX );
            toolBar.SetButtonStyle( 4, TBBS_CHECKBOX );
            toolBar.SetButtonStyle( 5, TBBS_CHECKBOX );

            const UINT idArray[] = {
            IDM_LINES, IDM_RECTANGLES, IDM_ELLIPSES,
            IDM_ENLARGE, IDM_ORG, IDM_RESET
            };

            BOOL status3 = toolBar.SetButtons( idArray, 6 );
            toolBar.UpdateWindow();
            BOOL status4 = toolBar.ShowWindow( SW_SHOW );
            toolBar.Invalidate();
            this->Invalidate();

            B Offline
            B Offline
            BobInNJ
            wrote on last edited by
            #5

            Thank you for the response. I tried your code and I found that it did not compile due to the fact that it calls Create with four arguments and Create takes at more three. I do not understand what the purpose of the first argument (NULL) is. Therefore, I took the NULL argument to Create. I left the other three (this, IDR_TOOLBAR and the flags) in. After doing so, the code compiled but when run, it did not produce a tool bar. I am thinking that problem might be related to my resource file. Below is the relevant part of my resource file:

            IDR_TOOLBAR BITMAP "TOOLBAR.BMP"
            IDR_TOOLBAR TOOLBAR 16, 15
            BEGIN
            BUTTON IDM_LINES
            BUTTON IDM_RECTANGLES
            BUTTON IDM_ELLIPSES
            SEPARATOR
            BUTTON IDM_SHOWTB
            BUTTON IDM_EXIT
            END

            Observe that the name of the bit map and the name of the tool bar is the same. I believe it is suppose to be that way, right? Any other ideas? Bob

            modified on Monday, June 22, 2009 5:30 PM

            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