SelectingListCtrl.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 5k
Category:

Graph program

Development Platform:

Visual C++

  1. // RadioListBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SelectingListCtrl.h"
  5. #include ".selectinglistctrl.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CSelectingListCtrl
  13. CSelectingListCtrl::CSelectingListCtrl()
  14. {
  15. }
  16. CSelectingListCtrl::~CSelectingListCtrl()
  17. {
  18. }
  19. BEGIN_MESSAGE_MAP(CSelectingListCtrl, CListCtrl)
  20. //{{AFX_MSG_MAP(CSelectingListCtrl)
  21. ON_WM_CTLCOLOR_REFLECT()
  22. //}}AFX_MSG_MAP
  23. ON_WM_CHAR()
  24. ON_WM_LBUTTONDOWN()
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CSelectingListCtrl message handlers
  28. void  CSelectingListCtrl::SetMultiSelectMode(bool msm)
  29. {
  30. DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE); 
  31.     if (msm) 
  32. dwStyle &= ~(LVS_SINGLESEL); 
  33. else
  34. dwStyle |= LVS_SINGLESEL;
  35. SetWindowLong( m_hWnd, GWL_STYLE, dwStyle);
  36. }
  37. void CSelectingListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  38. {
  39.      CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  40.  DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);
  41.  bool isMultiSelect = !(dwStyle & LVS_SINGLESEL);
  42.      // just draws focus rectangle when listbox is empty
  43.      if (lpDrawItemStruct->itemID == (UINT)-1)
  44.      {
  45. /* if (this->IsWindowEnabled())
  46.  {
  47.           if (lpDrawItemStruct->itemAction & ODA_FOCUS)
  48.                pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
  49.  }*/
  50.           return;
  51.      }
  52.      else
  53.      {
  54.           int selChange   = lpDrawItemStruct->itemAction & ODA_SELECT;
  55.           int focusChange = lpDrawItemStruct->itemAction & ODA_FOCUS;
  56.           int drawEntire  = lpDrawItemStruct->itemAction & ODA_DRAWENTIRE;
  57.           if (selChange || drawEntire) {
  58.                BOOL sel = lpDrawItemStruct->itemState & ODS_SELECTED;
  59.                // Draws background rectangle
  60.    if (this->IsWindowEnabled())
  61.    {
  62. pDC->FillSolidRect(&lpDrawItemStruct->rcItem, 
  63.   ::GetSysColor((GetExStyle()&WS_EX_TRANSPARENT)?COLOR_BTNFACE:COLOR_WINDOW));
  64.    }
  65.    else
  66.    {
  67.    pDC->FillSolidRect(&lpDrawItemStruct->rcItem,GetSysColor(COLOR_BTNFACE));
  68.    }
  69.    // Draw radio button
  70.    int h = lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top;
  71.                CRect rect(lpDrawItemStruct->rcItem.left+1, lpDrawItemStruct->rcItem.top+1, 
  72.    lpDrawItemStruct->rcItem.left+h-2, lpDrawItemStruct->rcItem.top+h-2);
  73.    UINT control = (isMultiSelect)?DFCS_BUTTONCHECK:DFCS_BUTTONRADIO;
  74.    if (this->IsWindowEnabled())
  75.       pDC->DrawFrameControl(&rect, DFC_BUTTON, control | (sel?DFCS_CHECKED:0));
  76.    else
  77.   pDC->DrawFrameControl(&rect, DFC_BUTTON, control | (sel?DFCS_CHECKED:0) |  DFCS_INACTIVE);
  78.                // Draws item text
  79.                pDC->SetTextColor(COLOR_WINDOWTEXT);
  80.                pDC->SetBkMode(TRANSPARENT);
  81.                lpDrawItemStruct->rcItem.left += h;
  82.    CString itT;
  83.    //GetText(lpDrawItemStruct->itemID,itT);
  84.    itT = GetItemText(lpDrawItemStruct->itemID,0);
  85.    if (this->IsWindowEnabled()) 
  86. pDC->DrawText(/*(LPCTSTR)lpDrawItemStruct->itemData*/itT, &lpDrawItemStruct->rcItem, DT_LEFT);
  87.    else
  88.    {
  89.    COLORREF oldCol  = pDC->GetTextColor();
  90.    COLORREF col = RGB(GetRValue(::GetSysColor(COLOR_BTNFACE))-20,
  91.    GetGValue(::GetSysColor(COLOR_BTNFACE))-20,
  92.    GetBValue(::GetSysColor(COLOR_BTNFACE))-20);
  93.    pDC->SetTextColor(col);
  94.    pDC->DrawText(/*(LPCTSTR)lpDrawItemStruct->itemData*/itT, &lpDrawItemStruct->rcItem, DT_LEFT);
  95.    pDC->SetTextColor(oldCol);
  96.    }
  97.           }
  98.   // draws focus rectangle
  99.  /* if (this->IsWindowEnabled())
  100.              if (focusChange || (drawEntire && (lpDrawItemStruct->itemState & ODS_FOCUS)))
  101.                pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);*/
  102.      }
  103. }
  104. HBRUSH CSelectingListCtrl::CtlColor(CDC* pDC, UINT nCtlColor) 
  105. {
  106.     // If transparent style selected...
  107. if (nCtlColor==CTLCOLOR_LISTBOX)
  108. {
  109. if ( (GetExStyle()&WS_EX_TRANSPARENT))
  110. return (HBRUSH)::GetSysColorBrush(COLOR_BTNFACE);
  111. else
  112. {
  113. if (!this->IsWindowEnabled())
  114. return (HBRUSH)::GetSysColorBrush(COLOR_BTNFACE);
  115. else
  116. return (HBRUSH)::GetSysColorBrush(COLOR_WINDOW);
  117. }
  118. }
  119. return NULL;
  120. }
  121. void CSelectingListCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  122. {
  123. // TODO: Add your message handler code here and/or call default
  124. //CListCtrl::OnChar(nChar, nRepCnt, nFlags);
  125. }
  126. void CSelectingListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  127. {
  128. //CListCtrl::OnLButtonDown(nFlags,point);
  129. LVHITTESTINFO lvhti;
  130. int nItem;
  131. UINT nState;
  132. lvhti.pt = point;
  133. nItem = SubItemHitTest(&lvhti);
  134. if (nItem == -1)
  135. return;
  136. nState = GetItemState(nItem, LVIS_SELECTED);
  137. DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);
  138. bool isMultiSelect = !(dwStyle & LVS_SINGLESEL);
  139. if (nState & LVIS_SELECTED)
  140. SetItemState(nItem, isMultiSelect?0:LVIS_SELECTED, LVIS_SELECTED);
  141. else
  142. SetItemState(nItem, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED 
  143. | LVIS_SELECTED);
  144. NMITEMACTIVATE nm;
  145. memset(&nm,0,sizeof(NMITEMACTIVATE));
  146. nm.iItem = nItem;
  147. nm.hdr.hwndFrom = this->m_hWnd;
  148. nm.hdr.code = NM_CLICK;
  149. GetParent()->SendMessage(WM_NOTIFY,0,(LPARAM)&nm);
  150. }