SkinComboBox.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. // SkinComboBox.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Control.h"
  5. #include "SkinComboBox.h"
  6. static WNDPROC m_pWndProc=0;
  7. extern "C" LRESULT FAR PASCAL ComboBoxListBoxProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  8. {
  9. return CallWindowProc(m_pWndProc, hWnd, nMsg, wParam, lParam);
  10. }
  11. //////////////////////////////////////////////////////////////////////////
  12. BEGIN_MESSAGE_MAP(CSkinComboBox_Edit, CEdit)
  13. ON_WM_ERASEBKGND()
  14. ON_WM_MOUSEMOVE()
  15. ON_WM_MOUSELEAVE()
  16. ON_WM_SETFOCUS()
  17. ON_WM_KILLFOCUS()
  18. ON_WM_CTLCOLOR_REFLECT()
  19. ON_WM_LBUTTONDOWN()
  20. ON_WM_LBUTTONDBLCLK()
  21. ON_WM_SETCURSOR()
  22. ON_WM_LBUTTONUP()
  23. END_MESSAGE_MAP()
  24. CSkinComboBox_Edit::CSkinComboBox_Edit( void )
  25. {
  26. m_hOwnerWnd = NULL;
  27. m_bMouseTracking = FALSE;
  28. m_bIsDefText = FALSE;
  29. }
  30. CSkinComboBox_Edit::~CSkinComboBox_Edit( void )
  31. {
  32. }
  33. void CSkinComboBox_Edit::SetOwnerWnd( HWND hWnd )
  34. {
  35. m_hOwnerWnd = hWnd;
  36. }
  37. void CSkinComboBox_Edit::SetDefaultText( LPCTSTR lpszText )
  38. {
  39. m_strDefText = lpszText;
  40. }
  41. BOOL CSkinComboBox_Edit::IsDefaultText()
  42. {
  43. return m_bIsDefText;
  44. }
  45. void CSkinComboBox_Edit::SetDefaultTextMode( BOOL bIsDefText )
  46. {
  47. if (bIsDefText == m_bIsDefText)
  48. return;
  49. m_bIsDefText = bIsDefText;
  50. if (m_bIsDefText)
  51. {
  52. SetWindowText(m_strDefText);
  53. }
  54. else
  55. {
  56. SetWindowText(_T(""));
  57. }
  58. }
  59. BOOL CSkinComboBox_Edit::OnEraseBkgnd(CDC* pDC)
  60. {
  61. return TRUE;
  62. }
  63. void CSkinComboBox_Edit::OnMouseMove(UINT nFlags, CPoint point)
  64. {
  65. if (!m_bMouseTracking)
  66. {
  67. m_bMouseTracking = TRUE;
  68. TrackMouseLeave(GetSafeHwnd());
  69. if (::IsWindow(m_hOwnerWnd))
  70. ::SendMessage(m_hOwnerWnd, WM_CBO_EDIT_MOUSE_HOVER, 0, 0);
  71. }
  72. __super::OnMouseMove(nFlags, point);
  73. }
  74. void CSkinComboBox_Edit::OnMouseLeave()
  75. {
  76. m_bMouseTracking = FALSE;
  77. if (::IsWindow(m_hOwnerWnd))
  78. ::SendMessage(m_hOwnerWnd, WM_CBO_EDIT_MOUSE_LEAVE, 0, 0);
  79. __super::OnMouseLeave();
  80. }
  81. void CSkinComboBox_Edit::OnSetFocus(CWnd* pOldWnd)
  82. {
  83. __super::OnSetFocus(pOldWnd);
  84. if (m_bIsDefText)
  85. {
  86. m_bIsDefText = FALSE;
  87. SetWindowText(TEXT(""));
  88. }
  89. //将焦点返回到原焦点
  90. if ( GetStyle() & ES_READONLY )
  91. {
  92. if( pOldWnd != NULL && pOldWnd->GetSafeHwnd() != NULL )
  93. pOldWnd->SetFocus();
  94. }
  95. }
  96. void CSkinComboBox_Edit::OnLButtonDown(UINT nFlags, CPoint point)
  97. {
  98. //将焦点返回到原焦点
  99. if ( GetStyle() & ES_READONLY )
  100. {
  101. if (::IsWindow(m_hOwnerWnd))
  102. ::SendMessage(m_hOwnerWnd, WM_LBUTTONDOWN,0,0);
  103. return;
  104. }
  105. __super::OnLButtonDown(nFlags, point);
  106. }
  107. void CSkinComboBox_Edit::OnLButtonDblClk(UINT nFlags, CPoint point)
  108. {
  109. //将焦点返回到原焦点
  110. if ( GetStyle() & ES_READONLY )
  111. {
  112. return;
  113. }
  114. __super::OnLButtonDblClk(nFlags, point);
  115. }
  116. LRESULT CSkinComboBox_Edit::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  117. {
  118. //如果只读属性,且有选中消息则过滤掉
  119. if ( ( GetStyle() & ES_READONLY ) && (message == EM_SETSEL) )
  120. {
  121. return TRUE;
  122. }
  123. return __super::DefWindowProc(message, wParam, lParam);
  124. }
  125. BOOL CSkinComboBox_Edit::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  126. {
  127. //将焦点返回到原焦点
  128. if ( GetStyle() & ES_READONLY )
  129. {
  130. SetCursor(LoadCursor(NULL,IDC_ARROW));
  131. return TRUE;
  132. }
  133. return __super::OnSetCursor(pWnd, nHitTest, message);
  134. }
  135. void CSkinComboBox_Edit::OnKillFocus(CWnd* pNewWnd)
  136. {
  137. __super::OnKillFocus(pNewWnd);
  138. if (GetWindowTextLength() <= 0 && !m_strDefText.IsEmpty())
  139. {
  140. m_bIsDefText = TRUE;
  141. SetWindowText(m_strDefText);
  142. }
  143. }
  144. HBRUSH CSkinComboBox_Edit::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
  145. {
  146. bool bIsReadOnly = ((GetStyle() & ES_READONLY) != 0) ? true : false;
  147. pDC->SetBkMode(TRANSPARENT);
  148. if (m_bIsDefText)
  149. {
  150. if( bIsReadOnly )
  151. pDC->SetTextColor(m_colReadOnlyText);
  152. else pDC->SetTextColor(m_colDefText);
  153. }
  154. else
  155. {
  156. if( bIsReadOnly )
  157. pDC->SetTextColor(m_colReadOnlyText);
  158. else pDC->SetTextColor(m_colNormalText);
  159. }
  160. if ( m_colBack == RGB(255,255,255) ) return (HBRUSH)NULL_BRUSH;
  161. //背景色
  162. HBRUSH hBr = CreateSolidBrush(m_colBack);
  163. return (HBRUSH) hBr;
  164. //return (HBRUSH)NULL_BRUSH;
  165. }
  166. //////////////////////////////////////////////////////////////////////////
  167. BEGIN_MESSAGE_MAP(CSkinComboBox_ListBox, CListBox)
  168. ON_WM_SHOWWINDOW()
  169. ON_WM_DESTROY()
  170. ON_WM_RBUTTONUP()
  171. END_MESSAGE_MAP()
  172. CSkinComboBox_ListBox::CSkinComboBox_ListBox( void )
  173. {
  174. m_hOwnerWnd = NULL;
  175. m_pBackImgN = NULL;
  176. m_pSelectImg = NULL;
  177. }
  178. CSkinComboBox_ListBox::~CSkinComboBox_ListBox( void )
  179. {
  180. }
  181. void CSkinComboBox_ListBox::SetOwnerWnd( HWND hWnd )
  182. {
  183. m_hOwnerWnd = hWnd;
  184. }
  185. void CSkinComboBox_ListBox::OnShowWindow(BOOL bShow, UINT nStatus)
  186. {
  187. CListBox::OnShowWindow(bShow, nStatus);
  188. if (!bShow)
  189. {
  190. ::SendMessage(m_hOwnerWnd, WM_CBO_LIST_HIDE, 0, 0);
  191. }
  192. }
  193. BOOL CSkinComboBox_ListBox::SetBackNormalImage( LPCTSTR lpszFileName, CONST LPRECT lpNinePart /*= NULL*/ )
  194. {
  195. RenderEngine->RemoveImage(m_pBackImgN);
  196. m_pBackImgN = RenderEngine->GetImage(lpszFileName);
  197. if (m_pBackImgN != NULL)
  198. m_pBackImgN->SetNinePart(lpNinePart);
  199. return (m_pBackImgN != NULL) ? TRUE : FALSE;
  200. }
  201. BOOL CSkinComboBox_ListBox::SetSelectImage( LPCTSTR lpszFileName, CONST LPRECT lpNinePart /*= NULL*/ )
  202. {
  203. RenderEngine->RemoveImage(m_pSelectImg);
  204. m_pSelectImg = RenderEngine->GetImage(lpszFileName);
  205. if (m_pSelectImg != NULL)
  206. m_pSelectImg->SetNinePart(lpNinePart);
  207. return (m_pSelectImg != NULL) ? TRUE : FALSE;
  208. }
  209. void CSkinComboBox_ListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  210. {
  211. //变量定义
  212. CRect rcItem=lpDrawItemStruct->rcItem;
  213. CDC * pDCControl=CDC::FromHandle(lpDrawItemStruct->hDC);
  214. //创建缓冲
  215. CDC BufferDC;
  216. CBitmap ImageBuffer;
  217. BufferDC.CreateCompatibleDC(pDCControl);
  218. ImageBuffer.CreateCompatibleBitmap(pDCControl,rcItem.Width(),rcItem.Height());
  219. //设置环境
  220. BufferDC.SelectObject(&ImageBuffer);
  221. BufferDC.SelectObject(GetCtrlFont());
  222. //获取字符
  223. CString strString;
  224. GetText(lpDrawItemStruct->itemID,strString);
  225. //计算位置
  226. CRect rcString;
  227. rcString.SetRect(4,0,rcItem.Width()-8,rcItem.Height());
  228. //颜色定义
  229. COLORREF crTextColor=((lpDrawItemStruct->itemState&ODS_SELECTED)!=0)?m_colSelectText:m_colNormalText;
  230. COLORREF crBackColor=((lpDrawItemStruct->itemState&ODS_SELECTED)!=0)?RGB(10,36,106):RGB(255,255,255);
  231. //绘画背景
  232. BufferDC.FillSolidRect(0,0,rcItem.Width(),rcItem.Height(),crBackColor);
  233. if ( (lpDrawItemStruct->itemState&ODS_SELECTED) != 0 )
  234. {
  235. if ( m_pSelectImg!= NULL && !m_pSelectImg->IsNull() )
  236. {
  237. rcItem.DeflateRect(1,1,1,1);
  238. m_pSelectImg->Draw(&BufferDC,CRect(0,0,rcItem.Width(),rcItem.Height()));
  239. }
  240. }
  241. //绘画字符
  242. BufferDC.SetBkMode(TRANSPARENT);
  243. BufferDC.SetTextColor(crTextColor);
  244. BufferDC.DrawText(strString,&rcString,DT_VCENTER|DT_SINGLELINE);
  245. //绘画界面
  246. pDCControl->BitBlt(rcItem.left,rcItem.top,rcItem.Width(),rcItem.Height(),&BufferDC,0,0,SRCCOPY);
  247. //清理资源
  248. BufferDC.DeleteDC();
  249. ImageBuffer.DeleteObject();
  250. }
  251. void CSkinComboBox_ListBox::OnDestroy()
  252. {
  253. __super::OnDestroy();
  254. RenderEngine->RemoveImage(m_pBackImgN);
  255. RenderEngine->RemoveImage(m_pSelectImg);
  256. }
  257. void CSkinComboBox_ListBox::DrawListFrame()
  258. {
  259. CRect rcWindow;
  260. GetWindowRect(&rcWindow);
  261. rcWindow.OffsetRect(-rcWindow.left, -rcWindow.top);
  262. CDC *pDC = GetWindowDC();
  263. if( pDC == NULL ) pDC = GetDC();
  264. RenderEngine->DrawRect(pDC->GetSafeHdc(),rcWindow,1,m_colFrameNormal);
  265. ReleaseDC(pDC);
  266. }
  267. void CSkinComboBox_ListBox::OnRButtonUp(UINT nFlags, CPoint point)
  268. {
  269. CRect rcItem(0,0,0,0);
  270. int nIndex = -1;
  271. for (int i =0; i<GetCount(); i++)
  272. {
  273. GetItemRect(i, &rcItem);
  274. if(rcItem.PtInRect(point))
  275. {
  276. if ( m_hOwnerWnd != NULL ) nIndex = i;
  277. break;
  278. }
  279. }
  280. ::PostMessage(m_hOwnerWnd,WM_CBO_LIST_RBUTTONUP,nIndex,0);
  281. __super::OnRButtonUp(nFlags, point);
  282. }
  283. //////////////////////////////////////////////////////////////////////////
  284. // CSkinComboBox
  285. IMPLEMENT_DYNAMIC(CSkinComboBox, CComboBox)
  286. CSkinComboBox::CSkinComboBox()
  287. {
  288. m_lpBgImgN = NULL;
  289. m_lpBgImgH = NULL;
  290. m_lpArrowImgN = NULL;
  291. m_lpArrowImgH = NULL;
  292. m_lpArrowImgP = NULL;
  293. m_bFocus = m_bPress = m_bHover = m_bMouseTracking = FALSE;
  294. m_bArrowPress = FALSE;
  295. m_bArrowHover = FALSE;
  296. m_nArrowWidth = 17;
  297. m_rcArrow.SetRectEmpty();
  298. m_cyItemHeight = 15;
  299. m_nEditHeight = 20;
  300. }
  301. CSkinComboBox::~CSkinComboBox()
  302. {
  303. }
  304. BEGIN_MESSAGE_MAP(CSkinComboBox, CComboBox)
  305. ON_WM_CREATE()
  306. ON_WM_ERASEBKGND()
  307. ON_WM_PAINT()
  308. ON_WM_LBUTTONDOWN()
  309. ON_WM_MOUSEMOVE()
  310. ON_WM_MOUSELEAVE()
  311. ON_WM_DESTROY()
  312. ON_WM_SIZE()
  313. ON_MESSAGE(WM_CBO_EDIT_MOUSE_HOVER, OnEditMouseHover)
  314. ON_MESSAGE(WM_CBO_EDIT_MOUSE_LEAVE, OnEditMouseLeave)
  315. ON_MESSAGE(WM_CBO_LIST_RBUTTONUP,OnRButtonUp)
  316. ON_MESSAGE(WM_CBO_LIST_HIDE, OnListHide)
  317. ON_MESSAGE(WM_CTLCOLORLISTBOX,OnCtlColorListBox)
  318. END_MESSAGE_MAP()
  319. BOOL CSkinComboBox::SetBgNormalPic( LPCTSTR lpszFileName, RECT * lpNinePart /*= NULL*/ )
  320. {
  321. RenderEngine->RemoveImage(m_lpBgImgN);
  322. m_lpBgImgN = RenderEngine->GetImage(lpszFileName);
  323. if (m_lpBgImgN != NULL)
  324. m_lpBgImgN->SetNinePart(lpNinePart);
  325. return (m_lpBgImgN != NULL) ? TRUE : FALSE;
  326. }
  327. BOOL CSkinComboBox::SetBgHotPic( LPCTSTR lpszFileName, RECT * lpNinePart /*= NULL*/ )
  328. {
  329. RenderEngine->RemoveImage(m_lpBgImgH);
  330. m_lpBgImgH = RenderEngine->GetImage(lpszFileName);
  331. if (m_lpBgImgH != NULL)
  332. m_lpBgImgH->SetNinePart(lpNinePart);
  333. return (m_lpBgImgH != NULL) ? TRUE : FALSE;
  334. }
  335. BOOL CSkinComboBox::SetArrowNormalPic( LPCTSTR lpszFileName )
  336. {
  337. RenderEngine->RemoveImage(m_lpArrowImgN);
  338. m_lpArrowImgN = RenderEngine->GetImage(lpszFileName);
  339. return (m_lpArrowImgN != NULL) ? TRUE : FALSE;
  340. }
  341. BOOL CSkinComboBox::SetArrowHotPic( LPCTSTR lpszFileName )
  342. {
  343. RenderEngine->RemoveImage(m_lpArrowImgH);
  344. m_lpArrowImgH =RenderEngine->GetImage(lpszFileName);
  345. return (m_lpArrowImgH != NULL) ? TRUE : FALSE;
  346. }
  347. BOOL CSkinComboBox::SetArrowPushedPic( LPCTSTR lpszFileName )
  348. {
  349. RenderEngine->RemoveImage(m_lpArrowImgP);
  350. m_lpArrowImgP = RenderEngine->GetImage(lpszFileName);
  351. return (m_lpArrowImgP != NULL) ? TRUE : FALSE;
  352. }
  353. void CSkinComboBox::SetDefaultText( LPCTSTR lpszText )
  354. {
  355. m_SkinComboBoxEdit.SetDefaultText(lpszText);
  356. }
  357. BOOL CSkinComboBox::IsDefaultText()
  358. {
  359. return m_SkinComboBoxEdit.IsDefaultText();
  360. }
  361. void CSkinComboBox::SetArrowWidth( int nWidth )
  362. {
  363. m_nArrowWidth = nWidth;
  364. }
  365. // CSkinComboBox 消息处理程序
  366. void CSkinComboBox::PreSubclassWindow()
  367. {
  368. //变量定义
  369. COMBOBOXINFO ComboBoxInfo;
  370. ComboBoxInfo.cbSize=sizeof(ComboBoxInfo);
  371. //绑定控件
  372. if (GetComboBoxInfo(&ComboBoxInfo)==TRUE)
  373. {
  374. if (ComboBoxInfo.hwndItem!=NULL)
  375. {
  376. m_SkinComboBoxEdit.SetOwnerWnd(GetSafeHwnd());
  377. m_SkinComboBoxEdit.SubclassWindow(ComboBoxInfo.hwndItem);
  378. }
  379. if (ComboBoxInfo.hwndList!=NULL)
  380. {
  381. m_SkinComboBoxList.SetOwnerWnd(GetSafeHwnd());
  382. m_SkinComboBoxList.SubclassWindow(ComboBoxInfo.hwndList);
  383. }
  384. }
  385. CComboBox::PreSubclassWindow();
  386. }
  387. int CSkinComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
  388. {
  389. if (CComboBox::OnCreate(lpCreateStruct) == -1)
  390. return -1;
  391. //变量定义
  392. COMBOBOXINFO ComboBoxInfo;
  393. ComboBoxInfo.cbSize=sizeof(ComboBoxInfo);
  394. //绑定控件
  395. if (GetComboBoxInfo(&ComboBoxInfo)==TRUE)
  396. {
  397. if (ComboBoxInfo.hwndItem!=NULL)
  398. {
  399. m_SkinComboBoxEdit.SetOwnerWnd(GetSafeHwnd());
  400. m_SkinComboBoxEdit.SubclassWindow(ComboBoxInfo.hwndItem);
  401. }
  402. if (ComboBoxInfo.hwndList!=NULL)
  403. {
  404. m_SkinComboBoxList.SetOwnerWnd(GetSafeHwnd());
  405. m_SkinComboBoxList.SubclassWindow(ComboBoxInfo.hwndList);
  406. }
  407. }
  408. //设置默认字体
  409. SetFont(CFont::FromHandle(RenderEngine->GetDeaultFont()));
  410. return 0;
  411. }
  412. BOOL CSkinComboBox::OnEraseBkgnd(CDC* pDC)
  413. {
  414. return TRUE;
  415. }
  416. void CSkinComboBox::OnPaint()
  417. {
  418. CPaintDC dc(this);
  419. CRect rcClient;
  420. GetClientRect(&rcClient);
  421. CMemoryDC MemDC(&dc,rcClient);
  422. CRect rcArrow;
  423. HRGN hRgn2 = NULL;
  424. if (m_lpArrowImgN != NULL && !m_lpArrowImgN->IsNull())
  425. {
  426. int cxIcon = m_nArrowWidth;
  427. int cyIcon = m_lpArrowImgN->GetHeight();
  428. CalcCenterRect(rcClient, cxIcon, cyIcon, rcArrow);
  429. rcArrow.right = rcClient.right - 2;
  430. rcArrow.left = rcArrow.right - cxIcon;
  431. }
  432. DrawParentWndBg(GetSafeHwnd(),MemDC->GetSafeHdc());
  433. if (m_bHover)
  434. {
  435. if (m_lpBgImgH != NULL && !m_lpBgImgH->IsNull())
  436. {
  437. m_lpBgImgH->Draw(&MemDC, rcClient);
  438. }
  439. else
  440. {
  441. if (m_lpBgImgN != NULL && !m_lpBgImgN->IsNull())
  442. m_lpBgImgN->Draw(&MemDC, rcClient);
  443. }
  444. }
  445. else
  446. {
  447. if (m_lpBgImgN != NULL && !m_lpBgImgN->IsNull())
  448. m_lpBgImgN->Draw(&MemDC, rcClient);
  449. }
  450. if (m_bArrowPress)
  451. {
  452. if (m_lpArrowImgP != NULL && !m_lpArrowImgP->IsNull())
  453. m_lpArrowImgP->Draw(&MemDC, rcArrow);
  454. }
  455. else if (m_bArrowHover)
  456. {
  457. if (m_lpArrowImgH != NULL && !m_lpArrowImgH->IsNull())
  458. m_lpArrowImgH->Draw(&MemDC, rcArrow);
  459. }
  460. else
  461. {
  462. if (m_lpArrowImgN != NULL && !m_lpArrowImgN->IsNull())
  463. m_lpArrowImgN->Draw(&MemDC, rcArrow);
  464. }
  465. //绘制边框
  466. m_SkinComboBoxList.DrawListFrame();
  467. }
  468. void CSkinComboBox::OnLButtonDown(UINT nFlags, CPoint point)
  469. {
  470. //设置焦点
  471. SetFocus();
  472. //效验数据
  473. if( m_SkinComboBoxEdit.GetSafeHwnd() == NULL ) return;
  474. if ( (m_SkinComboBoxEdit.GetStyle() & ES_READONLY) == 0 )
  475. {
  476. if (VerdictOverButton(point))
  477. {
  478. m_bArrowPress = TRUE;
  479. //显示列表
  480. ShowDropDown(GetDroppedState()==FALSE);
  481. }
  482. }
  483. else
  484. {
  485. m_bArrowPress = TRUE;
  486. //显示列表
  487. ShowDropDown(GetDroppedState()==FALSE);
  488. m_SkinComboBoxEdit.SetSel(0,0);
  489. }
  490. //更新按钮
  491. RedrawWindow(&m_rcArrow,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  492. //__super::OnLButtonDown(nFlags, point);
  493. }
  494. void CSkinComboBox::OnMouseMove(UINT nFlags, CPoint point)
  495. {
  496. BOOL bRePaint = FALSE;
  497. if (!m_bMouseTracking)
  498. {
  499. TrackMouseLeave(GetSafeHwnd());
  500. m_bMouseTracking = TRUE;
  501. m_bHover = TRUE;
  502. bRePaint = TRUE;
  503. }
  504. if (VerdictOverButton(point))
  505. {
  506. if (!m_bArrowHover)
  507. {
  508. m_bArrowHover = TRUE;
  509. bRePaint = TRUE;
  510. }
  511. }
  512. else
  513. {
  514. if (m_bArrowHover)
  515. {
  516. m_bArrowHover = FALSE;
  517. bRePaint = TRUE;
  518. }
  519. }
  520. if (bRePaint)
  521. RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  522. __super::OnMouseMove(nFlags, point);
  523. }
  524. void CSkinComboBox::OnMouseLeave()
  525. {
  526. m_bMouseTracking = FALSE;
  527. if (!m_SkinComboBoxList.IsWindowVisible())
  528. {
  529. CPoint pt;
  530. GetCursorPos(&pt);
  531. CRect rcWindow;
  532. GetWindowRect(&rcWindow);
  533. if (!rcWindow.PtInRect(pt))
  534. m_bHover = FALSE;
  535. m_bArrowHover = FALSE;
  536. RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  537. }
  538. __super::OnMouseLeave();
  539. }
  540. void CSkinComboBox::OnDestroy()
  541. {
  542. __super::OnDestroy();
  543. RenderEngine->RemoveImage(m_lpBgImgN);
  544. RenderEngine->RemoveImage(m_lpBgImgH);
  545. RenderEngine->RemoveImage(m_lpArrowImgN);
  546. RenderEngine->RemoveImage(m_lpArrowImgH);
  547. RenderEngine->RemoveImage(m_lpArrowImgP);
  548. RemoveScorll();
  549. }
  550. void CSkinComboBox::OnSize(UINT nType, int cx, int cy)
  551. {
  552. __super::OnSize(nType, cx, cy);
  553. if ( m_SkinComboBoxEdit.GetSafeHwnd() == NULL ) return;
  554. CRect rcClient;
  555. GetClientRect(&rcClient);
  556. CRect rcEdit;
  557. m_SkinComboBoxEdit.GetWindowRect(&rcEdit);
  558. ScreenToClient(&rcEdit);
  559. CDC* pDC = m_SkinComboBoxEdit.GetDC();
  560. TEXTMETRIC tm = {0};
  561. pDC->GetTextMetrics(&tm);
  562. int nFontHeight = tm.tmHeight + tm.tmExternalLeading;
  563. int nMargin = (rcEdit.Height() - nFontHeight) / 2;
  564. m_SkinComboBoxEdit.ReleaseDC(pDC);
  565. rcEdit.DeflateRect(0, nMargin);
  566. rcEdit.right = rcClient.right - 2 - m_nArrowWidth;
  567. m_SkinComboBoxEdit.MoveWindow(&rcEdit, FALSE);
  568. m_rcArrow.left = rcClient.right - 2 - m_nArrowWidth;
  569. m_rcArrow.right = m_rcArrow.left + m_nArrowWidth;
  570. m_rcArrow.top = rcClient.top;
  571. m_rcArrow.bottom = rcClient.bottom;
  572. }
  573. LRESULT CSkinComboBox::OnEditMouseHover( WPARAM wParam, LPARAM lParam )
  574. {
  575. if (!m_bHover)
  576. {
  577. m_bHover = TRUE;
  578. RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  579. }
  580. return 0;
  581. }
  582. LRESULT CSkinComboBox::OnEditMouseLeave( WPARAM wParam, LPARAM lParam )
  583. {
  584. CPoint pt;
  585. GetCursorPos(&pt);
  586. CRect rcWindow;
  587. GetWindowRect(&rcWindow);
  588. if (!rcWindow.PtInRect(pt))
  589. {
  590. if (m_bHover)
  591. {
  592. m_bHover = FALSE;
  593. RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  594. }
  595. }
  596. return 0;
  597. }
  598. LRESULT CSkinComboBox::OnListHide( WPARAM wParam, LPARAM lParam )
  599. {
  600. m_bHover = FALSE;
  601. m_bArrowHover = FALSE;
  602. m_bArrowPress = FALSE;
  603. RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  604. return 0;
  605. }
  606. bool CSkinComboBox::VerdictOverButton( CPoint MousePoint )
  607. {
  608. //获取位置
  609. CRect rcClient;
  610. GetClientRect(&rcClient);
  611. //下拉列表
  612. if ((rcClient.PtInRect(MousePoint)==TRUE)&&((GetStyle()&CBS_SIMPLE)!=0)) return true;
  613. //坐标计算
  614. if ((MousePoint.y>(rcClient.Height()-1))||(MousePoint.y<1)) return false;
  615. if ((MousePoint.x<(rcClient.Width()-(INT)m_rcArrow.Width()-1))||(MousePoint.x>(rcClient.Width()-1))) return false;
  616. return true;
  617. }
  618. void CSkinComboBox::SetDropList()
  619. {
  620. //效验数据
  621. if ( m_SkinComboBoxEdit.GetSafeHwnd() == NULL ) return;
  622. //m_SkinComboBoxEdit.EnableWindow(FALSE);
  623. m_SkinComboBoxEdit.SetReadOnly();
  624. }
  625. void CSkinComboBox::SetEditTextColor( COLORREF col )
  626. {
  627. //效验数据
  628. if ( m_SkinComboBoxEdit.GetSafeHwnd() == NULL ) return;
  629. if ( (m_SkinComboBoxEdit.GetStyle() & ES_READONLY) == 0)
  630. m_SkinComboBoxEdit.m_colNormalText = col;
  631. else m_SkinComboBoxEdit.m_colReadOnlyText = col;
  632. RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
  633. }
  634. CSkinComboBox_Edit * CSkinComboBox::GetEditWnd()
  635. {
  636. if ( m_SkinComboBoxEdit.GetSafeHwnd() == NULL ) return NULL;
  637. return &m_SkinComboBoxEdit;
  638. }
  639. CSkinComboBox_ListBox * CSkinComboBox::GetListBoxWnd()
  640. {
  641. if ( m_SkinComboBoxList.GetSafeHwnd() == NULL ) return NULL;
  642. return &m_SkinComboBoxList;
  643. }
  644. void CSkinComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  645. {
  646. lpMeasureItemStruct->itemHeight = m_cyItemHeight;
  647. }
  648. void CSkinComboBox::SetAllItemHeight(UINT cyItemHeight)
  649. {
  650. m_cyItemHeight = cyItemHeight;
  651. }
  652. LRESULT CSkinComboBox::OnCtlColorListBox( WPARAM wParam, LPARAM lParam )
  653. {
  654. HWND hListBoxWnd = m_SkinComboBoxList.GetSafeHwnd();
  655. if ( hListBoxWnd == NULL)
  656. {
  657. HWND hWnd = (HWND)lParam;
  658. if (hWnd != 0 && hWnd != m_hWnd)
  659. {
  660. hListBoxWnd = hWnd;
  661. m_pWndProc = (WNDPROC)GetWindowLong(hListBoxWnd, GWL_WNDPROC);
  662. SetWindowLong(hListBoxWnd, GWL_WNDPROC, (LONG)ComboBoxListBoxProc);
  663. }
  664. }
  665. return DefWindowProc(WM_CTLCOLORLISTBOX, wParam, lParam);
  666. }
  667. BOOL CSkinComboBox::SetScrollImage( LPCTSTR pszFileName )
  668. {
  669. if ( m_SkinComboBoxList.GetSafeHwnd() == FALSE ) return FALSE;
  670. return m_SkinComboBoxList.SetScrollImage(&m_SkinComboBoxList,pszFileName);
  671. }
  672. LRESULT CSkinComboBox::OnRButtonUp( WPARAM wParam, LPARAM lParam )
  673. {
  674. CWnd *pWnd = GetParent();
  675. if ( (pWnd != NULL) && (pWnd->GetSafeHwnd() != NULL) )
  676. {
  677. pWnd->PostMessage(WM_CBO_RBUTTONUP,wParam,lParam);
  678. }
  679. return 0L;
  680. }
  681. BOOL CSkinComboBox::CreateControl(CWnd* pParentWnd)
  682. {
  683. if( !Create(WS_CHILD|WS_VISIBLE|CBS_DROPDOWN | CBS_SORT |WS_VSCROLL | WS_TABSTOP,CRect(0,0,0,0),pParentWnd,0) )
  684. return FALSE;
  685. m_pOwnWnd = this;
  686. return TRUE;
  687. }
  688. void CSkinComboBox::SetAttribute( LPCTSTR pstrName, LPCTSTR pstrValue )
  689. {
  690. ISkinControl::SetAttribute(pstrName,pstrValue);
  691. LPTSTR pstr = NULL;
  692. if( _tcscmp(pstrName, _T("droplist")) == 0 )
  693. {
  694. if( _tcscmp(pstrValue, _T("true")) == 0 )
  695. {
  696. ModifyStyle(0,CBS_OWNERDRAWVARIABLE|CBS_HASSTRINGS);
  697. SetDropList();
  698. }
  699. }
  700. else if( _tcscmp(pstrName, _T("defaulttext")) == 0 ) SetDefaultText(pstrValue);
  701. else if( _tcscmp(pstrName, _T("arrownormalimg")) == 0 ) SetArrowNormalPic(pstrValue);
  702. else if( _tcscmp(pstrName, _T("arrowhotimg")) == 0 ) SetArrowHotPic(pstrValue);
  703. else if( _tcscmp(pstrName, _T("arrowpushimg")) == 0 ) SetArrowPushedPic(pstrValue);
  704. else if( _tcscmp(pstrName, _T("arrowwidth")) == 0 ) SetArrowWidth(_ttoi(pstrValue));
  705. else if( _tcscmp(pstrName, _T("boxheight")) == 0 ) SetEditItemHeight(_ttoi(pstrValue));
  706. else if( _tcscmp(pstrName, _T("itemheight")) == 0 ) SetAllItemHeight(_ttoi(pstrValue));
  707. else if( _tcscmp(pstrName, _T("textcolor")) == 0 )
  708. {
  709. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  710. SetEditTextColor( _tcstoul(pstrValue, &pstr, 16));
  711. }
  712. else if( _tcscmp(pstrName, _T("bkcolor")) == 0 )
  713. {
  714. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  715. m_SkinComboBoxEdit.m_colBack = _tcstoul(pstrValue, &pstr, 16);
  716. }
  717. else if( _tcscmp(pstrName, _T("format")) == 0 )
  718. {
  719. m_SkinComboBoxEdit.ModifyStyle(0,_ttoi(pstrValue));
  720. }
  721. }
  722. void CSkinComboBox::ParseItem(CXmlNode *root )
  723. {
  724. if( root == NULL ) return;
  725. int nAttributes = root->GetAttributeCount();
  726. LPCTSTR pstrClass = NULL;
  727. LPCTSTR pstrName = NULL;
  728. LPCTSTR pstrValue = NULL;
  729. for( int i = 0; i < nAttributes; i++ )
  730. {
  731. pstrClass = root->GetName();
  732. pstrName = root->GetAttributeName(i);
  733. pstrValue = root->GetAttributeValue(i);
  734. if( _tcscmp(pstrClass, _T("normalimage")) == 0 )
  735. {
  736. if( _tcscmp(pstrName, _T("value")) == 0 ) SetBgNormalPic(pstrValue);
  737. else if( _tcscmp(pstrName, _T("ninepart")) == 0 )
  738. {
  739. LPTSTR pstr = NULL;
  740. CRect rc;
  741. rc.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  742. rc.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  743. rc.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  744. rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  745. if (m_lpBgImgN != NULL)
  746. m_lpBgImgN->SetNinePart(&rc);
  747. }
  748. }
  749. else if( _tcscmp(pstrClass, _T("hotimage")) == 0 )
  750. {
  751. if( _tcscmp(pstrName, _T("value")) == 0 ) SetBgHotPic(pstrValue);
  752. else if( _tcscmp(pstrName, _T("ninepart")) == 0 )
  753. {
  754. LPTSTR pstr = NULL;
  755. CRect rc;
  756. rc.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  757. rc.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  758. rc.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  759. rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  760. if (m_lpBgImgH != NULL)
  761. m_lpBgImgH->SetNinePart(&rc);
  762. }
  763. }
  764. else if( _tcscmp(pstrClass, _T("item")) == 0 )
  765. {
  766. if( _tcscmp(pstrName, _T("text")) == 0 ) AddString(pstrValue);
  767. }
  768. else if( _tcscmp(pstrClass, _T("ListBox")) == 0 )
  769. {
  770. LPTSTR pstr = NULL;
  771. CSkinComboBox_ListBox *pListBox = GetListBoxWnd();
  772. if ( pListBox != NULL && pListBox->GetSafeHwnd() != NULL )
  773. {
  774. if( _tcscmp(pstrName, _T("selectimage")) == 0 ) pListBox->SetSelectImage(pstrValue);
  775. else if( _tcscmp(pstrName, _T("ninepart")) == 0 )
  776. {
  777. CRect rc;
  778. rc.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  779. rc.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  780. rc.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  781. rc.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  782. if (pListBox->m_pSelectImg != NULL)
  783. pListBox->m_pSelectImg->SetNinePart(&rc);
  784. }
  785. }
  786. else if( _tcscmp(pstrName, _T("framecolor")) == 0 )
  787. {
  788. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  789. SetFrameColor( _tcstoul(pstrValue, &pstr, 16));
  790. }
  791. }
  792. }
  793. }