Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS General Discussion » How To add button at the CExtToolControlBar dynamicly? Collapse All
Subject Author Date
Artur Shingirei Sep 14, 2004 - 7:21 AM

Hello.

How I can Add a button to the toolbar dynamicly?

toolbar created:
//At headr file
CExtToolControlBar        m_wndToolBar;

//at cpp file
if( !m_wndToolBar.Create(
            _T("")"),
            this,
            AFX_IDW_TOOLBAR
            )
        ||
        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)
        )
    {
        TRACE0("Failed to create toolbar\n");
        return -1; // fail to create
    }

Technical Support Sep 15, 2004 - 7:53 AM

Dear Artur,

Your code simply creates a toolbar and loads its buttons from the resources. You can insert any additional button by inserting the following code snippet immediately after "f( !m_wndToolBar.Create(...":

int nButtonsCount =  m_wndToolbar.GetButtonsCount();
// append separator to the end
m_wndToolbar.InsertButton(
   nButtonsCount ++,
   ID_SEPARATOR
);

// append button specified with ID_YOUR_COMMAND
m_wndToolbar.InsertButton(
   nButtonsCount ++,
   ID_YOUR_COMMAND
);

Please note that any command identifier such as ID_YOUR_COMMAND that you are going to use in toolbars must be registered in the command manager. The Prof-UIS toolbar control stores only command identifiers and gets the additional information related to them ( associated icons, tooltips, and etc.) from the command manager. The following code may be used to allocate a new dynamic command identifier in the command manager:
// set it to zero to allow command manager
// to find dynamic identifier atomatically
UINT nCmdID = ID_YOUR_COMMAND;
LPCTSTR strProfileName =
   g_CmdManager->ProfileNameFromWnd(
   m_wndToolbar.GetSafeHwnd()
);
ASSERT( strProfileName != NULL );
CExtCmdItem * pCmdItem =
   g_CmdManager->CmdAllocPtr(
   strProfileName,
   nCmdID
);
ASSERT( pCmdItem != NULL );
   pCmdItem->m_sTipTool = _T("tooltip text");
   pCmdItem->m_sTipStatus = _T("statusbar text");
   pCmdItem->m_sMenuText = _T("text in menu");
   pCmdItem->m_sToolbarText = _T("text in toolbar");
// you may also load an 16x16 icon
// and assign it to the command
HICON hIcon = (HICON)::LoadImage(
   ::AfxGetResourceHandle(),
   MAKEINTRESOURCE(IDI_YOUR_ICON),
   IMAGE_ICON,
   16,
   16,
   0
);
ASSERT( hIcon != NULL );
g_CmdManager->CmdSetIcon(
   strProfileName,
   pCmdItem->m_nCmdID,
   hIcon,
   false // use hIcon handle as is
);

You can also allocate new commands and insert buttons into toolbars dynamically at run-time, after the execution of CMainFrame::OnCreate is completed. For this purpose, the same code lines may be used but then you need to recalculate the layout of toolbars’ parent window:
CFrameWnd * pFrame =
   m_wndToolbar.GetParentFrame();
ASSERT_VALID( pFrame );
pFrame->RecalcLayout();

Finally, if you want to remove a button from a toolbar, you need to know the button’s index first.
int nButtonIndex =
   wndToolbar.CommandToIndex( nCmdID )
if( nButtonIndex >= 0 )
   wndToolbar.RemoveButton( nButtonIndex ); 

Artur Shingirei Sep 20, 2004 - 3:11 AM

Hello.
Thank You for fast replay.

I have a following trouble:
I try to add Button to the ToolBar from DLL.

//Code at DLL:
extern "C" DLL_API void InitToolBar(CExtToolControlBar* pToolBar);

DLL_API void InitToolBar(CExtToolControlBar* pToolBar)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    int nButtonsCount = pToolBar->GetButtonsCount();
    
    pToolBar->InsertButton(nButtonsCount ++,ID_SEPARATOR);
}
At the main APP:
...

//Init hMyDll
// Create ToolBar

typedef void (*DLLPOINTERINIT_TOOLBAR) (CExtToolControlBar*);
pInitToolBar = (DLLPOINTERINIT_TOOLBAR)GetProcAddress(hMyDll, _T("InitToolBar"));


...
void CMainFrame::OnTest()
{
    pInitToolBar(&m_wndToolBar);
}



At the moment of a call ’pToolBar->GetButtonsCount();’ returns 40519288 my ToolBar at that time has 16 buttons.
at the next calling of ’pToolBar->InsertButton(nButtonsCount ++,ID_SEPARATOR);’ program falls.

Can You help me at that situation?