📄 indicator.cpp
字号:
// Indicator.cpp : implementation file
//
#include "stdafx.h"
//#include "hovbutt.h"
#include "Indicator.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CIndicator
BEGIN_MESSAGE_MAP(CIndicator, CBitmapButton)
//{{AFX_MSG_MAP(CIndicatorButton)
// ON_WM_MOUSEMOVE()
// ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHoverButton construction / destruction
CIndicator::CIndicator()
{
// To start with, the button is switched off and we are NOT tracking the mouse
m_IndicatorState = INDICATOR_ON;
m_bMouseTracking = FALSE;
}
CIndicator::~CIndicator()
{
}
/////////////////////////////////////////////////////////////////////////////
// CIndicator message handlers
/*void CIndicator::OnMouseMove(UINT nFlags, CPoint point)
{
CBitmapIndicator::OnMouseMove(nFlags, point);
// 1. Mouse has moved and we are not tracking this button, or
// 2. mouse has moved and the cursor was not above this window
// == Is equivalent to WM_MOUSEENTER (for which there is no message)
if((!m_bMouseTracking || GetCapture()!=this) && (m_IndicatorState == INDICATOR_OFF))
{
OnMouseEnter();
}
else
{
if(m_IndicatorState == INDICATOR_OVER)
{
CRect rc;
GetClientRect(&rc);
if(!rc.PtInRect(point)) // The mouse cursor is no longer above this button
OnMouseLeave();
}
}
}
void CIndicator::OnMouseEnter(void)
{
// We are now tracking the mouse, OVER this button
m_bMouseTracking = TRUE;
m_IndicatorState = INDICATOR_OVER;
// Ensure that mouse input is sent to the button
SetCapture();
Invalidate();
UpdateWindow();
}
void CIndicator::OnMouseLeave(void)
{
// We are not tracking the mouse, this button is OFF.
m_IndicatorState = INDICATOR_OFF;
m_bMouseTracking = FALSE;
// Release mouse capture from the button and restore normal mouse input
Invalidate();
UpdateWindow();
ReleaseCapture();
}
void CIndicator::OnLButtonUp(UINT nFlags, CPoint point)
{
SetIndicatorState(BUTTON_ON); // Highlight button
CBitmapIndicator::OnLButtonUp(nFlags, point);
}*/
// Purpose: Set the new state of the button
// Return: Returns the old state of the button
// Parameters: nState = Either ON or OFF. The default is OFF. This is NOT tri-state button!
INDICATOR_STATE CIndicator::SetIndicatorState(INDICATOR_STATE nState)
{
INDICATOR_STATE nOldState = (INDICATOR_STATE)GetCheck();
m_IndicatorState = nState;
switch(m_IndicatorState)
{
case INDICATOR_ON:
EnableWindow(TRUE);
SetState(INDICATOR_ON);
break;
case INDICATOR_GREYED:
EnableWindow(FALSE);
break;
case INDICATOR_OFF:
EnableWindow(TRUE);
SetState(INDICATOR_OFF);
break;
}
return(nOldState);
}
// Draws the buttons in their relevant state, and text labels
void CIndicator::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC memDC;
CBitmap* pOld=NULL;
CBitmap* pBitmap=NULL;
CDC* pDC;
CRect rc;
int iSaveDC;
pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
memDC.CreateCompatibleDC(pDC);
VERIFY(pDC);
iSaveDC=pDC->SaveDC();
rc.CopyRect(&lpDrawItemStruct->rcItem);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(GetSysColor(COLOR_WINDOWFRAME));// Black text color
switch(m_IndicatorState)
{
case INDICATOR_ON:
pBitmap=&m_bmpIndicatorOn;
break;
case INDICATOR_OFF:
pBitmap=&m_bmpIndicatorOff;
break;
case INDICATOR_GREYED:
pBitmap=&m_bmpIndicatorGrey;
break;
}
CString strTitle;
GetWindowText(strTitle);
if (pBitmap->m_hObject)
{
CRect rcBitmap(rc);
BITMAP bmpInfo;
CSize size;
// Text
size = pDC->GetTextExtent(strTitle);
rcBitmap.OffsetRect(size.cx+5,0);
// Draw bitmap
if(!pBitmap->GetBitmap(&bmpInfo))
return;
if(pDC==NULL)AfxMessageBox("Stop at Indicator.cpp Ln167");
pOld=memDC.SelectObject((CBitmap*) pBitmap);
if (pOld==NULL)
return; //Destructors will clean up
if(!pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, SRCCOPY))
return;
if(pDC==NULL)AfxMessageBox("Stop at Indicator.cpp Ln174");
memDC.SelectObject(pOld);
if(memDC==NULL)
return;
}
CRect rcText(rc);
UINT nFormat = DT_CENTER;
if(m_IndicatorState == INDICATOR_GREYED)
{
rcText.OffsetRect(1,1);
pDC->SetTextColor(GetSysColor(COLOR_BTNHIGHLIGHT));
pDC->DrawText(strTitle,rcText,nFormat);
rcText.OffsetRect(-1,-1);
pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
pDC->DrawText(strTitle,rcText,nFormat);
}
else
pDC->DrawText(strTitle,rcText,nFormat);
pDC->RestoreDC(iSaveDC);
}
BOOL CIndicator::LoadBitmaps(UINT nBitmapOn, UINT nBitmapOff,
UINT nBitmapGrey)
{
return LoadBitmaps(MAKEINTRESOURCE(nBitmapOn),
MAKEINTRESOURCE(nBitmapOff),
MAKEINTRESOURCE(nBitmapGrey));
}
BOOL CIndicator::LoadBitmaps(LPCSTR lpszBitmapOn, LPCSTR lpszBitmapOff,
LPCSTR lpszBitmapGrey)
{
BOOL bAllLoaded=TRUE;
//Delete old ones
m_bmpIndicatorOn.DeleteObject();
m_bmpIndicatorOff.DeleteObject();
m_bmpIndicatorGrey.DeleteObject();
if(lpszBitmapOn!=NULL)
{
if (!m_bmpIndicatorOn.LoadBitmap(lpszBitmapOn))
{
TRACE0("Failed to load up bitmap of bitmap Indicator\n");
return bAllLoaded=FALSE;
}
}
if (lpszBitmapOff!=NULL)
{
if (!m_bmpIndicatorOff.LoadBitmap(lpszBitmapOff))
{
TRACE0("Failed to load down bitmap of bitmap Indicator\n");
return bAllLoaded=FALSE;
}
}
if (lpszBitmapGrey!=NULL)
{
if (!m_bmpIndicatorGrey.LoadBitmap(lpszBitmapGrey))
{
TRACE0("Failed to load focussed bitmap of bitmap Indicator\n");
return bAllLoaded=FALSE;
}
}
return bAllLoaded;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -