MfcToolkit/controls/MyStatic.cpp

171 строка
4.0 KiB
C++

// Copyright (c) Wiesław Šoltés. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "StdAfx.h"
#include "MyStatic.h"
namespace controls
{
CMyStatic::CMyStatic()
{
this->bIsBold = false;
this->szToolTipText = _T("");
this->bHaveToolTipText = false;
}
CMyStatic::~CMyStatic()
{
m_BoldFont.DeleteObject();
m_StdFont.DeleteObject();
}
void CMyStatic::PreSubclassWindow()
{
CFont* pFont = GetFont();
if (!pFont)
{
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == nullptr)
hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
if (hFont)
pFont = CFont::FromHandle(hFont);
}
ASSERT(pFont->GetSafeHandle());
LOGFONT lf;
pFont->GetLogFont(&lf);
m_StdFont.CreateFontIndirect(&lf);
lf.lfWeight = FW_BOLD;
m_BoldFont.CreateFontIndirect(&lf);
SetBold(bIsBold);
CStatic::PreSubclassWindow();
EnableToolTips(TRUE);
}
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
END_MESSAGE_MAP()
void CMyStatic::SetTooltipText(CString szText)
{
this->szToolTipText = szText;
this->bHaveToolTipText = true;
}
CString CMyStatic::GetTooltipText()
{
return this->szToolTipText;
}
void CMyStatic::DelTooltipText()
{
this->bHaveToolTipText = false;
this->szToolTipText = _T("");
}
bool CMyStatic::HaveTooltipText()
{
return this->bHaveToolTipText;
}
void CMyStatic::SetBold(bool bBold)
{
if (::IsWindow(GetSafeHwnd()))
{
if (bBold == true)
{
SetFont(&m_BoldFont);
bIsBold = true;
}
else
{
SetFont(&m_StdFont);
bIsBold = false;
}
Invalidate();
}
}
bool CMyStatic::GetBold()
{
return bIsBold;
}
INT_PTR CMyStatic::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
CRect rcTemp;
GetClientRect(rcTemp);
if (!rcTemp.PtInRect(point))
return -1;
pTI->hwnd = m_hWnd;
pTI->uId = 1;
pTI->lpszText = LPSTR_TEXTCALLBACK;
pTI->rect = rcTemp;
return pTI->uId;
// return CMyStatic::OnToolHitTest(point, pTI);
}
BOOL CMyStatic::OnToolTipText(UINT id, NMHDR *pNMHDR, LRESULT *pResult)
{
TOOLTIPTEXTA *pTTTA = (TOOLTIPTEXTA *)pNMHDR;
TOOLTIPTEXTW *pTTTW = (TOOLTIPTEXTW *)pNMHDR;
static CString szTipText;
UINT_PTR nID = pNMHDR->idFrom;
if (nID == 0)
return FALSE;
if (this->HaveTooltipText())
szTipText = this->GetTooltipText();
else
GetWindowText(szTipText);
// length of tooltips buffer
static const int nMyTooltipsWidth = 4096;
// check the tooltip text length
if (szTipText.GetLength() > nMyTooltipsWidth)
return FALSE;
::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, (LPARAM)nMyTooltipsWidth);
TCHAR szBuff[nMyTooltipsWidth] = _T("");
_stprintf_s(szBuff, _T("%s"), (LPCTSTR)szTipText);
#ifndef _UNICODE
if (pNMHDR->code == TTN_NEEDTEXTA)
{
pTTTA->lpszText = szBuff;
}
else
{
wchar_t szTmpBuff[nMyTooltipsWidth];
_mbstowcsz(szTmpBuff, szBuff, szTipText.GetLength() + 1);
pTTTW->lpszText = szTmpBuff;
}
#else
if (pNMHDR->code == TTN_NEEDTEXTA)
{
char szTmpBuff[nMyTooltipsWidth];
_wcstombsz(szTmpBuff, szBuff, szTipText.GetLength() + 1);
pTTTA->lpszText = szTmpBuff;
}
else
{
pTTTW->lpszText = szBuff;
}
#endif
*pResult = 0;
return TRUE;
}
}