天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 39|回复: 0

MFC_Combo Box下拉框

[复制链接]

该用户从未签到

主题

0

回帖

2912

积分

管理员

积分
2912
发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
1.MFC的Combo  Box控件会自动排序,并不按加入顺序显示,可以去资源文件进行以下设置,使其初始化按照加入Combo Box的顺序显示:



2.下拉框的索引,从0开始

3.删除下拉框的值

m_cmbDb.DeleteString(nSel);
4.删除下拉框的所有值

m_cmbDb.ResetContent();
5.带位图的下拉框



添加一个线型位图



CImageList* pImageList = new CImageList();
m_ImageList.Create(500,4, ILC_COLOR32, 0, 4);
HBITMAP hBmp == ::resourceManager()->lineStyleResource()->getBmp(strLineStyleName);
m_ImageList.Add(CBitmap::FromHandle(hBmp), RGB(0, 0, 0));
AddString(strLineStyleName.c_str());

HBITMAP CMdUILineStyleResource::getBmpToName(MdString strName)
{   
    CMdDbLine pLine;
    pLine.setStartPoint(CMdGePoint3d::kOrigin);
    pLine.setEndPoint(CMdGePoint3d(500,0.0,0.0));
    pLine.setColorIndex(0);

    CMdDbObjectId lineTypeTableId = ::mdCurWnd()->dbService()->database()->getLinetypeTableId();
    CMdDbLinetypeTablePtr pLinetypeTable = CMdDbLinetypeTable::cast(lineTypeTableId.openObject(MdDb::kForRead));
    if (pLinetypeTable->has(strName.c_str()))
    {
        CMdDbObjectId linetypeId = pLinetypeTable->getAt(strName);
        pLine.setLinetype(linetypeId);
    }
   
    CMdDbEntityPtrArray arrEnts;
    arrEnts.push_back(&pLine);
    CMdCmColor cmColor;
    cmColor.setColorIndex(7);

    HWND hDeskWnd = GetDesktopWindow();
    HDC hDeskDC = GetDC(hDeskWnd);   
    HBITMAP hBmp = ::mdCurWnd()->gsService()->getBitmapFromEntities(::mdCurWnd()->dbService()->database(), arrEnts, 500, 4, cmColor, hDeskDC);

    return hBmp;
}


//getBitmapFromEntities为自己实现的函数
HBITMAP CMdGsThisWndDrawing::getBitmapFromEntities(CMdDbDatabase* pDb, const CMdDbEntityPtrArray& arrEnts, int cx, int cy, const CMdCmColor& backGround, HDC hDC/* = NULL*/)
{
    OdDbDatabase* pOdDb = OdDbDatabase::cast(pDb->getOdObject());

    HDC hMemDC = NULL;
    HBITMAP hBmp = NULL;
    if (!hDC)
    {
        HWND hDeskTopWnd = GetDesktopWindow();
        HDC hDeskTopDC = GetDC(hDeskTopWnd);
        hMemDC = CreateCompatibleDC(hDeskTopDC);
        hBmp = CreateCompatibleBitmap(hDeskTopDC, cx, cy);

        ReleaseDC(hDeskTopWnd, hDeskTopDC);
    }
    else
    {
        hMemDC =  CreateCompatibleDC(hDC);
        hBmp = CreateCompatibleBitmap(hDC, cx, cy);
    }

    HGDIOBJ hOldBmp = SelectObject(hMemDC, hBmp);

    COLORREF backColor = RGB(backGround.red(), backGround.green(), backGround.blue());

    //WCharPtrToMdStrD(sModuleName, OdWinOpenGLModuleName);
    //WCharPtrToMdStrD(sModuleName, OdWinBitmapModuleName);
    WCharPtrToMdStrD(sModuleName, OdWinGDIModuleName);
    IMdModule* pModule = ::mdCoreService()->getModule(sModuleName);
    if (!pModule)
    {
        pModule = ::mdCoreService()->loadModule(sModuleName, false);
        if (!pModule)
        {
            return NULL;
        }
    }

    OdGsModulePtr pGsModule = (OdGsModule*)pModule->module();
    OdGsDevicePtr pDevice = pGsModule->createDevice();

    if (pDevice.isNull())
    {
        return NULL;
    }

    OdRxDictionaryPtr pProperties = pDevice->properties();
    if(pProperties.get())
    {
        if(pProperties->has(L"WindowHDC"))
            pProperties->putAt(L"WindowHDC",  OdRxVariantValue((OdIntPtr)hMemDC));
        if (pProperties->has(L"ClearScreen"))
            pDevice->properties()->putAt(L"ClearScreen", OdRxVariantValue(true));       
        if (pProperties->has(L"DoubleBufferEnabled"))
            pDevice->properties()->putAt(L"DoubleBufferEnabled", OdRxVariantValue(bool(false)));
    }
   
    OdGiContextForDbDatabasePtr pGiContextForDbDatabase = OdGiContextForDbDatabase::createObject();
    pGiContextForDbDatabase->setDatabase(pOdDb);
    pGiContextForDbDatabase->enableGsModel(true);
    pGiContextForDbDatabase->enableKeepPSLayoutHelperView(true);
    pDevice->setUserGiContext(pGiContextForDbDatabase);

    OdGsViewPtr pView = pDevice->createView();
    pDevice->addView(pView);

    OdGsModelPtr pModel = pDevice->createModel();

    OdGeExtents3d ext;
    for (int i=0;i<(int)arrEnts.size();i++)
    {
        OdGeExtents3d subExt;
        OdDbEntityPtr pEnt = OdDbEntity::cast(arrEnts[i]->getOdObject());
        pEnt->getGeomExtents(subExt);
        ext.addExt(subExt);
        pView->add(pEnt, pModel);
    }

    pView->setViewportBorderVisibility(false);

    if(pGiContextForDbDatabase->isPlotGeneration())
    {
        pView->setViewport(OdGePoint2d(0.,0.), OdGePoint2d(1.,1.));
    }

    pView->show();

    pView->setClearColor(OdGsView::kPaletteBackground);
   
    pView->setViewport(OdGePoint2d(0,0), OdGePoint2d(1,1));
    pView->setView(        OdGePoint3d(ext.center().x,ext.center().y,ext.center().z),
        OdGePoint3d(ext.center().x,ext.center().y,ext.center().z - 1),
        OdGeVector3d(0, -1.0, 0),
        (ext.maxPoint().x - ext.minPoint().x)*1.1 , (ext.maxPoint().y - ext.minPoint().y)*1.1,
        OdGsView::kParallel);

    pView->setLineweightToDcScale(0);
    pView->setMode(OdGsView::k2DOptimized);

    const ODCOLORREF *palette = odcmAcadPalette(backColor);

    typedef OdArray<ODCOLORREF, OdMemoryAllocator<ODCOLORREF> > ODGSPALETTE;
    ODGSPALETTE pPalCpy;
    pPalCpy.insert(pPalCpy.begin(), palette, palette+256);
    pPalCpy[0] = backColor;
    pDevice->setLogicalPalette(pPalCpy.asArrayPtr(), 256);
    pDevice->setBackgroundColor(backColor);
    pGiContextForDbDatabase->setPaletteBackground(backColor);

    OdGsDCRect gsRect(0, cx, cy, 0);
    pDevice->onSize(gsRect);

    pDevice->update();

    SelectObject(hMemDC, hOldBmp);
    DeleteDC(hMemDC);

    return hBmp;
}
OdGsView::setLineweightToDcScale:设置在此视图端口对象中显示线宽的比例因子。

setLineweightToDcScale函数的定义形式为:

  virtual void setLineweightToDcScale(double scale) = 0;
第一个参数:scale:比例因子。

重写DrawItem

void CLineStyleComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC* pDC = CDC::FromHandle (lpDrawItemStruct->hDC);
    ASSERT_VALID (pDC);

    int nIndexDC = pDC->SaveDC ();

    CRect rect = lpDrawItemStruct->rcItem;
    int nItem = lpDrawItemStruct->itemID;

    BOOL bIsSelected = (lpDrawItemStruct->itemState & ODS_SELECTED);

    //OnFillLbItem(pDC, (int)lpDrawItemStruct->itemID, rect, FALSE, (lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED);

    pDC->SetBkMode(TRANSPARENT);

    if (nItem >= 0)
    {
        CRect rectImage = rect;
        CRect rectText = rect;
        CRect rectBItem = rect;

        rectImage.left -= 15;
        rectImage.top += (rect.Height() - MD_UI_LINETYPE_COMBOBOX_TEXT_HEIGHT)/2 ;
        rectImage.bottom = rectImage.top + MD_UI_LINETYPE_COMBOBOX_TEXT_HEIGHT;
        rectImage.right = rect.right - MD_UI_LINETYPE_COMBOBOX_TEXT_WIDTH;

        rectBItem.left = rectImage.right + 4;
        rectText.left = rectBItem.left+2;
        //m_ImageList.DrawEx(pDC, nItem, CPoint(rectImage.left, rectImage.top), CSize(rectImage.Width(), rectImage.Height()), CLR_DEFAULT, CLR_DEFAULT, ILD_IMAGE);
        m_ImageList.DrawIndirect(pDC, nItem, CPoint(rectImage.left, rectImage.top), CSize(rectImage.Width(), rectImage.Height()), CPoint(0,0));

        OnFillLbItem(pDC, (int)lpDrawItemStruct->itemID, rectBItem, FALSE, (lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED);

        if ((lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED)
        {
            CPen pen(PS_DOT, 0, RGB(128,128,128));
            HPEN hOldPen = (HPEN)pDC->SelectObject(&pen);
            pDC->MoveTo(rectBItem.left, rectBItem.top);
            pDC->LineTo(rectBItem.left, rectBItem.bottom-1);
            pDC->LineTo(rectBItem.right-1, rectBItem.bottom-1);
            pDC->LineTo(rectBItem.right-1, rectBItem.top);
            pDC->LineTo(rectBItem.left, rectBItem.top);
            pDC->SelectObject(hOldPen);
        }

        CString strText;
        GetLBText(nItem, strText);
        BOOL bTextTrancated = pDC->GetTextExtent(strText).cx > rectText.Width();

        pDC->DrawText (strText, rectText, DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS | DT_NOPREFIX);

    }

    pDC->RestoreDC (nIndexDC);
}
添加一个线宽位图:



    m_mapLineWidth[MdDb::kLnWtByLayer] = MD_T("ByLayer");
    m_mapLineWidth[MdDb::kLnWtByBlock] = MD_T("ByBlock");
    m_mapLineWidth[MdDb::kLnWtByLwDefault] = MD_T("默认");
    m_mapLineWidth[MdDb::kLnWt000] = MD_T("0.00 mm");
    m_mapLineWidth[MdDb::kLnWt005] = MD_T("0.05 mm");
    m_mapLineWidth[MdDb::kLnWt009] = MD_T("0.09 mm");
    m_mapLineWidth[MdDb::kLnWt013] = MD_T("0.13 mm");
    m_mapLineWidth[MdDb::kLnWt015] = MD_T("0.15 mm");
    m_mapLineWidth[MdDb::kLnWt018] = MD_T("0.18 mm");
    m_mapLineWidth[MdDb::kLnWt020] = MD_T("0.20 mm");
    m_mapLineWidth[MdDb::kLnWt025] = MD_T("0.25 mm");
    m_mapLineWidth[MdDb::kLnWt030] = MD_T("0.30 mm");
    m_mapLineWidth[MdDb::kLnWt035] = MD_T("0.35 mm");
    m_mapLineWidth[MdDb::kLnWt040] = MD_T("0.40 mm");
    m_mapLineWidth[MdDb::kLnWt050] = MD_T("0.50 mm");
    m_mapLineWidth[MdDb::kLnWt053] = MD_T("0.53 mm");
    m_mapLineWidth[MdDb::kLnWt060] = MD_T("0.60 mm");
    m_mapLineWidth[MdDb::kLnWt070] = MD_T("0.70 mm");
    m_mapLineWidth[MdDb::kLnWt080] = MD_T("0.80 mm");
    m_mapLineWidth[MdDb::kLnWt090] = MD_T("0.90 mm");
    m_mapLineWidth[MdDb::kLnWt100] = MD_T("1.00 mm");
    m_mapLineWidth[MdDb::kLnWt106] = MD_T("1.06 mm");
    m_mapLineWidth[MdDb::kLnWt120] = MD_T("1.20 mm");
    m_mapLineWidth[MdDb::kLnWt140] = MD_T("1.40 mm");
    m_mapLineWidth[MdDb::kLnWt158] = MD_T("1.58 mm");
    m_mapLineWidth[MdDb::kLnWt200] = MD_T("2.00 mm");
    m_mapLineWidth[MdDb::kLnWt211] = MD_T("2.11 mm");


CImageList* pImageList = new CImageList();
m_ImageList.Create(500,4, ILC_COLOR32, 0, 4);

std::map<MdDb::LineWeight, MdString>::iterator it = m_mapLineWidth.begin();
for (; it != m_mapLineWidth.end(); ++it)
{
    AddString(it->second.c_str());
    HBITMAP hBmp == ::resourceManager()->lineWidthResource()->getBmp(it->first);
    m_ImageList.Add(CBitmap::FromHandle(hBmp), RGB(0, 0, 0));
}


HBITMAP CMdUILineWidthResource::getBmpToName(MdDb::LineWeight nLineWeight)
{
    CMdDbLine pLine;
    pLine.setStartPoint(CMdGePoint3d::kOrigin);
    pLine.setEndPoint(CMdGePoint3d(MD_UI_LINEWIDTH_COMBOBOX_BITMAP_WIDTH,0.0,0.0));
    pLine.setColorIndex(0);
    pLine.setLineWeight(nLineWeight);
   
    CMdDbEntityPtrArray arrEnts;
    arrEnts.push_back(&pLine);
    CMdCmColor cmColor;
    cmColor.setColorIndex(7);

    HWND hDeskWnd = GetDesktopWindow();
    HDC hDeskDC = GetDC(hDeskWnd);   

    bool bOldLWDisplay = ::mdCurWnd()->dbService()->LWDISPLAY();
    ::mdCurWnd()->dbService()->setLWDISPLAY(true);

    HBITMAP hBmp = ::mdCurWnd()->gsService()->getBitmapFromEntities(::mdCurWnd()->dbService()->database(), arrEnts, MD_UI_LINEWIDTH_COMBOBOX_BITMAP_WIDTH, MD_UI_LINEWIDTH_COMBOBOX_BITMAP_HEIGHT, cmColor, hDeskDC);

    ::mdCurWnd()->dbService()->setLWDISPLAY(bOldLWDisplay);

    return hBmp;
}


重写DrawItem

void CILineWidthComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC* pDC = CDC::FromHandle (lpDrawItemStruct->hDC);
    ASSERT_VALID (pDC);

    int nIndexDC = pDC->SaveDC ();

    CRect rect = lpDrawItemStruct->rcItem;
    int nItem = lpDrawItemStruct->itemID;

    BOOL bIsSelected = (lpDrawItemStruct->itemState & ODS_SELECTED);

    //OnFillLbItem(pDC, (int)lpDrawItemStruct->itemID, rect, FALSE, (lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED);

    pDC->SetBkMode(TRANSPARENT);

    if (nItem >= 0)
    {
        CRect rectImage = rect;
        CRect rectText = rect;
        CRect rectBItem = rect;

        rectImage.left -= 20;
        rectImage.top += (rect.Height() - MD_UI_LINEWIDTH_COMBOBOX_BITMAP_HEIGHT)/2 ;
        rectImage.bottom = rectImage.top + MD_UI_LINEWIDTH_COMBOBOX_BITMAP_HEIGHT;
        rectImage.right = rect.right - MD_UI_LINEWIDTH_COMBOBOX_TEXT_WIDTH;

        rectBItem.left = rectImage.right + 4;
        rectText.left = rectBItem.left+2;
        //m_ImageList.DrawEx(pDC, nItem, CPoint(rectImage.left, rectImage.top), CSize(rectImage.Width(), rectImage.Height()), CLR_DEFAULT, CLR_DEFAULT, ILD_IMAGE);
        m_ImageList.DrawIndirect(pDC, nItem, CPoint(rectImage.left, rectImage.top), CSize(rectImage.Width(), rectImage.Height()), CPoint(0,0));

        OnFillLbItem(pDC, (int)lpDrawItemStruct->itemID, rectBItem, FALSE, (lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED);

        if ((lpDrawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED)
        {
            CPen pen(PS_DOT, 0, RGB(128,128,128));
            HPEN hOldPen = (HPEN)pDC->SelectObject(&pen);
            pDC->MoveTo(rectBItem.left, rectBItem.top);
            pDC->LineTo(rectBItem.left, rectBItem.bottom-1);
            pDC->LineTo(rectBItem.right-1, rectBItem.bottom-1);
            pDC->LineTo(rectBItem.right-1, rectBItem.top);
            pDC->LineTo(rectBItem.left, rectBItem.top);
            pDC->SelectObject(hOldPen);
        }

        CString strText;
        GetLBText(nItem, strText);
        BOOL bTextTrancated = pDC->GetTextExtent(strText).cx > rectText.Width();

        pDC->DrawText (strText, rectText, DT_SINGLELINE | DT_VCENTER | DT_END_ELLIPSIS | DT_NOPREFIX);

    }

    pDC->RestoreDC (nIndexDC);
}
添加一个颜色位图:



COLORREF clr = RGB(192,192,192);
AddColor(clr, T_D("白色"));

int CBCGPColorComboBox::AddColor(COLORREF color, const CString& strColorName)
{
        ASSERT_VALID(this);
        ASSERT(GetSafeHwnd() != NULL);

        int nIndex = 0;

        if (GetCount() == 0 && m_bIsNoneColor)
        {
                nIndex = AddString(m_strNoneColor);
                if (nIndex >= 0)
                {
                        SetItemData(nIndex, (DWORD_PTR)-1);
                }
        }

        nIndex = AddString(strColorName);
        if (nIndex >= 0)
        {
                SetItemData(nIndex, (DWORD_PTR)color);

                if (nIndex == m_nDelayedSelection)
                {
                        SetCurSel(nIndex);
                        m_nDelayedSelection = -1;
                }
        }

        return nIndex;
}
重写DrawItem

void CColorComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CBCGPColorComboBox::DrawItem(lpDrawItemStruct);

    int nIndex = lpDrawItemStruct->itemID;
    int nCount = GetCount();
    if (m_bEnableCustomization && nIndex == nCount-1)
    {
        HBITMAP hBmp = ::resourceManager()->colorResource()->getBmp(IDB_UI_COLOR_CUSTOMIZATION);
        if (!hBmp)
        {
            return;
        }
        CDC* pDC = CDC::FromHandle (lpDrawItemStruct->hDC);
        ASSERT_VALID (pDC);

        CDC memDC;
        memDC.CreateCompatibleDC(pDC);
        HBITMAP hOldBmp = (HBITMAP)memDC.SelectObject(hBmp);
   
        const int nPadding = 2;
        CRect rect = lpDrawItemStruct->rcItem;
        rect.right = rect.left + rect.Height();
        rect.DeflateRect(nPadding, nPadding);

        BITMAP bmp;
        GetObject(hBmp, sizeof(BITMAP), (LPBYTE)&bmp);
        pDC->StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
   
        memDC.SelectObject(hOldBmp);

        memDC.DeleteDC();
    }
}

[code]https://my.oschina.net/u/2930533/blog/786454[/code]

 

 

 

 

MFC_Combo Box下拉框
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|中国膜结构网_中国空间膜结构协会

GMT+8, 2024-11-1 11:43 , Processed in 0.125705 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表