1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include "StdAfx.h"
#include ".\mylistbox.h"
CMyListBox::CMyListBox(void)
{
p_iAlign = DT_LEFT | DT_VCENTER;
CListBox::CListBox();
}
CMyListBox::~CMyListBox(void)
{
CListBox::~CListBox();
}
void CMyListBox::SetLineAlign(UINT iAlign){
p_iAlign = iAlign;
}
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
LPCTSTR lpszText=NULL ;
CString str;
// si le style est <> de LBS_HASSTRINGS on considère que la valeur à afficher
//est dans ItemData.
if(lpDrawItemStruct->itemID!=CB_ERR && !(GetStyle() & LBS_HASSTRINGS))
lpszText=(LPCTSTR) lpDrawItemStruct->itemData;
else
{
// Récupération de la chaîne grâce à l'index stocké dans ItemID.
GetText(lpDrawItemStruct->itemID,str);
lpszText= str;
}
ASSERT(lpszText != NULL);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Also, erase
// rect by filling it with the background color.
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT)); // couleur du texte
dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.FillSolidRect(&lpDrawItemStruct->rcItem,
::GetSysColor(COLOR_HIGHLIGHT)); // couleur de fond
// Petit cadre rouge
CBrush br(RGB(255, 0, 0));
dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
// En cas de sélection, on reduit la zone ou le texte peut etre écris
lpDrawItemStruct->rcItem.top++;
lpDrawItemStruct->rcItem.left++;
lpDrawItemStruct->rcItem.right--;
lpDrawItemStruct->rcItem.bottom--;
}
else
dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
// Draw the text.
dc.DrawText(
lpszText,
strlen(lpszText),
&lpDrawItemStruct->rcItem,
p_iAlign|DT_SINGLELINE);
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor(crOldTextColor);
dc.SetBkColor(crOldBkColor);
dc.Detach();
}
void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
static int cyItem=-1; //Height of a listbox item
if (-1 == cyItem){
CFont* theFont;
CDC* theDC;
TEXTMETRIC tm;
/*
* Attempt to get the font of the dialog. However,
* on the first attempt in the life of the dialog,
* this could fail; in that case use the system font.
*/
if ((theFont = GetParent()->GetFont()) == NULL)
theFont = theFont->FromHandle((HFONT)::GetStockObject(SYSTEM_FONT));
theDC = GetParent()->GetDC();
theFont = theDC->SelectObject(theFont);
/*
* Item height is the maximum of the font height and the
* bitmap height. We know that all bitmaps are the same
* size, so we just get information for one of them.
*/
theDC->GetTextMetrics(&tm);
cyItem=tm.tmHeight;
GetParent()->ReleaseDC(theDC);
}
// Return Result
lpMeasureItemStruct->itemHeight = cyItem;
} |
Partager