duilib Combo 支持 编辑
ComboEdit.h
#ifndef __UICOMBOEDIT_H__ #define __UICOMBOEDIT_H__ #pragma once namespace DuiLib { class CComboEditWnd; class DUILIB_API CComboEditUI:public CComboUI { public: CComboEditUI(); LPCTSTR GetClass() const; LPVOID GetInterface(LPCTSTR pstrName); CDuiString GetText() const; void DoEvent(TEventUI& event); int GetFont(); void SetFont(int iFont); UINT GetTextStyle(); void SetTextStyle(UINT iTextStyle); int GetMaxChar(); void SetMaxChar(int iMaxChar); DWORD GetNativeEditBkColor(); void SetNativeEditBkColor(DWORD dwColor); DWORD GetTextColor(); void SetTextColor(DWORD dwColor); CDuiString m_sText; CComboEditWnd* m_pEditWnd; BOOL SelectItem(int iIndex, BOOL bTakeFocus = FALSE, BOOL bTriggerEvent = TRUE); void PaintText(HDC hDC); private: int GetCurSel() const; int m_iMaxChar; int m_iFont; UINT m_uTextStyle; DWORD m_dwNativeEditBkColor; DWORD m_dwTextColor; DWORD m_dwDisabledTextColor; }; }// end Duilib #endif // __UICOMBOEDIT_H__
ComboEdit.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | #include "stdafx.h" #include "UIComboEdit.h" #include <olectl.h> namespace DuiLib { class DUILIB_API CComboEditWnd : public CWindowWnd { public : CComboEditWnd(); void Init(CComboEditUI* pOwner); RECT CalPos(); LPCTSTR GetWindowClassName() const ; LPCTSTR GetSuperClassName() const ; void OnFinalMessage( HWND hWnd); LRESULT HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT OnKillFocus( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); LRESULT OnEditChanged( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled); protected : enum { DEFAULT_TIMERID = 20, }; CComboEditUI* m_pOwner; HBRUSH m_hBkBrush; BOOL m_bInit; BOOL m_bDrawCaret; }; CComboEditWnd::CComboEditWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(FALSE), m_bDrawCaret(FALSE) { } void CComboEditWnd::Init(CComboEditUI* pOwner) { m_pOwner = pOwner; RECT rcPos = CalPos(); UINT uStyle = WS_CHILD | ES_AUTOHSCROLL ; UINT uTextStyle = m_pOwner->GetTextStyle(); if (uTextStyle & DT_LEFT) uStyle |= ES_LEFT; else if (uTextStyle & DT_CENTER) uStyle |= ES_CENTER; else if (uTextStyle & DT_RIGHT) uStyle |= ES_RIGHT; Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos); HFONT hFont = NULL; int iFontIndex = m_pOwner->GetFont(); if (iFontIndex != -1) hFont = m_pOwner->GetManager()->GetFont(iFontIndex); if (hFont == NULL) hFont = m_pOwner->GetManager()->GetDefaultFontInfo()->hFont; SetWindowFont(m_hWnd, hFont, TRUE); Edit_LimitText(m_hWnd, m_pOwner->GetMaxChar()); Edit_SetText(m_hWnd, m_pOwner->GetText()); Edit_SetModify(m_hWnd, FALSE); SendMessage(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELPARAM(0, 0)); Edit_Enable(m_hWnd, m_pOwner->IsEnabled() == TRUE); //Styls ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE); ::SetFocus(m_hWnd); int nSize = GetWindowTextLength(m_hWnd); Edit_SetSel(m_hWnd, nSize, nSize); m_bInit = TRUE; } RECT CComboEditWnd::CalPos() { CDuiRect rcPos = m_pOwner->GetPos(); RECT rcInset = m_pOwner->GetTextPadding(); rcPos.left += rcInset.left; rcPos.top += rcInset.top; rcPos.right -= rcInset.right; rcPos.bottom -= rcInset.bottom; LONG lEditHeight = m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())-> tm .tmHeight; if (lEditHeight < rcPos.GetHeight()) { rcPos.top += (rcPos.GetHeight() - lEditHeight) / 2; rcPos.bottom = rcPos.top + lEditHeight; } CControlUI* pParent = m_pOwner; RECT rcParent; while (pParent = pParent->GetParent()) { if (!pParent->IsVisible()) { rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0; break ; } rcParent = pParent->GetClientPos(); if (!::IntersectRect(&rcPos, &rcPos, &rcParent)) { rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0; break ; } } return rcPos; } LPCTSTR CComboEditWnd::GetWindowClassName() const { return _T( "ComboEditWnd" ); } LPCTSTR CComboEditWnd::GetSuperClassName() const { return WC_EDIT; } void CComboEditWnd::OnFinalMessage( HWND hWnd) { m_pOwner->Invalidate(); // Clear reference and die if (m_hBkBrush != NULL) ::DeleteObject(m_hBkBrush); m_pOwner->GetManager()->RemoveNativeWindow(hWnd); m_pOwner->m_pEditWnd= NULL; delete this ; } LRESULT CComboEditWnd::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; BOOL bHandled = TRUE; if (uMsg == WM_CREATE) { m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd); if (m_pOwner->GetManager()->IsLayered()) { ::SetTimer(m_hWnd, DEFAULT_TIMERID, ::GetCaretBlinkTime(), NULL); } bHandled = FALSE; } else if (uMsg == WM_KILLFOCUS) lRes = OnKillFocus(uMsg, wParam, lParam, bHandled); else if (uMsg == OCM_COMMAND) { if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE) lRes = OnEditChanged(uMsg, wParam, lParam, bHandled); else if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_UPDATE) { RECT rcClient; ::GetClientRect(m_hWnd, &rcClient); ::InvalidateRect(m_hWnd, &rcClient, FALSE); } } else if (uMsg == WM_KEYDOWN && TCHAR (wParam) == VK_RETURN) { m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_RETURN); } else if (uMsg == OCM__BASE + WM_CTLCOLOREDIT || uMsg == OCM__BASE + WM_CTLCOLORSTATIC) { if (m_pOwner->GetManager()->IsLayered() && !m_pOwner->GetManager()->IsPainting()) { m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd); } DWORD clrColor = m_pOwner->GetNativeEditBkColor(); if (clrColor == 0xFFFFFFFF) return 0; ::SetBkMode(( HDC )wParam, TRANSPARENT); DWORD dwTextColor = m_pOwner->GetTextColor(); ::SetTextColor(( HDC )wParam, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor))); if (clrColor < 0xFF000000) { if (m_hBkBrush != NULL) ::DeleteObject(m_hBkBrush); RECT rcWnd = m_pOwner->GetManager()->GetNativeWindowRect(m_hWnd); HBITMAP hBmpEditBk = CRenderEngine::GenerateBitmap(m_pOwner->GetManager(), rcWnd, m_pOwner, clrColor); m_hBkBrush = ::CreatePatternBrush(hBmpEditBk); ::DeleteObject(hBmpEditBk); } else { if (m_hBkBrush == NULL) { m_hBkBrush = ::CreateSolidBrush(RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor))); } } return ( LRESULT )m_hBkBrush; } else if (uMsg == WM_PAINT) { if (m_pOwner->GetManager()->IsLayered()) { m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd); } bHandled = FALSE; } else if (uMsg == WM_PRINT) { if (m_pOwner->GetManager()->IsLayered()) { lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam); if (m_pOwner->IsEnabled() && m_bDrawCaret) { // todo:判断是否enabled RECT rcClient; ::GetClientRect(m_hWnd, &rcClient); POINT ptCaret; ::GetCaretPos(&ptCaret); RECT rcCaret = { ptCaret.x, ptCaret.y, ptCaret.x, ptCaret.y + rcClient.bottom - rcClient.top }; CRenderEngine::DrawLine(( HDC )wParam, rcCaret, 1, 0xFF000000); } return lRes; } bHandled = FALSE; } else if (uMsg == WM_TIMER) { if (wParam == DEFAULT_TIMERID) { m_bDrawCaret = !m_bDrawCaret; RECT rcClient; ::GetClientRect(m_hWnd, &rcClient); ::InvalidateRect(m_hWnd, &rcClient, FALSE); return 0; } bHandled = FALSE; } else bHandled = FALSE; if (!bHandled) return CWindowWnd::HandleMessage(uMsg, wParam, lParam); return lRes; } LRESULT CComboEditWnd::OnKillFocus( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL & bHandled) { LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam); if (( HWND )wParam != m_pOwner->GetManager()->GetPaintWindow()) { ::SendMessage(m_pOwner->GetManager()->GetPaintWindow(), WM_KILLFOCUS, wParam, lParam); } SendMessage(WM_CLOSE); return lRes; } LRESULT CComboEditWnd::OnEditChanged( UINT /*uMsg*/ , WPARAM /*wParam*/ , LPARAM /*lParam*/ , BOOL & /*bHandled*/ ) { if (!m_bInit) return 0; if (m_pOwner == NULL) return 0; // Copy text back int cchLen = ::GetWindowTextLength(m_hWnd) + 1; LPTSTR pstr = static_cast < LPTSTR >(_alloca(cchLen * sizeof ( TCHAR ))); ASSERT(pstr); if (pstr == NULL) return 0; ::GetWindowText(m_hWnd, pstr, cchLen); m_pOwner->m_sText = pstr; m_pOwner->SetToolTip(pstr); m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_TEXTCHANGED); if (m_pOwner->GetManager()->IsLayered()) m_pOwner->Invalidate(); return 0; } CComboEditUI::CComboEditUI() { m_iMaxChar = -1; m_pEditWnd = NULL; m_iFont = 1; m_uTextStyle = DT_VCENTER | DT_SINGLELINE; m_dwNativeEditBkColor = 0x00000000; m_dwTextColor = 0x00000000; m_dwDisabledTextColor = 0; } LPCTSTR CComboEditUI::GetClass() const { return DUI_CTR_COMBOEDIT; } LPVOID CComboEditUI::GetInterface( LPCTSTR pstrName) { if (_tcscmp(pstrName, DUI_CTR_COMBO) == 0) return static_cast <CComboEditUI*>( this ); return CComboUI::GetInterface(pstrName); } DuiLib::CDuiString CComboEditUI::GetText() const { return m_sText; } void CComboEditUI::DoEvent(TEventUI& event) { if (!IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND) { if (m_pParent != NULL) m_pParent->DoEvent(event); else CComboUI::DoEvent(event); return ; } if (event.Type == UIEVENT_KILLFOCUS) { if (m_pEditWnd) { m_pEditWnd->ShowWindow(FALSE, FALSE); } return ; } if ( event.Type == UIEVENT_DBLCLICK || event.Type== UIEVENT_RBUTTONDOWN) { if (m_pEditWnd) { m_pEditWnd->ShowWindow(TRUE, TRUE); } if (m_pEditWnd) return ; m_pEditWnd = new CComboEditWnd(); ASSERT(m_pEditWnd); m_pEditWnd->Init( this ); } CComboUI::DoEvent(event); } int CComboEditUI::GetFont() { return m_iFont; } void CComboEditUI::SetFont( int iFont) { m_iFont = iFont; } UINT CComboEditUI::GetTextStyle() { return m_uTextStyle; } void CComboEditUI::SetTextStyle( UINT iTextStyle) { m_uTextStyle = iTextStyle; } int CComboEditUI::GetMaxChar() { return m_iMaxChar; } void CComboEditUI::SetMaxChar( int iMaxChar) { m_iMaxChar = iMaxChar; } DWORD CComboEditUI::GetNativeEditBkColor() { return m_dwNativeEditBkColor; } void CComboEditUI::SetNativeEditBkColor( DWORD dwColor) { m_dwNativeEditBkColor = dwColor; } DWORD CComboEditUI::GetTextColor() { return m_dwTextColor; } void CComboEditUI::SetTextColor( DWORD dwColor) { m_dwTextColor = dwColor; } BOOL CComboEditUI::SelectItem( int iIndex, BOOL bTakeFocus /*= FALSE*/ , BOOL bTriggerEvent /*= TRUE*/ ) { if (m_bSelectCloseFlag && m_pWindow != NULL) m_pWindow->Close(); if (iIndex == m_iCurSel) return TRUE; int iOldSel = m_iCurSel; if (m_iCurSel >= 0) { CControlUI* pControl = static_cast <CControlUI*>(m_items[m_iCurSel]); if (!pControl) return FALSE; IListItemUI* pListItem = static_cast <IListItemUI*>(pControl->GetInterface(DUI_CTR_ILISTITEM)); if (pListItem != NULL) pListItem->Select(FALSE, bTriggerEvent); m_iCurSel = -1; } if (iIndex < 0) return FALSE; if (m_items.GetSize() == 0) return FALSE; if (iIndex >= m_items.GetSize()) iIndex = m_items.GetSize() - 1; CControlUI* pControl = static_cast <CControlUI*>(m_items[iIndex]); if (!pControl || !pControl->IsVisible() || !pControl->IsEnabled()) return FALSE; m_sText = pControl->GetText(); SetToolTip(m_sText); IListItemUI* pListItem = static_cast <IListItemUI*>(pControl->GetInterface(DUI_CTR_ILISTITEM)); if (pListItem == NULL) return FALSE; m_iCurSel = iIndex; if (m_pWindow != NULL || bTakeFocus) pControl->SetFocus(); pListItem->Select(TRUE, bTriggerEvent); if (m_pManager != NULL && bTriggerEvent) m_pManager->SendNotify( this , DUI_MSGTYPE_ITEMSELECT, m_iCurSel, iOldSel); Invalidate(); return TRUE; } void CComboEditUI::PaintText( HDC hDC) { if (!m_bShowText) { return ; } if (m_pEditWnd && IsWindowVisible(m_pEditWnd->GetHWND())) { return ; } RECT rcText = m_rcItem; rcText.left += m_rcTextPadding.left; rcText.right -= m_rcTextPadding.right; rcText.top += m_rcTextPadding.top; rcText.bottom -= m_rcTextPadding.bottom; if (m_iCurSel >= 0) { CListLabelElementUI* pControl = static_cast <CListLabelElementUI*>(m_items[m_iCurSel]); if (pControl) { CDuiString strIcon = pControl->GetIcon(); SIZE szIcon = pControl->GetIconSize(); if (strIcon != _T( "" )) { IListOwnerUI* pOwner = pControl->GetOwner(); if (pOwner == NULL) return ; TListInfoUI* pInfo = pOwner->GetListInfo(); if (pInfo == NULL) return ; int left, right; left =0; right = left + szIcon.cx; TDrawInfo diIcon; CDuiString pStrImage; int bottom, top = (rcText.bottom - rcText.top - szIcon.cy) / 2; bottom = top + szIcon.cy; pStrImage.SmallFormat(_T( "file='%s' dest='%d,%d,%d,%d'" ), strIcon.GetData(), left, top, right, bottom); diIcon.Clear(); diIcon.sDrawString = pStrImage; CRenderEngine::DrawImage(hDC, m_pManager, rcText, m_rcPaint, diIcon); m_uTextStyle |= pInfo->uTextStyle; } rcText.left += szIcon.cx; } } if (m_dwTextColor == 0) m_dwTextColor = m_pManager->GetDefaultFontColor(); if (m_dwDisabledTextColor == 0) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor(); if (m_sText.IsEmpty()) return ; if (IsEnabled()) { CRenderEngine::DrawText(hDC, m_pManager, rcText, m_sText, m_dwTextColor, \ m_iFont, DT_SINGLELINE | m_uTextStyle); } else { CRenderEngine::DrawText(hDC, m_pManager, rcText, m_sText, m_dwDisabledTextColor, \ m_iFont, DT_SINGLELINE | m_uTextStyle); } } int CComboEditUI::GetCurSel() const { return -1; } } // end duilib |
效果,右键或者双击启动编辑功能
<Default name="ComboEdit" value="normalimage="file='res\buttons\combo_normal.png' corner='20,0,20,0'" hotimage="file='res\buttons\combo_down.png' corner='20,0,20,0'" pushedimage="file='res\buttons\combo_down.png' corner='20,0,20,0'" vscrollbar="true" textpadding="6,0,20,0" itemtextpadding="6,0,0,0" " />
签名档:
从事网络安全和编程的我,很希望能找到志同道合的朋友交流。
欢迎cn博客的好友拍砖,留言。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】