📄 textbar.cpp
字号:
// TextBar.cpp : implementation file
//
#include "stdafx.h"
#include "CustomBars.h"
#include "TextBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTextBar
CTextBar::CTextBar()
{
}
CTextBar::~CTextBar()
{
}
BEGIN_MESSAGE_MAP(CTextBar, CControlBar)
//{{AFX_MSG_MAP(CTextBar)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
IMPLEMENT_DYNAMIC(CTextBar, CControlBar)
/////////////////////////////////////////////////////////////////////////////
// CTextBar message handlers
CSize CTextBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
CSize size = CControlBar::CalcFixedLayout( bStretch, bHorz );
// Compute bar width
CRect rc;
GetParentFrame()->GetWindowRect( &rc );
size.cx = rc.Width(); // should be more than wide enough...
// Compute bar height
CClientDC dc( this );
CString strText = _T( "AaBcCcXxYyZz" ); // dummy string to compute text height
CFont* pOldFont = dc.SelectObject( &m_Font );
size.cy = dc.GetTextExtent( strText ).cy + TOP_MARGIN * 2;
dc.SelectObject( pOldFont );
return size;
}
BOOL CTextBar::Create( CWnd* pParentWnd, DWORD dwStyle, UINT nID )
{
ASSERT_VALID(pParentWnd); // must have a parent
// Style must contain CBRS_TOP or CBRS_BOTTOM
ASSERT( (dwStyle & CBRS_TOP) || (dwStyle & CBRS_BOTTOM) );
// 1 - Save the style
dwStyle |= WS_CHILD | WS_VISIBLE | CBRS_SIZE_FIXED;
m_dwStyle = dwStyle;
dwStyle &= ~CBRS_ALL;
dwStyle |= CCS_NOPARENTALIGN|CCS_NOMOVEY|CCS_NODIVIDER|CCS_NORESIZE;
// 2 - Create the window
CRect rc;
rc.SetRectEmpty();
if( !CWnd::Create( NULL, NULL, dwStyle, rc, pParentWnd, nID ) )
{
return FALSE;
TRACE0( "Unable to create text bar window\n" );
}
return TRUE;
}
int CTextBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CControlBar::OnCreate(lpCreateStruct) == -1)
return -1;
// 1 - Prepare LOGFONT structure
CClientDC dc( this ); // need a DC for GetDeviceCaps() below
LOGFONT lf; // logical font structure
::ZeroMemory( &lf, sizeof( lf ) );
// 24 point Times bold
lf.lfHeight = - MulDiv( 24, dc.GetDeviceCaps( LOGPIXELSX ), 72 );
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = FW_BOLD;
lf.lfItalic = FALSE;
lf.lfUnderline = FALSE;
lf.lfStrikeOut = FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
// 2 - Create font
if( !m_Font.CreateFontIndirect( &lf ) )
{
return FALSE;
}
return 0;
}
void CTextBar::OnPaint()
{
CPaintDC dc(this); // device context for painting
// 1 - Retrieve window text
CString strText;
GetWindowText( strText );
// 2 - Select correct font
CFont* pOldFont = dc.SelectObject( &m_Font );
dc.SetBkMode( TRANSPARENT );
// 3 - Draw text
dc.TextOut( LEFT_MARGIN, TOP_MARGIN, strText );
dc.SelectObject( pOldFont );
}
BOOL CTextBar::OnEraseBkgnd(CDC* pDC)
{
// 1 - Find out area to erase
CRect rc;
pDC->GetClipBox( &rc );
// 2 - Create the correct brush and select into DC
CBrush brush( ::GetSysColor( COLOR_BTNFACE ) );
CBrush* pOldBrush = pDC->SelectObject( &brush );
// 3 - Erase the background
pDC->PatBlt( rc.left, rc.top,
rc.Width(), rc.Height(),
PATCOPY );
pDC->SelectObject( pOldBrush );
return TRUE; // Background was erased
}
// ---------------------------------------------------
// CCmdUI and UPDATE_COMMAND_UI processing
// The CTextBarCmdUI is private to this file: its
// only purpose is to make sure that the SetText()
// function is defined to operate correctly on a
// CTextBar object
class CTextBarCmdUI : public CCmdUI
{
public: // re-implementations only
virtual void SetText(LPCTSTR lpszText);
};
void CTextBarCmdUI::SetText(LPCTSTR lpszText)
{
// 1 - Get a pointer to our CTextBar object
CTextBar* pTextBar = (CTextBar*)m_pOther;
ASSERT(pTextBar != NULL);
ASSERT_KINDOF(CTextBar, pTextBar);
ASSERT(m_nIndex < m_nIndexMax);
// 2 - Change the text bar window's text
CString strCurrentText;
pTextBar->GetWindowText(strCurrentText);
if( strCurrentText != lpszText ) // prevent flicker
{
pTextBar->SetWindowText(lpszText);
pTextBar->Invalidate();
}
}
void CTextBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
// 1 - Create the CCmdUI object, initialize its members
CTextBarCmdUI state;
state.m_pOther = this;
state.m_nIndex = 0;
state.m_nIndexMax = 1; // only one element to update
state.m_nID = GetDlgCtrlID();
// 2 - Send CN_UPDATE_COMMAND_UI message to the text bar itself
if (CWnd::OnCmdMsg(state.m_nID, CN_UPDATE_COMMAND_UI, &state, NULL))
return;
// 3 - Send CN_UPDATE_COMMAND_UI message to the owner frame window
state.DoUpdate(pTarget, FALSE);
// 4 - Update the dialog controls added to the text bar
UpdateDialogControls(pTarget, bDisableIfNoHndler);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -