Windows 介面:OnCustomDraw 設定 CListCtrol 任意行的文本字型顏色以及背景色

來源:互聯網
上載者:User

1,首先使用 ClassWizard 建立一個 CListCtrl 的衍生類別,在它的標頭檔訊息響應函數中添加:

 // Generated message map functions
protected:
 //{{AFX_MSG(CScanFileList) //}}AFX_MSG
 afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);

 DECLARE_MESSAGE_MAP() 

2,相應的,類實現檔案添加:

BEGIN_MESSAGE_MAP(CScanFileList, CListCtrlEx)
 //{{AFX_MSG_MAP(CScanFileList)
 ...
 ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
 ...
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

3,函數實現:

void CScanFileList::OnCustomDraw ( NMHDR* pNMHDR, LRESULT* pResult )
{
 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
 
    // Take the default processing unless we
    // set this to something else below.
    *pResult = CDRF_DODEFAULT;
 
    // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.
 
    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
 {
        *pResult = CDRF_NOTIFYITEMDRAW;
 }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
 {

// 這裡僅僅比較文本,注意:index == pLVCD->nmcd.dwItemSpec
  if (GetItemText(pLVCD->nmcd.dwItemSpec, 2) == m_lpszEncrypt)
  {
   pLVCD->clrText = RGB(255,0,0);
   pLVCD->clrTextBk = RGB(255,0,0);
  }

         // Tell Windows to paint the control itself.
        *pResult = CDRF_DODEFAULT;
 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.