MfcToolkit/controls/MyComboBoxEdit.cpp

135 строки
3.4 KiB
C++
Исходник Обычный вид История

2017-11-23 18:37:59 +03:00
// 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 "MyComboBoxEdit.h"
2018-01-21 18:08:54 +03:00
namespace controls
2017-11-23 18:37:59 +03:00
{
2018-01-21 18:08:54 +03:00
IMPLEMENT_DYNAMIC(CMyComboBoxEdit, CEdit)
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
CMyComboBoxEdit::CMyComboBoxEdit()
{
this->szToolTipText = _T("");
this->bHaveToolTipText = false;
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
CMyComboBoxEdit::~CMyComboBoxEdit()
{
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
void CMyComboBoxEdit::PreSubclassWindow()
{
CEdit::PreSubclassWindow();
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
EnableToolTips(TRUE);
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
BEGIN_MESSAGE_MAP(CMyComboBoxEdit, CEdit)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
ON_CONTROL_REFLECT(EN_CHANGE, &CMyComboBoxEdit::OnEnChange)
END_MESSAGE_MAP()
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
void CMyComboBoxEdit::SetTooltipText(CString szText)
{
this->szToolTipText = szText;
this->bHaveToolTipText = true;
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
CString CMyComboBoxEdit::GetTooltipText()
{
return this->szToolTipText;
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
void CMyComboBoxEdit::DelTooltipText()
{
this->bHaveToolTipText = false;
this->szToolTipText = _T("");
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
bool CMyComboBoxEdit::HaveTooltipText()
{
return this->bHaveToolTipText;
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
INT_PTR CMyComboBoxEdit::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 CEdit::OnToolHitTest(point, pTI);
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
BOOL CMyComboBoxEdit::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;
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
if (this->HaveTooltipText())
szTipText = this->GetTooltipText();
else
GetWindowText(szTipText);
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
// length of tooltips buffer
static const int nMyTooltipsWidth = 4096;
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
// check the tooltip text length
if (szTipText.GetLength() > nMyTooltipsWidth)
return FALSE;
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, (LPARAM)nMyTooltipsWidth);
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
TCHAR szBuff[nMyTooltipsWidth] = _T("");
_stprintf_s(szBuff, _T("%s"), (LPCTSTR)szTipText);
2017-11-23 18:37:59 +03:00
#ifndef _UNICODE
2018-01-21 18:08:54 +03:00
if (pNMHDR->code == TTN_NEEDTEXTA)
{
pTTTA->lpszText = szBuff;
}
else
{
wchar_t szTmpBuff[nMyTooltipsWidth];
_mbstowcsz(szTmpBuff, szBuff, szTipText.GetLength() + 1);
pTTTW->lpszText = szTmpBuff;
}
2017-11-23 18:37:59 +03:00
#else
2018-01-21 18:08:54 +03:00
if (pNMHDR->code == TTN_NEEDTEXTA)
{
char szTmpBuff[nMyTooltipsWidth];
_wcstombsz(szTmpBuff, szBuff, szTipText.GetLength() + 1);
pTTTA->lpszText = szTmpBuff;
}
else
{
pTTTW->lpszText = szBuff;
}
2017-11-23 18:37:59 +03:00
#endif
2018-01-21 18:08:54 +03:00
*pResult = 0;
return TRUE;
}
2017-11-23 18:37:59 +03:00
2018-01-21 18:08:54 +03:00
void CMyComboBoxEdit::OnEnChange()
{
CString szText;
this->GetWindowText(szText);
this->GetParent()->GetParent()->SendMessage(WM_MY_EN_CHANGE, (WPARAM)&szText, 0);
}
2017-11-23 18:37:59 +03:00
}