当前位置:网站首页>WTL self drawn control library (cqscheckcomboxbox)
WTL self drawn control library (cqscheckcomboxbox)
2022-04-23 05:19:00 【houxian1103】
summary :
CQsCheckComboxBox Inherit and CComboBox, Draw the background by self drawing , And the drawing of various selected states .
The code implementation is as follows :
****/
#pragma once;
#include "QsInclude.h"
#pragma warning(disable: 4312)
#pragma warning(disable: 4311)
extern WNDPROC m_pWndProc;
LRESULT ComboBoxListBoxProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
class CQsCheckComboxBox :
public CWindowImpl< CQsCheckComboxBox, CComboBox >, public COwnerDraw< CQsCheckComboxBox >
{
typedef CWindowImpl< CQsCheckComboxBox, CComboBox > theBaseClass;
public:
BEGIN_MSG_MAP(CQsCheckComboxBox)
MESSAGE_HANDLER( CB_ADDSTRING, OnAddString )
MESSAGE_HANDLER( CB_INSERTSTRING, OnInsertString )
MESSAGE_HANDLER( CB_DELETESTRING, OnDeleteString )
MESSAGE_HANDLER( WM_CTLCOLORLISTBOX, OnCtlColorListBox )
MESSAGE_HANDLER( CBN_DROPDOWN, OnCbnDropdown )
MESSAGE_HANDLER( WM_GETTEXT, OnGetText )
MESSAGE_HANDLER( WM_GETTEXTLENGTH, OnGetTextLength )
CHAIN_MSG_MAP_ALT( COwnerDraw< CQsCheckComboxBox >, 1 )
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
/** *@method CQsCheckComboxBox *@brief Constructors * *@param void *@return */
CQsCheckComboxBox( void )
{
m_hListBox = NULL;
m_bTextUpdated = FALSE;
m_strText = _T("");
m_strbkText = _T("");
}
/** *@method ~CQsCheckComboxBox *@brief Destructor * *@param void *@return */
~CQsCheckComboxBox( void )
{
}
/** *@method Create *@brief adopt Create To create a window . * *@param HWND hWndParent parent window *@param _U_RECT rect = NULL create window rect *@param LPCTSTR szWindowName = NULL the window name *@param DWORD dwStyle = 0 base window style *@param DWORD dwExStyle = 0 extended window style *@param _U_MENUorID MenuOrID = 0U menu or ID *@param LPVOID lpCreateParam = NULL create param *@return HWND Not NULL success return TRUE, failed return NULL */
HWND Create(HWND hWndParent, _U_RECT rect = NULL, LPCTSTR szWindowName = NULL,
DWORD dwStyle = 0, DWORD dwExStyle = 0,
_U_MENUorID MenuOrID = 0U, LPVOID lpCreateParam = NULL)
{
// Remove the CBS_SIMPLE and CBS_DROPDOWN styles and add the one I'm designed for
dwStyle &= ~0xF;
dwStyle |= CBS_DROPDOWNLIST;
// Make sure to use the CBS_OWNERDRAWVARIABLE style
dwStyle |= CBS_OWNERDRAWVARIABLE;
// Use default strings. We need the itemdata to store checkmarks
dwStyle |= CBS_HASSTRINGS;
return theBaseClass::Create(hWndParent, rect, szWindowName, dwStyle, dwExStyle, MenuOrID, lpCreateParam);
}
/** *@method SubclassWindow *@brief Class object association * *@param HWND hWnd Object handle *@return BOOL Successfully returns TRUE, Failure to return FALSE */
BOOL SubclassWindow(HWND hWnd)
{
DWORD dwStyle = CBS_DROPDOWNLIST|CBS_OWNERDRAWVARIABLE|CBS_HASSTRINGS;
BOOL bRet = theBaseClass::SubclassWindow( hWnd );
ModifyStyle(0,dwStyle);
return bRet;
}
/** *@method SetBkText *@brief Set the background Text * *@param CString & strBkText *@return void */
void SetBkText(CString& strBkText)
{
m_strbkText = strBkText;
}
/** *@method DrawItem *@brief Button redraw function * *@param LPDRAWITEMSTRUCT lpDrawItemStruct See MSN *@return void */
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
HDC dc = lpDrawItemStruct->hDC;
CRect rcBitmap = lpDrawItemStruct->rcItem;
CRect rcText = lpDrawItemStruct->rcItem;
CString strText;
// 0 - No check, 1 - Empty check, 2 - Checked
bool bCheck = false;
// Check if we are drawing the static portion of the combobox
if ((LONG)lpDrawItemStruct->itemID < 0)
{
// Make sure the m_strText member is updated
RecalcText();
// Get the text
strText = m_strText;
}
// Otherwise it is one of the items
else
{
int nLen = GetLBTextLen( lpDrawItemStruct->itemID );
TCHAR *str = new TCHAR[nLen+1];
GetLBText(lpDrawItemStruct->itemID, str);
strText = str;
bCheck = m_vtChekcList[lpDrawItemStruct->itemID];
TEXTMETRIC metrics;
GetTextMetrics(dc, &metrics);
rcBitmap.left = 0;
rcBitmap.right = rcBitmap.left + metrics.tmHeight + metrics.tmExternalLeading + 6;
rcBitmap.top += 1;
rcBitmap.bottom -= 1;
rcText.left = rcBitmap.right;
UINT nState = DFCS_BUTTONCHECK;
if (bCheck)
{
nState |= DFCS_CHECKED;
}
// Draw the checkmark using DrawFrameControl
DrawFrameControl(dc, rcBitmap, DFC_BUTTON, nState);
delete [] str;
}
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
}
else
{
SetBkColor(dc, GetSysColor(COLOR_WINDOW));
SetTextColor(dc, GetSysColor(COLOR_WINDOWTEXT));
}
if(strText.GetLength()>0)
{
// Erase and draw
ExtTextOut(dc, 0, 0, ETO_OPAQUE, &rcText, 0, 0, 0);
DrawText(dc, ' ' + strText, strText.GetLength() + 1, &rcText, DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS);
}
else
{
HWND hwnd = GetFocus();
if(hwnd!=m_hWnd)
{
CRect rectBk;
GetClientRect(&rectBk);
rectBk.left = rectBk.left+5;
DrawText(dc, m_strbkText, m_strbkText.GetLength() + 1, &rectBk, DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS);
}
}
if ((lpDrawItemStruct->itemState & (ODS_FOCUS|ODS_SELECTED)) == (ODS_FOCUS|ODS_SELECTED))
{
DrawFocusRect(dc, &rcText);
}
}
/** *@method SetCheck *@brief Used to set item The state of * *@param int nIndex Set the status of ID *@param bool bFlag The status value , Checked or unchecked *@return int */
int SetCheck( int nIndex, bool bFlag )
{
m_vtChekcList[(UINT)nIndex] = bFlag;
m_bTextUpdated = FALSE;
Invalidate(FALSE);
return 0;
}
/** *@method GetCheck *@brief obtain item The state of * *@param int nIndex item Mark (Id) *@return bool */
bool GetCheck( int nIndex )
{
return m_vtChekcList[(UINT)nIndex];
}
/** *@method SelectAll *@brief CQsCheckComboxBox Control Item All check Or all Uncheck * *@param bool bFlag item Of check state ,true For the selection ,false For unselected *@return void */
void SelectAll( bool bFlag )
{
int nCount = GetCount();
for (int i = 0; i < nCount; i++)
{
SetCheck(i, bFlag);
}
}
/** *@method OnAddString *@brief CB_ADDSTRING Message response function , See MSDN * *@param UINT uMsg news ID, by CB_ADDSTRING *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnAddString(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
m_vtChekcList.push_back( false );
bHandled = FALSE;
return 0;
}
/** *@method OnInsertString *@brief CB_INSERTSTRING Message response function , See MSDN * *@param UINT uMsg news ID, by CB_ADDSTRING *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnInsertString(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
int nIndex = (int)wParam;
if ( -1 == nIndex )
{
m_vtChekcList.push_back( false );
}
else
{
std::vector<bool>::iterator it;
for ( int i = 0; i < nIndex; i++, it++ );
m_vtChekcList.insert( it, false );
}
bHandled = FALSE;
return 0;
}
/** *@method OnDeleteString *@brief CB_DELETESTRING Message response function , See MSDN * *@param UINT uMsg news ID, by CB_DELETESTRING *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnDeleteString(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
int nIndex = (int)wParam;
std::vector<bool>::iterator it;
for ( int i = 0; i < nIndex; i++, it++ );
m_vtChekcList.erase( it );
bHandled = FALSE;
return 0;
}
/** *@method OnCtlColorListBox *@brief WM_CTLCOLORLISTBOX Message response function , See MSDN * *@param UINT uMsg news ID, by WM_CTLCOLORLISTBOX *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnCtlColorListBox(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/ )
{
// If the listbox hasn't been subclassed yet, do so...
if (m_hListBox == 0)
{
HWND hWnd = (HWND)lParam;
if (hWnd != 0 && hWnd != m_hWnd)
{
// Save the listbox handle
m_hListBox = hWnd;
// Do the subclassing
m_pWndProc = (WNDPROC)(::GetWindowLong(m_hListBox, GWL_WNDPROC));
::SetWindowLong(m_hListBox, GWL_WNDPROC, (LONG)ComboBoxListBoxProc);
::SetWindowLong(m_hListBox, GWL_USERDATA, (LONG)this);
}
}
return DefWindowProc(WM_CTLCOLORLISTBOX, wParam, lParam);
}
/** *@method RecalcText *@brief CQSCheckComboxBox Control recalculates Text function * *@return void */
void RecalcText()
{
if (!m_bTextUpdated)
{
CString strText;
// Get the list count
UINT nCount = (UINT)GetCount();
// Get the list separator
TCHAR szBuffer[10] = {
0};
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, szBuffer, sizeof(szBuffer));
CString strSeparator = szBuffer;
// If none found, the the ';'
if (strSeparator.GetLength() == 0)
{
strSeparator = ';';
}
// Trim extra spaces
strSeparator.TrimRight();
// And one...
strSeparator += ' ';
for (UINT i = 0; i < nCount; i++)
{
if (m_vtChekcList[i])
{
//CString strItem;
int nLen = GetLBTextLen( i );
TCHAR *str = new TCHAR[nLen + 1];
GetLBText(i, str);
CString strItem( str );
if (!strText.IsEmpty())
{
strText += strSeparator;
}
strText += strItem;
delete [] str;
}
}
// Set the text
m_strText = strText;
m_bTextUpdated = TRUE;
}
}
/** *@method OnCbnDropdown *@brief CBN_DROPDOWN Message response function , See MSDN * *@param UINT uMsg news ID, by WM_CTLCOLORLISTBOX *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnCbnDropdown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
bHandled = FALSE;
return 0;
}
/** *@method OnGetText *@brief WM_GETTEXT Message response function , See MSDN * *@param UINT uMsg news ID, by WM_GETTEXT *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnGetText(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
// TODO: Add control notification handler code here
// Make sure the text is updated
RecalcText();
if (lParam == 0)
return 0;
// Copy the 'fake' window text
lstrcpyn((TCHAR *)lParam, m_strText, (INT)wParam);
return m_strText.GetLength();
}
/** *@method OnGetTextLength *@brief WM_GETTEXTLENGTH Message response function , See MSDN * *@param UINT uMsg news ID, by WM_GETTEXTLENGTH *@param WPARAM wParam Not used *@param LPARAM lParam See MSDN *@param BOOL& bHandled See MSDN *@return LRESULT */
LRESULT OnGetTextLength(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// Make sure the text is updated
RecalcText();
return m_strText.GetLength();
}
CString m_strText;
BOOL m_bTextUpdated;
HWND m_hListBox;
CString m_strbkText;
private:
std::vector<bool> m_vtChekcList; // list Of item Of checkbox Check flag vector
};
版权声明
本文为[houxian1103]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230516142014.html
边栏推荐
- Mariadb的半同步复制
- Collaboration future object and concurrent futures
- Interesting prime number problem hdu5750
- Low code and no code considerations
- 看板快速启动指南
- Live delivery form template - automatically display pictures - automatically associate series products
- Acid of MySQL transaction
- Logrus set log format and output function name
- Barcode generation and decoding, QR code generation and decoding
- Routing parameters
猜你喜欢
我这位老程序员对时代危险和机遇的一点感悟?
PIP free export with path (@ file: / / /) notes
4 most common automated test challenges and Countermeasures
Devops life cycle, all you want to know is here!
源码剖析Redis中如何使用跳表的
To understand Devops, you must read these ten books!
Transaction isolation level of MySQL transactions
Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)
mariadb数据库的主从复制
《2021年IT行业项目管理调查报告》重磅发布!
随机推荐
4 most common automated test challenges and Countermeasures
Qingdao agile tour, coming!
4 个最常见的自动化测试挑战及应对措施
Musk and twitter storm drama
【openh264】cmake: msopenh264-static
On distributed lock
My old programmer's perception of the dangers and opportunities of the times?
A trinomial expression that causes a null pointer
2022/4/22
Acid of MySQL transaction
The source of anxiety of graduating college students looking for technology development jobs
低代码和无代码的注意事项
Some experience in using MySQL / tidb database [slowly updating...]
Logrus set log format and output function name
Kubectl command automatic replenishment
API slow interface analysis
直播带货表格模板-自动显示图片-自动关联系列商品
青岛敏捷之旅,来了!
#define 定义常量和宏,指针和结构体
The 2021 more reading report was released, and the book consumption potential of post-95 and Post-00 rose