BtnST.cpp 49 KB

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