Knowledge Base Nr: 00318 tabstopcombobox.cpp - http://www.swe-kaiser.de

MFC: tabstops für comboboxen

  
//(nur pixelgenau bei verwendung eines nicht proportionalen fonts - ansonsten ownerdraw verwenden!)

CString CHelperFunc::FormatCombobox(CComboBox* pComboBox, CString sText, int nTabStops[])
{
CString sFormattedText("");

CDC* pDC = pComboBox->GetDC();

CSize szSpace = pDC->GetTextExtent(L" ");

int nSubStart = 0;
int nSubEnd = 0;
int nTabStop = 0;

while (true)
{
nSubEnd = sText.Find('\t', nSubStart);
if (nSubEnd<0)
{
sFormattedText += sText.Mid(nSubStart);
break;
}

nSubEnd++;
sFormattedText += sText.Mid(nSubStart, nSubEnd-nSubStart-1);

CSize sz = pDC->GetTextExtent(sFormattedText);
float nFillSpaces = ((nTabStops[nTabStop] - sz.cx) / szSpace.cx) + 0.5f;

sFormattedText += CString(' ', (int)nFillSpaces);

nSubStart = nSubEnd;
nTabStop++;
}

pComboBox->ReleaseDC(pDC);

return sFormattedText;
}

### verwendung:

sCurve.Format(L"%ws\t%ws %d\t%ws %ws"
, szKNummer
, theApp.Translate(L"Revision")
, nRevision
, theApp.Translate(szCharttype)
, sSpeed);

int nTabStops[] = { 250, 400};

sCurve = CHelperFunc::FormatCombobox(pcbKurve, sCurve, nTabStops);

int nIndex = pcbKurve->AddString(sCurve);