OAMFLATBUTTON.CPP
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:5k
- // OAMFlatButton.cpp : implementation file
- //
- #include "stdafx.h"
- #include "OAMFlatButton.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // COAMFlatButton
- COAMFlatButton::COAMFlatButton()
- {
- m_nState = 0;
- m_bLBtnDown = FALSE;
- m_bFlatLook = TRUE;
- m_bPainted = FALSE;
- m_clrHilite = ::GetSysColor(COLOR_BTNHIGHLIGHT);
- m_clrShadow = ::GetSysColor(COLOR_BTNSHADOW);
- m_clrDkShad = ::GetSysColor(COLOR_3DDKSHADOW);
- m_clrNormal = ::GetSysColor(COLOR_BTNFACE);
- m_clrTextGy = ::GetSysColor(COLOR_GRAYTEXT);
- m_clrTextNm = ::GetSysColor(COLOR_BTNTEXT);
- }
- COAMFlatButton::~COAMFlatButton()
- {
- }
- BEGIN_MESSAGE_MAP(COAMFlatButton, CButton)
- //{{AFX_MSG_MAP(COAMFlatButton)
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_TIMER()
- ON_WM_SYSCOLORCHANGE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // COAMFlatButton message handlers
- void COAMFlatButton::OnMouseMove(UINT nFlags, CPoint point)
- {
- if (m_bFlatLook) {
- SetTimer(1, 10, NULL);
- OnTimer(1);
- }
- CButton::OnMouseMove(nFlags, point);
- }
- void COAMFlatButton::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_bLBtnDown = TRUE;
- CButton::OnLButtonDown(nFlags, point);
- }
- void COAMFlatButton::OnLButtonUp(UINT nFlags, CPoint point)
- {
- m_bLBtnDown = FALSE;
- CButton::OnLButtonUp(nFlags, point);
- }
- void COAMFlatButton::OnTimer(UINT nIDEvent)
- {
- CRect rcItem;
- GetWindowRect(rcItem);
- CPoint ptCursor;
- GetCursorPos(&ptCursor);
- if ((m_bLBtnDown==TRUE) || (!rcItem.PtInRect(ptCursor)))
- {
- KillTimer (1);
- if (m_bPainted == TRUE) {
- InvalidateRect (NULL);
- }
- m_bPainted = FALSE;
- return;
- }
- // On mouse over, show raised button.
- else if(m_bFlatLook)
- {
- // Get the device context for the client area.
- CDC *pDC = GetDC();
- if (m_bPainted == FALSE)
- {
- // repaint client area.
- GetClientRect(rcItem);
- pDC->FillSolidRect(rcItem, m_clrNormal);
- DrawIcon(pDC);
-
- // draw the button rect.
- pDC->Draw3dRect(rcItem, m_clrHilite, m_clrShadow);
- m_bPainted = TRUE;
- }
- ReleaseDC (pDC);
- }
-
- CButton::OnTimer(nIDEvent);
- }
- void COAMFlatButton::OnSysColorChange()
- {
- CButton::OnSysColorChange();
- m_clrHilite = ::GetSysColor(COLOR_BTNHIGHLIGHT);
- m_clrShadow = ::GetSysColor(COLOR_BTNSHADOW);
- m_clrDkShad = ::GetSysColor(COLOR_3DDKSHADOW);
- m_clrNormal = ::GetSysColor(COLOR_BTNFACE);
- m_clrTextGy = ::GetSysColor(COLOR_GRAYTEXT);
- m_clrTextNm = ::GetSysColor(COLOR_BTNTEXT);
- }
- void COAMFlatButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- // ASSERT(lpDrawItemStruct != NULL);
- CDC* pDC = GetDC();
-
- // Get the button state.
- m_nState = lpDrawItemStruct->itemState;
- // Copy the rect, and fill the background.
- m_rcItem.CopyRect(&lpDrawItemStruct->rcItem);
- pDC->FillSolidRect(m_rcItem, m_clrNormal);
- if(m_bFlatLook)
- {
- // Draw button pressed.
- if ((m_nState & ODS_SELECTED)) {
- pDC->Draw3dRect (m_rcItem, m_clrShadow, m_clrHilite);
- }
- // Draw button flat.
- else {
- pDC->Draw3dRect (m_rcItem, m_clrNormal, m_clrNormal);
- }
- }
- else
- {
- CRect rcItem(m_rcItem);
- rcItem.DeflateRect(1,1);
- // Draw button pressed.
- if ((m_nState & ODS_SELECTED)) {
- pDC->Draw3dRect (m_rcItem, m_clrDkShad, m_clrHilite);
- pDC->Draw3dRect (rcItem, m_clrShadow, m_clrNormal);
- }
- // Draw button raised.
- else {
- pDC->Draw3dRect (m_rcItem, m_clrHilite, m_clrDkShad);
- pDC->Draw3dRect (rcItem, m_clrNormal, m_clrShadow);
- }
- }
- // Save the item state, set background to transparent.
- pDC->SetBkMode( TRANSPARENT );
- DrawIcon(pDC);
- ReleaseDC (pDC);
- }
- void COAMFlatButton::DrawIcon(CDC *pDC)
- {
- // if an icon is associated with button, draw it.
- HICON hIcon = GetIcon();
- HBITMAP hBitmap = GetBitmap();
- if (hIcon)
- {
- CRect rcWnd;
- GetWindowRect(&rcWnd);
-
- int left = (rcWnd.Width()-m_sizeIcon.cx)/2;
- int right = left+m_sizeIcon.cx;
- int top = (rcWnd.Height()-m_sizeIcon.cy)/2;
- int bottom = top+m_sizeIcon.cy;
- m_rcItem.CopyRect(CRect(left,top,right,bottom));
- if ((m_nState & ODS_SELECTED)) {
- m_rcItem.OffsetRect(1,1);
- }
- DrawIconEx(
- pDC->GetSafeHdc(),
- m_rcItem.left,
- m_rcItem.top,
- hIcon,
- m_rcItem.Width(),
- m_rcItem.Height(),
- NULL,
- (HBRUSH)NULL,
- DI_NORMAL);
- }
- else if (hBitmap)
- {
- // not implemented.
- }
- else
- {
- if (m_nState & ODS_DISABLED)
- pDC->SetTextColor(m_clrTextGy);
- else
- pDC->SetTextColor(m_clrTextNm);
- CFont newFont, *oldFont;
- NONCLIENTMETRICS ncm;
- ncm.cbSize = sizeof(NONCLIENTMETRICS);
- VERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
- sizeof(NONCLIENTMETRICS), &ncm, 0));
- newFont.CreateFontIndirect(&ncm.lfMessageFont);
- oldFont = pDC->SelectObject(&newFont);
- CString strText;
- GetWindowText(strText);
- pDC->DrawText(strText, m_rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- pDC->SelectObject(oldFont);
- }
- }
- void COAMFlatButton::SetIcon(HICON hIcon, CSize size)
- {
- m_sizeIcon = size;
- ::DestroyIcon( CButton::SetIcon(hIcon) );
- }