Home > Software engineering >  CListbox re-paint, item text can not display immediately after the form is initialized, takes to the
CListbox re-paint, item text can not display immediately after the form is initialized, takes to the

Time:02-24

The code is as follows:
CWLWListBox. H:
 class CWLWListBox: public CListBox 
{
DECLARE_DYNAMIC (CWLWListBox)

Public:
CWLWListBox ();
Virtual ~ CWLWListBox ();

Int AddString (LPCTSTR lpszItem);
Int AddString (LPCTSTR lpszItem, a COLORREF RGB);
Int InsertString (int nIndex, LPCTSTR lpszItem);
Int InsertString (int nIndex, LPCTSTR lpszItem, a COLORREF RGB);
Void SetItemColor (int nIndex, a COLORREF RGB);

BOOL m_nSelectChange;
COLORREF m_clrFore;//the foreground color
COLORREF m_clrBack;//background color
The CBrush m_brush;

Protected:
DECLARE_MESSAGE_MAP ()
Public:
Afx_msg BOOL OnEraseBkgnd * pDC (CDC);
Afx_msg void OnLbnSelchange ();
Afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
Virtual void DrawItem (LPDRAWITEMSTRUCT LPDRAWITEMSTRUCT/* */);
Afx_msg void OnHScroll (UINT nSBCode, UINT nPos, CScrollBar * pScrollBar);
Afx_msg void OnVScroll (UINT nSBCode, UINT nPos, CScrollBar * pScrollBar);
Afx_msg void OnPaint ();
Virtual void MeasureItem (LPMEASUREITEMSTRUCT LPMEASUREITEMSTRUCT/* */);
};


CWLWListBox. CPP:
//WLWListBox. CPP: implementation file 
//

# include "stdafx. H"
# include "WLWListBox. H"


//CWLWListBox

IMPLEMENT_DYNAMIC (CWLWListBox CListBox)

CWLWListBox: : CWLWListBox ()
{
M_nSelectChange=FALSE;
}

CWLWListBox: : ~ CWLWListBox ()
{
}

Int CWLWListBox: : AddString (LPCTSTR lpszItem)
{
Return ((CListBox *) this) - & gt; AddString (lpszItem);
}

Int CWLWListBox: : AddString (LPCTSTR lpszItem, a COLORREF RGB)
{
Int nIndex=CListBox: : AddString (lpszItem);

If (nIndex & gt;=0)
SetItemData (nIndex, RGB);

Return nIndex.
}

Int CWLWListBox: : InsertString (int nIndex, LPCTSTR lpszItem)
{
Return ((CListBox *) this) - & gt; InsertString (nIndex lpszItem);
}

Int CWLWListBox: : InsertString (int nIndex, LPCTSTR lpszItem, a COLORREF RGB)
{
Int nItemIndex=((CListBox *) this) - & gt; InsertString (nIndex lpszItem);

If (nItemIndex & gt;=0)
SetItemData (nItemIndex, RGB);

Return nItemIndex;
}

Void CWLWListBox: : SetItemColor (int nIndex, a COLORREF RGB)
{
SetItemData (nIndex, RGB);
RedrawWindow ();
}


BEGIN_MESSAGE_MAP (CWLWListBox CListBox)
ON_WM_ERASEBKGND ()
ON_CONTROL_REFLECT (LBN_SELCHANGE, & amp; CWLWListBox: : OnLbnSelchange)
ON_WM_LBUTTONDOWN ()
ON_WM_HSCROLL ()
ON_WM_VSCROLL ()
ON_WM_PAINT ()
END_MESSAGE_MAP ()



//CWLWListBox message handler

BOOL CWLWListBox: : OnEraseBkgnd (pDC) CDC *
{
//TODO: add the message handler code and/or invoke the default

//pDC - & gt; SetBkMode (TRANSPARENT);
//CBitmap bitmap.
//bitmap. LoadBitmap (IDB_BITMAP2);

//the CDC dcCompatible;
//dcCompatible CreateCompatibleDC (pDC);
//CBitmap * pOldbitmap=dcCompatible. SelectObject (& amp; Bitmap);

//CRect the rect.
//GetClientRect (& amp; The rect);//access controls the rectangular area of the client area

//BITMAP bm.
//bitmap. GetBitmap (& amp; Bm);
//int nBmWidth=bm. BmWidth;
//int nbmHeight=bm. BmHeight;
//pDC - & gt; StretchBlt (0, 0, the rect. Width (), the rect. Height (), & amp; DcCompatible, 0, 0, nBmWidth nbmHeight, SRCCOPY);
////pDC - & gt; BitBlt (0, 0, the rect. Width (), the rect. Height (), & amp; DcCompatible, 0, 0, SRCCOPY);
//dcCompatible SelectObject (pOldbitmap);
//return TRUE;

Return CListBox: : OnEraseBkgnd (pDC);
}


Void CWLWListBox: : OnLbnSelchange ()
{
//TODO: add the control notification handler code
//m_nSelectChange=TRUE;
}


Void CWLWListBox: : OnLButtonDown (UINT nFlags, CPoint point)
{
//TODO: add the message handler code and/or invoke the default
If (m_nSelectChange)
{
M_nSelectChange=FALSE;
Invalidate ().
}
CListBox: : OnLButtonDown (nFlags, point);
}

Void CWLWListBox: : DrawItem (LPDRAWITEMSTRUCT LPDRAWITEMSTRUCT)
{

If (lpDrawItemStruct - & gt; ItemID==1)
{
DrawFocusRect (lpDrawItemStruct - & gt; HDC, & amp; LpDrawItemStruct - & gt; RcItem);
return;
}

The CDC * pDC=CDC: : FromHandle (lpDrawItemStruct - & gt; HDC);

COLORREF clrOld;
Cstrings sText;

COLORREF clrNew=(a COLORREF) (lpDrawItemStruct - & gt; ItemData);

//item in the selected state, set the highlight
If ((lpDrawItemStruct - & gt; ItemState & amp; ODS_SELECTED) & amp; &
(lpDrawItemStruct - & gt; ItemAction & amp; (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush brush (: : GetSysColor (COLOR_HIGHLIGHT));
PDC - & gt; FillRect (& amp; LpDrawItemStruct - & gt; RcItem, & amp; Brush);
}

//lost item selected
if (! (lpDrawItemStruct - & gt; ItemState & amp; ODS_SELECTED) & amp; &
(lpDrawItemStruct - & gt; ItemAction & amp; ODA_SELECT))
{
CBrush brush (: : GetSysColor (COLOR_WINDOW));
PDC - & gt; FillRect (& amp; LpDrawItemStruct - & gt; RcItem, & amp; Brush);
}

//the item gains focus
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related