GdipButton.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. //
  2. // GdipButton.cpp : Version 1.0 - see article at CodeProject.com
  3. //
  4. // Author: Darren Sessions
  5. //
  6. //
  7. // Description:
  8. // GdipButton is a CButton derived control that uses GDI+
  9. // to support alternate image formats
  10. //
  11. // History
  12. // Version 1.0 - 2008 June 10
  13. // - Initial public release
  14. //
  15. // License:
  16. // This software is released under the Code Project Open License (CPOL),
  17. // which may be found here: http://www.codeproject.com/info/eula.aspx
  18. // You are free to use this software in any way you like, except that you
  19. // may not sell this source code.
  20. //
  21. // This software is provided "as is" with no expressed or implied warranty.
  22. // I accept no liability for any damage or loss of business that this
  23. // software may cause.
  24. //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. #include "stdafx.h"
  27. #include "GdipButton.h"
  28. #include "CGdiPlusBitmap.h"
  29. #include "MemDC.h"
  30. #include "CP_Main.h"
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CGdipButton
  38. CGdipButton::CGdipButton()
  39. {
  40. m_pStdImage = NULL;
  41. m_pAltImage = NULL;
  42. m_bHaveBitmaps = FALSE;
  43. m_bHaveAltImage = FALSE;
  44. m_pCurBtn = NULL;
  45. m_bIsDisabled = FALSE;
  46. m_bIsToggle = FALSE;
  47. m_bIsHovering = FALSE;
  48. m_bIsTracking = FALSE;
  49. m_nCurType = STD_TYPE;
  50. m_pToolTip = NULL;
  51. }
  52. CGdipButton::~CGdipButton()
  53. {
  54. if(m_pStdImage) delete m_pStdImage;
  55. if(m_pAltImage) delete m_pAltImage;
  56. if(m_pToolTip) delete m_pToolTip;
  57. }
  58. BEGIN_MESSAGE_MAP(CGdipButton, CButton)
  59. //{{AFX_MSG_MAP(CGdipButton)
  60. ON_WM_DRAWITEM()
  61. ON_WM_ERASEBKGND()
  62. ON_WM_CTLCOLOR_REFLECT()
  63. ON_WM_MOUSEMOVE()
  64. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  65. ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
  66. //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68. BOOL CGdipButton::LoadStdImageDPI(int dpi, UINT id96, UINT id120, UINT id144, UINT id168, UINT id192, LPCTSTR pType, UINT id225, UINT id250, UINT id275, UINT id300, UINT id325, UINT id350)
  69. {
  70. BOOL ret = FALSE;
  71. if (dpi >= 336 && id350 != 0)
  72. {
  73. ret = LoadStdImage(id350, pType);
  74. }
  75. else if (dpi >= 312 && id325 != 0)
  76. {
  77. ret = LoadStdImage(id325, pType);
  78. }
  79. else if (dpi >= 288 && id300 != 0)
  80. {
  81. ret = LoadStdImage(id300, pType);
  82. }
  83. else if (dpi >= 264 && id275 != 0)
  84. {
  85. ret = LoadStdImage(id275, pType);
  86. }
  87. else if (dpi >= 240 && id250 != 0)
  88. {
  89. ret = LoadStdImage(id250, pType);
  90. }
  91. else if (dpi >= 216 && id225 != 0)
  92. {
  93. ret = LoadStdImage(id225, pType);
  94. }
  95. else if (dpi >= 192)
  96. {
  97. ret = LoadStdImage(id192, pType);
  98. }
  99. else if (dpi >= 168)
  100. {
  101. ret = LoadStdImage(id168, pType);
  102. }
  103. else if (dpi >= 144)
  104. {
  105. ret = LoadStdImage(id144, pType);
  106. }
  107. else if (dpi >= 120)
  108. {
  109. ret = LoadStdImage(id120, pType);
  110. }
  111. else
  112. {
  113. ret = LoadStdImage(id96, pType);
  114. }
  115. return ret;
  116. }
  117. //=============================================================================
  118. //
  119. // LoadStdImage()
  120. //
  121. // Purpose: The LoadStdImage() Loads the image for the button. This
  122. // function must be called at a minimum or the button wont do
  123. // anything.
  124. //
  125. // Parameters:
  126. // [IN] id
  127. // resource id, one of the resources already imported with the
  128. // resource editor, usually begins with IDR_
  129. //
  130. // [IN] pType
  131. // pointer to string describing the resource type
  132. //
  133. // Returns: BOOL
  134. // Non zero if successful, otherwise zero
  135. //
  136. //=============================================================================
  137. BOOL CGdipButton::LoadStdImage(UINT id, LPCTSTR pType)
  138. {
  139. m_pStdImage = new CGdiPlusBitmapResource;
  140. return m_pStdImage->Load(id, pType);
  141. }
  142. void CGdipButton::Reset()
  143. {
  144. delete m_pStdImage;
  145. m_pStdImage = NULL;
  146. delete m_pAltImage;
  147. m_pAltImage = NULL;
  148. delete m_pToolTip;
  149. m_pToolTip = NULL;
  150. m_bHaveBitmaps = FALSE;
  151. m_bHaveAltImage = FALSE;
  152. m_dcStd.DeleteDC();
  153. m_dcStdP.DeleteDC();
  154. m_dcStdH.DeleteDC();
  155. m_dcBk.DeleteDC();
  156. }
  157. void CGdipButton::Test(CString c)
  158. {
  159. m_pStdImage = new CGdiPlusBitmapResource;
  160. m_pStdImage->Loads(c);
  161. }
  162. //=============================================================================
  163. //
  164. // LoadAltImage()
  165. //
  166. // Purpose: The LoadAltImage() Loads the altername image for the button.
  167. // This function call is optional
  168. // Parameters:
  169. // [IN] id
  170. // resource id, one of the resources already imported with the
  171. // resource editor, usually begins with IDR_
  172. //
  173. // [IN] pType
  174. // pointer to string describing the resource type
  175. //
  176. // Returns: BOOL
  177. // Non zero if successful, otherwise zero
  178. //
  179. //=============================================================================
  180. BOOL CGdipButton::LoadAltImage(UINT id, LPCTSTR pType)
  181. {
  182. m_bHaveAltImage = TRUE;
  183. m_pAltImage = new CGdiPlusBitmapResource;
  184. return (m_pAltImage->Load(id, pType));
  185. }
  186. //=============================================================================
  187. //
  188. // The framework calls this member function when a child control is about to
  189. // be drawn. All the bitmaps are created here on the first call. Every thing
  190. // is done with a memory DC except the background, which get's it's information
  191. // from the parent. The background is needed for transparent portions of PNG
  192. // images. An always on top app (such as Task Manager) that is in the way can
  193. // cause it to get an incorrect background. To avoid this, the parent should
  194. // call the SetBkGnd function with a memory DC when it creates the background.
  195. //
  196. //=============================================================================
  197. HBRUSH CGdipButton::CtlColor(CDC* pScreenDC, UINT nCtlColor)
  198. {
  199. if(!m_bHaveBitmaps)
  200. {
  201. if(!m_pStdImage)
  202. {
  203. return NULL; // Load the standard image with LoadStdImage()
  204. }
  205. CBitmap bmp, *pOldBitmap;
  206. CRect rect;
  207. GetClientRect(rect);
  208. // do everything with mem dc
  209. CMemDCEx pDC(pScreenDC, rect);
  210. Gdiplus::Graphics graphics(pDC->m_hDC);
  211. // background
  212. if (m_dcBk.m_hDC == NULL)
  213. {
  214. CRect rect1;
  215. CClientDC clDC(GetParent());
  216. GetWindowRect(rect1);
  217. GetParent()->ScreenToClient(rect1);
  218. m_dcBk.CreateCompatibleDC(&clDC);
  219. bmp.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
  220. pOldBitmap = m_dcBk.SelectObject(&bmp);
  221. m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
  222. bmp.DeleteObject();
  223. }
  224. // standard image
  225. if (m_dcStd.m_hDC == NULL)
  226. {
  227. PaintBk(pDC);
  228. /*graphics.DrawImage(*m_pStdImage, 0, 0);
  229. m_dcStd.CreateCompatibleDC(pDC);
  230. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  231. pOldBitmap = m_dcStd.SelectObject(&bmp);
  232. m_dcStd.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  233. bmp.DeleteObject();*/
  234. float width = (float)m_pStdImage->m_pBitmap->GetWidth();
  235. float height = (float)m_pStdImage->m_pBitmap->GetHeight();
  236. RectF grect; grect.X = 0, grect.Y = 0; grect.Width = width; grect.Height = height;
  237. graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel);
  238. m_dcStd.CreateCompatibleDC(pDC);
  239. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  240. pOldBitmap = m_dcStd.SelectObject(&bmp);
  241. m_dcStd.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  242. bmp.DeleteObject();
  243. // standard image pressed
  244. if (m_dcStdP.m_hDC == NULL)
  245. {
  246. PaintBk(pDC);
  247. //graphics.DrawImage(*m_pStdImage, 1, 1);
  248. //m_dcStdP.CreateCompatibleDC(pDC);
  249. //bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  250. //pOldBitmap = m_dcStdP.SelectObject(&bmp);
  251. //m_dcStdP.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  252. //bmp.DeleteObject();
  253. float width = (float)m_pStdImage->m_pBitmap->GetWidth();
  254. float height = (float)m_pStdImage->m_pBitmap->GetHeight();
  255. RectF grect; grect.X = 0, grect.Y = 0; grect.Width = width; grect.Height = height;
  256. graphics.DrawImage(*m_pStdImage, grect, -1, -1, width, height, UnitPixel);
  257. m_dcStdP.CreateCompatibleDC(pDC);
  258. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  259. pOldBitmap = m_dcStdP.SelectObject(&bmp);
  260. m_dcStdP.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  261. bmp.DeleteObject();
  262. }
  263. // standard image hot
  264. if(m_dcStdH.m_hDC == NULL)
  265. {
  266. PaintBk(pDC);
  267. ColorMatrix HotMat = { 1.05f, 0.00f, 0.00f, 0.00f, 0.00f,
  268. 0.00f, 1.05f, 0.00f, 0.00f, 0.00f,
  269. 0.00f, 0.00f, 1.05f, 0.00f, 0.00f,
  270. 0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
  271. 0.05f, 0.05f, 0.05f, 0.00f, 1.00f };
  272. ImageAttributes ia;
  273. ia.SetColorMatrix(&HotMat);
  274. float width = (float)m_pStdImage->m_pBitmap->GetWidth();
  275. float height = (float)m_pStdImage->m_pBitmap->GetHeight();
  276. RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;
  277. graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel, &ia);
  278. m_dcStdH.CreateCompatibleDC(pDC);
  279. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  280. pOldBitmap = m_dcStdH.SelectObject(&bmp);
  281. m_dcStdH.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  282. bmp.DeleteObject();
  283. }
  284. // grayscale image
  285. if(m_dcGS.m_hDC == NULL)
  286. {
  287. PaintBk(pDC);
  288. ColorMatrix GrayMat = { 0.30f, 0.30f, 0.30f, 0.00f, 0.00f,
  289. 0.59f, 0.59f, 0.59f, 0.00f, 0.00f,
  290. 0.11f, 0.11f, 0.11f, 0.00f, 0.00f,
  291. 0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
  292. 0.00f, 0.00f, 0.00f, 0.00f, 1.00f };
  293. ImageAttributes ia;
  294. ia.SetColorMatrix(&GrayMat);
  295. float width = (float)m_pStdImage->m_pBitmap->GetWidth();
  296. float height = (float)m_pStdImage->m_pBitmap->GetHeight();
  297. RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;
  298. graphics.DrawImage(*m_pStdImage, grect, 0, 0, width, height, UnitPixel, &ia);
  299. m_dcGS.CreateCompatibleDC(pDC);
  300. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  301. pOldBitmap = m_dcGS.SelectObject(&bmp);
  302. m_dcGS.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  303. bmp.DeleteObject();
  304. }
  305. }
  306. // alternate image
  307. if( (m_dcAlt.m_hDC == NULL) && m_bHaveAltImage )
  308. {
  309. PaintBk(pDC);
  310. graphics.DrawImage(*m_pAltImage, 0, 0);
  311. m_dcAlt.CreateCompatibleDC(pDC);
  312. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  313. pOldBitmap = m_dcAlt.SelectObject(&bmp);
  314. m_dcAlt.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  315. bmp.DeleteObject();
  316. // alternate image pressed
  317. if( (m_dcAltP.m_hDC == NULL) && m_bHaveAltImage )
  318. {
  319. PaintBk(pDC);
  320. graphics.DrawImage(*m_pAltImage, 1, 1);
  321. m_dcAltP.CreateCompatibleDC(pDC);
  322. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  323. pOldBitmap = m_dcAltP.SelectObject(&bmp);
  324. m_dcAltP.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  325. bmp.DeleteObject();
  326. }
  327. // alternate image hot
  328. if(m_dcAltH.m_hDC == NULL)
  329. {
  330. PaintBk(pDC);
  331. ColorMatrix HotMat = { 1.05f, 0.00f, 0.00f, 0.00f, 0.00f,
  332. 0.00f, 1.05f, 0.00f, 0.00f, 0.00f,
  333. 0.00f, 0.00f, 1.05f, 0.00f, 0.00f,
  334. 0.00f, 0.00f, 0.00f, 1.00f, 0.00f,
  335. 0.05f, 0.05f, 0.05f, 0.00f, 1.00f };
  336. ImageAttributes ia;
  337. ia.SetColorMatrix(&HotMat);
  338. float width = (float)m_pStdImage->m_pBitmap->GetWidth();
  339. float height = (float)m_pStdImage->m_pBitmap->GetHeight();
  340. RectF grect; grect.X=0, grect.Y=0; grect.Width = width; grect.Height = height;
  341. graphics.DrawImage(*m_pAltImage, grect, 0, 0, width, height, UnitPixel, &ia);
  342. m_dcAltH.CreateCompatibleDC(pDC);
  343. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  344. pOldBitmap = m_dcAltH.SelectObject(&bmp);
  345. m_dcAltH.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);
  346. bmp.DeleteObject();
  347. }
  348. }
  349. if(m_pCurBtn == NULL)
  350. {
  351. m_pCurBtn = &m_dcStd;
  352. }
  353. m_bHaveBitmaps = TRUE;
  354. }
  355. return NULL;
  356. }
  357. //=============================================================================
  358. // paint the background
  359. //=============================================================================
  360. void CGdipButton::PaintBk(CDC *pDC)
  361. {
  362. CRect rect;
  363. GetClientRect(rect);
  364. pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
  365. }
  366. //=============================================================================
  367. // paint the bitmap currently pointed to with m_pCurBtn
  368. //=============================================================================
  369. void CGdipButton::PaintBtn(CDC *pDC)
  370. {
  371. CRect rect;
  372. GetClientRect(rect);
  373. pDC->BitBlt(0, 0, rect.Width(), rect.Height(), m_pCurBtn, 0, 0, SRCCOPY);
  374. }
  375. //=============================================================================
  376. // enables the toggle mode
  377. // returns if it doesn't have the alternate image
  378. //=============================================================================
  379. void CGdipButton::EnableToggle(BOOL bEnable)
  380. {
  381. if(!m_bHaveAltImage) return;
  382. m_bIsToggle = bEnable;
  383. // this actually makes it start in the std state since toggle is called before paint
  384. if(bEnable) m_pCurBtn = &m_dcAlt;
  385. else m_pCurBtn = &m_dcStd;
  386. }
  387. //=============================================================================
  388. // sets the image type and disabled state then repaints
  389. //=============================================================================
  390. void CGdipButton::SetImage(int type)
  391. {
  392. m_nCurType = type;
  393. (type == DIS_TYPE) ? m_bIsDisabled = TRUE : m_bIsDisabled = FALSE;
  394. Invalidate();
  395. }
  396. //=============================================================================
  397. // set the control to owner draw
  398. //=============================================================================
  399. void CGdipButton::PreSubclassWindow()
  400. {
  401. // Set control to owner draw
  402. ModifyStyle(0, BS_OWNERDRAW, SWP_FRAMECHANGED);
  403. CButton::PreSubclassWindow();
  404. }
  405. //=============================================================================
  406. // disable double click
  407. //=============================================================================
  408. BOOL CGdipButton::PreTranslateMessage(MSG* pMsg)
  409. {
  410. if (pMsg->message == WM_LBUTTONDBLCLK)
  411. pMsg->message = WM_LBUTTONDOWN;
  412. if (m_pToolTip != NULL)
  413. {
  414. if (::IsWindow(m_pToolTip->m_hWnd))
  415. {
  416. m_pToolTip->RelayEvent(pMsg);
  417. }
  418. }
  419. return CButton::PreTranslateMessage(pMsg);
  420. }
  421. //=============================================================================
  422. // overide the erase function
  423. //=============================================================================
  424. BOOL CGdipButton::OnEraseBkgnd(CDC* pDC)
  425. {
  426. return TRUE;
  427. }
  428. //=============================================================================
  429. // Paint the button depending on the state of the mouse
  430. //=============================================================================
  431. void CGdipButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  432. {
  433. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  434. // handle disabled state
  435. if(m_bIsDisabled)
  436. {
  437. m_pCurBtn = &m_dcGS;
  438. PaintBtn(pDC);
  439. return;
  440. }
  441. BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);
  442. // handle toggle button
  443. if(m_bIsToggle && bIsPressed)
  444. {
  445. (m_nCurType == STD_TYPE) ? m_nCurType = ALT_TYPE : m_nCurType = STD_TYPE;
  446. }
  447. if(bIsPressed)
  448. {
  449. if(m_nCurType == STD_TYPE)
  450. m_pCurBtn = &m_dcStdP;
  451. else
  452. m_pCurBtn = &m_dcAltP;
  453. }
  454. else if(m_bIsHovering)
  455. {
  456. if(m_nCurType == STD_TYPE)
  457. m_pCurBtn = &m_dcStdH;
  458. else
  459. m_pCurBtn = &m_dcAltH;
  460. }
  461. else
  462. {
  463. if(m_nCurType == STD_TYPE)
  464. m_pCurBtn = &m_dcStd;
  465. else
  466. m_pCurBtn = &m_dcAlt;
  467. }
  468. // paint the button
  469. PaintBtn(pDC);
  470. }
  471. //=============================================================================
  472. LRESULT CGdipButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
  473. //=============================================================================
  474. {
  475. m_bIsHovering = TRUE;
  476. Invalidate();
  477. DeleteToolTip();
  478. // Create a new Tooltip with new Button Size and Location
  479. SetToolTipText(m_tooltext);
  480. if (m_pToolTip != NULL)
  481. {
  482. if (::IsWindow(m_pToolTip->m_hWnd))
  483. {
  484. //Display ToolTip
  485. m_pToolTip->Update();
  486. }
  487. }
  488. return 0;
  489. }
  490. //=============================================================================
  491. LRESULT CGdipButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
  492. //=============================================================================
  493. {
  494. m_bIsTracking = FALSE;
  495. m_bIsHovering = FALSE;
  496. Invalidate();
  497. return 0;
  498. }
  499. //=============================================================================
  500. void CGdipButton::OnMouseMove(UINT nFlags, CPoint point)
  501. //=============================================================================
  502. {
  503. if (!m_bIsTracking)
  504. {
  505. TRACKMOUSEEVENT tme;
  506. tme.cbSize = sizeof(tme);
  507. tme.hwndTrack = m_hWnd;
  508. tme.dwFlags = TME_LEAVE|TME_HOVER;
  509. tme.dwHoverTime = 1;
  510. m_bIsTracking = _TrackMouseEvent(&tme);
  511. }
  512. CButton::OnMouseMove(nFlags, point);
  513. }
  514. //=============================================================================
  515. //
  516. // Call this member function with a memory DC from the code that paints
  517. // the parents background. Passing the screen DC defeats the purpose of
  518. // using this function.
  519. //
  520. //=============================================================================
  521. void CGdipButton::SetBkGnd(CDC* pDC)
  522. {
  523. CRect rect, rectS;
  524. CBitmap bmp, *pOldBitmap;
  525. GetClientRect(rect);
  526. GetWindowRect(rectS);
  527. GetParent()->ScreenToClient(rectS);
  528. m_dcBk.DeleteDC();
  529. m_dcBk.CreateCompatibleDC(pDC);
  530. bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  531. pOldBitmap = m_dcBk.SelectObject(&bmp);
  532. m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, rectS.left, rectS.top, SRCCOPY);
  533. bmp.DeleteObject();
  534. }
  535. //=============================================================================
  536. // Set the tooltip with a string resource
  537. //=============================================================================
  538. void CGdipButton::SetToolTipText(UINT nId, BOOL bActivate)
  539. {
  540. // load string resource
  541. m_tooltext.LoadString(nId);
  542. // If string resource is not empty
  543. if (m_tooltext.IsEmpty() == FALSE)
  544. {
  545. SetToolTipText(m_tooltext, bActivate);
  546. }
  547. }
  548. //=============================================================================
  549. // Set the tooltip with a CString
  550. //=============================================================================
  551. void CGdipButton::SetToolTipText(CString spText, BOOL bActivate)
  552. {
  553. // We cannot accept NULL pointer
  554. if (spText.IsEmpty()) return;
  555. // Initialize ToolTip
  556. InitToolTip();
  557. m_tooltext = spText;
  558. // If there is no tooltip defined then add it
  559. if (m_pToolTip->GetToolCount() == 0)
  560. {
  561. CRect rectBtn;
  562. GetClientRect(rectBtn);
  563. m_pToolTip->AddTool(this, m_tooltext, rectBtn, 1);
  564. }
  565. // Set text for tooltip
  566. m_pToolTip->UpdateTipText(m_tooltext, this, 1);
  567. m_pToolTip->SetDelayTime(2000);
  568. m_pToolTip->Activate(bActivate);
  569. }
  570. //=============================================================================
  571. void CGdipButton::InitToolTip()
  572. //=============================================================================
  573. {
  574. if (m_pToolTip == NULL)
  575. {
  576. m_pToolTip = new CToolTipCtrl;
  577. // Create ToolTip control
  578. m_pToolTip->Create(this);
  579. m_pToolTip->Activate(TRUE);
  580. }
  581. }
  582. //=============================================================================
  583. void CGdipButton::DeleteToolTip()
  584. //=============================================================================
  585. {
  586. // Destroy Tooltip incase the size of the button has changed.
  587. if (m_pToolTip != NULL)
  588. {
  589. delete m_pToolTip;
  590. m_pToolTip = NULL;
  591. }
  592. }