MyHeaderCtrl.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:14k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // MyHeaderCtrl.cpp : implementation file
- //
- #include "stdafx.h"
- #include "File.h"
- #include "MyHeaderCtrl.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyHeaderCtrl
- static const CString strOfficeFontName = _T("Tahoma");
- static const CString strDefaultFontName = _T("MS Sans Serif");
- static const CString strVertFontName = _T("Arial");
- static const CString strMarlettFontName = _T("Marlett");
- static int CALLBACK FontFamalyProcFonts (const LOGFONT FAR* lplf,
- const TEXTMETRIC FAR* /*lptm*/,
- ULONG /*ulFontType*/,
- LPARAM /*lParam*/)
- {
- ASSERT (lplf != NULL);
- CString strFont = lplf->lfFaceName;
- return strFont.CollateNoCase (strOfficeFontName) == 0 ? 0 : 1;
- }
- CMyHeaderCtrl::CMyHeaderCtrl()
- {
- m_bIsMousePressed = FALSE;
- m_bMultipleSort = FALSE;
- m_bAscending = TRUE;
- m_nHighlightedItem = -1;
- clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
- clrBtnShadow = ::GetSysColor(COLOR_BTNSHADOW);
- clrBtnHilite = ::GetSysColor(COLOR_BTNHIGHLIGHT);
- clrBtnDkShadow = ::GetSysColor(COLOR_3DDKSHADOW);
- clrBtnLight = ::GetSysColor(COLOR_3DLIGHT);
- clrWindowFrame = ::GetSysColor(COLOR_WINDOWFRAME);
- clrBtnText = ::GetSysColor(COLOR_BTNTEXT);
- brBtnFace.Detach ();
- brBtnFace.CreateSysColorBrush (COLOR_BTNFACE);
- //------------------
- // Initialize fonts:
- //------------------
- NONCLIENTMETRICS info;
- info.cbSize = sizeof(info);
- ::SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof(info), &info, 0);
- LOGFONT lf;
- memset (&lf, 0, sizeof (LOGFONT));
- CWindowDC dc (NULL);
- lf.lfCharSet = (BYTE) GetTextCharsetInfo (dc.GetSafeHdc (), NULL, 0);
- lf.lfHeight = info.lfMenuFont.lfHeight;
- lf.lfWeight = info.lfMenuFont.lfWeight;
- lf.lfItalic = info.lfMenuFont.lfItalic;
- _tcscpy (lf.lfFaceName, info.lfMenuFont.lfFaceName);
- BOOL fUseSystemFont = (info.lfMenuFont.lfCharSet > SYMBOL_CHARSET);
- if (!fUseSystemFont)
- {
- if (::EnumFontFamilies (dc.GetSafeHdc (), NULL, FontFamalyProcFonts, 0) == 0)
- _tcscpy (lf.lfFaceName, strOfficeFontName);
- else _tcscpy (lf.lfFaceName, strDefaultFontName);
- }
- fontRegular.CreateFontIndirect (&lf);
- }
- CMyHeaderCtrl::~CMyHeaderCtrl()
- {
- }
- BEGIN_MESSAGE_MAP(CMyHeaderCtrl, CHeaderCtrl)
- //{{AFX_MSG_MAP(CMyHeaderCtrl)
- ON_WM_ERASEBKGND()
- ON_WM_PAINT()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_CANCELMODE()
- //}}AFX_MSG_MAP
- ON_MESSAGE(HDM_LAYOUT, OnLayout)
- ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyHeaderCtrl message handlers
- void CMyHeaderCtrl::OnDrawItem (CDC* pDC, int iItem, CRect rect, BOOL bIsPressed, BOOL bIsHighlighted)
- {
- ASSERT_VALID (this);
- ASSERT_VALID (pDC);
- const int nTextMargin = 5;
- //-------------
- // Draw border:
- //-------------
- if (bIsPressed)
- {
- pDC->Draw3dRect (rect,clrBtnShadow,clrBtnShadow);
- rect.left++; rect.top++;
- }else pDC->Draw3dRect (rect,clrBtnHilite,clrBtnShadow);
- if (iItem < 0) return;
- int nSortVal = 0;
- if (m_mapColumnsStatus.Lookup (iItem, nSortVal) && nSortVal != 0)
- {
- //-----------------
- // Draw sort arrow:
- //-----------------
- CRect rectArrow = rect;
- rectArrow.left = rectArrow.right - rectArrow.Height ();
- rect.right = rectArrow.left+1;
- m_bAscending = nSortVal > 0;
- OnDrawSortArrow (pDC, rectArrow);
- }
- HD_ITEM hdItem;
- memset (&hdItem, 0, sizeof (hdItem));
- hdItem.mask = HDI_FORMAT | HDI_BITMAP | HDI_TEXT | HDI_IMAGE;
- TCHAR szText [256];
- hdItem.pszText = szText;
- hdItem.cchTextMax = 255;
- if (!GetItem (iItem, &hdItem)) return;
- CBitmap bmp;
- bmp.LoadBitmap(IDB_BACK);
- BITMAP bmpInfo;
- bmp.GetBitmap(&bmpInfo);
- CDC dcMem;
- dcMem.CreateCompatibleDC(pDC);
- CBitmap *pOldBitmap=dcMem.SelectObject(&bmp);
- pDC->StretchBlt(rect.left,rect.top,rect.Width()-1,rect.Height(),&dcMem,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY);
- dcMem.SelectObject(pOldBitmap);
- //-----------------------
- // Draw bitmap and image:
- //-----------------------
- if ((hdItem.fmt & HDF_IMAGE) && hdItem.iImage >= 0)
- {
- //---------------------------------------
- // The column has a image from imagelist:
- //---------------------------------------
- CImageList* pImageList = GetImageList ();
- if (pImageList != NULL)
- {
- int cx = 0;
- int cy = 0;
- VERIFY (::ImageList_GetIconSize (*pImageList, &cx, &cy));
- CPoint pt = rect.TopLeft ();
- pt.x ++;
- pt.y = (rect.top + rect.bottom - cy) / 2;
- VERIFY (pImageList->Draw (pDC, hdItem.iImage, pt, ILD_NORMAL));
- rect.left += cx;
- }
- }
- if ((hdItem.fmt & (HDF_BITMAP | HDF_BITMAP_ON_RIGHT)) && hdItem.hbm != NULL)
- {
- CBitmap* pBmp = CBitmap::FromHandle (hdItem.hbm);
- ASSERT_VALID (pBmp);
- BITMAP bmp;
- pBmp->GetBitmap (&bmp);
- CRect rectBitmap = rect;
- if (hdItem.fmt & HDF_BITMAP_ON_RIGHT)
- {
- rectBitmap.right--;
- rect.right = rectBitmap.left = rectBitmap.right - bmp.bmWidth;
- }else
- {
- rectBitmap.left++;
- rect.left = rectBitmap.right = rectBitmap.left + bmp.bmWidth;
- }
- rectBitmap.top += max (0, (rectBitmap.Height () - bmp.bmHeight) / 2);
- rectBitmap.bottom = rectBitmap.top + bmp.bmHeight;
- pDC->DrawState (rectBitmap.TopLeft (), rectBitmap.Size (), pBmp, DSS_NORMAL);
- }
- //-----------
- // Draw text:
- //-----------
- if (hdItem.fmt & HDF_STRING)
- {
- CRect rectLabel = rect;
- rectLabel.DeflateRect (nTextMargin, 0);
- CString strLabel = hdItem.pszText;
- UINT uiTextFlags = DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX;
- if (hdItem.fmt & HDF_CENTER) uiTextFlags |= DT_CENTER;
- else if (hdItem.fmt & HDF_RIGHT) uiTextFlags |= DT_RIGHT;
- pDC->SetTextColor(RGB(255,255,255));
- pDC->DrawText (strLabel, rectLabel, uiTextFlags);
- }
- }
- //***************************************************************************************
- void CMyHeaderCtrl::SetSortColumn (int iColumn, BOOL bAscending, BOOL bAdd)
- {
- ASSERT_VALID (this);
- if (iColumn < 0)
- {
- m_mapColumnsStatus.RemoveAll ();
- return;
- }
- if (bAdd)
- {
- if (!m_bMultipleSort)
- {
- ASSERT (FALSE);
- bAdd = FALSE;
- }
- }
- if (!bAdd)
- {
- m_mapColumnsStatus.RemoveAll ();
- }
- m_mapColumnsStatus.SetAt (iColumn, bAscending ? 1 : -1);
- RedrawWindow ();
- }
- //***************************************************************************************
- void CMyHeaderCtrl::RemoveSortColumn (int iColumn)
- {
- ASSERT_VALID (this);
- m_mapColumnsStatus.RemoveKey (iColumn);
- RedrawWindow ();
- }
- //***************************************************************************************
- BOOL CMyHeaderCtrl::OnEraseBkgnd(CDC* /*pDC*/)
- {
- return TRUE;
- }
- //***************************************************************************************
- void CMyHeaderCtrl::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- CRect rectClip;
- dc.GetClipBox (rectClip);
- CRect rectClient;
- GetClientRect (rectClient);
- CDC* pDC = &dc;
- BOOL bMemDC = FALSE;
- CDC dcMem;
- CBitmap bmp;
- CBitmap* pOldBmp = NULL;
- if (dcMem.CreateCompatibleDC (&dc) &&bmp.CreateCompatibleBitmap (&dc, rectClient.Width (),rectClient.Height ()))
- {
- //-------------------------------------------------------------
- // Off-screen DC successfully created. Better paint to it then!
- //-------------------------------------------------------------
- bMemDC = TRUE;
- pOldBmp = dcMem.SelectObject (&bmp);
- pDC = &dcMem;
- }
- OnFillBackground (pDC);
- CFont* pOldFont = (CFont*) pDC->SelectObject (&fontRegular);
- ASSERT_VALID (pOldFont);
- pDC->SetTextColor (clrBtnText);
- pDC->SetBkMode (TRANSPARENT);
- CRect rect;
- GetClientRect(rect);
- CRect rectItem;
- int nCount = GetItemCount ();
- for (int i = 0; i < nCount; i++)
- {
- //------------------
- // Is item pressed?
- //------------------
- CPoint ptCursor;
- ::GetCursorPos (&ptCursor);
- ScreenToClient (&ptCursor);
- HDHITTESTINFO hdHitTestInfo;
- hdHitTestInfo.pt = ptCursor;
- int iHit = SendMessage (HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
- BOOL bIsHighlighted = iHit == i && (hdHitTestInfo.flags & HHT_ONHEADER);
- BOOL bIsPressed = m_bIsMousePressed && bIsHighlighted;
- GetItemRect (i, rectItem);
- CRgn rgnClip;
- rgnClip.CreateRectRgnIndirect (&rectItem);
- pDC->SelectClipRgn (&rgnClip);
- //-----------
- // Draw item:
- //-----------
- OnDrawItem (pDC, i, rectItem, bIsPressed, m_nHighlightedItem == i);
- pDC->SelectClipRgn (NULL);
- }
- //--------------------
- // Draw "tail border":
- //--------------------
- if (nCount == 0)
- {
- rectItem = rect;
- rectItem.right++;
- }
- else
- {
- rectItem.left = rectItem.right;
- rectItem.right = rect.right + 1;
- }
- OnDrawItem (pDC, -1, rectItem, FALSE, FALSE);
- pDC->SelectObject (pOldFont);
- if (bMemDC)
- {
- //--------------------------------------
- // Copy the results to the on-screen DC:
- //--------------------------------------
- dc.BitBlt (rectClip.left, rectClip.top, rectClip.Width(), rectClip.Height(),
- &dcMem, rectClip.left, rectClip.top, SRCCOPY);
- dcMem.SelectObject(pOldBmp);
- }
- }
- //***************************************************************************************
- void CMyHeaderCtrl::OnFillBackground (CDC* pDC)
- {
- ASSERT_VALID (this);
- ASSERT_VALID (pDC);
- CRect rectClient;
- GetClientRect (rectClient);
- // pDC->FillRect (rectClient, &brBtnFace);
- CBrush b;
- CBitmap bmp;
- bmp.LoadBitmap( IDB_BACK );
- b.CreatePatternBrush( &bmp );
- pDC->FillRect (rectClient, &b);
- }
- //***************************************************************************************
- void CMyHeaderCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_bIsMousePressed = TRUE;
- CHeaderCtrl::OnLButtonDown(nFlags, point);
- }
- //***************************************************************************************
- void CMyHeaderCtrl::OnLButtonUp(UINT nFlags, CPoint point)
- {
- m_bIsMousePressed = FALSE;
- CHeaderCtrl::OnLButtonUp(nFlags, point);
- }
- //***************************************************************************************
- void CMyHeaderCtrl::OnDrawSortArrow (CDC* pDC, CRect rectArrow)
- {
- ASSERT_VALID (pDC);
- ASSERT_VALID (this);
- CPen penLight (1, PS_SOLID,clrBtnHilite);
- CPen penDark (1, PS_SOLID,clrBtnDkShadow);
- CPen* pPenOld = pDC->SelectObject (&penLight);
- ASSERT_VALID (pPenOld);
- CBitmap bmp;
- if (!m_bAscending) bmp.LoadBitmap(IDB_SORT1);
- else bmp.LoadBitmap(IDB_SORT2);
- BITMAP bmpInfo;
- bmp.GetBitmap(&bmpInfo);
- CDC dcMem;
- dcMem.CreateCompatibleDC(pDC);
- CBitmap *pOldBitmap=dcMem.SelectObject(&bmp);
- pDC->StretchBlt(rectArrow.left,rectArrow.top,rectArrow.Width(),rectArrow.Height(),&dcMem,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY);
- dcMem.SelectObject(pOldBitmap);
- pDC->SelectObject (pPenOld);
- }
- //*********************************************************************************
- void CMyHeaderCtrl::EnableMultipleSort (BOOL bEnable)
- {
- ASSERT_VALID (this);
- if (m_bMultipleSort == bEnable) return;
- m_bMultipleSort = bEnable;
- if (!m_bMultipleSort)
- {
- m_mapColumnsStatus.RemoveAll ();
- if (GetSafeHwnd () != NULL) RedrawWindow ();
- }
- }
- //*********************************************************************************
- int CMyHeaderCtrl::GetSortColumn () const
- {
- ASSERT_VALID (this);
- if (m_bMultipleSort)
- {
- TRACE0("Call CMyHeaderCtrl::GetColumnState for muliple sortn");
- ASSERT (FALSE);
- return -1;
- }
- int nCount = GetItemCount ();
- for (int i = 0; i < nCount; i++)
- {
- int nSortVal = 0;
- if (m_mapColumnsStatus.Lookup (i, nSortVal) && nSortVal != 0)
- {
- return i;
- }
- }
- return -1;
- }
- //*********************************************************************************
- BOOL CMyHeaderCtrl::IsAscending () const
- {
- ASSERT_VALID (this);
- if (m_bMultipleSort)
- {
- TRACE0("Call CBCGHeaderCtrl::GetColumnState for muliple sortn");
- ASSERT (FALSE);
- return -1;
- }
- int nCount = GetItemCount ();
- for (int i = 0; i < nCount; i++)
- {
- int nSortVal = 0;
- if (m_mapColumnsStatus.Lookup (i, nSortVal) &&
- nSortVal != 0)
- {
- return nSortVal > 0;
- }
- }
- return i;
- }
- //*********************************************************************************
- int CMyHeaderCtrl::GetColumnState (int iColumn) const
- {
- int nSortVal = 0;
- m_mapColumnsStatus.Lookup (iColumn, nSortVal);
- return nSortVal;
- }
- //**********************************************************************************
- void CMyHeaderCtrl::OnMouseMove(UINT nFlags, CPoint point)
- {
- if ((nFlags & MK_LBUTTON) == 0)
- {
- HDHITTESTINFO hdHitTestInfo;
- hdHitTestInfo.pt = point;
- int nPrevHighlightedItem = m_nHighlightedItem;
- m_nHighlightedItem = SendMessage (HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
- if ((hdHitTestInfo.flags & HHT_ONHEADER) == 0)
- {
- m_nHighlightedItem = -1;
- }
- if (nPrevHighlightedItem != m_nHighlightedItem)
- {
- RedrawWindow ();
- }
- }
- CHeaderCtrl::OnMouseMove(nFlags, point);
- }
- //*****************************************************************************************
- LRESULT CMyHeaderCtrl::OnMouseLeave(WPARAM,LPARAM)
- {
- if (m_nHighlightedItem >= 0)
- {
- m_nHighlightedItem = -1;
- RedrawWindow ();
- }
- return 0;
- }
- //*****************************************************************************************
- void CMyHeaderCtrl::OnCancelMode()
- {
- CHeaderCtrl::OnCancelMode();
- if (m_nHighlightedItem >= 0)
- {
- m_nHighlightedItem = -1;
- RedrawWindow ();
- }
- }
- LRESULT CMyHeaderCtrl::OnLayout(WPARAM, LPARAM lParam)
- {
- LPHDLAYOUT lphdlayout = (LPHDLAYOUT)lParam;
- if (m_bStaticBorder)
- lphdlayout->prc->right += GetSystemMetrics(SM_CXBORDER)*2;
- return CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);
- }