Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » MFC custom CListCtrl DrawItem to draw a BITMAP*

This thread is locked; no one can reply to it. rss feed Print
MFC custom CListCtrl DrawItem to draw a BITMAP*
anto80
Member #3,230
February 2003
avatar

Hello all.

I'm morking on a MSVC MFC Project that uses Allegro.
I have a custom CListCtrl control that inherits CListCtrl's properties.

I have a custom draw method to display it. So, this control has the flags "owner draw fixed" and "owner data".
I also have overriden the virtual function void DrawItem(LPDRAWITEMSTRUCT lpdrawitemstruct) to be able to draw what I like.

Each column can be one of the following types :

MODELDATATYPE_SZ           :  null terminated string
MODELDATATYPE_FIXED       :  real in fixed point
MODELDATATYPE_bool         :  bool (True or False)
MODELDATATYPE_TEXTURE      :  BITMAP*
MODELDATATYPE_TEXTUREDIMS  :  BITMAP* w,h

Displaying the text (any type except MODELDATATYPE_TEXTURE BITMAP*) is working fine.
But i must be doing something wrong when blitting with blit_to_hdc().

here is the code.

1/*-------------------------------------------------------------------------*/
2/*-------------------------- Draw an item in pDC --------------------------*/
3/*-------------------------------------------------------------------------*/
4void CModelListCtrl::DrawItem(LPDRAWITEMSTRUCT lpdrawitemstruct)
5{
6 int nItem;
7 CDC* pDC;
8 char szBuf[64];
9 
10 
11 nItem = lpdrawitemstruct->itemID;
12 pDC = CDC::FromHandle(lpdrawitemstruct->hDC);
13 m_HDC = &(lpdrawitemstruct->hDC);
14 ASSERT(nItem >= 0);
15 
16 ASSERT(nItem < m_pModel->m_nNbTextures);
17 DrawSubItem(pDC, nItem, 0, MODELDATATYPE_TEXTURE, &nItem);
18 DrawSubItem(pDC, nItem, 1, MODELDATATYPE_TEXTUREDIMS, &nItem);
19 DrawSubItem(pDC, nItem, 2, MODELDATATYPE_SZ , m_pModel->m_Texs[nItem].szName);
20}
21 
22/*-------------------------------------------------------------------------*/
23/*------------------------------ Draw Sub Item ----------------------------*/
24/*-------------------------------------------------------------------------*/
25void CModelListCtrl::DrawSubItem(CDC *pDC, int nItem, int nSubItem, int nDatatype, void* pt)
26{
27 CRect rect;
28 char szBuf[16];
29 int nIndex;
30 
31 CListCtrl::GetSubItemRect(nItem, nSubItem, LVIR_BOUNDS, rect);
32
33 pDC->FillSolidRect(rect, m_nBackColor);
34
35
36 if (pt != NULL)
37 {
38 // pDC->SaveDC();
39 pDC->SetBkMode(TRANSPARENT);
40 pDC->SetTextColor(m_nTextColor);
41 rect.left+=2;
42 
43 switch (nDatatype)
44 {
45 case MODELDATATYPE_SZ:
46 pDC->DrawText((char*)pt, &rect, DT_LEFT | DT_SINGLELINE);
47 break;
48 
49 case MODELDATATYPE_bool:
50 pDC->DrawText((*(bool*)pt == 0)?"False":"True",
51 &rect, DT_LEFT | DT_SINGLELINE);
52 break;
53 
54 case MODELDATATYPE_FIXED:
55 rect.right -= 2;
56 sprintf(szBuf, "%2.3f", fixtof(*(fixed*)pt));
57 pDC->DrawText(szBuf, &rect, DT_RIGHT | DT_SINGLELINE);
58 break;
59 
60 case MODELDATATYPE_INT:
61 rect.right -= 2;
62 sprintf(szBuf, "%d", *(int*)pt);
63 pDC->DrawText(szBuf, &rect, DT_RIGHT | DT_SINGLELINE);
64 break;
65
66 case MODELDATATYPE_TEXTURE:
67 nIndex = *(int*)pt;
68 if (nIndex >= 0 && nIndex < m_pModel->m_nNbTextures)
69 {
70 /*
71 pDC->BitBlt(rect.left, rect.top,
72 MIN(rect.right, m_pModel->m_nTexPreviewDim),
73 MIN(rect.bottom, m_pModel->m_nTexPreviewDim),
74 CDC::FromHandle(win_get_dc(m_pModel->m_Texs[nIndex].bmpPreview)),
75 0,0, SRCCOPY);
76 */
77
78 /*
79 set_palette_to_hdc(pDC->m_hDC, NULL); // if I call this it crashes !!
80 */
81 blit_to_hdc(m_pModel->m_Texs[nIndex].bmpPreview,
82 pDC->m_hDC,
83 0, 0, 0, 0,
84 m_pModel->m_nTexPreviewDim, m_pModel->m_nTexPreviewDim);
85
86 }
87 break;
88 
89 case MODELDATATYPE_TEXTUREDIMS:
90 nIndex = *(int*)pt;
91 if (nIndex >= 0 && nIndex < m_pModel->m_nNbTextures)
92 {
93 sprintf(szBuf, "%d * %d",
94 m_pModel->m_Texs[nIndex].bmpImage->w,
95 m_pModel->m_Texs[nIndex].bmpImage->h);
96 pDC->DrawText(szBuf, &rect, DT_LEFT | DT_SINGLELINE);
97 }
98 break;
99 }
100
101 // pDC->RestoreDC(-1);
102 }
103}

Thanks a lot for any help!!

___________
Currently working on his action/puzzle game CIPHER PUSHER : Blocks/Vortexes/Seafood! Facebook - Twitter - webpage

A J
Member #3,025
December 2002
avatar

MFC is a lost cause, i hope you dont plan to invest to much in it.

___________________________
The more you talk, the more AJ is right. - ML

anto80
Member #3,230
February 2003
avatar

Anyone except AJ knows the trick? (even if it's a lost cause....)
Thank you.

___________
Currently working on his action/puzzle game CIPHER PUSHER : Blocks/Vortexes/Seafood! Facebook - Twitter - webpage

A J
Member #3,025
December 2002
avatar

when you dont get much of a reply, i would consider that as a measure of how much everyone has already realized.

___________________________
The more you talk, the more AJ is right. - ML

Go to: