SkinEdit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SkinEdit.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Control.h"
  5. #include "SkinEdit.h"
  6. // CSkinEdit
  7. IMPLEMENT_DYNAMIC(CSkinEdit, CEdit)
  8. CSkinEdit::CSkinEdit()
  9. {
  10. m_pBackImgN = NULL;
  11. m_pBackImgH = NULL;
  12. m_pIconImg = NULL;
  13. m_bFocus = m_bPress = m_bHover = m_bMouseTracking = FALSE;
  14. m_nIconWidth = 0;
  15. m_bHandCursor = false;
  16. m_bIsDefText = FALSE;
  17. m_cPwdChar = 0;
  18. m_ptClient.SetPoint(0,0);
  19. m_colBack=RGB(255,255,255);
  20. }
  21. CSkinEdit::~CSkinEdit()
  22. {
  23. }
  24. BEGIN_MESSAGE_MAP(CSkinEdit, CEdit)
  25. ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
  26. ON_WM_NCCALCSIZE()
  27. ON_WM_NCPAINT()
  28. ON_WM_ERASEBKGND()
  29. ON_WM_LBUTTONDOWN()
  30. ON_WM_LBUTTONUP()
  31. ON_WM_MOUSEMOVE()
  32. ON_WM_DESTROY()
  33. ON_WM_SETFOCUS()
  34. ON_WM_KILLFOCUS()
  35. ON_WM_SETCURSOR()
  36. ON_WM_CTLCOLOR_REFLECT()
  37. ON_WM_NCHITTEST()
  38. ON_WM_NCLBUTTONUP()
  39. END_MESSAGE_MAP()
  40. BOOL CSkinEdit::SetBackNormalImage( LPCTSTR lpszFileName, CONST LPRECT lpNinePart /*= NULL*/ )
  41. {
  42. RenderEngine->RemoveImage(m_pBackImgN);
  43. m_pBackImgN = RenderEngine->GetImage(lpszFileName);
  44. if (m_pBackImgN != NULL)
  45. m_pBackImgN->SetNinePart(lpNinePart);
  46. return (m_pBackImgN != NULL) ? TRUE : FALSE;
  47. }
  48. BOOL CSkinEdit::SetBackHotImage( LPCTSTR lpszFileName,CONST LPRECT lpNinePart /*= NULL*/ )
  49. {
  50. RenderEngine->RemoveImage(m_pBackImgH);
  51. m_pBackImgH = RenderEngine->GetImage(lpszFileName);
  52. if (m_pBackImgH != NULL)
  53. m_pBackImgH->SetNinePart(lpNinePart);
  54. return (m_pBackImgH != NULL) ? TRUE : FALSE;
  55. }
  56. BOOL CSkinEdit::SetIconImage( LPCTSTR lpszFileName,bool bHandCursor /*= false*/ )
  57. {
  58. RenderEngine->RemoveImage(m_pIconImg);
  59. m_pIconImg = RenderEngine->GetImage(lpszFileName);
  60. if (m_pIconImg != NULL)
  61. {
  62. m_nIconWidth = m_pIconImg->GetWidth();
  63. // CRect rcClient;
  64. // GetClientRect(&rcClient);
  65. // rcClient.right-=m_nIconWidth;
  66. //
  67. // SetRect(&rcClient);
  68. //CRect rcWindow;
  69. //GetWindowRect(&rcWindow);
  70. //GetParent()->ScreenToClient(&rcWindow);
  71. //rcWindow.right+=1;
  72. //MoveWindow(rcWindow,TRUE);
  73. }
  74. m_bHandCursor = bHandCursor;
  75. return (m_pIconImg != NULL) ? TRUE : FALSE;
  76. }
  77. void CSkinEdit::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
  78. {
  79. if (NULL != m_pIconImg && !m_pIconImg->IsNull())
  80. {
  81. if (bCalcValidRects)
  82. {
  83. lpncsp->rgrc[0].left += m_ptClient.x;
  84. lpncsp->rgrc[0].top += m_ptClient.y;
  85. lpncsp->rgrc[0].right -= m_nIconWidth;
  86. lpncsp->rgrc[0].bottom -= m_ptClient.y;
  87. lpncsp->rgrc[1] = lpncsp->rgrc[0];
  88. }
  89. }
  90. else
  91. {
  92. if (bCalcValidRects)
  93. {
  94. lpncsp->rgrc[0].left += 3;
  95. lpncsp->rgrc[0].top += 3;
  96. lpncsp->rgrc[0].right -= 3;
  97. lpncsp->rgrc[0].bottom -= 1;
  98. //lpncsp->rgrc[1] = lpncsp->rgrc[0];
  99. }
  100. }
  101. CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
  102. }
  103. void CSkinEdit::OnNcPaint()
  104. {
  105. CRect rcWindow;
  106. GetWindowRect(&rcWindow);
  107. rcWindow.OffsetRect(-rcWindow.left, -rcWindow.top);
  108. CDC *pWindowDC = GetWindowDC();
  109. CRect rcIcon;
  110. if (m_pIconImg != NULL && !m_pIconImg->IsNull())
  111. {
  112. int cxIcon = m_pIconImg->GetWidth();
  113. int cyIcon = m_pIconImg->GetHeight();
  114. CalcCenterRect(rcWindow, cxIcon, cyIcon, rcIcon);
  115. rcIcon.right = rcWindow.right - 2;
  116. rcIcon.left = rcIcon.right - cxIcon;
  117. }
  118. //pWindowDC->FillSolidRect(&rcWindow,m_colBack);
  119. //DrawParentWndBg(GetSafeHwnd(),pWindowDC->GetSafeHdc());
  120. //不要刷新整个客户区,客户区因为有字符串的缘故,当拉伸窗口时,非客户区和客户区两者因为刷新不同步,会造成客户区文字闪烁
  121. RenderEngine->DrawRect(pWindowDC->GetSafeHdc(),rcWindow,3,m_colBack);
  122. if (m_bHover)
  123. {
  124. if (m_pBackImgH != NULL && !m_pBackImgH->IsNull())
  125. {
  126. m_pBackImgH->DrawFrame(pWindowDC, rcWindow);
  127. }
  128. else
  129. {
  130. if (m_pBackImgN != NULL && !m_pBackImgN->IsNull())
  131. m_pBackImgN->DrawFrame(pWindowDC, rcWindow);
  132. }
  133. }
  134. else
  135. {
  136. if (m_pBackImgN != NULL && !m_pBackImgN->IsNull())
  137. {
  138. m_pBackImgN->DrawFrame(pWindowDC, rcWindow);
  139. }
  140. }
  141. if (m_pIconImg != NULL && !m_pIconImg->IsNull())
  142. {
  143. //图标填充一下背景,防止未能刷新留下尾巴
  144. pWindowDC->FillSolidRect(&rcIcon,m_colBack);
  145. m_pIconImg->Draw(pWindowDC, rcIcon);
  146. }
  147. ReleaseDC(pWindowDC);
  148. }
  149. BOOL CSkinEdit::OnEraseBkgnd(CDC* pDC)
  150. {
  151. return TRUE;
  152. }
  153. void CSkinEdit::OnLButtonDown(UINT nFlags, CPoint point)
  154. {
  155. CRect rcIcon;
  156. CPoint pt;
  157. GetCursorPos(&pt);
  158. GetIconRect(rcIcon);
  159. if( PtInRect(&rcIcon,pt)) return;
  160. m_bPress = TRUE;
  161. __super::OnLButtonDown(nFlags, point);
  162. }
  163. void CSkinEdit::OnLButtonUp(UINT nFlags, CPoint point)
  164. {
  165. CRect rcIcon;
  166. CPoint pt;
  167. GetCursorPos(&pt);
  168. GetIconRect(rcIcon);
  169. if( PtInRect(&rcIcon,pt))
  170. {
  171. return;
  172. }
  173. else
  174. {
  175. if (m_bPress)
  176. {
  177. m_bPress = FALSE;
  178. RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
  179. }
  180. }
  181. __super::OnLButtonUp(nFlags, point);
  182. }
  183. void CSkinEdit::OnMouseMove(UINT nFlags, CPoint point)
  184. {
  185. if (!m_bMouseTracking)
  186. {
  187. TrackMouseLeave(GetSafeHwnd());
  188. m_bMouseTracking = TRUE;
  189. m_bHover = TRUE;
  190. RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
  191. }
  192. __super::OnMouseMove(nFlags, point);
  193. }
  194. LRESULT CSkinEdit::OnMouseLeave( WPARAM wparam, LPARAM lparam )
  195. {
  196. m_bMouseTracking = FALSE;
  197. m_bHover = FALSE;
  198. RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
  199. return TRUE;
  200. }
  201. void CSkinEdit::OnDestroy()
  202. {
  203. __super::OnDestroy();
  204. RenderEngine->RemoveImage(m_pBackImgN);
  205. RenderEngine->RemoveImage(m_pBackImgH);
  206. RenderEngine->RemoveImage(m_pIconImg);
  207. RemoveScorll();
  208. }
  209. void CSkinEdit::OnSetFocus(CWnd* pOldWnd)
  210. {
  211. __super::OnSetFocus(pOldWnd);
  212. if (m_bIsDefText)
  213. {
  214. m_bIsDefText = FALSE;
  215. SetPasswordChar(m_cPwdChar);
  216. SetWindowText(_T(""));
  217. }
  218. m_bFocus = TRUE;
  219. //Invalidate(FALSE);
  220. OnNcPaint();
  221. }
  222. void CSkinEdit::OnKillFocus(CWnd* pNewWnd)
  223. {
  224. __super::OnKillFocus(pNewWnd);
  225. if (GetWindowTextLength() <= 0 && !m_strDefText.IsEmpty())
  226. {
  227. m_bIsDefText = TRUE;
  228. m_cPwdChar = GetPasswordChar();
  229. SetPasswordChar(0);
  230. SetWindowText(m_strDefText);
  231. }
  232. m_bFocus = FALSE;
  233. //Invalidate(FALSE);
  234. OnNcPaint();
  235. }
  236. BOOL CSkinEdit::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  237. {
  238. CRect rcIcon;
  239. CPoint pt;
  240. GetCursorPos(&pt);
  241. GetIconRect(rcIcon);
  242. if( PtInRect(&rcIcon,pt))
  243. {
  244. if( m_bHandCursor )
  245. SetCursor(LoadCursor(NULL,IDC_HAND));
  246. else
  247. SetCursor(LoadCursor(NULL,IDC_ARROW));
  248. return TRUE;
  249. }
  250. return __super::OnSetCursor(pWnd, nHitTest, message);
  251. }
  252. void CSkinEdit::GetIconRect( RECT &rcIcon )
  253. {
  254. CRect rcWindow;
  255. GetWindowRect(&rcWindow);
  256. CalcCenterRect(rcWindow, m_nIconWidth, rcWindow.Height(), rcIcon);
  257. rcIcon.right = rcWindow.right - 2;
  258. rcIcon.left = rcIcon.right - m_nIconWidth;
  259. }
  260. BOOL CSkinEdit::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  261. {
  262. return __super::CreateEx(
  263. WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_RIGHTSCROLLBAR|WS_EX_NOPARENTNOTIFY|WS_EX_CLIENTEDGE,
  264. TEXT("EDIT"),
  265. NULL,
  266. WS_VISIBLE|WS_CHILDWINDOW|WS_TABSTOP|ES_LEFT|ES_AUTOHSCROLL|dwStyle,
  267. rect,pParentWnd,nID);
  268. return __super::Create(dwStyle, rect, pParentWnd, nID);
  269. }
  270. void CSkinEdit::SetDefaultText( LPCTSTR lpszText )
  271. {
  272. m_strDefText = lpszText;
  273. }
  274. void CSkinEdit::SetDefaultTextMode( BOOL bIsDefText )
  275. {
  276. if (bIsDefText == m_bIsDefText)
  277. return;
  278. m_bIsDefText = bIsDefText;
  279. if (m_bIsDefText)
  280. {
  281. m_cPwdChar = GetPasswordChar();
  282. SetPasswordChar(0);
  283. SetWindowText(m_strDefText);
  284. }
  285. else
  286. {
  287. SetPasswordChar(m_cPwdChar);
  288. SetWindowText(_T(""));
  289. }
  290. }
  291. HBRUSH CSkinEdit::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
  292. {
  293. pDC->SetBkMode(TRANSPARENT);
  294. if (m_bIsDefText)
  295. pDC->SetTextColor(m_colDefText);
  296. else
  297. pDC->SetTextColor(m_colNormalText);
  298. if ( m_colBack == RGB(255,255,255) ) return (HBRUSH)NULL_BRUSH;
  299. //背景色
  300. HBRUSH hBr = CreateSolidBrush(m_colBack);
  301. return (HBRUSH) hBr;
  302. }
  303. LRESULT CSkinEdit::OnNcHitTest(CPoint point)
  304. {
  305. CRect rcIcon;
  306. GetIconRect(rcIcon);
  307. if( PtInRect(&rcIcon,point))
  308. {
  309. return HTBORDER;
  310. }
  311. return __super::OnNcHitTest(point);
  312. }
  313. void CSkinEdit::OnNcLButtonUp(UINT nHitTest, CPoint point)
  314. {
  315. CWnd *pWnd = GetParent();
  316. if ( pWnd != NULL && pWnd->GetSafeHwnd() != NULL )
  317. {
  318. CRect rcIcon;
  319. CPoint pt;
  320. GetCursorPos(&pt);
  321. GetIconRect(rcIcon);
  322. if( PtInRect(&rcIcon,pt))
  323. {
  324. //m_bSendMsg = true;
  325. pWnd->PostMessage(WM_EDIT_CLICK,GetDlgCtrlID());
  326. //m_bSendMsg = false;
  327. }
  328. }
  329. __super::OnNcLButtonUp(nHitTest, point);
  330. }
  331. void CSkinEdit::SetClientPoint( CPoint pt )
  332. {
  333. m_ptClient = pt;
  334. }
  335. void CSkinEdit::ParseItem( CXmlNode *root )
  336. {
  337. if( root == NULL ) return;
  338. int nAttributes = root->GetAttributeCount();
  339. LPCTSTR pstrClass = NULL;
  340. LPCTSTR pstrName = NULL;
  341. LPCTSTR pstrValue = NULL;
  342. for( int i = 0; i < nAttributes; i++ )
  343. {
  344. pstrClass = root->GetName();
  345. pstrName = root->GetAttributeName(i);
  346. pstrValue = root->GetAttributeValue(i);
  347. if( _tcscmp(pstrClass, _T("bknormalimg")) == 0 )
  348. {
  349. if( _tcscmp(pstrName, _T("value")) == 0 ) SetBackNormalImage(pstrValue);
  350. else if( _tcscmp(pstrName, _T("ninepart")) == 0 )
  351. {
  352. LPTSTR pstr = NULL;
  353. CRect rc;
  354. rc.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  355. rc.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  356. rc.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  357. rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  358. if (m_pBackImgN != NULL)
  359. m_pBackImgN->SetNinePart(&rc);
  360. }
  361. }
  362. else if( _tcscmp(pstrClass, _T("bkhotimg")) == 0 )
  363. {
  364. if( _tcscmp(pstrName, _T("value")) == 0 ) SetBackHotImage(pstrValue);
  365. else if( _tcscmp(pstrName, _T("ninepart")) == 0 )
  366. {
  367. LPTSTR pstr = NULL;
  368. CRect rc;
  369. rc.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  370. rc.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  371. rc.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  372. rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  373. if (m_pBackImgH != NULL)
  374. m_pBackImgH->SetNinePart(&rc);
  375. }
  376. }
  377. }
  378. }
  379. void CSkinEdit::SetAttribute( LPCTSTR pstrName, LPCTSTR pstrValue )
  380. {
  381. ISkinControl::SetAttribute(pstrName,pstrValue);
  382. if( _tcscmp(pstrName, _T("defaulttext")) == 0 )
  383. {
  384. SetDefaultText(pstrValue);
  385. SetDefaultTextMode(TRUE);
  386. }
  387. else if( _tcscmp(pstrName, _T("iconimage")) == 0 )
  388. {
  389. SetIconImage(pstrValue);
  390. }
  391. else if( _tcscmp(pstrName, _T("passwordchar")) == 0 )
  392. {
  393. SetPasswordChar(pstrValue[0]);
  394. }
  395. else if( _tcscmp(pstrName, _T("clientpos")) == 0 )
  396. {
  397. LPTSTR pstr = NULL;
  398. CPoint pt;
  399. pt.x = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  400. pt.y = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  401. SetClientPoint(pt);
  402. }
  403. else if( _tcscmp(pstrName, _T("handcur")) == 0 )
  404. {
  405. m_bHandCursor = _tcscmp(pstrValue, _T("true")) == 0;
  406. }
  407. else if( _tcscmp(pstrName, _T("scrollimage")) == 0 )
  408. {
  409. SetScrollImage(this,pstrValue);
  410. }
  411. //else if( _tcscmp(pstrName, _T("multiline")) == 0 )
  412. //{
  413. // if( _tcscmp(pstrValue, _T("true")) == 0 )
  414. // // //SetWindowLong(GetSafeHwnd(),GWL_STYLE,GetWindowLong(GetSafeHwnd(),GWL_STYLE)| ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL| WS_VSCROLL);
  415. // ModifyStyle(ES_AUTOHSCROLL,ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL| WS_VSCROLL );
  416. // //ModifyStyle(ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL| WS_VSCROLL,0);
  417. //}
  418. }
  419. BOOL CSkinEdit::CreateControl( CWnd* pParentWnd )
  420. {
  421. if( !CEdit::Create(WS_VISIBLE|WS_CHILD,CRect(0,0,0,0),pParentWnd,0) )
  422. return FALSE;
  423. SetFont(CFont::FromHandle(RenderEngine->GetDeaultFont()));
  424. m_pOwnWnd = this;
  425. return TRUE;
  426. }
  427. //////////////////////////////////////////////////////////////////////////
  428. IMPLEMENT_DYNAMIC(CMultiSkinEdit, CSkinEdit)
  429. BEGIN_MESSAGE_MAP(CMultiSkinEdit,CSkinEdit)
  430. END_MESSAGE_MAP()
  431. BOOL CMultiSkinEdit::CreateControl( CWnd* pParentWnd )
  432. {
  433. if( !CEdit::Create(WS_VISIBLE|WS_CHILD|ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL| WS_VSCROLL,CRect(0,0,0,0),pParentWnd,0) )
  434. return FALSE;
  435. SetFont(CFont::FromHandle(RenderEngine->GetDeaultFont()));
  436. m_pOwnWnd = this;
  437. return TRUE;
  438. }