Reputation: 2598
In order to add a toolbar to my MFC dialog baced class, I tried all ways of adding resources>toolbars
but they didn't work. Finally I came up to the point to create a toolbar dynamically. This is the code that I have used:
Resource.h
#define IDB_PanTbrBtn 139
#define IDB_NewTbrBtn 140
#define IDB_ZoomInTbrBtn 141
#define IDB_ZoomOutTbrBtn 142
#define IDC_FirstToolBar 1011
#define IDC_NEWTBRBTN 1012
#define IDC_ZOOMINTBRBTN 1013
#define IDC_ZOOMOUTTBRBTN 1014
#define IDC_PANTBRBTN 1015
InitialJobProject2Dlg.h : header file for the dialog baced project
#pragma once
#include "WndResizer.h"
#include "afxdlgs.h"
#include "FilesWorkFlow.h"
#include "OpenGLControl.h"
CWndResizer m_resizer;
CMFCToolBar m_FirstToolBar;
FilesWorkFlow *m_files;
COpenGLControl *m_oglWindow;
InitialJobProject2Dlg.cpp : the codes related to the toolbar in the function OnInitDialog()
bool bAnchored = false;
bAnchored = m_resizer.Hook(this);
assert(bAnchored);
bool ToolbarCreated = m_FirstToolBar.CreateEx(this, AFX_DEFAULT_TOOLBAR_STYLE, 100 );
if(ToolbarCreated)
{
m_FirstToolBar.SetDlgCtrlID(IDC_FirstToolBar);
bAnchored = m_resizer.SetAnchor(IDC_FirstToolBar,ANCHOR_LEFT | ANCHOR_TOP);
assert(bAnchored);
m_FirstToolBar.SetPaneStyle(m_FirstToolBar.GetPaneStyle() & ~(CBRS_GRIPPER|CBRS_SIZE_DYNAMIC|CBRS_BORDER_ANY));
VERIFY(m_FirstToolBar.LoadBitmapW(IDB_NewTbrBtn));
VERIFY(m_FirstToolBar.GetImages()->Load(IDB_NewTbrBtn,nullptr,TRUE));
int imageIndex = m_FirstToolBar.GetImages()->GetCount();
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_NEWTBRBTN,imageIndex));
VERIFY(m_FirstToolBar.LoadBitmapW(IDB_PanTbrBtn));
VERIFY(m_FirstToolBar.GetImages()->Load(IDB_PanTbrBtn,nullptr,TRUE));
imageIndex = m_FirstToolBar.GetImages()->GetCount();
m_FirstToolBar.InsertButton( CMFCToolBarButton( IDC_PANTBRBTN,imageIndex));
VERIFY(m_FirstToolBar.LoadBitmapW(IDB_ZoomInTbrBtn));
VERIFY(m_FirstToolBar.GetImages()->Load(IDB_ZoomInTbrBtn,nullptr,TRUE));
imageIndex = m_FirstToolBar.GetImages()->GetCount();
m_FirstToolBar.InsertButton( CMFCToolBarButton( IDC_ZOOMINTBRBTN,imageIndex));
VERIFY(m_FirstToolBar.LoadBitmapW(IDB_ZoomOutTbrBtn));
VERIFY(m_FirstToolBar.GetImages()->Load(IDB_ZoomOutTbrBtn,nullptr,TRUE));
imageIndex = m_FirstToolBar.GetImages()->GetCount();
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_ZOOMOUTTBRBTN,imageIndex));
CSize size = m_FirstToolBar.CalcFixedLayout( FALSE, TRUE );
m_FirstToolBar.SetWindowPos( NULL, 0, 0, size.cx + 10, size.cy + 10 , SWP_NOACTIVATE | SWP_NOZORDER );
}
this is my project's resource view:
and this one is the res folder of my program:
images that I want to be shown as the icons of toolbar buttons are 48x48 ,32 bit depth bitmap images but I had the same problem with 24x24 ones
The problem is when I run the program:
IT's clear that there is just one button as the toolbar button but I have inserted four buttons dynamically as you see in the code.
and the image is not shown even for this known button.
My code does not have any compiler or run-time error so I don't understand what is happening and whats the problem?
and since I'm new to MFC(this is my first program of MFC) I didn't know that adding a toolbar to a dialog-based application is such a hard task!!!! **
**So I created a dialog-based project at start and now that my program has been developed, I do need a toolbar
please help me. this is the forth question I have posted about adding a toolbar on a dialog based MFC application.
after writing the code to add a toolbar to a dialog-based mfc the dialog doesn't run
My toolbar on a dialog based mfc application is not shown
having trouble with LoadToolBarEx function of the CMFCToolBar class and set ID for the COpenGLControl class
But the problem has not been completely solved yet?
**Please introduce me a good reference that has taught adding toolbars to the MFC dialogs step by step from the scratch and has been useful for yourself
Oh and if there's a need to my project, it is downloadable here
as @Edward Clements suggested I changed the code to this but nothing changed.
VERIFY(m_FirstToolBar.LoadBitmap(IDB_NewTbrBtn));
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_NEWTBRBTN,1));
VERIFY(m_FirstToolBar.LoadBitmap(IDB_PanTbrBtn));
m_FirstToolBar.InsertButton( CMFCToolBarButton( IDC_PANTBRBTN,2));
VERIFY(m_FirstToolBar.LoadBitmap(IDB_ZoomInTbrBtn));
m_FirstToolBar.InsertButton( CMFCToolBarButton( IDC_ZOOMINTBRBTN,3));
VERIFY(m_FirstToolBar.LoadBitmap(IDB_ZoomOutTbrBtn));
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_ZOOMOUTTBRBTN,4));
Upvotes: 0
Views: 1704
Reputation: 5142
Firstly, according to the MFC sources, LoadBitmap()
[NOT LoadBitmapW()
, that seems to happen because of VS Intellisense picking up a #define from WinUser.h] adds the bitmap to the image list, so calling m_FirstToolBar.GetImages()->Load()
will load the image twice.
Secondly, the InsertButton()
should specify the index of the image of the button, m_FirstToolBar.GetImages()->GetCount()
will always point to an invalid index value.
VERIFY(m_FirstToolBar.LoadBitmap(IDB_NewTbrBtn));
VERIFY(m_FirstToolBar.LoadBitmap(IDB_PanTbrBtn));
VERIFY(m_FirstToolBar.LoadBitmap(IDB_ZoomInTbrBtn));
VERIFY(m_FirstToolBar.LoadBitmap(IDB_ZoomOutTbrBtn));
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_NEWTBRBTN, 0));
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_PANTBRBTN, 1));
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_ZOOMINTBRBTN, 2));
m_FirstToolBar.InsertButton(CMFCToolBarButton(IDC_ZOOMOUTTBRBTN, 3));
Upvotes: 1