Trying to Create a Toolbar using the class ToolBar
-
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 -
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 BobWhere 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.
-
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.
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
-
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
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(); -
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();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
ENDObserve 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