📄 focustomstatic.cpp
字号:
// Copyright UCanCode Software Technology Inc, All Rights Reserved
// You can contact us.
// Support@UCanCode.net
// http://www.ucancode.net
/********************************************************************/
// FOCustomStatic.cpp : implementation file
//
#include "stdafx.h"
#include "newpicker.h"
#include "FOCustomStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFOCustomStatic
IMPLEMENT_DYNAMIC(CFOCustomStatic, CStatic)
BEGIN_MESSAGE_MAP(CFOCustomStatic, CStatic)
//{{AFX_MSG_MAP(CFOCustomStatic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFOCustomStatic construction
CFOCustomStatic::CFOCustomStatic()
{
m_rcFace.SetRectEmpty();
m_pOldFont = NULL;
m_nBorder = 4;
}
CFOCustomStatic::~CFOCustomStatic()
{
}
CSize CFOCustomStatic::GetTextExtent(CDC& dc, CString strCaption)
{
CSize textExtent(0,0);
if (!strCaption.IsEmpty())
{
CRect rcCaption(0,0,0,0);
dc.DrawText(strCaption, strCaption.GetLength(),
rcCaption, DT_SINGLELINE|DT_CALCRECT);
textExtent = rcCaption.Size();
}
return textExtent;
}
void CFOCustomStatic::DrawCaption(CDC& dc, CPoint pt, CString strCaption,UINT uState)
{
uState;
if (!strCaption.IsEmpty())
{
int mode = dc.SetBkMode(TRANSPARENT);
dc.TextOut(pt.x,pt.y,strCaption,strCaption.GetLength());
dc.SetBkMode(mode);
}
}
void CFOCustomStatic::RecalcLayout()
{
if (m_hWnd)
{
Invalidate(FALSE);
}
}
void CFOCustomStatic::SizeToContent()
{
CSize size;
GetSizeToContent(size);
VERIFY(SetWindowPos(NULL, -1, -1, size.cx, size.cy,
SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE));
RecalcLayout();
}
void CFOCustomStatic::GetSizeToContent(CSize& size)
{
CClientDC dc(this);
OnPrepareDC(dc);
CRect rcFace;
CRect rcFocus;
CRect rcBorder;
CRect rcButton(0,0,100,100);
GetFocusRect(dc, rcButton, rcFocus);
GetFaceRect(dc, rcButton, rcFace, TRUE);
rcFocus.NormalizeRect();
rcButton.NormalizeRect();
rcBorder.SetRect(
rcFocus.left - rcButton.left,
rcFocus.top - rcButton.top,
rcButton.right - rcFocus.right,
rcButton.bottom - rcFocus.bottom);
// See if the inside rectangle is completely
// contained in the outside rectangle.
CRect rcIntersect;
rcIntersect.IntersectRect(rcFocus, rcButton);
// When the button is depressed the face is offset.
rcFace.right += 1;
rcFace.bottom += 1;
size.cx = rcFace.Width() + rcBorder.left + rcBorder.right;
size.cy = rcFace.Height() + rcBorder.top + rcBorder.bottom;
OnClearDC(dc);
}
int CFOCustomStatic::SetFocusBorder(int cxFocusBorder)
{
int cxOldFocusBorder = m_nBorder;
m_nBorder = cxFocusBorder;
RecalcLayout();
return cxOldFocusBorder;
}
void CFOCustomStatic::GetFocusRect(CDC& dc, CRect rcButton, CRect& rc)
{
UNUSED_ALWAYS(dc);
rc = rcButton;
rc.DeflateRect(m_nBorder, m_nBorder);
}
void CFOCustomStatic::GetFaceRect(CDC& dc, CRect rcButton, CRect& rcFace,
BOOL bSizeToContent)
{
if (bSizeToContent)
{
CString strCaption;
GetWindowText(strCaption);
CSize textExtent = GetTextExtent(dc, strCaption);
rcFace.SetRect(0, 0, textExtent.cx, textExtent.cy);
// Leave a slight border between the face and the focus.
if (!rcFace.IsRectEmpty())
rcFace.InflateRect(1, 1);
}
else
{
GetFocusRect(dc, rcButton, rcFace);
rcFace.DeflateRect(1,1);
}
}
void CFOCustomStatic::OnDrawFrame(CDC& dc, CRect& rc, UINT uState)
{
uState |= DFCS_BUTTONPUSH|DFCS_ADJUSTRECT;
VERIFY(dc.DrawFrameControl(&rc, DFC_BUTTON, uState));
}
void CFOCustomStatic::OnDrawFocus(CDC& dc, const CRect& rc)
{
dc.DrawFocusRect(rc);
}
void CFOCustomStatic::OnDrawFace(CDC& dc, const CRect& rc, UINT uState)
{
CString strCaption;
GetWindowText(strCaption);
CSize textExtent = GetTextExtent(dc, strCaption);
if (textExtent.cx)
{
CPoint pt;
pt.x = rc.left + (rc.Width() - textExtent.cx) / 2;
pt.y = rc.top + (rc.Height() - textExtent.cy) / 2 - 1;
DrawCaption(dc, pt, strCaption, uState);
}
}
void CFOCustomStatic::OnPrepareDC(CDC& dc)
{
m_pOldFont = dc.SelectObject(GetFont());
}
void CFOCustomStatic::OnClearDC(CDC& dc)
{
dc.SelectObject(m_pOldFont);
}
/////////////////////////////////////////////////////////////////////////////
// CFOCustomStatic overrides
void CFOCustomStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC* pButtonDC = CDC::FromHandle(dc.m_hDC);
OnPrepareDC(*pButtonDC);
CRect rcFace;
CRect rcFocus;
CRect rcButton;
GetClientRect(&rcButton);
GetFaceRect(*pButtonDC, rcButton, rcFace);
GetFocusRect(*pButtonDC, rcButton, rcFocus);
// Determine which device context we are using.
CDC* pDC = pButtonDC;
// Draw the button itself.
OnDrawFrame(*pDC, rcButton, DFCS_BUTTONPUSH|DFCS_ADJUSTRECT);
// Don't draw over the frame.
m_rcFace = rcButton;
pDC->IntersectClipRect(m_rcFace);
// Draw the focus if we have it.
pDC->IntersectClipRect(rcFace);
OnDrawFace(*pDC, rcFace, 0);
OnClearDC(*pButtonDC);
// Do not call CStatic::OnPaint() for painting messages
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -