CMFCToolBarComboBoxButton::GetByCmd returns NULL
-
I created a CMFCToolBarComboBoxButton in a CMFCToolBar as following:
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM lp)
{
UINT uiToolBarId = (UINT) wp;
if (uiToolBarId == IDR_TOOLBAR_FILES)
{
CMFCToolBarComboBoxButton omboButton1(IDC_SRC_PRO_LIST, GetCmdMgr ()->GetCmdImage(IDC_SRC_PRO_LIST, FALSE), CBS_DROPDOWNLIST);comboButton1.EnableWindow(FALSE); comboButton1.SetCenterVert(); comboButton1.SetDropDownHeight(200); comboButton1.SetFlatMode(); m\_wndFilesBar.ReplaceButton (IDC\_SRC\_PRO\_LIST, comboButton1); }
}
It runs well. But when I want to add some content into it from another thread.I defined a function:
int CMainFrame::CBAddItem(LPCTSTR lpszItem, DWORD_PTR dwData)
{
int r = -1;
CMFCToolBarComboBoxButton* pSrcCombo = CMFCToolBarComboBoxButton::GetByCmd (IDC_SRC_PRO_LIST);
if(pSrcCombo)
{
r = pSrcCombo->AddItem(lpszItem, dwData);
}
return r;
}The function CMFCToolBarComboBoxButton::GetByCmd always return NULL. And I traced this function, found:
int __stdcall CMFCToolBar::GetCommandButtons(UINT uiCmd, CObList& listButtons)
{
.....
for (POSITION posTlb = afxAllToolBars.GetHeadPosition(); posTlb != NULL;)
{
CMFCToolBar* pToolBar = (CMFCToolBar*) afxAllToolBars.GetNext(posTlb);
ENSURE(pToolBar != NULL);if (CWnd::FromHandlePermanent(pToolBar->m\_hWnd) != NULL)
CWnd::FromHandlePermanent(pToolBar->m_hWnd) always return NULL. So why?
-
I created a CMFCToolBarComboBoxButton in a CMFCToolBar as following:
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM lp)
{
UINT uiToolBarId = (UINT) wp;
if (uiToolBarId == IDR_TOOLBAR_FILES)
{
CMFCToolBarComboBoxButton omboButton1(IDC_SRC_PRO_LIST, GetCmdMgr ()->GetCmdImage(IDC_SRC_PRO_LIST, FALSE), CBS_DROPDOWNLIST);comboButton1.EnableWindow(FALSE); comboButton1.SetCenterVert(); comboButton1.SetDropDownHeight(200); comboButton1.SetFlatMode(); m\_wndFilesBar.ReplaceButton (IDC\_SRC\_PRO\_LIST, comboButton1); }
}
It runs well. But when I want to add some content into it from another thread.I defined a function:
int CMainFrame::CBAddItem(LPCTSTR lpszItem, DWORD_PTR dwData)
{
int r = -1;
CMFCToolBarComboBoxButton* pSrcCombo = CMFCToolBarComboBoxButton::GetByCmd (IDC_SRC_PRO_LIST);
if(pSrcCombo)
{
r = pSrcCombo->AddItem(lpszItem, dwData);
}
return r;
}The function CMFCToolBarComboBoxButton::GetByCmd always return NULL. And I traced this function, found:
int __stdcall CMFCToolBar::GetCommandButtons(UINT uiCmd, CObList& listButtons)
{
.....
for (POSITION posTlb = afxAllToolBars.GetHeadPosition(); posTlb != NULL;)
{
CMFCToolBar* pToolBar = (CMFCToolBar*) afxAllToolBars.GetNext(posTlb);
ENSURE(pToolBar != NULL);if (CWnd::FromHandlePermanent(pToolBar->m\_hWnd) != NULL)
CWnd::FromHandlePermanent(pToolBar->m_hWnd) always return NULL. So why?
Check if you met the requirements described at the MSDN page Multithreading: Programming Tips[^]. A common solution to access MFC objects from other threads is posting user defined messages. Then the manipulation of the object occurs inside the thread that owns the MFC object. If your thread is a worker thread see the CP article Using Worker Threads[^] for examples.