UIControl.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. #include "StdAfx.h"
  2. namespace DuiLib
  3. {
  4. IMPLEMENT_DUICONTROL(CControlUI)
  5. CControlUI::CControlUI()
  6. :m_pManager(NULL),
  7. m_pParent(NULL),
  8. m_bUpdateNeeded(true),
  9. m_bMenuUsed(false),
  10. m_bVisible(true),
  11. m_bInternVisible(true),
  12. m_bFocused(false),
  13. m_bEnabled(true),
  14. m_bMouseEnabled(true),
  15. m_bKeyboardEnabled(true),
  16. m_bFloat(false),
  17. m_uFloatAlign(0),
  18. m_bSetPos(false),
  19. m_bRichEvent(false),
  20. m_bDragEnabled(false),
  21. m_bDropEnabled(false),
  22. m_bResourceText(false),
  23. m_chShortcut('\0'),
  24. m_pTag(NULL),
  25. m_dwBackColor(0),
  26. m_dwBackColor2(0),
  27. m_dwBackColor3(0),
  28. m_dwForeColor(0),
  29. m_dwBorderColor(0),
  30. m_dwFocusBorderColor(0),
  31. m_bColorHSL(false),
  32. m_nBorderSize(0),
  33. m_nBorderStyle(PS_SOLID),
  34. m_nTooltipWidth(300),
  35. m_wCursor(0),
  36. m_instance(NULL)
  37. {
  38. m_cXY.cx = m_cXY.cy = 0;
  39. m_cxyFixed.cx = m_cxyFixed.cy = 0;
  40. m_cxyMin.cx = m_cxyMin.cy = 0;
  41. m_cxyMax.cx = m_cxyMax.cy = 9999;
  42. m_cxyBorderRound.cx = m_cxyBorderRound.cy = 0;
  43. ::ZeroMemory(&m_rcPadding, sizeof(RECT));
  44. ::ZeroMemory(&m_rcItem, sizeof(RECT));
  45. ::ZeroMemory(&m_rcPaint, sizeof(RECT));
  46. ::ZeroMemory(&m_rcBorderSize, sizeof(RECT));
  47. m_piFloatPercent.left = m_piFloatPercent.top = m_piFloatPercent.right = m_piFloatPercent.bottom = 0.0f;
  48. }
  49. CControlUI::~CControlUI()
  50. {
  51. if (OnDestroy) OnDestroy(this);
  52. RemoveAllCustomAttribute();
  53. if (m_pManager != NULL) m_pManager->ReapObjects(this);
  54. }
  55. CDuiString CControlUI::GetName() const
  56. {
  57. return m_sName;
  58. }
  59. void CControlUI::SetName(LPCTSTR pstrName)
  60. {
  61. m_sName = pstrName;
  62. }
  63. LPVOID CControlUI::GetInterface(LPCTSTR pstrName)
  64. {
  65. if (_tcsicmp(pstrName, DUI_CTR_CONTROL) == 0) return this;
  66. return NULL;
  67. }
  68. LPCTSTR CControlUI::GetClass() const
  69. {
  70. return _T("ControlUI");
  71. }
  72. UINT CControlUI::GetControlFlags() const
  73. {
  74. return 0;
  75. }
  76. bool CControlUI::Activate()
  77. {
  78. if (!IsVisible()) return false;
  79. if (!IsEnabled()) return false;
  80. return true;
  81. }
  82. CPaintManagerUI* CControlUI::GetManager() const
  83. {
  84. return m_pManager;
  85. }
  86. void CControlUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent, bool bInit)
  87. {
  88. m_pManager = pManager;
  89. m_pParent = pParent;
  90. if (bInit && m_pParent) Init();
  91. }
  92. CControlUI* CControlUI::GetParent() const
  93. {
  94. return m_pParent;
  95. }
  96. bool CControlUI::SetTimer(UINT nTimerID, UINT nElapse)
  97. {
  98. if (m_pManager == NULL) return false;
  99. return m_pManager->SetTimer(this, nTimerID, nElapse);
  100. }
  101. void CControlUI::KillTimer(UINT nTimerID)
  102. {
  103. if (m_pManager == NULL) return;
  104. m_pManager->KillTimer(this, nTimerID);
  105. }
  106. CDuiString CControlUI::GetText() const
  107. {
  108. if (!IsResourceText()) return m_sText;
  109. return CResourceManager::GetInstance()->GetText(m_sText);
  110. }
  111. void CControlUI::SetText(LPCTSTR pstrText)
  112. {
  113. if (m_sText == pstrText) return;
  114. m_sText = pstrText;
  115. // 解析xml换行符
  116. m_sText.Replace(_T("{\\n}"), _T("\n"));
  117. Invalidate();
  118. }
  119. bool CControlUI::IsResourceText() const
  120. {
  121. return m_bResourceText;
  122. }
  123. void CControlUI::SetResourceText(bool bResource)
  124. {
  125. if (m_bResourceText == bResource) return;
  126. m_bResourceText = bResource;
  127. Invalidate();
  128. }
  129. bool CControlUI::IsDragEnabled() const
  130. {
  131. return m_bDragEnabled;
  132. }
  133. void CControlUI::SetDragEnable(bool bDrag)
  134. {
  135. m_bDragEnabled = bDrag;
  136. }
  137. bool CControlUI::IsDropEnabled() const
  138. {
  139. return m_bDropEnabled;
  140. }
  141. void CControlUI::SetDropEnable(bool bDrop)
  142. {
  143. m_bDropEnabled = bDrop;
  144. }
  145. bool CControlUI::IsRichEvent() const
  146. {
  147. return m_bRichEvent;
  148. }
  149. void CControlUI::SetRichEvent(bool bEnable)
  150. {
  151. m_bRichEvent = bEnable;
  152. }
  153. LPCTSTR CControlUI::GetGradient()
  154. {
  155. return m_sGradient;
  156. }
  157. void CControlUI::SetGradient(LPCTSTR pStrImage)
  158. {
  159. if (m_sGradient == pStrImage) return;
  160. m_sGradient = pStrImage;
  161. Invalidate();
  162. }
  163. DWORD CControlUI::GetBkColor() const
  164. {
  165. return m_dwBackColor;
  166. }
  167. void CControlUI::SetBkColor(DWORD dwBackColor)
  168. {
  169. if (m_dwBackColor == dwBackColor) return;
  170. m_dwBackColor = dwBackColor;
  171. Invalidate();
  172. }
  173. DWORD CControlUI::GetBkColor2() const
  174. {
  175. return m_dwBackColor2;
  176. }
  177. void CControlUI::SetBkColor2(DWORD dwBackColor)
  178. {
  179. if (m_dwBackColor2 == dwBackColor) return;
  180. m_dwBackColor2 = dwBackColor;
  181. Invalidate();
  182. }
  183. DWORD CControlUI::GetBkColor3() const
  184. {
  185. return m_dwBackColor3;
  186. }
  187. void CControlUI::SetBkColor3(DWORD dwBackColor)
  188. {
  189. if (m_dwBackColor3 == dwBackColor) return;
  190. m_dwBackColor3 = dwBackColor;
  191. Invalidate();
  192. }
  193. DWORD CControlUI::GetForeColor() const
  194. {
  195. return m_dwForeColor;
  196. }
  197. void CControlUI::SetForeColor(DWORD dwForeColor)
  198. {
  199. if (m_dwForeColor == dwForeColor) return;
  200. m_dwForeColor = dwForeColor;
  201. Invalidate();
  202. }
  203. LPCTSTR CControlUI::GetBkImage()
  204. {
  205. return m_sBkImage;
  206. }
  207. void CControlUI::SetBkImage(LPCTSTR pStrImage)
  208. {
  209. if (m_sBkImage == pStrImage) return;
  210. m_sBkImage = pStrImage;
  211. Invalidate();
  212. }
  213. LPCTSTR CControlUI::GetForeImage() const
  214. {
  215. return m_sForeImage;
  216. }
  217. void CControlUI::SetForeImage(LPCTSTR pStrImage)
  218. {
  219. if (m_sForeImage == pStrImage) return;
  220. m_sForeImage = pStrImage;
  221. Invalidate();
  222. }
  223. DWORD CControlUI::GetBorderColor() const
  224. {
  225. return m_dwBorderColor;
  226. }
  227. void CControlUI::SetBorderColor(DWORD dwBorderColor)
  228. {
  229. if (m_dwBorderColor == dwBorderColor) return;
  230. m_dwBorderColor = dwBorderColor;
  231. Invalidate();
  232. }
  233. DWORD CControlUI::GetFocusBorderColor() const
  234. {
  235. return m_dwFocusBorderColor;
  236. }
  237. void CControlUI::SetFocusBorderColor(DWORD dwBorderColor)
  238. {
  239. if (m_dwFocusBorderColor == dwBorderColor) return;
  240. m_dwFocusBorderColor = dwBorderColor;
  241. Invalidate();
  242. }
  243. bool CControlUI::IsColorHSL() const
  244. {
  245. return m_bColorHSL;
  246. }
  247. void CControlUI::SetColorHSL(bool bColorHSL)
  248. {
  249. if (m_bColorHSL == bColorHSL) return;
  250. m_bColorHSL = bColorHSL;
  251. Invalidate();
  252. }
  253. int CControlUI::GetBorderSize() const
  254. {
  255. if (m_pManager != NULL) return m_pManager->GetDPIObj()->Scale(m_nBorderSize);
  256. return m_nBorderSize;
  257. }
  258. void CControlUI::SetBorderSize(int nSize)
  259. {
  260. if (m_nBorderSize == nSize) return;
  261. m_nBorderSize = nSize;
  262. Invalidate();
  263. }
  264. RECT CControlUI::GetBorderRectSize() const
  265. {
  266. RECT rcBorderSize = m_rcBorderSize;
  267. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  268. return rcBorderSize;
  269. }
  270. void CControlUI::SetBorderSize(RECT rc)
  271. {
  272. m_rcBorderSize = rc;
  273. Invalidate();
  274. }
  275. SIZE CControlUI::GetBorderRound() const
  276. {
  277. SIZE cxyBorderRound = m_cxyBorderRound;
  278. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyBorderRound);
  279. return cxyBorderRound;
  280. }
  281. void CControlUI::SetBorderRound(SIZE cxyRound)
  282. {
  283. m_cxyBorderRound = cxyRound;
  284. Invalidate();
  285. }
  286. bool CControlUI::DrawImage(HDC hDC, LPCTSTR pStrImage, LPCTSTR pStrModify)
  287. {
  288. return CRenderEngine::DrawImageString(hDC, m_pManager, m_rcItem, m_rcPaint, pStrImage, pStrModify, m_instance);
  289. }
  290. const RECT& CControlUI::GetPos() const
  291. {
  292. return m_rcItem;
  293. }
  294. RECT CControlUI::GetRelativePos() const
  295. {
  296. CControlUI* pParent = GetParent();
  297. if (pParent != NULL)
  298. {
  299. RECT rcParentPos = pParent->GetPos();
  300. CDuiRect rcRelativePos(m_rcItem);
  301. rcRelativePos.Offset(-rcParentPos.left, -rcParentPos.top);
  302. return rcRelativePos;
  303. }
  304. else
  305. {
  306. return CDuiRect(0, 0, 0, 0);
  307. }
  308. }
  309. RECT CControlUI::GetClientPos() const
  310. {
  311. return m_rcItem;
  312. }
  313. void CControlUI::SetPos(RECT rc, bool bNeedInvalidate)
  314. {
  315. if (rc.right < rc.left) rc.right = rc.left;
  316. if (rc.bottom < rc.top) rc.bottom = rc.top;
  317. CDuiRect invalidateRc = m_rcItem;
  318. if (::IsRectEmpty(&invalidateRc)) invalidateRc = rc;
  319. m_rcItem = rc;
  320. if (m_pManager == NULL) return;
  321. if (!m_bSetPos)
  322. {
  323. m_bSetPos = true;
  324. if (OnSize) OnSize(this);
  325. m_bSetPos = false;
  326. }
  327. m_bUpdateNeeded = false;
  328. if (bNeedInvalidate && IsVisible())
  329. {
  330. invalidateRc.Join(m_rcItem);
  331. CControlUI* pParent = this;
  332. RECT rcTemp;
  333. RECT rcParent;
  334. while (pParent = pParent->GetParent())
  335. {
  336. if (!pParent->IsVisible()) return;
  337. rcTemp = invalidateRc;
  338. rcParent = pParent->GetPos();
  339. if (!::IntersectRect(&invalidateRc, &rcTemp, &rcParent)) return;
  340. }
  341. m_pManager->Invalidate(invalidateRc);
  342. }
  343. }
  344. void CControlUI::Move(SIZE szOffset, bool bNeedInvalidate)
  345. {
  346. m_cXY.cx += szOffset.cx;
  347. m_cXY.cy += szOffset.cy;
  348. NeedParentUpdate();
  349. }
  350. int CControlUI::GetWidth() const
  351. {
  352. return m_rcItem.right - m_rcItem.left;
  353. }
  354. int CControlUI::GetHeight() const
  355. {
  356. return m_rcItem.bottom - m_rcItem.top;
  357. }
  358. int CControlUI::GetX() const
  359. {
  360. return m_rcItem.left;
  361. }
  362. int CControlUI::GetY() const
  363. {
  364. return m_rcItem.top;
  365. }
  366. RECT CControlUI::GetPadding() const
  367. {
  368. RECT rcPadding = m_rcPadding;
  369. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcPadding);
  370. return rcPadding;
  371. }
  372. void CControlUI::SetPadding(RECT rcPadding)
  373. {
  374. m_rcPadding = rcPadding;
  375. NeedParentUpdate();
  376. }
  377. SIZE CControlUI::GetFixedXY() const
  378. {
  379. SIZE cXY = m_cXY;
  380. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cXY);
  381. return cXY;
  382. }
  383. void CControlUI::SetFixedXY(SIZE szXY)
  384. {
  385. m_cXY.cx = szXY.cx;
  386. m_cXY.cy = szXY.cy;
  387. NeedParentUpdate();
  388. }
  389. SIZE CControlUI::GetFixedSize() const
  390. {
  391. SIZE cxyFixed = m_cxyFixed;
  392. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyFixed);
  393. return cxyFixed;
  394. }
  395. int CControlUI::GetFixedWidth() const
  396. {
  397. SIZE cxyFixed = m_cxyFixed;
  398. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyFixed);
  399. return cxyFixed.cx;
  400. }
  401. void CControlUI::SetFixedWidth(int cx)
  402. {
  403. if (cx < 0) return;
  404. m_cxyFixed.cx = cx;
  405. NeedParentUpdate();
  406. }
  407. int CControlUI::GetFixedHeight() const
  408. {
  409. SIZE cxyFixed = m_cxyFixed;
  410. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyFixed);
  411. return cxyFixed.cy;
  412. }
  413. void CControlUI::SetFixedHeight(int cy)
  414. {
  415. if (cy < 0) return;
  416. m_cxyFixed.cy = cy;
  417. NeedParentUpdate();
  418. }
  419. int CControlUI::GetMinWidth() const
  420. {
  421. SIZE cxyMin = m_cxyMin;
  422. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMin);
  423. return cxyMin.cx;
  424. }
  425. void CControlUI::SetMinWidth(int cx)
  426. {
  427. if (m_cxyMin.cx == cx) return;
  428. if (cx < 0) return;
  429. m_cxyMin.cx = cx;
  430. NeedParentUpdate();
  431. }
  432. int CControlUI::GetMaxWidth() const
  433. {
  434. SIZE cxyMax = m_cxyMax;
  435. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMax);
  436. return cxyMax.cx;
  437. }
  438. void CControlUI::SetMaxWidth(int cx)
  439. {
  440. if (m_cxyMax.cx == cx) return;
  441. if (cx < 0) return;
  442. m_cxyMax.cx = cx;
  443. NeedParentUpdate();
  444. }
  445. int CControlUI::GetMinHeight() const
  446. {
  447. SIZE cxyMin = m_cxyMin;
  448. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMin);
  449. return cxyMin.cy;
  450. }
  451. void CControlUI::SetMinHeight(int cy)
  452. {
  453. if (m_cxyMin.cy == cy) return;
  454. if (cy < 0) return;
  455. m_cxyMin.cy = cy;
  456. NeedParentUpdate();
  457. }
  458. int CControlUI::GetMaxHeight() const
  459. {
  460. SIZE cxyMax = m_cxyMax;
  461. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMax);
  462. return cxyMax.cy;
  463. }
  464. void CControlUI::SetMaxHeight(int cy)
  465. {
  466. if (m_cxyMax.cy == cy) return;
  467. if (cy < 0) return;
  468. m_cxyMax.cy = cy;
  469. NeedParentUpdate();
  470. }
  471. TPercentInfo CControlUI::GetFloatPercent() const
  472. {
  473. return m_piFloatPercent;
  474. }
  475. void CControlUI::SetFloatPercent(TPercentInfo piFloatPercent)
  476. {
  477. m_piFloatPercent = piFloatPercent;
  478. NeedParentUpdate();
  479. }
  480. void CControlUI::SetFloatAlign(UINT uAlign)
  481. {
  482. m_uFloatAlign = uAlign;
  483. NeedParentUpdate();
  484. }
  485. UINT CControlUI::GetFloatAlign() const
  486. {
  487. return m_uFloatAlign;
  488. }
  489. CDuiString CControlUI::GetToolTip() const
  490. {
  491. if (!IsResourceText()) return m_sToolTip;
  492. return CResourceManager::GetInstance()->GetText(m_sToolTip);
  493. }
  494. void CControlUI::SetToolTip(LPCTSTR pstrText)
  495. {
  496. CDuiString strTemp(pstrText);
  497. strTemp.Replace(_T("<n>"), _T("\r\n"));
  498. m_sToolTip = strTemp;
  499. }
  500. void CControlUI::SetToolTipWidth(int nWidth)
  501. {
  502. m_nTooltipWidth = nWidth;
  503. }
  504. int CControlUI::GetToolTipWidth(void)
  505. {
  506. if (m_pManager != NULL) return m_pManager->GetDPIObj()->Scale(m_nTooltipWidth);
  507. return m_nTooltipWidth;
  508. }
  509. WORD CControlUI::GetCursor()
  510. {
  511. return m_wCursor;
  512. }
  513. void CControlUI::SetCursor(WORD wCursor)
  514. {
  515. m_wCursor = wCursor;
  516. Invalidate();
  517. }
  518. TCHAR CControlUI::GetShortcut() const
  519. {
  520. return m_chShortcut;
  521. }
  522. void CControlUI::SetShortcut(TCHAR ch)
  523. {
  524. m_chShortcut = ch;
  525. }
  526. bool CControlUI::IsContextMenuUsed() const
  527. {
  528. return m_bMenuUsed;
  529. }
  530. void CControlUI::SetContextMenuUsed(bool bMenuUsed)
  531. {
  532. m_bMenuUsed = bMenuUsed;
  533. }
  534. const CDuiString& CControlUI::GetUserData()
  535. {
  536. return m_sUserData;
  537. }
  538. void CControlUI::SetUserData(LPCTSTR pstrText)
  539. {
  540. m_sUserData = pstrText;
  541. }
  542. UINT_PTR CControlUI::GetTag() const
  543. {
  544. return m_pTag;
  545. }
  546. void CControlUI::SetTag(UINT_PTR pTag)
  547. {
  548. m_pTag = pTag;
  549. }
  550. bool CControlUI::IsVisible() const
  551. {
  552. return m_bVisible && m_bInternVisible;
  553. }
  554. void CControlUI::SetVisible(bool bVisible)
  555. {
  556. if (m_bVisible == bVisible) return;
  557. bool v = IsVisible();
  558. m_bVisible = bVisible;
  559. if (m_bFocused) m_bFocused = false;
  560. if (!bVisible && m_pManager && m_pManager->GetFocus() == this)
  561. {
  562. m_pManager->SetFocus(NULL);
  563. }
  564. if (IsVisible() != v)
  565. {
  566. NeedParentUpdate();
  567. }
  568. }
  569. void CControlUI::SetInternVisible(bool bVisible)
  570. {
  571. m_bInternVisible = bVisible;
  572. if (!bVisible && m_pManager && m_pManager->GetFocus() == this)
  573. {
  574. m_pManager->SetFocus(NULL);
  575. }
  576. }
  577. bool CControlUI::IsEnabled() const
  578. {
  579. return m_bEnabled;
  580. }
  581. void CControlUI::SetEnabled(bool bEnabled)
  582. {
  583. if (m_bEnabled == bEnabled) return;
  584. m_bEnabled = bEnabled;
  585. Invalidate();
  586. }
  587. bool CControlUI::IsMouseEnabled() const
  588. {
  589. return m_bMouseEnabled;
  590. }
  591. void CControlUI::SetMouseEnabled(bool bEnabled)
  592. {
  593. m_bMouseEnabled = bEnabled;
  594. }
  595. bool CControlUI::IsKeyboardEnabled() const
  596. {
  597. return m_bKeyboardEnabled;
  598. }
  599. void CControlUI::SetKeyboardEnabled(bool bEnabled)
  600. {
  601. m_bKeyboardEnabled = bEnabled;
  602. }
  603. bool CControlUI::IsFocused() const
  604. {
  605. return m_bFocused;
  606. }
  607. void CControlUI::SetFocus()
  608. {
  609. if (m_pManager != NULL) m_pManager->SetFocus(this);
  610. }
  611. bool CControlUI::IsFloat() const
  612. {
  613. return m_bFloat;
  614. }
  615. void CControlUI::SetFloat(bool bFloat)
  616. {
  617. if (m_bFloat == bFloat) return;
  618. m_bFloat = bFloat;
  619. NeedParentUpdate();
  620. }
  621. CControlUI* CControlUI::FindControl(FINDCONTROLPROC Proc, LPVOID pData, UINT uFlags)
  622. {
  623. if ((uFlags & UIFIND_VISIBLE) != 0 && !IsVisible()) return NULL;
  624. if ((uFlags & UIFIND_ENABLED) != 0 && !IsEnabled()) return NULL;
  625. if ((uFlags & UIFIND_HITTEST) != 0 && (!m_bMouseEnabled || !::PtInRect(&m_rcItem, *static_cast<LPPOINT>(pData)))) return NULL;
  626. return Proc(this, pData);
  627. }
  628. void CControlUI::Invalidate()
  629. {
  630. if (!IsVisible()) return;
  631. RECT invalidateRc = m_rcItem;
  632. CControlUI* pParent = this;
  633. RECT rcTemp;
  634. RECT rcParent;
  635. while (pParent = pParent->GetParent())
  636. {
  637. rcTemp = invalidateRc;
  638. rcParent = pParent->GetPos();
  639. if (!::IntersectRect(&invalidateRc, &rcTemp, &rcParent))
  640. {
  641. return;
  642. }
  643. }
  644. if (m_pManager != NULL) m_pManager->Invalidate(invalidateRc);
  645. }
  646. bool CControlUI::IsUpdateNeeded() const
  647. {
  648. return m_bUpdateNeeded;
  649. }
  650. void CControlUI::NeedUpdate()
  651. {
  652. if (!IsVisible()) return;
  653. m_bUpdateNeeded = true;
  654. Invalidate();
  655. if (m_pManager != NULL) m_pManager->NeedUpdate();
  656. }
  657. void CControlUI::NeedParentUpdate()
  658. {
  659. if (GetParent())
  660. {
  661. GetParent()->NeedUpdate();
  662. GetParent()->Invalidate();
  663. }
  664. else
  665. {
  666. NeedUpdate();
  667. }
  668. if (m_pManager != NULL) m_pManager->NeedUpdate();
  669. }
  670. DWORD CControlUI::GetAdjustColor(DWORD dwColor)
  671. {
  672. if (!m_bColorHSL) return dwColor;
  673. short H, S, L;
  674. CPaintManagerUI::GetHSL(&H, &S, &L);
  675. return CRenderEngine::AdjustColor(dwColor, H, S, L);
  676. }
  677. void CControlUI::Init()
  678. {
  679. DoInit();
  680. if (OnInit) OnInit(this);
  681. }
  682. void CControlUI::DoInit()
  683. {
  684. }
  685. void CControlUI::Event(TEventUI& event)
  686. {
  687. if (OnEvent(&event)) DoEvent(event);
  688. }
  689. void CControlUI::ClearPushedState()
  690. {
  691. // ◆【滑动起手误按压 · 基类默认实现】
  692. //
  693. // 语义:把"因手指/鼠标按下而进入的按压视觉"复位为普通态,
  694. // **只改视觉,不发任何通知、不 Activate、不碰业务数据**。
  695. //
  696. // 这里刻意留空:绝大多数控件的"按下视觉"是 CControlUI 自己的
  697. // 状态机(m_uButtonState 那套,属于 CButtonUI)或根本不存在按压视觉,
  698. // 对它们调用本函数必须是**安全 no-op**。
  699. //
  700. // 真正需要覆盖的是那些**自绘、且继承自布局类**的业务控件 ——
  701. // 它们的按下态靠"换背景图/切样式"实现,与 m_uButtonState 无关。
  702. // 典型:本项目的 CDiandanFoodItemUI : CVerticalLayoutUI,
  703. // 它的 TOUCHDOWN 会 SetCardState("pressed"),但没有状态位可查。
  704. // 这类控件在自己的头文件里 override 本函数即可被库侧按需清扫,
  705. // 而库不需要认识业务的任何一个具体类。
  706. //
  707. // 调用方:CPaintManagerUI::ClearPushedStateAt(触摸链判定为滑动时,
  708. // 对起手命中的控件补一次清扫)。
  709. }
  710. void CControlUI::DoEvent(TEventUI& event)
  711. {
  712. // [触屏模型] 触摸事件不做“归一化”:CPaintManagerUI::OnTouchInput 只派发原生
  713. // UIEVENT_TOUCHDOWN / UIEVENT_TOUCHMOVE / UIEVENT_TOUCHUP,且只发给本次触摸命中的
  714. // 那一个控件(m_pEventTouch),由该控件自决:
  715. // · CContainerUI 认 TOUCHMOVE → 内容 1:1 跟手平移;
  716. // · CScrollBarUI 认 TOUCHDOWN/MOVE/UP → 拖滑块 / 点轨道翻页;
  717. // · 其余控件(含 CButtonUI)忽略触摸事件,点击一律走 Windows 为触摸模拟的
  718. // WM_LBUTTONDOWN/UP(原生鼠标路径)→ 故无需逐个改写控件。
  719. // 未认领的触摸事件沿本函数末尾的 m_pParent->DoEvent 冒泡给父容器。
  720. if (event.Type == UIEVENT_SETCURSOR)
  721. {
  722. if (GetCursor())
  723. {
  724. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(GetCursor())));
  725. }
  726. else
  727. {
  728. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
  729. }
  730. return;
  731. }
  732. if (event.Type == UIEVENT_SETFOCUS)
  733. {
  734. m_bFocused = true;
  735. Invalidate();
  736. return;
  737. }
  738. if (event.Type == UIEVENT_KILLFOCUS)
  739. {
  740. m_bFocused = false;
  741. Invalidate();
  742. return;
  743. }
  744. if (event.Type == UIEVENT_TIMER)
  745. {
  746. m_pManager->SendNotify(this, DUI_MSGTYPE_TIMER, event.wParam, event.lParam);
  747. return;
  748. }
  749. if (event.Type == UIEVENT_CONTEXTMENU)
  750. {
  751. if (IsContextMenuUsed())
  752. {
  753. m_pManager->SendNotify(this, DUI_MSGTYPE_MENU, event.wParam, event.lParam);
  754. return;
  755. }
  756. }
  757. if (m_pParent != NULL) m_pParent->DoEvent(event);
  758. }
  759. void CControlUI::SetVirtualWnd(LPCTSTR pstrValue)
  760. {
  761. m_sVirtualWnd = pstrValue;
  762. m_pManager->UsedVirtualWnd(true);
  763. }
  764. CDuiString CControlUI::GetVirtualWnd() const
  765. {
  766. CDuiString str;
  767. if (!m_sVirtualWnd.IsEmpty())
  768. {
  769. str = m_sVirtualWnd;
  770. }
  771. else
  772. {
  773. CControlUI* pParent = GetParent();
  774. if (pParent != NULL)
  775. {
  776. str = pParent->GetVirtualWnd();
  777. }
  778. else
  779. {
  780. str = _T("");
  781. }
  782. }
  783. return str;
  784. }
  785. void CControlUI::AddCustomAttribute(LPCTSTR pstrName, LPCTSTR pstrAttr)
  786. {
  787. if (pstrName == NULL || pstrName[0] == _T('\0') || pstrAttr == NULL || pstrAttr[0] == _T('\0')) return;
  788. if (m_mCustomAttrHash.Find(pstrName) == NULL)
  789. {
  790. CDuiString* pCostomAttr = new CDuiString(pstrAttr);
  791. if (pCostomAttr != NULL)
  792. {
  793. m_mCustomAttrHash.Set(pstrName, (LPVOID)pCostomAttr);
  794. }
  795. }
  796. }
  797. LPCTSTR CControlUI::GetCustomAttribute(LPCTSTR pstrName) const
  798. {
  799. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  800. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(pstrName));
  801. if (pCostomAttr) return pCostomAttr->GetData();
  802. return NULL;
  803. }
  804. bool CControlUI::RemoveCustomAttribute(LPCTSTR pstrName)
  805. {
  806. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  807. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(pstrName));
  808. if (!pCostomAttr) return false;
  809. delete pCostomAttr;
  810. return m_mCustomAttrHash.Remove(pstrName);
  811. }
  812. void CControlUI::RemoveAllCustomAttribute()
  813. {
  814. CDuiString* pCostomAttr;
  815. for (int i = 0; i < m_mCustomAttrHash.GetSize(); i++)
  816. {
  817. if (LPCTSTR key = m_mCustomAttrHash.GetAt(i))
  818. {
  819. pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(key));
  820. delete pCostomAttr;
  821. }
  822. }
  823. m_mCustomAttrHash.Resize();
  824. }
  825. void CControlUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  826. {
  827. // 样式表
  828. if (m_pManager != NULL && _tcsicmp(pstrName, _T("style")) == 0)
  829. {
  830. LPCTSTR pStyle = m_pManager->GetStyle(pstrValue);
  831. if (pStyle != NULL)
  832. {
  833. ApplyAttributeList(pStyle);
  834. return;
  835. }
  836. }
  837. // 属性
  838. if (_tcsicmp(pstrName, _T("innerstyle")) == 0)
  839. {
  840. ApplyAttributeList(pstrValue);
  841. }
  842. else if (_tcsicmp(pstrName, _T("pos")) == 0)
  843. {
  844. RECT rcPos = { 0 };
  845. LPTSTR pstr = NULL;
  846. rcPos.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  847. rcPos.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  848. rcPos.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  849. rcPos.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  850. SIZE szXY = { rcPos.left >= 0 ? rcPos.left : rcPos.right, rcPos.top >= 0 ? rcPos.top : rcPos.bottom };
  851. SetFixedXY(szXY);
  852. SetFixedWidth(abs(rcPos.right - rcPos.left));
  853. SetFixedHeight(abs(rcPos.bottom - rcPos.top));
  854. }
  855. else if (_tcsicmp(pstrName, _T("float")) == 0)
  856. {
  857. CDuiString nValue = pstrValue;
  858. // 动态计算相对比例
  859. if (nValue.Find(',') < 0)
  860. {
  861. SetFloat(_tcsicmp(pstrValue, _T("true")) == 0);
  862. }
  863. else
  864. {
  865. TPercentInfo piFloatPercent = { 0 };
  866. LPTSTR pstr = NULL;
  867. piFloatPercent.left = _tcstod(pstrValue, &pstr); ASSERT(pstr);
  868. piFloatPercent.top = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  869. piFloatPercent.right = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  870. piFloatPercent.bottom = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  871. SetFloatPercent(piFloatPercent);
  872. SetFloat(true);
  873. }
  874. }
  875. else if (_tcsicmp(pstrName, _T("floatalign")) == 0)
  876. {
  877. UINT uAlign = GetFloatAlign();
  878. // 解析文字属性
  879. while (*pstrValue != _T('\0'))
  880. {
  881. CDuiString sValue;
  882. while (*pstrValue == _T(',') || *pstrValue == _T(' ')) pstrValue = ::CharNext(pstrValue);
  883. while (*pstrValue != _T('\0') && *pstrValue != _T(',') && *pstrValue != _T(' '))
  884. {
  885. LPTSTR pstrTemp = ::CharNext(pstrValue);
  886. while (pstrValue < pstrTemp)
  887. {
  888. sValue += *pstrValue++;
  889. }
  890. }
  891. if (sValue.CompareNoCase(_T("null")) == 0)
  892. {
  893. uAlign = 0;
  894. }
  895. if (sValue.CompareNoCase(_T("left")) == 0)
  896. {
  897. uAlign &= ~(DT_CENTER | DT_RIGHT);
  898. uAlign |= DT_LEFT;
  899. }
  900. else if (sValue.CompareNoCase(_T("center")) == 0)
  901. {
  902. uAlign &= ~(DT_LEFT | DT_RIGHT);
  903. uAlign |= DT_CENTER;
  904. }
  905. else if (sValue.CompareNoCase(_T("right")) == 0)
  906. {
  907. uAlign &= ~(DT_LEFT | DT_CENTER);
  908. uAlign |= DT_RIGHT;
  909. }
  910. else if (sValue.CompareNoCase(_T("top")) == 0)
  911. {
  912. uAlign &= ~(DT_BOTTOM | DT_VCENTER);
  913. uAlign |= DT_TOP;
  914. }
  915. else if (sValue.CompareNoCase(_T("vcenter")) == 0)
  916. {
  917. uAlign &= ~(DT_TOP | DT_BOTTOM);
  918. uAlign |= DT_VCENTER;
  919. }
  920. else if (sValue.CompareNoCase(_T("bottom")) == 0)
  921. {
  922. uAlign &= ~(DT_TOP | DT_VCENTER);
  923. uAlign |= DT_BOTTOM;
  924. }
  925. }
  926. SetFloatAlign(uAlign);
  927. }
  928. else if (_tcsicmp(pstrName, _T("padding")) == 0)
  929. {
  930. RECT rcPadding = { 0 };
  931. LPTSTR pstr = NULL;
  932. rcPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  933. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  934. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  935. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  936. SetPadding(rcPadding);
  937. }
  938. else if (_tcsicmp(pstrName, _T("gradient")) == 0) SetGradient(pstrValue);
  939. else if (_tcsicmp(pstrName, _T("bkcolor")) == 0 || _tcsicmp(pstrName, _T("bkcolor1")) == 0)
  940. {
  941. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  942. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  943. LPTSTR pstr = NULL;
  944. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  945. SetBkColor(clrColor);
  946. }
  947. else if (_tcsicmp(pstrName, _T("bkcolor2")) == 0)
  948. {
  949. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  950. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  951. LPTSTR pstr = NULL;
  952. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  953. SetBkColor2(clrColor);
  954. }
  955. else if (_tcsicmp(pstrName, _T("bkcolor3")) == 0)
  956. {
  957. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  958. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  959. LPTSTR pstr = NULL;
  960. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  961. SetBkColor3(clrColor);
  962. }
  963. else if (_tcsicmp(pstrName, _T("forecolor")) == 0)
  964. {
  965. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  966. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  967. LPTSTR pstr = NULL;
  968. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  969. SetForeColor(clrColor);
  970. }
  971. else if (_tcsicmp(pstrName, _T("bordercolor")) == 0)
  972. {
  973. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  974. LPTSTR pstr = NULL;
  975. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  976. SetBorderColor(clrColor);
  977. }
  978. else if (_tcsicmp(pstrName, _T("focusbordercolor")) == 0)
  979. {
  980. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  981. LPTSTR pstr = NULL;
  982. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  983. SetFocusBorderColor(clrColor);
  984. }
  985. else if (_tcsicmp(pstrName, _T("colorhsl")) == 0) SetColorHSL(_tcsicmp(pstrValue, _T("true")) == 0);
  986. else if (_tcsicmp(pstrName, _T("bordersize")) == 0)
  987. {
  988. CDuiString nValue = pstrValue;
  989. if (nValue.Find(',') < 0)
  990. {
  991. SetBorderSize(_ttoi(pstrValue));
  992. RECT rcPadding = { 0 };
  993. SetBorderSize(rcPadding);
  994. }
  995. else
  996. {
  997. RECT rcPadding = { 0 };
  998. LPTSTR pstr = NULL;
  999. rcPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  1000. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  1001. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  1002. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  1003. SetBorderSize(rcPadding);
  1004. }
  1005. }
  1006. else if (_tcsicmp(pstrName, _T("leftbordersize")) == 0) SetLeftBorderSize(_ttoi(pstrValue));
  1007. else if (_tcsicmp(pstrName, _T("topbordersize")) == 0) SetTopBorderSize(_ttoi(pstrValue));
  1008. else if (_tcsicmp(pstrName, _T("rightbordersize")) == 0) SetRightBorderSize(_ttoi(pstrValue));
  1009. else if (_tcsicmp(pstrName, _T("bottombordersize")) == 0) SetBottomBorderSize(_ttoi(pstrValue));
  1010. else if (_tcsicmp(pstrName, _T("borderstyle")) == 0) SetBorderStyle(_ttoi(pstrValue));
  1011. else if (_tcsicmp(pstrName, _T("borderround")) == 0)
  1012. {
  1013. SIZE cxyRound = { 0 };
  1014. LPTSTR pstr = NULL;
  1015. cxyRound.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  1016. cxyRound.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  1017. SetBorderRound(cxyRound);
  1018. }
  1019. else if (_tcsicmp(pstrName, _T("bkimage")) == 0) SetBkImage(pstrValue);
  1020. else if (_tcsicmp(pstrName, _T("foreimage")) == 0) SetForeImage(pstrValue);
  1021. else if (_tcsicmp(pstrName, _T("width")) == 0) SetFixedWidth(_ttoi(pstrValue));
  1022. else if (_tcsicmp(pstrName, _T("height")) == 0) SetFixedHeight(_ttoi(pstrValue));
  1023. else if (_tcsicmp(pstrName, _T("minwidth")) == 0) SetMinWidth(_ttoi(pstrValue));
  1024. else if (_tcsicmp(pstrName, _T("minheight")) == 0) SetMinHeight(_ttoi(pstrValue));
  1025. else if (_tcsicmp(pstrName, _T("maxwidth")) == 0) SetMaxWidth(_ttoi(pstrValue));
  1026. else if (_tcsicmp(pstrName, _T("maxheight")) == 0) SetMaxHeight(_ttoi(pstrValue));
  1027. else if (_tcsicmp(pstrName, _T("name")) == 0) SetName(pstrValue);
  1028. else if (_tcsicmp(pstrName, _T("drag")) == 0) SetDragEnable(_tcsicmp(pstrValue, _T("true")) == 0);
  1029. else if (_tcsicmp(pstrName, _T("drop")) == 0) SetDropEnable(_tcsicmp(pstrValue, _T("true")) == 0);
  1030. else if (_tcsicmp(pstrName, _T("resourcetext")) == 0) SetResourceText(_tcsicmp(pstrValue, _T("true")) == 0);
  1031. else if (_tcsicmp(pstrName, _T("richevent")) == 0) SetRichEvent(_tcsicmp(pstrValue, _T("true")) == 0);
  1032. else if (_tcsicmp(pstrName, _T("text")) == 0) SetText(pstrValue);
  1033. else if (_tcsicmp(pstrName, _T("tooltip")) == 0) SetToolTip(pstrValue);
  1034. else if (_tcsicmp(pstrName, _T("userdata")) == 0) SetUserData(pstrValue);
  1035. else if (_tcsicmp(pstrName, _T("enabled")) == 0) SetEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1036. else if (_tcsicmp(pstrName, _T("mouse")) == 0) SetMouseEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1037. else if (_tcsicmp(pstrName, _T("keyboard")) == 0) SetKeyboardEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1038. else if (_tcsicmp(pstrName, _T("visible")) == 0) SetVisible(_tcsicmp(pstrValue, _T("true")) == 0);
  1039. else if (_tcsicmp(pstrName, _T("float")) == 0) SetFloat(_tcsicmp(pstrValue, _T("true")) == 0);
  1040. else if (_tcsicmp(pstrName, _T("shortcut")) == 0) SetShortcut(pstrValue[0]);
  1041. else if (_tcsicmp(pstrName, _T("menu")) == 0) SetContextMenuUsed(_tcsicmp(pstrValue, _T("true")) == 0);
  1042. else if (_tcsicmp(pstrName, _T("cursor")) == 0 && pstrValue)
  1043. {
  1044. if (_tcsicmp(pstrValue, _T("arrow")) == 0) SetCursor(DUI_ARROW);
  1045. else if (_tcsicmp(pstrValue, _T("ibeam")) == 0) SetCursor(DUI_IBEAM);
  1046. else if (_tcsicmp(pstrValue, _T("wait")) == 0) SetCursor(DUI_WAIT);
  1047. else if (_tcsicmp(pstrValue, _T("cross")) == 0) SetCursor(DUI_CROSS);
  1048. else if (_tcsicmp(pstrValue, _T("uparrow")) == 0) SetCursor(DUI_UPARROW);
  1049. else if (_tcsicmp(pstrValue, _T("size")) == 0) SetCursor(DUI_SIZE);
  1050. else if (_tcsicmp(pstrValue, _T("icon")) == 0) SetCursor(DUI_ICON);
  1051. else if (_tcsicmp(pstrValue, _T("sizenwse")) == 0) SetCursor(DUI_SIZENWSE);
  1052. else if (_tcsicmp(pstrValue, _T("sizenesw")) == 0) SetCursor(DUI_SIZENESW);
  1053. else if (_tcsicmp(pstrValue, _T("sizewe")) == 0) SetCursor(DUI_SIZEWE);
  1054. else if (_tcsicmp(pstrValue, _T("sizens")) == 0) SetCursor(DUI_SIZENS);
  1055. else if (_tcsicmp(pstrValue, _T("sizeall")) == 0) SetCursor(DUI_SIZEALL);
  1056. else if (_tcsicmp(pstrValue, _T("no")) == 0) SetCursor(DUI_NO);
  1057. else if (_tcsicmp(pstrValue, _T("hand")) == 0) SetCursor(DUI_HAND);
  1058. }
  1059. else if (_tcsicmp(pstrName, _T("virtualwnd")) == 0) SetVirtualWnd(pstrValue);
  1060. else
  1061. {
  1062. AddCustomAttribute(pstrName, pstrValue);
  1063. }
  1064. }
  1065. CControlUI* CControlUI::ApplyAttributeList(LPCTSTR pstrValue)
  1066. {
  1067. // 解析样式表
  1068. if (m_pManager != NULL)
  1069. {
  1070. LPCTSTR pStyle = m_pManager->GetStyle(pstrValue);
  1071. if (pStyle != NULL)
  1072. {
  1073. return ApplyAttributeList(pStyle);
  1074. }
  1075. }
  1076. CDuiString sXmlData = pstrValue;
  1077. sXmlData.Replace(_T("&quot;"), _T("\""));
  1078. sXmlData.Replace(_T("\r"), _T(" "));
  1079. sXmlData.Replace(_T("\n"), _T(" "));
  1080. sXmlData.Replace(_T("\t"), _T(" "));
  1081. sXmlData.Trim();
  1082. LPCTSTR pstrList = sXmlData.GetData();
  1083. // 解析样式属性
  1084. CDuiString sItem;
  1085. CDuiString sValue;
  1086. while (*pstrList != _T('\0'))
  1087. {
  1088. sItem.Empty();
  1089. sValue.Empty();
  1090. while (*pstrList != _T('\0') && (*pstrList == _T(' ')))
  1091. {
  1092. pstrList++;
  1093. }
  1094. while (*pstrList != _T('\0') && *pstrList != _T('='))
  1095. {
  1096. LPTSTR pstrTemp = ::CharNext(pstrList);
  1097. while (pstrList < pstrTemp)
  1098. {
  1099. sItem += *pstrList++;
  1100. }
  1101. }
  1102. ASSERT(*pstrList == _T('='));
  1103. if (*pstrList++ != _T('=')) return this;
  1104. ASSERT(*pstrList == _T('\"'));
  1105. if (*pstrList++ != _T('\"')) return this;
  1106. while (*pstrList != _T('\0') && *pstrList != _T('\"'))
  1107. {
  1108. LPTSTR pstrTemp = ::CharNext(pstrList);
  1109. while (pstrList < pstrTemp)
  1110. {
  1111. sValue += *pstrList++;
  1112. }
  1113. }
  1114. ASSERT(*pstrList == _T('\"'));
  1115. if (*pstrList++ != _T('\"')) return this;
  1116. SetAttribute(sItem, sValue);
  1117. if (*pstrList != _T(' ') && *pstrList != _T(','))
  1118. {
  1119. return this;
  1120. }
  1121. else
  1122. {
  1123. ++pstrList;
  1124. }
  1125. }
  1126. return this;
  1127. }
  1128. SIZE CControlUI::EstimateSize(SIZE szAvailable)
  1129. {
  1130. if (m_pManager != NULL)
  1131. return m_pManager->GetDPIObj()->Scale(m_cxyFixed);
  1132. return m_cxyFixed;
  1133. }
  1134. bool CControlUI::Paint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1135. {
  1136. if (pStopControl == this) return false;
  1137. if (!::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem)) return true;
  1138. if (!DoPaint(hDC, m_rcPaint, pStopControl)) return false;
  1139. return true;
  1140. }
  1141. bool CControlUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1142. {
  1143. // 绘制循序:背景颜色->背景图->状态图->文本->边框
  1144. SIZE cxyBorderRound = GetBorderRound();
  1145. RECT rcBorderSize = GetBorderRectSize();
  1146. if (cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0)
  1147. {
  1148. CRenderClip roundClip;
  1149. CRenderClip::GenerateRoundClip(hDC, m_rcPaint, m_rcItem, cxyBorderRound.cx, cxyBorderRound.cy, roundClip);
  1150. PaintBkColor(hDC);
  1151. PaintBkImage(hDC);
  1152. PaintStatusImage(hDC);
  1153. PaintForeColor(hDC);
  1154. PaintForeImage(hDC);
  1155. PaintText(hDC);
  1156. PaintBorder(hDC);
  1157. }
  1158. else
  1159. {
  1160. PaintBkColor(hDC);
  1161. PaintBkImage(hDC);
  1162. PaintStatusImage(hDC);
  1163. PaintForeColor(hDC);
  1164. PaintForeImage(hDC);
  1165. PaintText(hDC);
  1166. PaintBorder(hDC);
  1167. }
  1168. return true;
  1169. }
  1170. void CControlUI::PaintBkColor(HDC hDC)
  1171. {
  1172. if (m_dwBackColor != 0)
  1173. {
  1174. bool bVer = (m_sGradient.CompareNoCase(_T("hor")) != 0);
  1175. if (m_dwBackColor2 != 0)
  1176. {
  1177. if (m_dwBackColor3 != 0)
  1178. {
  1179. RECT rc = m_rcItem;
  1180. rc.bottom = (rc.bottom + rc.top) / 2;
  1181. CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(m_dwBackColor), GetAdjustColor(m_dwBackColor2), bVer, 8);
  1182. rc.top = rc.bottom;
  1183. rc.bottom = m_rcItem.bottom;
  1184. CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(m_dwBackColor2), GetAdjustColor(m_dwBackColor3), bVer, 8);
  1185. }
  1186. else
  1187. {
  1188. CRenderEngine::DrawGradient(hDC, m_rcItem, GetAdjustColor(m_dwBackColor), GetAdjustColor(m_dwBackColor2), bVer, 16);
  1189. }
  1190. }
  1191. else if (m_dwBackColor >= 0xFF000000) CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwBackColor));
  1192. else CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(m_dwBackColor));
  1193. }
  1194. }
  1195. void CControlUI::PaintBkImage(HDC hDC)
  1196. {
  1197. if (m_sBkImage.IsEmpty()) return;
  1198. if (!DrawImage(hDC, (LPCTSTR)m_sBkImage))
  1199. {
  1200. }
  1201. }
  1202. void CControlUI::PaintStatusImage(HDC hDC)
  1203. {
  1204. return;
  1205. }
  1206. void CControlUI::PaintForeColor(HDC hDC)
  1207. {
  1208. CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(m_dwForeColor));
  1209. }
  1210. void CControlUI::PaintForeImage(HDC hDC)
  1211. {
  1212. if (m_sForeImage.IsEmpty()) return;
  1213. DrawImage(hDC, (LPCTSTR)m_sForeImage);
  1214. }
  1215. void CControlUI::PaintText(HDC hDC)
  1216. {
  1217. return;
  1218. }
  1219. void CControlUI::PaintBorder(HDC hDC)
  1220. {
  1221. int nBorderSize = GetBorderSize();
  1222. SIZE cxyBorderRound = GetBorderRound();
  1223. RECT rcBorderSize = GetBorderRectSize();
  1224. if (m_dwBorderColor != 0 || m_dwFocusBorderColor != 0)
  1225. {
  1226. //画圆角边框
  1227. if (nBorderSize > 0 && (cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0))
  1228. {
  1229. if (IsFocused() && m_dwFocusBorderColor != 0)
  1230. CRenderEngine::DrawRoundRect(hDC, m_rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwFocusBorderColor), m_nBorderStyle);
  1231. else
  1232. CRenderEngine::DrawRoundRect(hDC, m_rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1233. }
  1234. else
  1235. {
  1236. if (IsFocused() && m_dwFocusBorderColor != 0 && nBorderSize > 0)
  1237. {
  1238. CRenderEngine::DrawRect(hDC, m_rcItem, nBorderSize, GetAdjustColor(m_dwFocusBorderColor), m_nBorderStyle);
  1239. }
  1240. else if (rcBorderSize.left > 0 || rcBorderSize.top > 0 || rcBorderSize.right > 0 || rcBorderSize.bottom > 0)
  1241. {
  1242. RECT rcBorder;
  1243. if (rcBorderSize.left > 0)
  1244. {
  1245. rcBorder = m_rcItem;
  1246. rcBorder.right = rcBorder.left;
  1247. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.left, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1248. }
  1249. if (rcBorderSize.top > 0)
  1250. {
  1251. rcBorder = m_rcItem;
  1252. rcBorder.bottom = rcBorder.top;
  1253. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.top, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1254. }
  1255. if (rcBorderSize.right > 0)
  1256. {
  1257. rcBorder = m_rcItem;
  1258. rcBorder.right -= 1;
  1259. rcBorder.left = rcBorder.right;
  1260. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.right, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1261. }
  1262. if (rcBorderSize.bottom > 0)
  1263. {
  1264. rcBorder = m_rcItem;
  1265. rcBorder.bottom -= 1;
  1266. rcBorder.top = rcBorder.bottom;
  1267. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.bottom, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1268. }
  1269. }
  1270. else if (nBorderSize > 0)
  1271. {
  1272. CRenderEngine::DrawRect(hDC, m_rcItem, nBorderSize, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1273. }
  1274. }
  1275. }
  1276. }
  1277. void CControlUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
  1278. {
  1279. return;
  1280. }
  1281. int CControlUI::GetLeftBorderSize() const
  1282. {
  1283. RECT rcBorderSize = m_rcBorderSize;
  1284. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1285. return rcBorderSize.left;
  1286. }
  1287. void CControlUI::SetLeftBorderSize(int nSize)
  1288. {
  1289. m_rcBorderSize.left = nSize;
  1290. Invalidate();
  1291. }
  1292. int CControlUI::GetTopBorderSize() const
  1293. {
  1294. RECT rcBorderSize = m_rcBorderSize;
  1295. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1296. return rcBorderSize.top;
  1297. }
  1298. void CControlUI::SetTopBorderSize(int nSize)
  1299. {
  1300. m_rcBorderSize.top = nSize;
  1301. Invalidate();
  1302. }
  1303. int CControlUI::GetRightBorderSize() const
  1304. {
  1305. RECT rcBorderSize = m_rcBorderSize;
  1306. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1307. return rcBorderSize.right;
  1308. }
  1309. void CControlUI::SetRightBorderSize(int nSize)
  1310. {
  1311. m_rcBorderSize.right = nSize;
  1312. Invalidate();
  1313. }
  1314. int CControlUI::GetBottomBorderSize() const
  1315. {
  1316. RECT rcBorderSize = m_rcBorderSize;
  1317. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1318. return rcBorderSize.bottom;
  1319. }
  1320. void CControlUI::SetBottomBorderSize(int nSize)
  1321. {
  1322. m_rcBorderSize.bottom = nSize;
  1323. Invalidate();
  1324. }
  1325. int CControlUI::GetBorderStyle() const
  1326. {
  1327. return m_nBorderStyle;
  1328. }
  1329. void CControlUI::SetBorderStyle(int nStyle)
  1330. {
  1331. m_nBorderStyle = nStyle;
  1332. Invalidate();
  1333. }
  1334. } // namespace DuiLib