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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// CMyListBox message handlers
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
LPDRAWITEMSTRUCT lpDStruct = lpDrawItemStruct; //use a shorter name
switch (lpDStruct->CtlID)
{
case IDC_MYLISTBOX:
{
switch (lpDStruct->itemAction)
{
case ODA_DRAWENTIRE:
if (lpDStruct->itemState & ODS_SELECTED)
{
DrawSelected(lpDStruct);
}
else
{
DrawEntire(lpDStruct);
}
break;
case ODA_FOCUS:
DrawFocused(lpDStruct);
break;
case ODA_SELECT:
//DrawSelected(lpDStruct);
if (lpDStruct->itemState & ODS_SELECTED)
{
DrawSelected(lpDStruct);
}
else
{
DrawEntire(lpDStruct);
}
break;
default:
break;
}
}
}
}
void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: Add your code to determine the size of specified item
//This function appears only to be called for owner draw variable
//which is what we want most of the time.
//With owener draw fixed - the item height appears to be fixed by the system
//and seemed to be the determined by the font height which can screws up
//things like butmaps - hence use ODV
LPMEASUREITEMSTRUCT lpMStruct=lpMeasureItemStruct;
lpMStruct->itemWidth=250;
lpMStruct->itemHeight=24;//as required
}
void CMyListBox::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//Pass this message onto the parent (in this case dialogbox) window
CListBox::OnLButtonDblClk(nFlags, point);
}
void CMyListBox::DrawEntire(LPDRAWITEMSTRUCT lpDStruct)
{
CRect rect(lpDStruct->rcItem);
HDC dc =lpDStruct->hDC;
MYLISTITEM *a = (MYLISTITEM*)lpDStruct->itemData;
HBRUSH brush, oldbrush;
HPEN pen,oldpen;
HFONT hFont, hOldFont;
LOGFONT logFont;
pen=CreatePen(PS_SOLID,1,RGB(255,255,255));
brush=CreateSolidBrush(RGB(255,255,255));
oldpen=(HPEN)SelectObject(dc,pen);
oldbrush=(HBRUSH)SelectObject(dc,brush);
//erase the background - draw rectangle using pen and brush
Rectangle (dc,rect.left,rect.top,rect.right+1,rect.bottom);
//draw the bitmap
DrawBitmaps(dc, rect,a->checked);
//Create the font
memset(&logFont,0,sizeof(logFont));
logFont.lfHeight = 16;
logFont.lfWeight = FW_BOLD;
strcpy(logFont.lfFaceName,"courier");
hFont = CreateFontIndirect(&logFont);
hOldFont = (HFONT)SelectObject(dc,hFont);
//Write Text
SetTextColor(dc,RGB(0,0,0));
SetBkMode(dc,TRANSPARENT);
TextOut(dc,rect.left+20,rect.top+2,a->title,strlen(a->title));
if (lpDStruct->itemState & ODS_FOCUS)
{
DrawFocusRect(dc,rect);
}
//clean up
SelectObject(dc,hOldFont);
SelectObject(dc,oldpen);
SelectObject(dc,oldbrush);
}
void CMyListBox::DrawFocused(LPDRAWITEMSTRUCT lpDStruct)
{
HDC dc =lpDStruct->hDC;
CRect rect(lpDStruct->rcItem);
DrawFocusRect(dc,&rect);
}
void CMyListBox::DrawSelected(LPDRAWITEMSTRUCT lpDStruct)
{
CRect rect(lpDStruct->rcItem);
HDC dc =lpDStruct->hDC;
MYLISTITEM *a = (MYLISTITEM *)lpDStruct->itemData;
//Create brushes, pens, bitmaps and font
HBRUSH brush, oldbrush;
HPEN pen,oldpen;
HFONT hFont, hOldFont;
LOGFONT logFont;
pen=CreatePen(PS_SOLID,1,RGB(0,0,255));//
brush=CreateSolidBrush(RGB(0,0,255));
oldpen=(HPEN)SelectObject(dc,pen);
oldbrush=(HBRUSH)SelectObject(dc,brush);
//draw background
Rectangle (dc,rect.left,rect.top,rect.right,rect.bottom);
//draw bitmap
DrawBitmaps(dc,rect,a->checked);
//Create the font
memset(&logFont,0,sizeof(logFont));
logFont.lfHeight = 24;
logFont.lfWeight = FW_BOLD;
strcpy(logFont.lfFaceName,"courier");
hFont = CreateFontIndirect(&logFont);
hOldFont = (HFONT)SelectObject(dc,hFont);
//Write the text
SetTextColor(dc,RGB(255,255,255));
SetBkMode(dc,TRANSPARENT);
TextOut(dc,rect.left+20,rect.top+2,a->title,strlen(a->title));
if (lpDStruct->itemState & ODS_FOCUS)
{
DrawFocusRect(dc,rect);
}
//clean up
SelectObject(dc,hOldFont);
SelectObject(dc,oldpen);
SelectObject(dc,oldbrush);
}
int CMyListBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
// TODO: Add your code to determine the sorting order of the specified items
// return -1 = item 1 sorts before item 2
// return 0 = item 1 and item 2 sort the same
// return 1 = item 1 sorts after item 2
/*
Only called if the OD listbox is created with 'sort' option
*/
return 0;
}
void CMyListBox::DrawBitmaps(HDC hDC, RECT rect,int checked)
{
HDC memdc, memdc1;
memdc=CreateCompatibleDC(hDC);
memdc1=CreateCompatibleDC(hDC);
HBITMAP bitmap,bitmap1, oldbitmap, oldbitmap1;
if (checked==1)
{
bitmap=LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_GREENTICK));
bitmap1=LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_GREENTICKMASK));
}
else
{
bitmap=LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_REDX));
bitmap1=LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_REDXMASK));
}
oldbitmap=(HBITMAP)SelectObject(memdc,bitmap);
oldbitmap1=(HBITMAP)SelectObject(memdc1,bitmap1);
//draw the bitmap
BitBlt(hDC,rect.left+2,rect.top+2,16,16,memdc,0,0,SRCINVERT);
BitBlt(hDC,rect.left+2,rect.top+2,16,16,memdc1,0,0,SRCAND);
BitBlt(hDC,rect.left+2,rect.top+2,16,16,memdc,0,0,SRCINVERT);
//cleanup
SelectObject(memdc,oldbitmap);
SelectObject(memdc1,oldbitmap1);
}
|