BtnST.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. #include "stdafx.h"
  2. #include "BtnST.h"
  3. // Download by http://www.codefans.net
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CButtonST
  11. // Mask for control's type
  12. #define BS_TYPEMASK SS_TYPEMASK
  13. CButtonST::CButtonST()
  14. {
  15. m_bIsPressed = FALSE;
  16. m_bIsFocused = FALSE;
  17. m_bIsDisabled = FALSE;
  18. m_bMouseOnButton = FALSE;
  19. FreeResources(FALSE);
  20. // Default type is "flat" button
  21. m_bIsFlat = TRUE;
  22. // Button will be tracked also if when the window is inactive (like Internet Explorer)
  23. m_bAlwaysTrack = TRUE;
  24. // By default draw border in "flat" button
  25. m_bDrawBorder = TRUE;
  26. // By default icon is aligned horizontally
  27. m_byAlign = ST_ALIGN_HORIZ;
  28. // By default, for "flat" button, don't draw the focus rect
  29. m_bDrawFlatFocus = FALSE;
  30. // By default the button is not the default button
  31. m_bIsDefault = FALSE;
  32. // Invalid value, since type still unknown
  33. m_nTypeStyle = BS_TYPEMASK;
  34. // By default the button is not a checkbox
  35. m_bIsCheckBox = FALSE;
  36. m_nCheck = 0;
  37. // Set default colors
  38. SetDefaultColors(FALSE);
  39. // No tooltip created
  40. m_ToolTip.m_hWnd = NULL;
  41. // Do not draw as a transparent button
  42. m_bDrawTransparent = FALSE;
  43. m_pbmpOldBk = NULL;
  44. // No URL defined
  45. SetURL(NULL);
  46. // No cursor defined
  47. m_hCursor = NULL;
  48. // No associated menu
  49. #ifndef BTNST_USE_BCMENU
  50. m_hMenu = NULL;
  51. #endif
  52. m_hParentWndMenu = NULL;
  53. m_bMenuDisplayed = FALSE;
  54. m_bShowDisabledBitmap = TRUE;
  55. } // End of CButtonST
  56. CButtonST::~CButtonST()
  57. {
  58. // Restore old bitmap (if any)
  59. if (m_dcBk.m_hDC && m_pbmpOldBk)
  60. {
  61. m_dcBk.SelectObject(m_pbmpOldBk);
  62. } // if
  63. FreeResources();
  64. // Destroy the cursor (if any)
  65. if (m_hCursor) ::DestroyCursor(m_hCursor);
  66. // Destroy the menu (if any)
  67. #ifdef BTNST_USE_BCMENU
  68. if (m_menuPopup.m_hMenu) m_menuPopup.DestroyMenu();
  69. #else
  70. if (m_hMenu) ::DestroyMenu(m_hMenu);
  71. #endif
  72. } // End of ~CButtonST
  73. BEGIN_MESSAGE_MAP(CButtonST, CButton)
  74. //{{AFX_MSG_MAP(CButtonST)
  75. ON_WM_SETCURSOR()
  76. ON_WM_KILLFOCUS()
  77. ON_WM_MOUSEMOVE()
  78. ON_WM_SYSCOLORCHANGE()
  79. ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
  80. ON_WM_ACTIVATE()
  81. ON_WM_ENABLE()
  82. ON_WM_CANCELMODE()
  83. ON_WM_GETDLGCODE()
  84. ON_WM_CTLCOLOR_REFLECT()
  85. //}}AFX_MSG_MAP
  86. #ifdef BTNST_USE_BCMENU
  87. ON_WM_MENUCHAR()
  88. ON_WM_MEASUREITEM()
  89. #endif
  90. ON_MESSAGE(BM_SETSTYLE, OnSetStyle)
  91. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  92. ON_MESSAGE(BM_SETCHECK, OnSetCheck)
  93. ON_MESSAGE(BM_GETCHECK, OnGetCheck)
  94. END_MESSAGE_MAP()
  95. void CButtonST::FreeResources(BOOL bCheckForNULL)
  96. {
  97. if (bCheckForNULL)
  98. {
  99. // Destroy icons
  100. // Note: the following two lines MUST be here! even if
  101. // BoundChecker says they are unnecessary!
  102. if (m_csIcons[0].hIcon) ::DestroyIcon(m_csIcons[0].hIcon);
  103. if (m_csIcons[1].hIcon) ::DestroyIcon(m_csIcons[1].hIcon);
  104. // Destroy bitmaps
  105. if (m_csBitmaps[0].hBitmap) ::DeleteObject(m_csBitmaps[0].hBitmap);
  106. if (m_csBitmaps[1].hBitmap) ::DeleteObject(m_csBitmaps[1].hBitmap);
  107. // Destroy mask bitmaps
  108. if (m_csBitmaps[0].hMask) ::DeleteObject(m_csBitmaps[0].hMask);
  109. if (m_csBitmaps[1].hMask) ::DeleteObject(m_csBitmaps[1].hMask);
  110. } // if
  111. ::ZeroMemory(&m_csIcons, sizeof(m_csIcons));
  112. ::ZeroMemory(&m_csBitmaps, sizeof(m_csBitmaps));
  113. } // End of FreeResources
  114. void CButtonST::PreSubclassWindow()
  115. {
  116. UINT nBS;
  117. nBS = GetButtonStyle();
  118. // Set initial control type
  119. m_nTypeStyle = nBS & BS_TYPEMASK;
  120. // Check if this is a checkbox
  121. if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
  122. // Set initial default state flag
  123. if (m_nTypeStyle == BS_DEFPUSHBUTTON)
  124. {
  125. // Set default state for a default button
  126. m_bIsDefault = TRUE;
  127. // Adjust style for default button
  128. m_nTypeStyle = BS_PUSHBUTTON;
  129. } // If
  130. // You should not set the Owner Draw before this call
  131. // (don't use the resource editor "Owner Draw" or
  132. // ModifyStyle(0, BS_OWNERDRAW) before calling PreSubclassWindow() )
  133. ASSERT(m_nTypeStyle != BS_OWNERDRAW);
  134. // Switch to owner-draw
  135. ModifyStyle(BS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
  136. CButton::PreSubclassWindow();
  137. } // End of PreSubclassWindow
  138. UINT CButtonST::OnGetDlgCode()
  139. {
  140. UINT nCode = CButton::OnGetDlgCode();
  141. // Tell the system if we want default state handling
  142. // (losing default state always allowed)
  143. nCode |= (m_bIsDefault ? DLGC_DEFPUSHBUTTON : DLGC_UNDEFPUSHBUTTON);
  144. return nCode;
  145. } // End of OnGetDlgCode
  146. BOOL CButtonST::PreTranslateMessage(MSG* pMsg)
  147. {
  148. InitToolTip();
  149. m_ToolTip.RelayEvent(pMsg);
  150. return CButton::PreTranslateMessage(pMsg);
  151. } // End of PreTranslateMessage
  152. LRESULT CButtonST::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  153. {
  154. if (message == WM_LBUTTONDBLCLK)
  155. {
  156. message = WM_LBUTTONDOWN;
  157. } // if
  158. /*
  159. switch (message)
  160. {
  161. case WM_LBUTTONDBLCLK:
  162. message = WM_LBUTTONDOWN;
  163. break;
  164. case WM_MOVE:
  165. CRect rect;
  166. GetClientRect(&rect);
  167. if (::IsWindow(m_ToolTip.m_hWnd))
  168. {
  169. if (m_ToolTip.GetToolCount() != 0)
  170. m_ToolTip.SetToolRect(this, 1, &rect);
  171. } // if
  172. break;
  173. } // switch
  174. */
  175. return CButton::DefWindowProc(message, wParam, lParam);
  176. } // End of DefWindowProc
  177. HBRUSH CButtonST::CtlColor(CDC* pDC, UINT nCtlColor)
  178. {
  179. return (HBRUSH)::GetStockObject(NULL_BRUSH);
  180. } // End of CtlColor
  181. void CButtonST::OnSysColorChange()
  182. {
  183. CButton::OnSysColorChange();
  184. m_dcBk.DeleteDC();
  185. m_bmpBk.DeleteObject();
  186. } // End of OnSysColorChange
  187. LRESULT CButtonST::OnSetStyle(WPARAM wParam, LPARAM lParam)
  188. {
  189. UINT nNewType = (wParam & BS_TYPEMASK);
  190. // Update default state flag
  191. if (nNewType == BS_DEFPUSHBUTTON)
  192. {
  193. m_bIsDefault = TRUE;
  194. } // if
  195. else if (nNewType == BS_PUSHBUTTON)
  196. {
  197. // Losing default state always allowed
  198. m_bIsDefault = FALSE;
  199. } // if
  200. // Can't change control type after owner-draw is set.
  201. // Let the system process changes to other style bits
  202. // and redrawing, while keeping owner-draw style
  203. return DefWindowProc(BM_SETSTYLE,
  204. (wParam & ~BS_TYPEMASK) | BS_OWNERDRAW, lParam);
  205. } // End of OnSetStyle
  206. LRESULT CButtonST::OnSetCheck(WPARAM wParam, LPARAM lParam)
  207. {
  208. ASSERT(m_bIsCheckBox);
  209. switch (wParam)
  210. {
  211. case BST_CHECKED:
  212. case BST_INDETERMINATE: // Indeterminate state is handled like checked state
  213. SetCheck(1);
  214. break;
  215. default:
  216. SetCheck(0);
  217. break;
  218. } // switch
  219. return 0;
  220. } // End of OnSetCheck
  221. LRESULT CButtonST::OnGetCheck(WPARAM wParam, LPARAM lParam)
  222. {
  223. ASSERT(m_bIsCheckBox);
  224. return GetCheck();
  225. } // End of OnGetCheck
  226. #ifdef BTNST_USE_BCMENU
  227. LRESULT CButtonST::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu)
  228. {
  229. LRESULT lResult;
  230. if (BCMenu::IsMenu(pMenu))
  231. lResult = BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
  232. else
  233. lResult = CButton::OnMenuChar(nChar, nFlags, pMenu);
  234. return lResult;
  235. } // End of OnMenuChar
  236. #endif
  237. #ifdef BTNST_USE_BCMENU
  238. void CButtonST::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  239. {
  240. BOOL bSetFlag = FALSE;
  241. if (lpMeasureItemStruct->CtlType == ODT_MENU)
  242. {
  243. if (IsMenu((HMENU)lpMeasureItemStruct->itemID) && BCMenu::IsMenu((HMENU)lpMeasureItemStruct->itemID))
  244. {
  245. m_menuPopup.MeasureItem(lpMeasureItemStruct);
  246. bSetFlag = TRUE;
  247. } // if
  248. } // if
  249. if (!bSetFlag) CButton::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
  250. } // End of OnMeasureItem
  251. #endif
  252. void CButtonST::OnEnable(BOOL bEnable)
  253. {
  254. CButton::OnEnable(bEnable);
  255. if (bEnable == FALSE)
  256. {
  257. CWnd* pWnd = GetParent()->GetNextDlgTabItem(this);
  258. if (pWnd)
  259. pWnd->SetFocus();
  260. else
  261. GetParent()->SetFocus();
  262. CancelHover();
  263. } // if
  264. } // End of OnEnable
  265. void CButtonST::OnKillFocus(CWnd * pNewWnd)
  266. {
  267. CButton::OnKillFocus(pNewWnd);
  268. CancelHover();
  269. } // End of OnKillFocus
  270. void CButtonST::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  271. {
  272. CButton::OnActivate(nState, pWndOther, bMinimized);
  273. if (nState == WA_INACTIVE) CancelHover();
  274. } // End of OnActivate
  275. void CButtonST::OnCancelMode()
  276. {
  277. CButton::OnCancelMode();
  278. CancelHover();
  279. } // End of OnCancelMode
  280. BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  281. {
  282. // If a cursor was specified then use it!
  283. if (m_hCursor != NULL)
  284. {
  285. ::SetCursor(m_hCursor);
  286. return TRUE;
  287. } // if
  288. return CButton::OnSetCursor(pWnd, nHitTest, message);
  289. } // End of OnSetCursor
  290. void CButtonST::CancelHover()
  291. {
  292. // Only for flat buttons
  293. if (m_bIsFlat)
  294. {
  295. if (m_bMouseOnButton)
  296. {
  297. m_bMouseOnButton = FALSE;
  298. Invalidate();
  299. } // if
  300. } // if
  301. } // End of CancelHover
  302. void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
  303. {
  304. CWnd* wndUnderMouse = NULL;
  305. CWnd* wndActive = this;
  306. TRACKMOUSEEVENT csTME;
  307. CButton::OnMouseMove(nFlags, point);
  308. ClientToScreen(&point);
  309. wndUnderMouse = WindowFromPoint(point);
  310. // If the mouse enter the button with the left button pressed then do nothing
  311. if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
  312. // If our button is not flat then do nothing
  313. if (m_bIsFlat == FALSE) return;
  314. if (m_bAlwaysTrack == FALSE) wndActive = GetActiveWindow();
  315. if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
  316. {
  317. if (!m_bMouseOnButton)
  318. {
  319. m_bMouseOnButton = TRUE;
  320. Invalidate();
  321. csTME.cbSize = sizeof(csTME);
  322. csTME.dwFlags = TME_LEAVE;
  323. csTME.hwndTrack = m_hWnd;
  324. ::_TrackMouseEvent(&csTME);
  325. } // if
  326. } else CancelHover();
  327. } // End of OnMouseMove
  328. // Handler for WM_MOUSELEAVE
  329. LRESULT CButtonST::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  330. {
  331. CancelHover();
  332. return 0;
  333. } // End of OnMouseLeave
  334. BOOL CButtonST::OnClicked()
  335. {
  336. SetFocus();
  337. if (m_bIsCheckBox)
  338. {
  339. m_nCheck = !m_nCheck;
  340. Invalidate();
  341. } // if
  342. else
  343. {
  344. // Handle the menu (if any)
  345. #ifdef BTNST_USE_BCMENU
  346. if (m_menuPopup.m_hMenu)
  347. #else
  348. if (m_hMenu)
  349. #endif
  350. {
  351. CRect rWnd;
  352. GetWindowRect(rWnd);
  353. m_bMenuDisplayed = TRUE;
  354. Invalidate();
  355. #ifdef BTNST_USE_BCMENU
  356. BCMenu *psub = (BCMenu*)m_menuPopup.GetSubMenu(0);
  357. DWORD dwRetValue = psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, this, NULL);
  358. #else
  359. HMENU hSubMenu = ::GetSubMenu(m_hMenu, 0);
  360. //DWORD dwRetValue = ::TrackPopupMenuEx(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON, rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);
  361. DWORD dwRetValue = ::TrackPopupMenuEx(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);
  362. #endif
  363. m_bMenuDisplayed = FALSE;
  364. Invalidate();
  365. if (dwRetValue)
  366. ::PostMessage(m_hParentWndMenu, WM_COMMAND, MAKEWPARAM(dwRetValue, 0), (LPARAM)NULL);
  367. } // if
  368. else
  369. {
  370. // Handle the URL (if any)
  371. if (_tcslen(m_szURL) > 0)
  372. {
  373. SHELLEXECUTEINFO csSEI;
  374. memset(&csSEI, 0, sizeof(csSEI));
  375. csSEI.cbSize = sizeof(SHELLEXECUTEINFO);
  376. csSEI.fMask = SEE_MASK_FLAG_NO_UI;
  377. csSEI.lpVerb = _T("open");
  378. csSEI.lpFile = m_szURL;
  379. csSEI.nShow = SW_SHOWMAXIMIZED;
  380. ::ShellExecuteEx(&csSEI);
  381. } // if
  382. } // else
  383. } // else
  384. return FALSE;
  385. } // End of OnClicked
  386. void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  387. {
  388. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  389. CPen* pOldPen;
  390. // Checkbox?
  391. if (m_bIsCheckBox)
  392. {
  393. m_bIsPressed = (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
  394. } // if
  395. else // Normal button OR other button style ...
  396. {
  397. m_bIsPressed = (lpDIS->itemState & ODS_SELECTED);
  398. // If there is a menu and it's displayed, draw the button as pressed
  399. if (
  400. #ifdef BTNST_USE_BCMENU
  401. m_menuPopup.m_hMenu
  402. #else
  403. m_hMenu
  404. #endif
  405. && m_bMenuDisplayed) m_bIsPressed = TRUE;
  406. } // else
  407. m_bIsFocused = (lpDIS->itemState & ODS_FOCUS);
  408. m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
  409. CRect itemRect = lpDIS->rcItem;
  410. pDC->SetBkMode(TRANSPARENT);
  411. if (m_bIsFlat == FALSE)
  412. {
  413. if (m_bIsFocused || m_bIsDefault)
  414. {
  415. CBrush br(RGB(0,0,0));
  416. pDC->FrameRect(&itemRect, &br);
  417. itemRect.DeflateRect(1, 1);
  418. } // if
  419. } // if
  420. // Prepare draw... paint button background
  421. // Draw transparent?
  422. if (m_bDrawTransparent)
  423. PaintBk(pDC);
  424. else
  425. OnDrawBackground(pDC, &itemRect);
  426. // Draw pressed button
  427. if (m_bIsPressed)
  428. {
  429. if (m_bIsFlat)
  430. {
  431. if (m_bDrawBorder)
  432. OnDrawBorder(pDC, &itemRect);
  433. }
  434. else
  435. {
  436. CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
  437. pDC->FrameRect(&itemRect, &brBtnShadow);
  438. }
  439. }
  440. else // ...else draw non pressed button
  441. {
  442. CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
  443. CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
  444. CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
  445. CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
  446. if (m_bIsFlat)
  447. {
  448. if (m_bMouseOnButton && m_bDrawBorder)
  449. OnDrawBorder(pDC, &itemRect);
  450. }
  451. else
  452. {
  453. // Draw top-left borders
  454. // White line
  455. pOldPen = pDC->SelectObject(&penBtnHiLight);
  456. pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  457. pDC->LineTo(itemRect.left, itemRect.top);
  458. pDC->LineTo(itemRect.right, itemRect.top);
  459. // Light gray line
  460. pDC->SelectObject(pen3DLight);
  461. pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
  462. pDC->LineTo(itemRect.left+1, itemRect.top+1);
  463. pDC->LineTo(itemRect.right, itemRect.top+1);
  464. // Draw bottom-right borders
  465. // Black line
  466. pDC->SelectObject(pen3DDKShadow);
  467. pDC->MoveTo(itemRect.left, itemRect.bottom-1);
  468. pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
  469. pDC->LineTo(itemRect.right-1, itemRect.top-1);
  470. // Dark gray line
  471. pDC->SelectObject(penBtnShadow);
  472. pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
  473. pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
  474. pDC->LineTo(itemRect.right-2, itemRect.top);
  475. //
  476. pDC->SelectObject(pOldPen);
  477. } // else
  478. } // else
  479. // Read the button's title
  480. CString sTitle;
  481. GetWindowText(sTitle);
  482. CRect captionRect = lpDIS->rcItem;
  483. // Draw the icon
  484. if (m_csIcons[0].hIcon)
  485. {
  486. DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
  487. } // if
  488. if (m_csBitmaps[0].hBitmap)
  489. {
  490. pDC->SetBkColor(RGB(255,255,255));
  491. DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
  492. } // if
  493. // Write the button title (if any)
  494. if (sTitle.IsEmpty() == FALSE)
  495. {
  496. // Draw the button's title
  497. // If button is pressed then "press" title also
  498. if (m_bIsPressed && m_bIsCheckBox == FALSE)
  499. captionRect.OffsetRect(1, 1);
  500. // ONLY FOR DEBUG
  501. //CBrush brBtnShadow(RGB(255, 0, 0));
  502. //pDC->FrameRect(&captionRect, &brBtnShadow);
  503. // Center text
  504. CRect centerRect = captionRect;
  505. pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
  506. captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
  507. /* RFU
  508. captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);
  509. captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);
  510. */
  511. pDC->SetBkMode(TRANSPARENT);
  512. /*
  513. pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
  514. TRUE, 0, (CBrush*)NULL);
  515. */
  516. if (m_bIsDisabled)
  517. {
  518. captionRect.OffsetRect(1, 1);
  519. pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
  520. pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
  521. captionRect.OffsetRect(-1, -1);
  522. pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
  523. pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
  524. } // if
  525. else
  526. {
  527. if (m_bMouseOnButton || m_bIsPressed)
  528. {
  529. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
  530. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
  531. } // if
  532. else
  533. {
  534. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
  535. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
  536. } // else
  537. pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
  538. } // if
  539. } // if
  540. if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
  541. {
  542. // Draw the focus rect
  543. if (m_bIsFocused)
  544. {
  545. CRect focusRect = itemRect;
  546. focusRect.DeflateRect(3, 3);
  547. pDC->DrawFocusRect(&focusRect);
  548. } // if
  549. } // if
  550. } // End of DrawItem
  551. void CButtonST::PaintBk(CDC* pDC)
  552. {
  553. CClientDC clDC(GetParent());
  554. CRect rect;
  555. CRect rect1;
  556. GetClientRect(rect);
  557. GetWindowRect(rect1);
  558. GetParent()->ScreenToClient(rect1);
  559. if (m_dcBk.m_hDC == NULL)
  560. {
  561. m_dcBk.CreateCompatibleDC(&clDC);
  562. m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
  563. m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
  564. m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
  565. } // if
  566. pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
  567. } // End of PaintBk
  568. HBITMAP CButtonST::CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor)
  569. {
  570. HBITMAP hMask = NULL;
  571. HDC hdcSrc = NULL;
  572. HDC hdcDest = NULL;
  573. HBITMAP hbmSrcT = NULL;
  574. HBITMAP hbmDestT = NULL;
  575. COLORREF crSaveBk;
  576. COLORREF crSaveDestText;
  577. hMask = ::CreateBitmap(dwWidth, dwHeight, 1, 1, NULL);
  578. if (hMask == NULL) return NULL;
  579. hdcSrc = ::CreateCompatibleDC(NULL);
  580. hdcDest = ::CreateCompatibleDC(NULL);
  581. hbmSrcT = (HBITMAP)::SelectObject(hdcSrc, hSourceBitmap);
  582. hbmDestT = (HBITMAP)::SelectObject(hdcDest, hMask);
  583. crSaveBk = ::SetBkColor(hdcSrc, crTransColor);
  584. ::BitBlt(hdcDest, 0, 0, dwWidth, dwHeight, hdcSrc, 0, 0, SRCCOPY);
  585. crSaveDestText = ::SetTextColor(hdcSrc, RGB(255, 255, 255));
  586. ::SetBkColor(hdcSrc,RGB(0, 0, 0));
  587. ::BitBlt(hdcSrc, 0, 0, dwWidth, dwHeight, hdcDest, 0, 0, SRCAND);
  588. SetTextColor(hdcDest, crSaveDestText);
  589. ::SetBkColor(hdcSrc, crSaveBk);
  590. ::SelectObject(hdcSrc, hbmSrcT);
  591. ::SelectObject(hdcDest, hbmDestT);
  592. ::DeleteDC(hdcSrc);
  593. ::DeleteDC(hdcDest);
  594. return hMask;
  595. } // End of CreateBitmapMask
  596. //
  597. // Parameters:
  598. // [IN] bHasTitle
  599. // TRUE if the button has a text
  600. // [IN] rpItem
  601. // A pointer to a RECT structure indicating the allowed paint area
  602. // [IN/OUT]rpTitle
  603. // A pointer to a CRect object indicating the paint area reserved for the
  604. // text. This structure will be modified if necessary.
  605. // [IN] bIsPressed
  606. // TRUE if the button is currently pressed
  607. // [IN] dwWidth
  608. // Width of the image (icon or bitmap)
  609. // [IN] dwHeight
  610. // Height of the image (icon or bitmap)
  611. // [OUT] rpImage
  612. // A pointer to a CRect object that will receive the area available to the image
  613. //
  614. void CButtonST::PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage)
  615. {
  616. CRect rBtn;
  617. rpImage->CopyRect(rpItem);
  618. switch (m_byAlign)
  619. {
  620. case ST_ALIGN_HORIZ:
  621. if (bHasTitle == FALSE)
  622. {
  623. // Center image horizontally
  624. rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
  625. }
  626. else
  627. {
  628. // Image must be placed just inside the focus rect
  629. rpImage->left += 3;
  630. rpTitle->left += dwWidth + 3;
  631. }
  632. // Center image vertically
  633. rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
  634. break;
  635. case ST_ALIGN_HORIZ_RIGHT:
  636. GetClientRect(&rBtn);
  637. if (bHasTitle == FALSE)
  638. {
  639. // Center image horizontally
  640. rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
  641. }
  642. else
  643. {
  644. // Image must be placed just inside the focus rect
  645. rpTitle->right = rpTitle->Width() - dwWidth - 3;
  646. rpTitle->left = 3;
  647. rpImage->left = rBtn.right - dwWidth - 3;
  648. // Center image vertically
  649. rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
  650. }
  651. break;
  652. case ST_ALIGN_VERT:
  653. // Center image horizontally
  654. rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
  655. if (bHasTitle == FALSE)
  656. {
  657. // Center image vertically
  658. rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
  659. }
  660. else
  661. {
  662. rpImage->top = 3;
  663. rpTitle->top += dwHeight;
  664. }
  665. break;
  666. }
  667. // If button is pressed then press image also
  668. if (bIsPressed && m_bIsCheckBox == FALSE)
  669. rpImage->OffsetRect(1, 1);
  670. } // End of PrepareImageRect
  671. void CButtonST::DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
  672. {
  673. BYTE byIndex = 0;
  674. // Select the icon to use
  675. if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
  676. byIndex = 0;
  677. else
  678. byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
  679. CRect rImage;
  680. PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);
  681. // Ole'!
  682. pDC->DrawState( rImage.TopLeft(),
  683. rImage.Size(),
  684. m_csIcons[byIndex].hIcon,
  685. (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
  686. (CBrush*)NULL);
  687. } // End of DrawTheIcon
  688. void CButtonST::DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rItem, CRect *rCaption, BOOL bIsPressed, BOOL bIsDisabled)
  689. {
  690. HDC hdcBmpMem = NULL;
  691. HBITMAP hbmOldBmp = NULL;
  692. HDC hdcMem = NULL;
  693. HBITMAP hbmT = NULL;
  694. BYTE byIndex = 0;
  695. // Select the bitmap to use
  696. if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
  697. byIndex = 0;
  698. else
  699. byIndex = (m_csBitmaps[1].hBitmap == NULL ? 0 : 1);
  700. CRect rImage;
  701. PrepareImageRect(bHasTitle, rItem, rCaption, bIsPressed, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, &rImage);
  702. hdcBmpMem = ::CreateCompatibleDC(pDC->m_hDC);
  703. hbmOldBmp = (HBITMAP)::SelectObject(hdcBmpMem, m_csBitmaps[byIndex].hBitmap);
  704. hdcMem = ::CreateCompatibleDC(NULL);
  705. hbmT = (HBITMAP)::SelectObject(hdcMem, m_csBitmaps[byIndex].hMask);
  706. if (bIsDisabled && m_bShowDisabledBitmap)
  707. {
  708. HDC hDC = NULL;
  709. HBITMAP hBitmap = NULL;
  710. hDC = ::CreateCompatibleDC(pDC->m_hDC);
  711. hBitmap = ::CreateCompatibleBitmap(pDC->m_hDC, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight);
  712. HBITMAP hOldBmp2 = (HBITMAP)::SelectObject(hDC, hBitmap);
  713. RECT rRect;
  714. rRect.left = 0;
  715. rRect.top = 0;
  716. rRect.right = rImage.right + 1;
  717. rRect.bottom = rImage.bottom + 1;
  718. ::FillRect(hDC, &rRect, (HBRUSH)RGB(255, 255, 255));
  719. COLORREF crOldColor = ::SetBkColor(hDC, RGB(255,255,255));
  720. ::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
  721. ::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
  722. ::SetBkColor(hDC, crOldColor);
  723. ::SelectObject(hDC, hOldBmp2);
  724. ::DeleteDC(hDC);
  725. pDC->DrawState( CPoint(rImage.left/*+1*/, rImage.top),
  726. CSize(m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight),
  727. hBitmap, DST_BITMAP | DSS_DISABLED);
  728. ::DeleteObject(hBitmap);
  729. } // if
  730. else
  731. {
  732. ::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
  733. ::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
  734. } // else
  735. ::SelectObject(hdcMem, hbmT);
  736. ::DeleteDC(hdcMem);
  737. ::SelectObject(hdcBmpMem, hbmOldBmp);
  738. ::DeleteDC(hdcBmpMem);
  739. } // End of DrawTheBitmap
  740. // This function creates a grayscale icon starting from a given icon.
  741. // The resulting icon will have the same size of the original one.
  742. //
  743. // Parameters:
  744. // [IN] hIcon
  745. // Handle to the original icon.
  746. //
  747. // Return value:
  748. // If the function succeeds, the return value is the handle to the newly created
  749. // grayscale icon.
  750. // If the function fails, the return value is NULL.
  751. //
  752. HICON CButtonST::CreateGrayscaleIcon(HICON hIcon)
  753. {
  754. HICON hGrayIcon = NULL;
  755. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  756. BITMAP bmp;
  757. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  758. ICONINFO csII, csGrayII;
  759. BOOL bRetValue = FALSE;
  760. bRetValue = ::GetIconInfo(hIcon, &csII);
  761. if (bRetValue == FALSE) return NULL;
  762. hMainDC = ::GetDC(m_hWnd);
  763. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  764. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  765. if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  766. if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
  767. {
  768. csGrayII.hbmColor = ::CreateBitmap(csII.xHotspot*2, csII.yHotspot*2, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
  769. if (csGrayII.hbmColor)
  770. {
  771. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
  772. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
  773. ::BitBlt(hMemDC2, 0, 0, csII.xHotspot*2, csII.yHotspot*2, hMemDC1, 0, 0, SRCCOPY);
  774. DWORD dwLoopY = 0, dwLoopX = 0;
  775. COLORREF crPixel = 0;
  776. BYTE byNewPixel = 0;
  777. for (dwLoopY = 0; dwLoopY < csII.yHotspot*2; dwLoopY++)
  778. {
  779. for (dwLoopX = 0; dwLoopX < csII.xHotspot*2; dwLoopX++)
  780. {
  781. crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
  782. byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
  783. if (crPixel) ::SetPixel(hMemDC2, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
  784. } // for
  785. } // for
  786. ::SelectObject(hMemDC1, hOldBmp1);
  787. ::SelectObject(hMemDC2, hOldBmp2);
  788. csGrayII.hbmMask = csII.hbmMask;
  789. csGrayII.fIcon = TRUE;
  790. hGrayIcon = ::CreateIconIndirect(&csGrayII);
  791. } // if
  792. ::DeleteObject(csGrayII.hbmColor);
  793. //::DeleteObject(csGrayII.hbmMask);
  794. } // if
  795. ::DeleteObject(csII.hbmColor);
  796. ::DeleteObject(csII.hbmMask);
  797. ::DeleteDC(hMemDC1);
  798. ::DeleteDC(hMemDC2);
  799. ::ReleaseDC(m_hWnd, hMainDC);
  800. return hGrayIcon;
  801. } // End of CreateGrayscaleIcon
  802. // This function assigns icons to the button.
  803. // Any previous icon or bitmap will be removed.
  804. //
  805. // Parameters:
  806. // [IN] nIconIn
  807. // ID number of the icon resource to show when the mouse is over the button.
  808. // Pass NULL to remove any icon from the button.
  809. // [IN] nIconOut
  810. // ID number of the icon resource to show when the mouse is outside the button.
  811. // Can be NULL.
  812. //
  813. // Return value:
  814. // BTNST_OK
  815. // Function executed successfully.
  816. // BTNST_INVALIDRESOURCE
  817. // Failed loading the specified resource.
  818. //
  819. DWORD CButtonST::SetIcon(int nIconIn, int nIconOut)
  820. {
  821. HICON hIconIn = NULL;
  822. HICON hIconOut = NULL;
  823. HINSTANCE hInstResource = NULL;
  824. // Find correct resource handle
  825. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
  826. // Set icon when the mouse is IN the button
  827. hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, 0, 0, 0);
  828. // Set icon when the mouse is OUT the button
  829. if (nIconOut)
  830. {
  831. if (nIconOut == (int)BTNST_AUTO_GRAY)
  832. hIconOut = BTNST_AUTO_GRAY;
  833. else
  834. hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, 0, 0, 0);
  835. } // if
  836. return SetIcon(hIconIn, hIconOut);
  837. } // End of SetIcon
  838. // This function assigns icons to the button.
  839. // Any previous icon or bitmap will be removed.
  840. //
  841. // Parameters:
  842. // [IN] hIconIn
  843. // Handle fo the icon to show when the mouse is over the button.
  844. // Pass NULL to remove any icon from the button.
  845. // [IN] hIconOut
  846. // Handle to the icon to show when the mouse is outside the button.
  847. // Can be NULL.
  848. //
  849. // Return value:
  850. // BTNST_OK
  851. // Function executed successfully.
  852. // BTNST_INVALIDRESOURCE
  853. // Failed loading the specified resource.
  854. //
  855. DWORD CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
  856. {
  857. BOOL bRetValue;
  858. ICONINFO ii;
  859. // Free any loaded resource
  860. FreeResources();
  861. if (hIconIn)
  862. {
  863. // Icon when mouse over button?
  864. m_csIcons[0].hIcon = hIconIn;
  865. // Get icon dimension
  866. ::ZeroMemory(&ii, sizeof(ICONINFO));
  867. bRetValue = ::GetIconInfo(hIconIn, &ii);
  868. if (bRetValue == FALSE)
  869. {
  870. FreeResources();
  871. return BTNST_INVALIDRESOURCE;
  872. } // if
  873. m_csIcons[0].dwWidth = (DWORD)(ii.xHotspot * 2);
  874. m_csIcons[0].dwHeight = (DWORD)(ii.yHotspot * 2);
  875. ::DeleteObject(ii.hbmMask);
  876. ::DeleteObject(ii.hbmColor);
  877. // Icon when mouse outside button?
  878. if (hIconOut)
  879. {
  880. if (hIconOut == BTNST_AUTO_GRAY)
  881. {
  882. hIconOut = CreateGrayscaleIcon(hIconIn);
  883. } // if
  884. m_csIcons[1].hIcon = hIconOut;
  885. // Get icon dimension
  886. ::ZeroMemory(&ii, sizeof(ICONINFO));
  887. bRetValue = ::GetIconInfo(hIconOut, &ii);
  888. if (bRetValue == FALSE)
  889. {
  890. FreeResources();
  891. return BTNST_INVALIDRESOURCE;
  892. } // if
  893. m_csIcons[1].dwWidth = (DWORD)(ii.xHotspot * 2);
  894. m_csIcons[1].dwHeight = (DWORD)(ii.yHotspot * 2);
  895. ::DeleteObject(ii.hbmMask);
  896. ::DeleteObject(ii.hbmColor);
  897. } // if
  898. } // if
  899. Invalidate();
  900. return BTNST_OK;
  901. } // End of SetIcon
  902. // This function assigns bitmaps to the button.
  903. // Any previous icon or bitmap will be removed.
  904. //
  905. // Parameters:
  906. // [IN] nBitmapIn
  907. // ID number of the bitmap resource to show when the mouse is over the button.
  908. // Pass NULL to remove any bitmap from the button.
  909. // [IN] crTransColorIn
  910. // Color (inside nBitmapIn) to be used as transparent color.
  911. // [IN] nBitmapOut
  912. // ID number of the bitmap resource to show when the mouse is outside the button.
  913. // Can be NULL.
  914. // [IN] crTransColorOut
  915. // Color (inside nBitmapOut) to be used as transparent color.
  916. //
  917. // Return value:
  918. // BTNST_OK
  919. // Function executed successfully.
  920. // BTNST_INVALIDRESOURCE
  921. // Failed loading the specified resource.
  922. // BTNST_FAILEDMASK
  923. // Failed creating mask bitmap.
  924. //
  925. DWORD CButtonST::SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut, COLORREF crTransColorOut)
  926. {
  927. HBITMAP hBitmapIn = NULL;
  928. HBITMAP hBitmapOut = NULL;
  929. HINSTANCE hInstResource = NULL;
  930. // Find correct resource handle
  931. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapIn), RT_BITMAP);
  932. // Load bitmap In
  933. hBitmapIn = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
  934. // Load bitmap Out
  935. if (nBitmapOut)
  936. hBitmapOut = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
  937. return SetBitmaps(hBitmapIn, crTransColorIn, hBitmapOut, crTransColorOut);
  938. } // End of SetBitmaps
  939. // This function assigns bitmaps to the button.
  940. // Any previous icon or bitmap will be removed.
  941. //
  942. // Parameters:
  943. // [IN] hBitmapIn
  944. // Handle fo the bitmap to show when the mouse is over the button.
  945. // Pass NULL to remove any bitmap from the button.
  946. // [IN] crTransColorIn
  947. // Color (inside hBitmapIn) to be used as transparent color.
  948. // [IN] hBitmapOut
  949. // Handle to the bitmap to show when the mouse is outside the button.
  950. // Can be NULL.
  951. // [IN] crTransColorOut
  952. // Color (inside hBitmapOut) to be used as transparent color.
  953. //
  954. // Return value:
  955. // BTNST_OK
  956. // Function executed successfully.
  957. // BTNST_INVALIDRESOURCE
  958. // Failed loading the specified resource.
  959. // BTNST_FAILEDMASK
  960. // Failed creating mask bitmap.
  961. //
  962. DWORD CButtonST::SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut, COLORREF crTransColorOut)
  963. {
  964. int nRetValue;
  965. BITMAP csBitmapSize;
  966. // Free any loaded resource
  967. FreeResources();
  968. if (hBitmapIn)
  969. {
  970. m_csBitmaps[0].hBitmap = hBitmapIn;
  971. m_csBitmaps[0].crTransparent = crTransColorIn;
  972. // Get bitmap size
  973. nRetValue = ::GetObject(hBitmapIn, sizeof(csBitmapSize), &csBitmapSize);
  974. if (nRetValue == 0)
  975. {
  976. FreeResources();
  977. return BTNST_INVALIDRESOURCE;
  978. } // if
  979. m_csBitmaps[0].dwWidth = (DWORD)csBitmapSize.bmWidth;
  980. m_csBitmaps[0].dwHeight = (DWORD)csBitmapSize.bmHeight;
  981. // Create mask for bitmap In
  982. m_csBitmaps[0].hMask = CreateBitmapMask(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  983. if (m_csBitmaps[0].hMask == NULL)
  984. {
  985. FreeResources();
  986. return BTNST_FAILEDMASK;
  987. } // if
  988. if (hBitmapOut)
  989. {
  990. m_csBitmaps[1].hBitmap = hBitmapOut;
  991. m_csBitmaps[1].crTransparent = crTransColorOut;
  992. // Get bitmap size
  993. nRetValue = ::GetObject(hBitmapOut, sizeof(csBitmapSize), &csBitmapSize);
  994. if (nRetValue == 0)
  995. {
  996. FreeResources();
  997. return BTNST_INVALIDRESOURCE;
  998. } // if
  999. m_csBitmaps[1].dwWidth = (DWORD)csBitmapSize.bmWidth;
  1000. m_csBitmaps[1].dwHeight = (DWORD)csBitmapSize.bmHeight;
  1001. // Create mask for bitmap Out
  1002. m_csBitmaps[1].hMask = CreateBitmapMask(hBitmapOut, m_csBitmaps[1].dwWidth, m_csBitmaps[1].dwHeight, crTransColorOut);
  1003. if (m_csBitmaps[1].hMask == NULL)
  1004. {
  1005. FreeResources();
  1006. return BTNST_FAILEDMASK;
  1007. } // if
  1008. } // if
  1009. } // if
  1010. Invalidate();
  1011. return BTNST_OK;
  1012. } // End of SetBitmaps
  1013. // This functions sets the button to have a standard or flat style.
  1014. //
  1015. // Parameters:
  1016. // [IN] bFlat
  1017. // If TRUE the button will have a flat style, else
  1018. // will have a standard style.
  1019. // By default, CButtonST buttons are flat.
  1020. // [IN] bRepaint
  1021. // If TRUE the control will be repainted.
  1022. //
  1023. // Return value:
  1024. // BTNST_OK
  1025. // Function executed successfully.
  1026. //
  1027. DWORD CButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
  1028. {
  1029. m_bIsFlat = bFlat;
  1030. if (bRepaint) Invalidate();
  1031. return BTNST_OK;
  1032. } // End of SetFlat
  1033. // This function sets the alignment type between icon/bitmap and text.
  1034. //
  1035. // Parameters:
  1036. // [IN] byAlign
  1037. // Alignment type. Can be one of the following values:
  1038. // ST_ALIGN_HORIZ Icon/bitmap on the left, text on the right
  1039. // ST_ALIGN_VERT Icon/bitmap on the top, text on the bottom
  1040. // ST_ALIGN_HORIZ_RIGHT Icon/bitmap on the right, text on the left
  1041. // By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.
  1042. // [IN] bRepaint
  1043. // If TRUE the control will be repainted.
  1044. //
  1045. // Return value:
  1046. // BTNST_OK
  1047. // Function executed successfully.
  1048. // BTNST_INVALIDALIGN
  1049. // Alignment type not supported.
  1050. //
  1051. DWORD CButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
  1052. {
  1053. switch (byAlign)
  1054. {
  1055. case ST_ALIGN_HORIZ:
  1056. case ST_ALIGN_HORIZ_RIGHT:
  1057. case ST_ALIGN_VERT:
  1058. m_byAlign = byAlign;
  1059. if (bRepaint) Invalidate();
  1060. return BTNST_OK;
  1061. break;
  1062. } // switch
  1063. return BTNST_INVALIDALIGN;
  1064. } // End of SetAlign
  1065. // This function sets the state of the checkbox.
  1066. // If the button is not a checkbox, this function has no meaning.
  1067. //
  1068. // Parameters:
  1069. // [IN] nCheck
  1070. // 1 to check the checkbox.
  1071. // 0 to un-check the checkbox.
  1072. // [IN] bRepaint
  1073. // If TRUE the control will be repainted.
  1074. //
  1075. // Return value:
  1076. // BTNST_OK
  1077. // Function executed successfully.
  1078. //
  1079. DWORD CButtonST::SetCheck(int nCheck, BOOL bRepaint)
  1080. {
  1081. if (m_bIsCheckBox)
  1082. {
  1083. if (nCheck == 0) m_nCheck = 0;
  1084. else m_nCheck = 1;
  1085. if (bRepaint) Invalidate();
  1086. } // if
  1087. return BTNST_OK;
  1088. } // End of SetCheck
  1089. // This function returns the current state of the checkbox.
  1090. // If the button is not a checkbox, this function has no meaning.
  1091. //
  1092. // Return value:
  1093. // The current state of the checkbox.
  1094. // 1 if checked.
  1095. // 0 if not checked or the button is not a checkbox.
  1096. //
  1097. int CButtonST::GetCheck()
  1098. {
  1099. return m_nCheck;
  1100. } // End of GetCheck
  1101. // This function sets all colors to a default value.
  1102. //
  1103. // Parameters:
  1104. // [IN] bRepaint
  1105. // If TRUE the control will be repainted.
  1106. //
  1107. // Return value:
  1108. // BTNST_OK
  1109. // Function executed successfully.
  1110. //
  1111. DWORD CButtonST::SetDefaultColors(BOOL bRepaint)
  1112. {
  1113. m_crColors[BTNST_COLOR_BK_IN] = ::GetSysColor(COLOR_BTNFACE);
  1114. m_crColors[BTNST_COLOR_FG_IN] = ::GetSysColor(COLOR_BTNTEXT);
  1115. m_crColors[BTNST_COLOR_BK_OUT] = ::GetSysColor(COLOR_BTNFACE);
  1116. m_crColors[BTNST_COLOR_FG_OUT] = ::GetSysColor(COLOR_BTNTEXT);
  1117. m_crColors[BTNST_COLOR_BK_FOCUS] = ::GetSysColor(COLOR_BTNFACE);
  1118. m_crColors[BTNST_COLOR_FG_FOCUS] = ::GetSysColor(COLOR_BTNTEXT);
  1119. if (bRepaint) Invalidate();
  1120. return BTNST_OK;
  1121. } // End of SetDefaultColors
  1122. // This function sets the color to use for a particular state.
  1123. //
  1124. // Parameters:
  1125. // [IN] byColorIndex
  1126. // Index of the color to set. Can be one of the following values:
  1127. // BTNST_COLOR_BK_IN Background color when mouse is over the button
  1128. // BTNST_COLOR_FG_IN Text color when mouse is over the button
  1129. // BTNST_COLOR_BK_OUT Background color when mouse is outside the button
  1130. // BTNST_COLOR_FG_OUT Text color when mouse is outside the button
  1131. // BTNST_COLOR_BK_FOCUS Background color when the button is focused
  1132. // BTNST_COLOR_FG_FOCUS Text color when the button is focused
  1133. // [IN] crColor
  1134. // New color.
  1135. // [IN] bRepaint
  1136. // If TRUE the control will be repainted.
  1137. //
  1138. // Return value:
  1139. // BTNST_OK
  1140. // Function executed successfully.
  1141. // BTNST_INVALIDINDEX
  1142. // Invalid color index.
  1143. //
  1144. DWORD CButtonST::SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint)
  1145. {
  1146. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1147. // Set new color
  1148. m_crColors[byColorIndex] = crColor;
  1149. if (bRepaint) Invalidate();
  1150. return BTNST_OK;
  1151. } // End of SetColor
  1152. // This functions returns the color used for a particular state.
  1153. //
  1154. // Parameters:
  1155. // [IN] byColorIndex
  1156. // Index of the color to get.
  1157. // See SetColor for the list of available colors.
  1158. // [OUT] crpColor
  1159. // A pointer to a COLORREF that will receive the color.
  1160. //
  1161. // Return value:
  1162. // BTNST_OK
  1163. // Function executed successfully.
  1164. // BTNST_INVALIDINDEX
  1165. // Invalid color index.
  1166. //
  1167. DWORD CButtonST::GetColor(BYTE byColorIndex, COLORREF* crpColor)
  1168. {
  1169. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1170. // Get color
  1171. *crpColor = m_crColors[byColorIndex];
  1172. return BTNST_OK;
  1173. } // End of GetColor
  1174. // This function applies an offset to the RGB components of the specified color.
  1175. // This function can be seen as an easy way to make a color darker or lighter than
  1176. // its default value.
  1177. //
  1178. // Parameters:
  1179. // [IN] byColorIndex
  1180. // Index of the color to set.
  1181. // See SetColor for the list of available colors.
  1182. // [IN] shOffsetColor
  1183. // A short value indicating the offset to apply to the color.
  1184. // This value must be between -255 and 255.
  1185. // [IN] bRepaint
  1186. // If TRUE the control will be repainted.
  1187. //
  1188. // Return value:
  1189. // BTNST_OK
  1190. // Function executed successfully.
  1191. // BTNST_INVALIDINDEX
  1192. // Invalid color index.
  1193. // BTNST_BADPARAM
  1194. // The specified offset is out of range.
  1195. //
  1196. DWORD CButtonST::OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint)
  1197. {
  1198. BYTE byRed = 0;
  1199. BYTE byGreen = 0;
  1200. BYTE byBlue = 0;
  1201. short shOffsetR = shOffset;
  1202. short shOffsetG = shOffset;
  1203. short shOffsetB = shOffset;
  1204. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1205. if (shOffset < -255 || shOffset > 255) return BTNST_BADPARAM;
  1206. // Get RGB components of specified color
  1207. byRed = GetRValue(m_crColors[byColorIndex]);
  1208. byGreen = GetGValue(m_crColors[byColorIndex]);
  1209. byBlue = GetBValue(m_crColors[byColorIndex]);
  1210. // Calculate max. allowed real offset
  1211. if (shOffset > 0)
  1212. {
  1213. if (byRed + shOffset > 255) shOffsetR = 255 - byRed;
  1214. if (byGreen + shOffset > 255) shOffsetG = 255 - byGreen;
  1215. if (byBlue + shOffset > 255) shOffsetB = 255 - byBlue;
  1216. shOffset = min(min(shOffsetR, shOffsetG), shOffsetB);
  1217. } // if
  1218. else
  1219. {
  1220. if (byRed + shOffset < 0) shOffsetR = -byRed;
  1221. if (byGreen + shOffset < 0) shOffsetG = -byGreen;
  1222. if (byBlue + shOffset < 0) shOffsetB = -byBlue;
  1223. shOffset = max(max(shOffsetR, shOffsetG), shOffsetB);
  1224. } // else
  1225. // Set new color
  1226. m_crColors[byColorIndex] = RGB(byRed + shOffset, byGreen + shOffset, byBlue + shOffset);
  1227. if (bRepaint) Invalidate();
  1228. return BTNST_OK;
  1229. } // End of OffsetColor
  1230. // This function sets the hilight logic for the button.
  1231. // Applies only to flat buttons.
  1232. //
  1233. // Parameters:
  1234. // [IN] bAlwaysTrack
  1235. // If TRUE the button will be hilighted even if the window that owns it, is
  1236. // not the active window.
  1237. // If FALSE the button will be hilighted only if the window that owns it,
  1238. // is the active window.
  1239. //
  1240. // Return value:
  1241. // BTNST_OK
  1242. // Function executed successfully.
  1243. //
  1244. DWORD CButtonST::SetAlwaysTrack(BOOL bAlwaysTrack)
  1245. {
  1246. m_bAlwaysTrack = bAlwaysTrack;
  1247. return BTNST_OK;
  1248. } // End of SetAlwaysTrack
  1249. // This function sets the cursor to be used when the mouse is over the button.
  1250. //
  1251. // Parameters:
  1252. // [IN] nCursorId
  1253. // ID number of the cursor resource.
  1254. // Pass NULL to remove a previously loaded cursor.
  1255. // [IN] bRepaint
  1256. // If TRUE the control will be repainted.
  1257. //
  1258. // Return value:
  1259. // BTNST_OK
  1260. // Function executed successfully.
  1261. // BTNST_INVALIDRESOURCE
  1262. // Failed loading the specified resource.
  1263. //
  1264. DWORD CButtonST::SetBtnCursor(int nCursorId, BOOL bRepaint)
  1265. {
  1266. HINSTANCE hInstResource = NULL;
  1267. // Destroy any previous cursor
  1268. if (m_hCursor)
  1269. {
  1270. ::DestroyCursor(m_hCursor);
  1271. m_hCursor = NULL;
  1272. } // if
  1273. // Load cursor
  1274. if (nCursorId)
  1275. {
  1276. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR);
  1277. // Load cursor resource
  1278. m_hCursor = (HCURSOR)::LoadImage(hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
  1279. // Repaint the button
  1280. if (bRepaint) Invalidate();
  1281. // If something wrong
  1282. if (m_hCursor == NULL) return BTNST_INVALIDRESOURCE;
  1283. } // if
  1284. return BTNST_OK;
  1285. } // End of SetBtnCursor
  1286. // This function sets if the button border must be drawn.
  1287. // Applies only to flat buttons.
  1288. //
  1289. // Parameters:
  1290. // [IN] bDrawBorder
  1291. // If TRUE the border will be drawn.
  1292. // [IN] bRepaint
  1293. // If TRUE the control will be repainted.
  1294. //
  1295. // Return value:
  1296. // BTNST_OK
  1297. // Function executed successfully.
  1298. //
  1299. DWORD CButtonST::DrawBorder(BOOL bDrawBorder, BOOL bRepaint)
  1300. {
  1301. m_bDrawBorder = bDrawBorder;
  1302. // Repaint the button
  1303. if (bRepaint) Invalidate();
  1304. return BTNST_OK;
  1305. } // End of DrawBorder
  1306. // This function sets if the focus rectangle must be drawn for flat buttons.
  1307. //
  1308. // Parameters:
  1309. // [IN] bDrawFlatFocus
  1310. // If TRUE the focus rectangle will be drawn also for flat buttons.
  1311. // [IN] bRepaint
  1312. // If TRUE the control will be repainted.
  1313. //
  1314. // Return value:
  1315. // BTNST_OK
  1316. // Function executed successfully.
  1317. //
  1318. DWORD CButtonST::DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
  1319. {
  1320. m_bDrawFlatFocus = bDrawFlatFocus;
  1321. // Repaint the button
  1322. if (bRepaint) Invalidate();
  1323. return BTNST_OK;
  1324. } // End of DrawFlatFocus
  1325. void CButtonST::InitToolTip()
  1326. {
  1327. if (m_ToolTip.m_hWnd == NULL)
  1328. {
  1329. // Create ToolTip control
  1330. m_ToolTip.Create(this);
  1331. // Create inactive
  1332. m_ToolTip.Activate(FALSE);
  1333. // Enable multiline
  1334. m_ToolTip.SendMessage(TTM_SETMAXTIPWIDTH, 0, 400);
  1335. } // if
  1336. } // End of InitToolTip
  1337. // This function sets the text to show in the button tooltip.
  1338. //
  1339. // Parameters:
  1340. // [IN] nText
  1341. // ID number of the string resource containing the text to show.
  1342. // [IN] bActivate
  1343. // If TRUE the tooltip will be created active.
  1344. //
  1345. void CButtonST::SetTooltipText(int nText, BOOL bActivate)
  1346. {
  1347. CString sText;
  1348. // Load string resource
  1349. sText.LoadString(nText);
  1350. // If string resource is not empty
  1351. if (sText.IsEmpty() == FALSE) SetTooltipText((LPCTSTR)sText, bActivate);
  1352. } // End of SetTooltipText
  1353. // This function sets the text to show in the button tooltip.
  1354. //
  1355. // Parameters:
  1356. // [IN] lpszText
  1357. // Pointer to a null-terminated string containing the text to show.
  1358. // [IN] bActivate
  1359. // If TRUE the tooltip will be created active.
  1360. //
  1361. void CButtonST::SetTooltipText(LPCTSTR lpszText, BOOL bActivate)
  1362. {
  1363. // We cannot accept NULL pointer
  1364. if (lpszText == NULL) return;
  1365. // Initialize ToolTip
  1366. InitToolTip();
  1367. // If there is no tooltip defined then add it
  1368. if (m_ToolTip.GetToolCount() == 0)
  1369. {
  1370. CRect rectBtn;
  1371. GetClientRect(rectBtn);
  1372. m_ToolTip.AddTool(this, lpszText, rectBtn, 1);
  1373. } // if
  1374. // Set text for tooltip
  1375. m_ToolTip.UpdateTipText(lpszText, this, 1);
  1376. m_ToolTip.Activate(bActivate);
  1377. } // End of SetTooltipText
  1378. // This function enables or disables the button tooltip.
  1379. //
  1380. // Parameters:
  1381. // [IN] bActivate
  1382. // If TRUE the tooltip will be activated.
  1383. //
  1384. void CButtonST::ActivateTooltip(BOOL bActivate)
  1385. {
  1386. // If there is no tooltip then do nothing
  1387. if (m_ToolTip.GetToolCount() == 0) return;
  1388. // Activate tooltip
  1389. m_ToolTip.Activate(bActivate);
  1390. } // End of EnableTooltip
  1391. // This function returns if the button is the default button.
  1392. //
  1393. // Return value:
  1394. // TRUE
  1395. // The button is the default button.
  1396. // FALSE
  1397. // The button is not the default button.
  1398. //
  1399. BOOL CButtonST::GetDefault()
  1400. {
  1401. return m_bIsDefault;
  1402. } // End of GetDefault
  1403. // This function enables the transparent mode.
  1404. // Note: this operation is not reversible.
  1405. // DrawTransparent should be called just after the button is created.
  1406. // Do not use trasparent buttons until you really need it (you have a bitmapped
  1407. // background) since each transparent button makes a copy in memory of its background.
  1408. // This may bring unnecessary memory use and execution overload.
  1409. //
  1410. // Parameters:
  1411. // [IN] bRepaint
  1412. // If TRUE the control will be repainted.
  1413. //
  1414. void CButtonST::DrawTransparent(BOOL bRepaint)
  1415. {
  1416. m_bDrawTransparent = TRUE;
  1417. // Restore old bitmap (if any)
  1418. if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
  1419. {
  1420. m_dcBk.SelectObject(m_pbmpOldBk);
  1421. } // if
  1422. m_bmpBk.DeleteObject();
  1423. m_dcBk.DeleteDC();
  1424. // Repaint the button
  1425. if (bRepaint) Invalidate();
  1426. } // End of DrawTransparent
  1427. // This function sets the URL that will be opened when the button is clicked.
  1428. //
  1429. // Parameters:
  1430. // [IN] lpszURL
  1431. // Pointer to a null-terminated string that contains the URL.
  1432. // Pass NULL to removed any previously specified URL.
  1433. //
  1434. // Return value:
  1435. // BTNST_OK
  1436. // Function executed successfully.
  1437. //
  1438. DWORD CButtonST::SetURL(LPCTSTR lpszURL)
  1439. {
  1440. // Remove any existing URL
  1441. memset(m_szURL, 0, sizeof(m_szURL));
  1442. if (lpszURL)
  1443. {
  1444. // Store the URL
  1445. _tcsncpy(m_szURL, lpszURL, _MAX_PATH);
  1446. } // if
  1447. return BTNST_OK;
  1448. } // End of SetURL
  1449. // This function associates a menu to the button.
  1450. // The menu will be displayed clicking the button.
  1451. //
  1452. // Parameters:
  1453. // [IN] nMenu
  1454. // ID number of the menu resource.
  1455. // Pass NULL to remove any menu from the button.
  1456. // [IN] hParentWnd
  1457. // Handle to the window that owns the menu.
  1458. // This window receives all messages from the menu.
  1459. // [IN] bRepaint
  1460. // If TRUE the control will be repainted.
  1461. //
  1462. // Return value:
  1463. // BTNST_OK
  1464. // Function executed successfully.
  1465. // BTNST_INVALIDRESOURCE
  1466. // Failed loading the specified resource.
  1467. //
  1468. #ifndef BTNST_USE_BCMENU
  1469. DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint)
  1470. {
  1471. HINSTANCE hInstResource = NULL;
  1472. // Destroy any previous menu
  1473. if (m_hMenu)
  1474. {
  1475. ::DestroyMenu(m_hMenu);
  1476. m_hMenu = NULL;
  1477. m_hParentWndMenu = NULL;
  1478. m_bMenuDisplayed = FALSE;
  1479. } // if
  1480. // Load menu
  1481. if (nMenu)
  1482. {
  1483. // Find correct resource handle
  1484. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nMenu), RT_MENU);
  1485. // Load menu resource
  1486. m_hMenu = ::LoadMenu(hInstResource, MAKEINTRESOURCE(nMenu));
  1487. m_hParentWndMenu = hParentWnd;
  1488. // If something wrong
  1489. if (m_hMenu == NULL) return BTNST_INVALIDRESOURCE;
  1490. } // if
  1491. // Repaint the button
  1492. if (bRepaint) Invalidate();
  1493. return BTNST_OK;
  1494. } // End of SetMenu
  1495. #endif
  1496. // This function associates a menu to the button.
  1497. // The menu will be displayed clicking the button.
  1498. // The menu will be handled by the BCMenu class.
  1499. //
  1500. // Parameters:
  1501. // [IN] nMenu
  1502. // ID number of the menu resource.
  1503. // Pass NULL to remove any menu from the button.
  1504. // [IN] hParentWnd
  1505. // Handle to the window that owns the menu.
  1506. // This window receives all messages from the menu.
  1507. // [IN] bWinXPStyle
  1508. // If TRUE the menu will be displayed using the new Windows XP style.
  1509. // If FALSE the menu will be displayed using the standard style.
  1510. // [IN] nToolbarID
  1511. // Resource ID of the toolbar to be associated to the menu.
  1512. // [IN] sizeToolbarIcon
  1513. // A CSize object indicating the size (in pixels) of each icon into the toolbar.
  1514. // All icons into the toolbar must have the same size.
  1515. // [IN] crToolbarBk
  1516. // A COLORREF value indicating the color to use as background for the icons into the toolbar.
  1517. // This color will be used as the "transparent" color.
  1518. // [IN] bRepaint
  1519. // If TRUE the control will be repainted.
  1520. //
  1521. // Return value:
  1522. // BTNST_OK
  1523. // Function executed successfully.
  1524. // BTNST_INVALIDRESOURCE
  1525. // Failed loading the specified resource.
  1526. //
  1527. #ifdef BTNST_USE_BCMENU
  1528. DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bWinXPStyle, UINT nToolbarID, CSize sizeToolbarIcon, COLORREF crToolbarBk, BOOL bRepaint)
  1529. {
  1530. BOOL bRetValue = FALSE;
  1531. // Destroy any previous menu
  1532. if (m_menuPopup.m_hMenu)
  1533. {
  1534. m_menuPopup.DestroyMenu();
  1535. m_hParentWndMenu = NULL;
  1536. m_bMenuDisplayed = FALSE;
  1537. } // if
  1538. // Load menu
  1539. if (nMenu)
  1540. {
  1541. m_menuPopup.SetMenuDrawMode(bWinXPStyle);
  1542. // Load menu
  1543. bRetValue = m_menuPopup.LoadMenu(nMenu);
  1544. // If something wrong
  1545. if (bRetValue == FALSE) return BTNST_INVALIDRESOURCE;
  1546. // Load toolbar
  1547. if (nToolbarID)
  1548. {
  1549. m_menuPopup.SetBitmapBackground(crToolbarBk);
  1550. m_menuPopup.SetIconSize(sizeToolbarIcon.cx, sizeToolbarIcon.cy);
  1551. bRetValue = m_menuPopup.LoadToolbar(nToolbarID);
  1552. // If something wrong
  1553. if (bRetValue == FALSE)
  1554. {
  1555. m_menuPopup.DestroyMenu();
  1556. return BTNST_INVALIDRESOURCE;
  1557. } // if
  1558. } // if
  1559. m_hParentWndMenu = hParentWnd;
  1560. } // if
  1561. // Repaint the button
  1562. if (bRepaint) Invalidate();
  1563. return BTNST_OK;
  1564. } // End of SetMenu
  1565. #endif
  1566. // This function is called every time the button background needs to be painted.
  1567. // If the button is in transparent mode this function will NOT be called.
  1568. // This is a virtual function that can be rewritten in CButtonST-derived classes
  1569. // to produce a whole range of buttons not available by default.
  1570. //
  1571. // Parameters:
  1572. // [IN] pDC
  1573. // Pointer to a CDC object that indicates the device context.
  1574. // [IN] pRect
  1575. // Pointer to a CRect object that indicates the bounds of the
  1576. // area to be painted.
  1577. //
  1578. // Return value:
  1579. // BTNST_OK
  1580. // Function executed successfully.
  1581. //
  1582. DWORD CButtonST::OnDrawBackground(CDC* pDC, LPCRECT pRect)
  1583. {
  1584. COLORREF crColor;
  1585. if (m_bMouseOnButton || m_bIsPressed)
  1586. crColor = m_crColors[BTNST_COLOR_BK_IN];
  1587. else
  1588. {
  1589. if (m_bIsFocused)
  1590. crColor = m_crColors[BTNST_COLOR_BK_FOCUS];
  1591. else
  1592. crColor = m_crColors[BTNST_COLOR_BK_OUT];
  1593. } // else
  1594. CBrush brBackground(crColor);
  1595. pDC->FillRect(pRect, &brBackground);
  1596. return BTNST_OK;
  1597. } // End of OnDrawBackground
  1598. // This function is called every time the button border needs to be painted.
  1599. // If the button is in standard (not flat) mode this function will NOT be called.
  1600. // This is a virtual function that can be rewritten in CButtonST-derived classes
  1601. // to produce a whole range of buttons not available by default.
  1602. //
  1603. // Parameters:
  1604. // [IN] pDC
  1605. // Pointer to a CDC object that indicates the device context.
  1606. // [IN] pRect
  1607. // Pointer to a CRect object that indicates the bounds of the
  1608. // area to be painted.
  1609. //
  1610. // Return value:
  1611. // BTNST_OK
  1612. // Function executed successfully.
  1613. //
  1614. DWORD CButtonST::OnDrawBorder(CDC* pDC, LPCRECT pRect)
  1615. {
  1616. if (m_bIsPressed)
  1617. pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
  1618. else
  1619. pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
  1620. return BTNST_OK;
  1621. } // End of OnDrawBorder
  1622. #undef BS_TYPEMASK