ComboBtn.cpp
上传用户:xyjtyn
上传日期:2007-01-01
资源大小:85k
文件大小:19k
源码类别:

按钮控件

开发平台:

Visual C++

  1. // colorctl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ComboBtn.h"
  5. void FillSolidRect(CDC * pDC, int x, int y, int cx, int cy, COLORREF clr)
  6. {
  7. ASSERT(pDC != NULL);
  8. ASSERT(pDC->m_hDC != NULL);
  9. COLORREF oldColor = pDC->SetBkColor(clr);
  10. CRect rect(x, y, x + cx, y + cy);
  11. pDC->ExtTextOut(0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
  12. pDC->SetBkColor(oldColor);
  13. }
  14. void Draw3dRect(CDC * pDC, int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
  15. {
  16. FillSolidRect(pDC, x, y, cx - 1, 1, clrTopLeft);
  17. FillSolidRect(pDC, x, y, 1, cy - 1, clrTopLeft);
  18. FillSolidRect(pDC, x + cx, y, -1, cy, clrBottomRight);
  19. FillSolidRect(pDC, x, y + cy, cx, -1, clrBottomRight);
  20. }
  21. void Draw3dRect(CDC * pDC, LPCRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight)
  22. {
  23. Draw3dRect(pDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left, 
  24. lpRect->bottom - lpRect->top, clrTopLeft, clrBottomRight);
  25. }
  26. #ifdef _DEBUG
  27. #undef THIS_FILE
  28. static char BASED_CODE THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CButtonCombo
  32. CButtonCombo::CButtonCombo()
  33. {
  34. m_bLeftBtnDown  = m_bRightBtnDown = FALSE;
  35. m_bLeftBtnOut  = FALSE;
  36. m_lItem = m_iRealItem = 0;
  37. }
  38. CButtonCombo::~CButtonCombo()
  39. {    
  40. m_wndColor.DestroyWindow();
  41. }
  42. BEGIN_MESSAGE_MAP(CButtonCombo, CWnd)
  43. //{{AFX_MSG_MAP(CButtonCombo)
  44. ON_WM_LBUTTONDOWN()
  45. ON_WM_LBUTTONUP()
  46. ON_WM_MOUSEMOVE()
  47. ON_WM_PAINT()
  48. ON_WM_ENABLE()
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CButtonCombo message handlers
  53. // rect  = size of the button
  54. // sizeGrid  = size of grid containing the color (x rectangles by y rectangles)
  55. // sizeBtn  = size of ech rectangle in the grid containing the color
  56. // pParent = parent of the button
  57. // nID = ID of the button
  58. // sTextItems = (optional) text to show when selecting a color
  59. BOOL CButtonCombo::Create(CRect rect, CSize sizeGrid, CSize sizeBtn, CWnd * pParent, 
  60. UINT nID, CString sTextItems)
  61. {              
  62. ASSERT(pParent);
  63. m_pParent = pParent;
  64. m_nID  = nID;
  65. if(!CWnd::Create(NULL, NULL, WS_BORDER | WS_VISIBLE | WS_CHILD, rect, m_pParent, m_nID))
  66. return FALSE;
  67. if (!m_wndColor.Create(rect.left, rect.bottom, sizeGrid, sizeBtn, this, sTextItems))
  68. return FALSE;        
  69. return TRUE;
  70. }
  71. // rect  = size of the button
  72. // sizeGrid  = size of grid containing the color (x rectangles by y rectangles)
  73. // nBitmapIDResource  = ID of the bitmap
  74. // iNbImages = number of images in the bitmap
  75. // nID = ID of the button
  76. // sTextItems = (optional) text to show when selecting a color
  77. BOOL CButtonCombo::Create(CRect rect, CSize sizeGrid, UINT nBitmapIDResource, int iNbImages, 
  78. CWnd * pParent, UINT nID, CString sTextItems)
  79. {
  80. ASSERT(pParent);
  81. m_pParent = pParent;
  82. m_nID  = nID;
  83. if(!CWnd::Create(NULL, NULL, WS_BORDER | WS_VISIBLE | WS_CHILD, rect, m_pParent, m_nID))
  84. return FALSE;
  85. if (!m_wndColor.Create(rect.left, rect.bottom, sizeGrid, nBitmapIDResource, 
  86. iNbImages, this, sTextItems))
  87. return FALSE;
  88. return TRUE;
  89. }
  90. void CButtonCombo::OnItemSelected(long lItem, int iRealItem)
  91. {                  
  92. m_lItem  = lItem;
  93. m_iRealItem    = iRealItem;
  94. m_bLeftBtnDown  = FALSE;
  95. m_bRightBtnDown = FALSE;
  96. m_wndColor.SetSelectedItem(m_iRealItem);
  97. m_wndColor.ShowWindow(m_bRightBtnDown ? SW_SHOW : SW_HIDE);
  98. Invalidate(FALSE);
  99. UpdateWindow();
  100. if (m_lItem >= 0)
  101. {                
  102. if (m_pParent->GetOwner()->IsKindOf(RUNTIME_CLASS(CDialog)))
  103. m_pParent->GetOwner()->SendMessage(WM_USER+21, (WPARAM)m_nID, (LPARAM)m_lItem);
  104. else
  105. AfxGetMainWnd()->SendMessageToDescendants(WM_USER+21, (WPARAM)m_nID, (LPARAM)m_lItem, TRUE, TRUE);
  106. }
  107. }
  108. void CButtonCombo::UnpressRightButton() 
  109. {
  110.     m_bRightBtnDown = FALSE;
  111.     Invalidate(FALSE);
  112.     UpdateWindow();
  113. }
  114. void CButtonCombo::OnLButtonDown(UINT nFlags, CPoint point) 
  115. {         
  116. CRect rect;
  117. GetClientRect(&rect);
  118. CRect rcLeft(rect.left, rect.top, rect.right - 15, rect.bottom);
  119. CRect rcRight(rect.right - 15, rect.top, rect.right, rect.bottom);
  120. if (rcLeft.PtInRect(point))
  121. {             
  122. m_bLeftBtnDown = TRUE;
  123. m_bRightBtnDown = FALSE;
  124. }
  125. else if (rcRight.PtInRect(point))
  126. m_bRightBtnDown ^= 1;
  127. else              
  128. m_bLeftBtnDown  = m_bRightBtnDown = FALSE;
  129.   
  130.    GetWindowRect(rect);
  131. m_wndColor.SetWindowPos(NULL, rect.left, rect.bottom, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  132. m_wndColor.SetSelectedItem(m_iRealItem);
  133. m_wndColor.ShowWindow(m_bRightBtnDown ? SW_SHOW : SW_HIDE);
  134. Invalidate(FALSE);
  135. UpdateWindow();
  136. SetCapture();
  137.  
  138. CWnd::OnLButtonDown(nFlags, point);
  139. }
  140. void CButtonCombo::OnLButtonUp(UINT nFlags, CPoint point) 
  141. {       
  142.     ReleaseCapture();
  143. if (m_bLeftBtnDown)
  144. OnItemSelected(m_lItem, m_iRealItem);
  145. m_bLeftBtnDown = FALSE;
  146. Invalidate(FALSE);
  147. UpdateWindow();
  148. CWnd::OnLButtonUp(nFlags, point);
  149. }
  150. void CButtonCombo::OnMouseMove(UINT nFlags, CPoint point) 
  151. {
  152. BOOL bOut = m_bLeftBtnOut;
  153. CRect rect;
  154. GetClientRect(&rect);
  155. CRect rcLeft(rect.left, rect.top, rect.right - 15, rect.bottom);
  156. if (rcLeft.PtInRect(point))
  157. m_bLeftBtnOut = FALSE;
  158. else
  159. m_bLeftBtnOut  = TRUE;
  160. if (bOut != m_bLeftBtnOut)
  161. {
  162. Invalidate(FALSE);
  163. UpdateWindow();
  164. }
  165. CWnd::OnMouseMove(nFlags, point);
  166. }
  167. void CButtonCombo::OnPaint() 
  168. {
  169. CPaintDC dc(this); // device context for painting
  170. CRect rect;
  171. GetClientRect(&rect);
  172. CRect rcLeft(rect.left, rect.top, rect.right - 15, rect.bottom-1);
  173. CRect rcRight(rect.right - 15, rect.top, rect.right - 1, rect.bottom-1);
  174. CDC memDC;
  175. CBitmap bitmap;
  176. bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
  177. memDC.CreateCompatibleDC(&dc);
  178. CBitmap * pOldBitmap = memDC.SelectObject(&bitmap);
  179. CBrush brBack(GetSysColor(COLOR_BTNFACE));
  180. memDC.FillRect(rect, &brBack);
  181. Draw3dRect(&memDC, &rect, GetSysColor(COLOR_BTNHIGHLIGHT), GetSysColor(COLOR_BTNSHADOW)); 
  182. CRect rcImage(rcLeft.left+1, rcLeft.top+1, rcLeft.right-1, rcLeft.bottom-1);
  183. if (!m_bLeftBtnDown || m_bLeftBtnOut) // Draw Button Up
  184. Draw3dRect(&memDC, &rcLeft, GetSysColor(COLOR_BTNHIGHLIGHT), GetSysColor(COLOR_BTNSHADOW)); 
  185. else
  186. {
  187. rcLeft.SetRect(rcLeft.left, rcLeft.top, rcLeft.right+1, rcLeft.bottom+1);
  188. Draw3dRect(&memDC, &rcLeft, GetSysColor(COLOR_BTNSHADOW), GetSysColor(COLOR_BTNFACE)); 
  189. rcImage.OffsetRect(1, 1);
  190. }
  191. m_wndColor.DrawBtnFace(&memDC, rcImage, m_lItem);
  192. if (!m_bRightBtnDown) // Draw Button Up
  193. Draw3dRect(&memDC, &rcRight, GetSysColor(COLOR_BTNHIGHLIGHT), GetSysColor(COLOR_BTNSHADOW)); 
  194. else
  195. {
  196. rcRight.SetRect(rcRight.left, rcRight.top, rcRight.right+1, rcRight.bottom+1);
  197. Draw3dRect(&memDC, &rcRight, GetSysColor(COLOR_BTNSHADOW), GetSysColor(COLOR_BTNFACE)); 
  198. rcRight.OffsetRect(1, 1);
  199. }
  200. DrawArrow(&memDC, &rcRight);
  201.     
  202.     if (!IsWindowEnabled())
  203. DrawDisabled(&memDC, rect);
  204. dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, rect.left, rect.top, SRCCOPY);
  205. memDC.SelectObject(pOldBitmap);
  206. brBack.DeleteObject();
  207. bitmap.DeleteObject();
  208. }
  209. void CButtonCombo::DrawDisabled( CDC * pDC, const CRect & rc )
  210. {
  211. // create a monochrome memory DC
  212. CDC memDC;
  213. memDC.CreateCompatibleDC(0);
  214. CBitmap bmp;
  215. bmp.CreateCompatibleBitmap(&memDC, rc.Width(), rc.Height());
  216. CBitmap * pOldBmp = memDC.SelectObject(&bmp);
  217. // build a mask
  218. memDC.PatBlt(0, 0, rc.Width(), rc.Height(), WHITENESS);
  219. pDC->SetBkColor(GetSysColor(COLOR_BTNFACE));
  220. memDC.BitBlt(0, 0, rc.Width(), rc.Height(), pDC, rc.left, rc.top, SRCCOPY);
  221. pDC->SetBkColor(GetSysColor(COLOR_BTNHIGHLIGHT));
  222. memDC.BitBlt(0, 0, rc.Width(), rc.Height(), pDC, rc.left, rc.top, SRCPAINT);
  223. // Copy the image from the toolbar into the memory DC
  224. // and draw it (grayed) back into the toolbar.
  225. CBrush  brShadow(GetSysColor(COLOR_BTNSHADOW)), 
  226. brHilight(GetSysColor(COLOR_BTNHIGHLIGHT)), 
  227. brFace(GetSysColor(COLOR_BTNFACE));
  228. // pDC->FillRect(rc, &brFace);
  229. pDC->SetBkColor(RGB(0, 0, 0));
  230. pDC->SetTextColor(RGB(255, 255, 255));
  231. CBrush * pOldBrush = pDC->SelectObject(&brHilight);
  232. pDC->BitBlt(rc.left+1, rc.top+1, rc.Width(), rc.Height(), &memDC, 0, 0, 0x00E20746L);
  233. pDC->SelectObject(&brShadow);
  234. pDC->BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &memDC, 0, 0, 0x00E20746L);
  235. // reset DCs
  236. pDC->SelectObject(pOldBrush);
  237. memDC.SelectObject(pOldBmp);
  238. memDC.DeleteDC();
  239. brShadow.DeleteObject();
  240. brHilight.DeleteObject();
  241. brFace.DeleteObject();
  242. bmp.DeleteObject();
  243. }
  244. void CButtonCombo::DrawArrow(CDC * pDC, CRect * pRect)
  245. {
  246. ASSERT(pRect);
  247. CPoint ptCenter(pRect->left + (pRect->Width() / 2), pRect->top + (pRect->Height() / 2));
  248. CPoint ptArrow[7];
  249. ptArrow[0].x = ptCenter.x-1;
  250. ptArrow[0].y = ptCenter.y-4;
  251. ptArrow[1].x = ptCenter.x-1;
  252. ptArrow[1].y = ptCenter.y-1;
  253. ptArrow[2].x = ptCenter.x-3;
  254. ptArrow[2].y = ptCenter.y-1;
  255. ptArrow[3].x = ptCenter.x;
  256. ptArrow[3].y = ptCenter.y+2;
  257. ptArrow[4].x = ptCenter.x+3;
  258. ptArrow[4].y = ptCenter.y-1;
  259. ptArrow[5].x = ptCenter.x+1;
  260. ptArrow[5].y = ptCenter.y-1;
  261. ptArrow[6].x = ptCenter.x+1;
  262. ptArrow[6].y = ptCenter.y-4;
  263. CBrush brBlack(RGB(0, 0, 0));
  264.    CBrush * pOldBrush = pDC->SelectObject(&brBlack);
  265. pDC->Polygon(ptArrow, 7);  
  266. pDC->MoveTo(ptCenter.x - 3, ptCenter.y + 4);
  267. pDC->LineTo(ptCenter.x + 4, ptCenter.y + 4);
  268.    pDC->SelectObject(pOldBrush);
  269. brBlack.DeleteObject();
  270. }
  271. void CButtonCombo::OnEnable(BOOL bEnable) 
  272. {
  273. CWnd::OnEnable(bEnable);
  274. Invalidate(FALSE);
  275. UpdateWindow();
  276. }
  277. /////////////////////////////////////////////////////////////////////////////
  278. // CWndCombo
  279. CWndCombo::CWndCombo()
  280. {         
  281. m_rgbBkColor  = GetSysColor(COLOR_BTNFACE);
  282. m_iSelectedItem = 0;
  283. m_iNbImages = -1; 
  284. }
  285. CWndCombo::~CWndCombo()
  286. {       
  287. if (m_bmImage.m_hObject)
  288. m_bmImage.DeleteObject();
  289. if (m_font.m_hObject)
  290. m_font.DeleteObject();
  291. }
  292. BEGIN_MESSAGE_MAP(CWndCombo, CWnd)
  293. //{{AFX_MSG_MAP(CWndCombo)
  294. ON_WM_LBUTTONDOWN()
  295. ON_WM_LBUTTONUP()
  296. ON_WM_MOUSEMOVE()
  297. ON_WM_PAINT()
  298. ON_WM_KILLFOCUS()
  299. //}}AFX_MSG_MAP
  300. END_MESSAGE_MAP()
  301. /////////////////////////////////////////////////////////////////////////////
  302. // CWndCombo message handlers
  303. BOOL CWndCombo::Create(int x, int y, CSize sizeGrid, CSize sizeBtn, CWnd * pParent, CString sTextItems)
  304. {
  305. ASSERT(pParent);  
  306. if (m_hWnd) return FALSE;
  307. m_sizeGrid = sizeGrid;
  308. m_sizeBtn  = sizeBtn;
  309. m_pParent  = pParent;
  310. m_iNbImages = m_sizeGrid.cx * m_sizeGrid.cy;
  311. CSize sizeText = UpdateTextItems(sTextItems);
  312. int iWidth = (m_sizeGrid.cx * m_sizeBtn.cx) + (m_sizeGrid.cx - 1) + 4; 
  313. int iRow = (sizeText.cy > 0) ? 0 : 1;
  314. int iHeight = (m_sizeGrid.cy * m_sizeBtn.cy) + (m_sizeGrid.cy - iRow) + 4 + sizeText.cy; 
  315. CRect rect(x, y, x + iWidth, y + iHeight);
  316. return CWnd::CreateEx(0, NULL, NULL, WS_CHILD | WS_POPUP, rect.left, rect.top, 
  317. rect.Width(), rect.Height(), m_pParent->m_hWnd, 0);
  318. }
  319. BOOL CWndCombo::Create(int x, int y, CSize sizeGrid, UINT nBitmapIDResource, int iNbImages, 
  320. CWnd * pParent, CString sTextItems)
  321. {
  322. ASSERT(pParent);  
  323. if (m_hWnd) return FALSE;
  324. m_pParent  = pParent;
  325. m_sizeGrid = sizeGrid;
  326. m_iNbImages = iNbImages;
  327. m_bmImage.LoadBitmap(nBitmapIDResource);
  328. BITMAP bm;
  329. m_bmImage.GetObject( sizeof( bm ), &bm );
  330. m_sizeBtn  = CSize(bm.bmWidth / m_iNbImages, bm.bmHeight);
  331. CSize sizeText = UpdateTextItems(sTextItems);
  332. int iWidth = (m_sizeGrid.cx * (m_sizeBtn.cx + 8)) + (m_sizeGrid.cx - 1) + 4; 
  333. int iRow = (sizeText.cy > 0) ? 0 : 1;
  334. int iHeight = (m_sizeGrid.cy * (m_sizeBtn.cy + 8)) + (m_sizeGrid.cy - iRow) + 4 + sizeText.cy; 
  335. CRect rect(x, y, x + iWidth, y + iHeight);
  336. return CWnd::CreateEx(0, NULL, NULL, WS_CHILD | WS_POPUP, rect.left, rect.top, 
  337. rect.Width(), rect.Height(), m_pParent->m_hWnd, 0);
  338. }
  339. CSize CWndCombo::UpdateTextItems(CString sTextItems)
  340. {
  341. if (sTextItems.IsEmpty())
  342. return CSize(0, 0);
  343. int i;
  344. m_lstTextItems.RemoveAll;
  345. while ((i = sTextItems.Find(';')) != -1)
  346. {
  347. m_lstTextItems.Add(sTextItems.Left(i));
  348. sTextItems = sTextItems.Mid(i+1);
  349. }
  350. m_lstTextItems.Add(sTextItems);
  351. CDC *pDC = GetDC();
  352. LOGFONT logfont;
  353. memset(&logfont, 0, sizeof(logfont));
  354. // 8 point height Sans Serif font
  355. logfont.lfHeight = -MulDiv(8, pDC->GetDeviceCaps(LOGPIXELSY), 72);
  356. logfont.lfWeight = FW_NORMAL;
  357. logfont.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
  358. static char BASED_CODE szFaceName[] = "MS Sans Serif";
  359. lstrcpy(logfont.lfFaceName, szFaceName);
  360. m_font.CreateFontIndirect(&logfont);
  361.     CSize sizeText = pDC->GetTextExtent("Test", 4);
  362.     ReleaseDC(pDC);
  363. return sizeText;
  364. }
  365. void CWndCombo::OnLButtonDown(UINT nFlags, CPoint point) 
  366. {
  367. CWnd::OnLButtonDown(nFlags, point);
  368. }
  369. void CWndCombo::OnLButtonUp(UINT nFlags, CPoint point) 
  370. {
  371. if (m_iSelectedItem != -1)
  372. {
  373. if (m_bmImage.m_hObject)
  374. ((CButtonCombo*)m_pParent)->OnItemSelected(m_iSelectedItem, m_iSelectedItem);
  375. else
  376. ((CButtonCombo*)m_pParent)->OnItemSelected(GetColor(m_iSelectedItem), m_iSelectedItem);
  377. }
  378. m_iSelectedItem = -1;
  379. ShowWindow(SW_HIDE);
  380. CWnd::OnLButtonUp(nFlags, point);
  381. }
  382. void CWndCombo::OnMouseMove(UINT nFlags, CPoint point) 
  383. {
  384. CRect rcItem;
  385. int iItem = GetItem(point, &rcItem);
  386. if (iItem != -1)
  387. m_iSelectedItem = iItem;
  388. Invalidate();
  389. UpdateWindow();
  390.                                     
  391. CWnd::OnMouseMove(nFlags, point);
  392. }
  393. int CWndCombo::GetItem(CPoint point, CRect * pRect)
  394. {
  395. ASSERT(pRect);
  396. pRect->SetRectEmpty();
  397. CRect rect;
  398. GetClientRect(rect);
  399. if (!rect.PtInRect(point))
  400. return -1;
  401. int iTop = rect.top + 2, iLeft, iItem;
  402. CRect rcBtn;
  403. for (int i = 0; i < m_sizeGrid.cy; i++)
  404.     {   
  405. rcBtn.top  = iTop;
  406. if (m_bmImage.m_hObject)
  407. rcBtn.bottom = rcBtn.top + m_sizeBtn.cy + 8;
  408. else
  409. rcBtn.bottom = rcBtn.top + m_sizeBtn.cy;
  410. iTop  = rcBtn.bottom + 2;
  411. if (point.y > rcBtn.bottom)
  412. continue;
  413.         else        
  414.         {
  415. iLeft = rect.left + 2;
  416. for (int j = 0; j < m_sizeGrid.cx; j++)
  417. {        
  418.      rcBtn.left = iLeft;
  419. if (m_bmImage.m_hObject)
  420. rcBtn.right = rcBtn.left + m_sizeBtn.cx + 8;
  421. else
  422. rcBtn.right = rcBtn.left + m_sizeBtn.cx;
  423. iLeft = rcBtn.right + 2;
  424. if (point.x > rcBtn.right)
  425. continue;             
  426. else              
  427. {
  428. *pRect = rcBtn;      
  429. iItem = (i * m_sizeGrid.cx) + j;
  430. if (iItem < m_iNbImages)
  431. return iItem;
  432. else
  433. return -1;
  434. }
  435. }                   
  436. }
  437. return -1;            
  438. }
  439. void CWndCombo::DrawBtnFace(CDC * pDC, CRect rect, long lItem)
  440. {            
  441. if (lItem == -1)
  442. return;
  443. if (m_bmImage.m_hObject)
  444. {            
  445. CPoint ptCenter(rect.left + (rect.Width() / 2), rect.top + (rect.Height() / 2));
  446. CPoint ptOffSet(ptCenter.x - (m_sizeBtn.cx / 2), ptCenter.y - (m_sizeBtn.cy / 2));
  447. CDC memDC;
  448. memDC.CreateCompatibleDC(pDC);
  449. CBitmap * pOldBitmap = memDC.SelectObject(&m_bmImage);
  450. pDC->BitBlt(ptOffSet.x, ptOffSet.y, m_sizeBtn.cx, m_sizeBtn.cy, &memDC, 
  451. (int) lItem * m_sizeBtn.cx, 0, SRCCOPY);
  452. memDC.SelectObject(pOldBitmap);
  453.     }
  454. else 
  455. {
  456. CBrush brRect(lItem);
  457. pDC->FillRect(CRect(rect.left+2, rect.top+2, rect.right-3, rect.bottom-2), &brRect);
  458. brRect.DeleteObject();
  459. }
  460. }
  461. void CWndCombo::OnPaint() 
  462. {
  463. CPaintDC dc(this); // device context for painting
  464. CRect rect;
  465. GetClientRect(&rect);
  466. CDC memDC;
  467. CBitmap bitmap;
  468. bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
  469. memDC.CreateCompatibleDC(&dc);
  470. CBitmap * pOldBitmap = memDC.SelectObject(&bitmap);
  471. CBrush brBack(m_rgbBkColor), brBlack(RGB(0, 0, 0));
  472. memDC.FillRect(rect, &brBack);
  473. Draw3dRect(&memDC, &rect, GetSysColor(COLOR_BTNHIGHLIGHT), GetSysColor(COLOR_WINDOWFRAME)); 
  474. int iTop = rect.top + 2, iLeft, iItem;
  475. CRect rcBtn;
  476. for (int i = 0; i < m_sizeGrid.cy; i++)
  477.     {        
  478. rcBtn.top  = iTop;
  479. if (m_bmImage.m_hObject)
  480. rcBtn.bottom = rcBtn.top + m_sizeBtn.cy + 8;
  481. else
  482. rcBtn.bottom = rcBtn.top + m_sizeBtn.cy;
  483. iLeft  = rect.left + 2;
  484. for (int j = 0; j < m_sizeGrid.cx; j++)
  485. {
  486.      rcBtn.left = iLeft;
  487. iItem  = (i * m_sizeGrid.cx) + j; 
  488. if (m_bmImage.m_hObject)
  489. {            
  490. rcBtn.right = rcBtn.left + m_sizeBtn.cx + 8;
  491. if (iItem < m_iNbImages)
  492. {
  493. CPoint ptCenter(rcBtn.left + (rcBtn.Width() / 2), rcBtn.top + (rcBtn.Height() / 2));
  494. CPoint ptOffSet(ptCenter.x - (m_sizeBtn.cx / 2), ptCenter.y - (m_sizeBtn.cy / 2));
  495. CDC tmpDC;
  496. tmpDC.CreateCompatibleDC(&dc);
  497. CBitmap * pOldBitmap = tmpDC.SelectObject(&m_bmImage);
  498. memDC.BitBlt(ptOffSet.x, ptOffSet.y, m_sizeBtn.cx, m_sizeBtn.cy,
  499. &tmpDC, iItem * m_sizeBtn.cx, 0, SRCCOPY);
  500. tmpDC.SelectObject(pOldBitmap);
  501. Draw3dRect(&memDC, &rcBtn, GetSysColor(COLOR_BTNSHADOW), GetSysColor(COLOR_BTNHIGHLIGHT)); 
  502. }
  503.             }
  504.             else
  505.             {
  506. rcBtn.right = rcBtn.left + m_sizeBtn.cx;
  507. Draw3dRect(&memDC, &rcBtn, GetSysColor(COLOR_BTNSHADOW), GetSysColor(COLOR_BTNHIGHLIGHT)); 
  508. CBrush brRect(GetColor(iItem));
  509. memDC.FillRect(CRect(rcBtn.left + 1, rcBtn.top + 1, rcBtn.right-1, rcBtn.bottom-1), &brRect);
  510. brRect.DeleteObject();
  511. }
  512. if (iItem == m_iSelectedItem)
  513. memDC.FrameRect(CRect(rcBtn.left-1, rcBtn.top-1, rcBtn.right+1, rcBtn.bottom+1), &brBlack);
  514. iLeft = rcBtn.right + 1;
  515. }
  516. iTop = rcBtn.bottom + 1;
  517. }
  518. if (m_lstTextItems.GetSize() > 0)
  519. {                  
  520. CRect rcText(rect.left + 2, iTop, rect.right - 2, rect.bottom - 2);
  521. CBrush brWhite(RGB(255, 255, 255));
  522. memDC.FillRect(rcText, &brWhite);
  523. Draw3dRect(&memDC, &rcText, GetSysColor(COLOR_BTNSHADOW), GetSysColor(COLOR_BTNHIGHLIGHT)); 
  524. brWhite.DeleteObject();
  525.         if (m_iSelectedItem != -1)
  526.         {   
  527.          CFont * pOldFont = memDC.SelectObject(&m_font);
  528. memDC.DrawText(m_lstTextItems[m_iSelectedItem], -1, rcText, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
  529.          memDC.SelectObject(pOldFont);
  530. }
  531. }
  532. dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, rect.left, rect.top, SRCCOPY);
  533. memDC.SelectObject(pOldBitmap);
  534. brBack.DeleteObject();
  535. brBlack.DeleteObject();
  536. bitmap.DeleteObject();
  537. }
  538. void CWndCombo::OnKillFocus(CWnd* pNewWnd) 
  539. {
  540. CWnd::OnKillFocus(pNewWnd);
  541. POINT pt;
  542. GetCursorPos(&pt);
  543. CRect rect;
  544. m_pParent->GetWindowRect(rect);
  545. if (!rect.PtInRect(pt))
  546. {
  547. ShowWindow(SW_HIDE);
  548.     CButtonCombo * pBtnColor = (CButtonCombo*)m_pParent;
  549.     pBtnColor->UnpressRightButton();
  550. }
  551. }
  552. COLORREF CWndCombo::GetColor(int iIndex)
  553. {
  554. const int  NUMENTRIES = 255;
  555. PALETTEENTRY palette[NUMENTRIES];       
  556. HDC hDC = ::GetDC(NULL);
  557. UINT uResult = GetSystemPaletteEntries(hDC, 0, NUMENTRIES, palette);        
  558. ::ReleaseDC(NULL, hDC);
  559. return RGB(palette[iIndex].peRed, palette[iIndex].peGreen, palette[iIndex].peBlue);
  560. }