CoolListBox.cpp
上传用户:sunh8215
上传日期:2010-02-13
资源大小:1616k
文件大小:7k
- /*####################################################################
- Filename: coollistbox.cpp
- ----------------------------------------------------
- Remarks: ...
- ----------------------------------------------------
- ####################################################################*/
- #include "stdafx.h"
- #include "resource.h"
- #include "CoolListBox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #include "CoolTabCtrl.h"
- /*####################################################################
- ------------------------------------------------
- CListBoxItem class
- ------------------------------------------------
- ####################################################################*/
- CListBoxItem::CListBoxItem(int nIndex, int nImage, CString strText, CCoolListBox *parent)
- {
- m_nIndex = nIndex;
- m_nImage = nImage;
- m_strText = strText;
- m_parent = parent;
- ID = 0;
- }
- void CListBoxItem::SetTipText(LPCTSTR strTipText)
- {
- m_strTipText = strTipText;
- }
- /*########################################################################
- ------------------------------------------------
- class CCoolListBox
- ------------------------------------------------
- ########################################################################*/
- CCoolListBox::CCoolListBox()
- {
- m_nHotItem = - 1;
- }
- CCoolListBox::~CCoolListBox()
- {
- while(!m_aItems.IsEmpty())
- {
- CListBoxItem* pItem = m_aItems.RemoveHead();
- delete pItem;
- pItem = NULL;
- }
- }
- BOOL CCoolListBox::Create(DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID)
- {
- // Make sure that the control is owner drawn.
- dwStyle |= LBS_OWNERDRAWVARIABLE | LBS_NOINTEGRALHEIGHT;
- if (!CListBox::Create(dwStyle, rect, pParentWnd, nID))
- {
- return FALSE;
- }
- // Set the font used by the bar.
- SetFont(CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT)));
- return TRUE;
- }
- /*########################################################################
- -------------------------------------
- CCoolListBox message handlers
- -------------------------------------
- ########################################################################*/
- BEGIN_MESSAGE_MAP(CCoolListBox, CListBox)
- //{{AFX_MSG_MAP(CCoolListBox)
- ON_WM_MEASUREITEM_REFLECT()
- ON_WM_DRAWITEM_REFLECT()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_TIMER()
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_BN_CLICKED(IDC_UPBUTTON, OnUpButton)
- ON_BN_CLICKED(IDC_DOWNBUTTON, OnDownButton)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- int CCoolListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListBox::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- m_wndTipctrl.Create(CSize(100,100),this,TIPS_AUTOSIZE | TIPS_TRANSPARENT);
- m_wndUpButton.Create("", WS_VISIBLE | WS_CHILD, CRect(0, 0, 0, 0), this, IDC_UPBUTTON);
- m_wndDownButton.Create("", WS_VISIBLE | WS_CHILD, CRect(0, 0, 0, 0), this, IDC_DOWNBUTTON);
- return 0;
- }
- void CCoolListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
- {
- lpMeasureItemStruct->itemHeight = 48;
- }
- void CCoolListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- CListBoxItem* pItem = (CListBoxItem*)lpDIS->itemData;
- if (pItem == NULL) return;
-
- CDC* pDC = CDC::FromHandle(lpDIS->hDC);
- CRect rcItem = lpDIS->rcItem;
- pDC->SetBkMode(TRANSPARENT);
- CPoint ptIcon;
- ptIcon.x = (rcItem.Width() - 32) / 2;
- ptIcon.y = rcItem.top + 2;
- // pDC->FillSolidRect(rcItem, RGB(255, 255, 255));
- m_imagelist.Draw(pDC, pItem->m_nImage, ptIcon, 0);
-
- CRect rcText(rcItem);
- rcText.top += 34;
- if ((m_nHotItem == lpDIS->itemID) && m_bHilight)
- {
- pDC->SetTextColor(RGB(0, 0, 255));
- }
- else
- {
- pDC->SetTextColor(RGB(0, 0, 0));
- }
- pDC->DrawText(pItem->m_strText, rcText, DT_CENTER | DT_SINGLELINE);
- }
- void CCoolListBox::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CListBox::OnLButtonDown(nFlags, point);
- int nIndex = GetCurSel();
- if (nIndex != -1)
- {
- CListBoxItem *pItem = (CListBoxItem*)GetItemData(nIndex);
-
- if (pItem != NULL)
- {
- AfxGetMainWnd()->SendMessage(WM_COMMAND, pItem->ID, 0);
- }
- else
- {
- AfxMessageBox("No Message");
- }
- }
- }
- void CCoolListBox::OnLButtonUp(UINT nFlags, CPoint point)
- {
- m_bLBDown = false;
- int nIndex = GetCurSel();
- SetCurSel(nIndex);
- CListBox::OnLButtonUp(nFlags, point);
- }
- void CCoolListBox::OnMouseMove(UINT nFlags, CPoint point)
- {
- m_point = point;
- SetTimer (1, 10, NULL);
- CListBox::OnMouseMove(nFlags, point);
- }
- void CCoolListBox::OnUpButton()
- {
- if (GetTopIndex() > 0)
- {
- SetTopIndex(GetTopIndex() - 1);
- ReLocation();
- Invalidate();
- }
- }
- void CCoolListBox::OnDownButton()
- {
- if (GetTopIndex() < this->GetCount() - 1)
- {
- SetTopIndex(GetTopIndex() + 1);
- ReLocation();
- Invalidate();
- return;
- }
- }
- void CCoolListBox::OnSize(UINT nType, int cx, int cy)
- {
- ReLocation();
- }
- void CCoolListBox::OnTimer(UINT nIDEvent)
- {
- if (nIDEvent == 1)
- {
- CRect rcWindow;
- GetWindowRect (rcWindow);
- CPoint point;
- GetCursorPos (&point);
- if (rcWindow.PtInRect(point))
- {
- m_bHilight = true;
- BOOL bOutSide;
- int nIndex = ItemFromPoint(m_point, bOutSide);
- if (nIndex != m_nHotItem && !bOutSide)
- {
- //SetCurSel(nIndex);
- CRect rect;
- GetItemRect(m_nHotItem, &rect);
- InvalidateRect(&rect);
- m_nHotItem = nIndex;
- GetItemRect(nIndex, &rect);
- InvalidateRect(&rect);
-
- ClientToScreen(&rect);
- m_wndTipctrl.SetRect(rect);
- CListBoxItem* pItem = GetItem(nIndex);
- if (pItem != NULL)
- {
- m_wndTipctrl.SetText(pItem->m_strTipText);
- m_wndTipctrl.SetTitle(pItem->m_strText);
- m_wndTipctrl.SetDelayTime(15);
- m_wndTipctrl.SetIcon(m_imagelist.ExtractIcon(pItem->m_nImage), CSize(32, 32));
- m_wndTipctrl.Show(point);
- }
- }
- }
- else
- {
- m_bHilight = false;
- CRect rect;
- GetItemRect(m_nHotItem, &rect);
- InvalidateRect(&rect);
- m_nHotItem = - 1;
- KillTimer(1);
- }
- }
- CListBox::OnTimer(nIDEvent);
- }
- /*########################################################################
- ------------------------------------------------
-
- ------------------------------------------------
- ########################################################################*/
- int CCoolListBox::AddItem(int nImage, LPCTSTR strText,UINT id, LPCTSTR strTipText)
- {
- CListBoxItem* pItem;
- int nindex = AddString(strText);
- pItem = new CListBoxItem(nindex, nImage, strText, this);
- pItem->SetTipText(strTipText);
- pItem->ID = id;
- ASSERT(pItem != NULL);
- m_aItems.AddTail(pItem);
- SetItemData(nindex, DWORD(pItem));
- return nindex;
- }
- BOOL CCoolListBox::SetImagelist(UINT uBitmap)
- {
- m_imagelist.DeleteImageList();
- m_imagelist.Create(32, 32, ILC_COLOR24|ILC_MASK, 8, 1);
- CBitmap bitmap;
- bitmap.LoadBitmap(uBitmap);
- m_imagelist.Add(&bitmap, RGB(255,0,255));
- return TRUE;
- }
- CListBoxItem* CCoolListBox::GetItem(int nIndex)
- {
- // Get the menu item.
- POSITION pos = m_aItems.FindIndex(nIndex);
- if (pos == 0) return NULL;
- CListBoxItem* pItem = m_aItems.GetAt(pos);
- ASSERT(pItem);
- return pItem;
- }
- void CCoolListBox::ReLocation()
- {
- CRect rect;
- GetClientRect(&rect);
- CRect rc(rect);
- rc.right -= 3; rc.top += 3;
- rc.left = rc.right - 12;
- rc.bottom = rc.top + 12;
-
- m_wndUpButton.MoveWindow(rc);
- rc = rect;
- rc.right -= 3; rc.bottom -= 3;
- rc.left = rc.right - 12;
- rc.top = rc.bottom - 12;
- m_wndDownButton.MoveWindow(rc);
- }