CMFCToolBarComboBoxButton无法显示内容 [英] CMFCToolBarComboBoxButton fails to show content

查看:135
本文介绍了CMFCToolBarComboBoxButton无法显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用MFC功能包在工具栏中使用下拉列表组合框.我可以显示组合框,但是它始终处于禁用状态,并且其中没有任何条目.

我如何准备:

I have been trying to use a drop list combo box in a toolbar using the MFC feature pack. I can get the combo box to appear, but it is always disabled and never has any entries in it.

How I prepare it:

// the font used by the controls shown
CFont stockFont;
stockFont.Attach(GetStockObject(DEFAULT_GUI_FONT));

// determine the size each control will need to be
int widthCombo = 0;
{
    CString itemText;
    // create a DC to measure the text size to use
    CWindowDC dc(NULL);
    dc.SaveDC();
    dc.SelectObject(&stockFont);

    // now measure how big the combo box needs to be
    itemText.LoadString(IDS_CONTROL_SIZING_TEXT);
    widthCombo = dc.GetTextExtent(itemText).cx;
    dc.RestoreDC(-1);
    // include borders for the combobox control
    widthCombo += GetSystemMetrics(SM_CXBORDER) * 2; // left and right borders
}

// its ok to use the stack as the toolbar takes a copy of the CMFCToolBarComboBoxButton object
CMFCToolBarComboBoxButton comboBox(ID_REPORT_SELECT_COMBO, -1, CBS_DROPDOWNLIST, widthCombo);
m_wndToolBar.ReplaceButton(ID_REPORT_SELECT_COMBO, comboBox, TRUE);
stockFont.Detach();


因此,我测量了一些默认文本来确定组合框应该有多大,然后我在最后两行用组合框替换了按钮.

到现在为止,所有操作均正常.然后,我尝试填充组合框:


So, I measure some default text to work out how big the combo box should be then I do the 2 lines at the end to replace the button with the combobox.

All works correctly up till this point. I then try and populate the combobox:

// put the list of available report formats into the control
// try and preserve any existing selection at the time of repopulating
// as the previously selected report style may have been renamed or deleted
int iIndex = f_FindToolbarItemIndex(m_wndToolBar, ID_REPORT_SELECT_COMBO);
if (iIndex >= 0)
{
    CMFCToolBarButton * pButton = m_wndToolBar.GetButton(iIndex);
    CMFCToolBarComboBoxButton * pComboButton = dynamic_cast<CMFCToolBarComboBoxButton*>(pButton);

    int sel = pComboButton->GetCurSel();
    if (sel != CB_ERR)
    {
        sel = pComboButton->GetItemData(sel); // index of selected report configuration
    }
    pComboButton->RemoveAllItems();
    // now add the report styles available
    DCBL::ReportLayoutRepository * repository = DCBL::ReportLayoutRepository::GetInstance();
    ASSERT(repository != NULL);

    size_t count = repository->NumReportLayouts();

    sel = min(sel, count - 1);  // limit selection to valid range
    sel = max(0, sel);          // possibly no previous selection

    int selectIndex = 0;
    for (size_t index = 0 ; index < count ; ++index)
    {
        CString name = repository->Layout(index).Name().c_str();
        int itemIndex = pComboButton->AddItem(name, index); // sort style
        if (index == sel)
        {
            selectIndex = index;
        }
    }
    pComboButton->SelectItem(selectIndex);
    // check that the items are being added
    int x = pComboButton->GetCount();



最后,在我的文本案例中,x的值为17,因此17项已添加到工具栏中的组合框.但是,它始终显示为禁用且没有任何项目.

在功能部件包中,这些东西应该很容易,但是我一直被这个东西撞在墙上.有任何建议或可能遗漏的步骤吗?



At the end, the value of x is 17 in my text case, so 17 items have been added to the combobox thats in the toolbar. Yet it always shows as disabled with no items.

This stuff should be easy in the feature pack, but I have been hitting my head against a wall on this one. Any suggestions or possible missed steps?

推荐答案

好吧,这取决于初始化顺序.我正在将内容添加到控件中,但是我是在大型机类的OnCreate中执行此操作的.当应用程序从序列化到注册表的任何数据中恢复其状态时,该组合框内容将被丢弃.

我只需要在应用程序执行完该步骤后添加组合内容.
Ok, it was down to initialisation order. I was adding the content to the control, but I was doing this in the OnCreate of the mainframe class. This combobox content would then be thrown away when the application restored its state from any data serialzed to the the registry.

I only needed to add the combo content after the application had already performed that step.


快速说明一下,如果我在CMainFrame类中为控件添加了UPDATE_COMMAND_UI处理程序,则可以启用控件,但是即使我已明确填充它,它也仍然没有内容.
As a quick note, if i add an UPDATE_COMMAND_UI handler for the control in the CMainFrame class I can enable the control, yet it still has no content even though I have definiately populated it.


请尝试处理:
Please try to process:
comboBox.EnableWindow(true);


之前:


before:

m_wndToolBar.ReplaceButton(ID_REPORT_SELECT_COMBO, comboBox, TRUE);


然后为您的父框架提供命令响应:


then provide a command reaction for your parent frame:

BEGIN_MESSAGE_MAP(CYourFrame, CFrameWndEx)
  //..
  ON_COMMAND(ID_CBBOX_CONN_PORT, &CYourFrame::OnClickReportCB)
  //..
END_MESSAGE_MAP()

void CYourFrame::OnClickReportCB()
{
  // nothing yet, just enable the button...
}


它应该使该框处于启用状态:)


It should make the box enabled :)


这篇关于CMFCToolBarComboBoxButton无法显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆