BtnST.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. #include "stdafx.h"
  2. #include "BtnST.h"
  3. #ifdef BTNST_USE_SOUND
  4. #pragma comment(lib, "winmm.lib")
  5. #include <Mmsystem.h>
  6. #endif
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CButtonST
  14. // Mask for control's type
  15. #define BTS_TYPEMASK SS_TYPEMASK
  16. #ifndef TTM_SETTITLE
  17. #define TTM_SETTITLEA (WM_USER + 32) // wParam = TTI_*, lParam = char* szTitle
  18. #define TTM_SETTITLEW (WM_USER + 33) // wParam = TTI_*, lParam = wchar* szTitle
  19. #ifdef UNICODE
  20. #define TTM_SETTITLE TTM_SETTITLEW
  21. #else
  22. #define TTM_SETTITLE TTM_SETTITLEA
  23. #endif
  24. #endif
  25. #ifndef TTS_BALLOON
  26. #define TTS_BALLOON 0x40
  27. #endif
  28. CButtonST::CButtonST()
  29. {
  30. m_bIsPressed = FALSE;
  31. m_bIsFocused = FALSE;
  32. m_bIsDisabled = FALSE;
  33. m_bMouseOnButton = FALSE;
  34. FreeResources(FALSE);
  35. // Default type is "flat" button
  36. m_bIsFlat = TRUE;
  37. // Button will be tracked also if when the window is inactive (like Internet Explorer)
  38. m_bAlwaysTrack = TRUE;
  39. // By default draw border in "flat" button
  40. m_bDrawBorder = TRUE;
  41. // By default icon is aligned horizontally
  42. m_byAlign = ST_ALIGN_HORIZ;
  43. // By default use usual pressed style
  44. SetPressedStyle(BTNST_PRESSED_LEFTRIGHT, FALSE);
  45. // By default, for "flat" button, don't draw the focus rect
  46. m_bDrawFlatFocus = FALSE;
  47. // By default the button is not the default button
  48. m_bIsDefault = FALSE;
  49. // Invalid value, since type still unknown
  50. m_nTypeStyle = BTS_TYPEMASK;
  51. // By default the button is not a checkbox
  52. m_bIsCheckBox = FALSE;
  53. m_nCheck = 0;
  54. // Set default colors
  55. SetDefaultColors(FALSE);
  56. // No tooltip created
  57. m_ToolTip.m_hWnd = NULL;
  58. m_dwToolTipStyle = 0;
  59. // Do not draw as a transparent button
  60. m_bDrawTransparent = FALSE;
  61. m_pbmpOldBk = NULL;
  62. // No URL defined
  63. SetURL(NULL);
  64. // No cursor defined
  65. m_hCursor = NULL;
  66. // No associated menu
  67. #ifndef BTNST_USE_BCMENU
  68. m_hMenu = NULL;
  69. #endif
  70. m_hParentWndMenu = NULL;
  71. m_bMenuDisplayed = FALSE;
  72. m_bShowDisabledBitmap = TRUE;
  73. m_ptImageOrg.x = 3;
  74. m_ptImageOrg.y = 3;
  75. // No defined callbacks
  76. ::ZeroMemory(&m_csCallbacks, sizeof(m_csCallbacks));
  77. #ifdef BTNST_USE_SOUND
  78. // No defined sounds
  79. ::ZeroMemory(&m_csSounds, sizeof(m_csSounds));
  80. #endif
  81. } // End of CButtonST
  82. CButtonST::~CButtonST()
  83. {
  84. // Restore old bitmap (if any)
  85. if (m_dcBk.m_hDC && m_pbmpOldBk)
  86. {
  87. m_dcBk.SelectObject(m_pbmpOldBk);
  88. } // if
  89. FreeResources();
  90. // Destroy the cursor (if any)
  91. if (m_hCursor) ::DestroyCursor(m_hCursor);
  92. // Destroy the menu (if any)
  93. #ifdef BTNST_USE_BCMENU
  94. if (m_menuPopup.m_hMenu) m_menuPopup.DestroyMenu();
  95. #else
  96. if (m_hMenu) ::DestroyMenu(m_hMenu);
  97. #endif
  98. } // End of ~CButtonST
  99. BEGIN_MESSAGE_MAP(CButtonST, CButton)
  100. //{{AFX_MSG_MAP(CButtonST)
  101. ON_WM_SETCURSOR()
  102. ON_WM_KILLFOCUS()
  103. ON_WM_MOUSEMOVE()
  104. ON_WM_SYSCOLORCHANGE()
  105. ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
  106. ON_WM_ACTIVATE()
  107. ON_WM_ENABLE()
  108. ON_WM_CANCELMODE()
  109. ON_WM_GETDLGCODE()
  110. ON_WM_CTLCOLOR_REFLECT()
  111. //}}AFX_MSG_MAP
  112. #ifdef BTNST_USE_BCMENU
  113. ON_WM_MENUCHAR()
  114. ON_WM_MEASUREITEM()
  115. #endif
  116. ON_MESSAGE(BM_SETSTYLE, OnSetStyle)
  117. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  118. ON_MESSAGE(BM_SETCHECK, OnSetCheck)
  119. ON_MESSAGE(BM_GETCHECK, OnGetCheck)
  120. END_MESSAGE_MAP()
  121. void CButtonST::FreeResources(BOOL bCheckForNULL)
  122. {
  123. if (bCheckForNULL)
  124. {
  125. // Destroy icons
  126. // Note: the following two lines MUST be here! even if
  127. // BoundChecker says they are unnecessary!
  128. if (m_csIcons[0].hIcon) ::DestroyIcon(m_csIcons[0].hIcon);
  129. if (m_csIcons[1].hIcon) ::DestroyIcon(m_csIcons[1].hIcon);
  130. // Destroy bitmaps
  131. if (m_csBitmaps[0].hBitmap) ::DeleteObject(m_csBitmaps[0].hBitmap);
  132. if (m_csBitmaps[1].hBitmap) ::DeleteObject(m_csBitmaps[1].hBitmap);
  133. // Destroy mask bitmaps
  134. if (m_csBitmaps[0].hMask) ::DeleteObject(m_csBitmaps[0].hMask);
  135. if (m_csBitmaps[1].hMask) ::DeleteObject(m_csBitmaps[1].hMask);
  136. } // if
  137. ::ZeroMemory(&m_csIcons, sizeof(m_csIcons));
  138. ::ZeroMemory(&m_csBitmaps, sizeof(m_csBitmaps));
  139. } // End of FreeResources
  140. void CButtonST::PreSubclassWindow()
  141. {
  142. UINT nBS;
  143. nBS = GetButtonStyle();
  144. // Set initial control type
  145. m_nTypeStyle = nBS & BTS_TYPEMASK;
  146. // Check if this is a checkbox
  147. if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
  148. // Set initial default state flag
  149. if (m_nTypeStyle == BS_DEFPUSHBUTTON)
  150. {
  151. // Set default state for a default button
  152. m_bIsDefault = TRUE;
  153. // Adjust style for default button
  154. m_nTypeStyle = BS_PUSHBUTTON;
  155. } // If
  156. // You should not set the Owner Draw before this call
  157. // (don't use the resource editor "Owner Draw" or
  158. // ModifyStyle(0, BS_OWNERDRAW) before calling PreSubclassWindow() )
  159. ASSERT(m_nTypeStyle != BS_OWNERDRAW);
  160. // Switch to owner-draw
  161. ModifyStyle(BTS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
  162. CButton::PreSubclassWindow();
  163. } // End of PreSubclassWindow
  164. UINT CButtonST::OnGetDlgCode()
  165. {
  166. UINT nCode = CButton::OnGetDlgCode();
  167. // Tell the system if we want default state handling
  168. // (losing default state always allowed)
  169. nCode |= (m_bIsDefault ? DLGC_DEFPUSHBUTTON : DLGC_UNDEFPUSHBUTTON);
  170. return nCode;
  171. } // End of OnGetDlgCode
  172. BOOL CButtonST::PreTranslateMessage(MSG* pMsg)
  173. {
  174. InitToolTip();
  175. m_ToolTip.RelayEvent(pMsg);
  176. if (pMsg->message == WM_LBUTTONDBLCLK)
  177. pMsg->message = WM_LBUTTONDOWN;
  178. return CButton::PreTranslateMessage(pMsg);
  179. } // End of PreTranslateMessage
  180. HBRUSH CButtonST::CtlColor(CDC* pDC, UINT nCtlColor)
  181. {
  182. return (HBRUSH)::GetStockObject(NULL_BRUSH);
  183. } // End of CtlColor
  184. void CButtonST::OnSysColorChange()
  185. {
  186. CButton::OnSysColorChange();
  187. m_dcBk.DeleteDC();
  188. m_bmpBk.DeleteObject();
  189. SetDefaultColors();
  190. } // End of OnSysColorChange
  191. LRESULT CButtonST::OnSetStyle(WPARAM wParam, LPARAM lParam)
  192. {
  193. UINT nNewType = (wParam & BTS_TYPEMASK);
  194. // Update default state flag
  195. if (nNewType == BS_DEFPUSHBUTTON)
  196. {
  197. m_bIsDefault = TRUE;
  198. } // if
  199. else if (nNewType == BS_PUSHBUTTON)
  200. {
  201. // Losing default state always allowed
  202. m_bIsDefault = FALSE;
  203. } // if
  204. // Can't change control type after owner-draw is set.
  205. // Let the system process changes to other style bits
  206. // and redrawing, while keeping owner-draw style
  207. return DefWindowProc(BM_SETSTYLE,
  208. (wParam & ~BTS_TYPEMASK) | BS_OWNERDRAW, lParam);
  209. } // End of OnSetStyle
  210. LRESULT CButtonST::OnSetCheck(WPARAM wParam, LPARAM lParam)
  211. {
  212. ASSERT(m_bIsCheckBox);
  213. switch (wParam)
  214. {
  215. case BST_CHECKED:
  216. case BST_INDETERMINATE: // Indeterminate state is handled like checked state
  217. SetCheck(1);
  218. break;
  219. default:
  220. SetCheck(0);
  221. break;
  222. } // switch
  223. return 0;
  224. } // End of OnSetCheck
  225. LRESULT CButtonST::OnGetCheck(WPARAM wParam, LPARAM lParam)
  226. {
  227. ASSERT(m_bIsCheckBox);
  228. return GetCheck();
  229. } // End of OnGetCheck
  230. #ifdef BTNST_USE_BCMENU
  231. LRESULT CButtonST::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu)
  232. {
  233. LRESULT lResult;
  234. if (BCMenu::IsMenu(pMenu))
  235. lResult = BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
  236. else
  237. lResult = CButton::OnMenuChar(nChar, nFlags, pMenu);
  238. return lResult;
  239. } // End of OnMenuChar
  240. #endif
  241. #ifdef BTNST_USE_BCMENU
  242. void CButtonST::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  243. {
  244. BOOL bSetFlag = FALSE;
  245. if (lpMeasureItemStruct->CtlType == ODT_MENU)
  246. {
  247. if (IsMenu((HMENU)lpMeasureItemStruct->itemID) && BCMenu::IsMenu((HMENU)lpMeasureItemStruct->itemID))
  248. {
  249. m_menuPopup.MeasureItem(lpMeasureItemStruct);
  250. bSetFlag = TRUE;
  251. } // if
  252. } // if
  253. if (!bSetFlag) CButton::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
  254. } // End of OnMeasureItem
  255. #endif
  256. void CButtonST::OnEnable(BOOL bEnable)
  257. {
  258. CButton::OnEnable(bEnable);
  259. if (bEnable == FALSE)
  260. {
  261. CWnd* pWnd = GetParent()->GetNextDlgTabItem(this);
  262. if (pWnd)
  263. pWnd->SetFocus();
  264. else
  265. GetParent()->SetFocus();
  266. CancelHover();
  267. } // if
  268. } // End of OnEnable
  269. void CButtonST::OnKillFocus(CWnd * pNewWnd)
  270. {
  271. CButton::OnKillFocus(pNewWnd);
  272. CancelHover();
  273. } // End of OnKillFocus
  274. void CButtonST::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  275. {
  276. CButton::OnActivate(nState, pWndOther, bMinimized);
  277. if (nState == WA_INACTIVE) CancelHover();
  278. } // End of OnActivate
  279. void CButtonST::OnCancelMode()
  280. {
  281. CButton::OnCancelMode();
  282. CancelHover();
  283. } // End of OnCancelMode
  284. BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  285. {
  286. // If a cursor was specified then use it!
  287. if (m_hCursor != NULL)
  288. {
  289. ::SetCursor(m_hCursor);
  290. return TRUE;
  291. } // if
  292. return CButton::OnSetCursor(pWnd, nHitTest, message);
  293. } // End of OnSetCursor
  294. void CButtonST::CancelHover()
  295. {
  296. // Only for flat buttons
  297. if (m_bIsFlat)
  298. {
  299. if (m_bMouseOnButton)
  300. {
  301. m_bMouseOnButton = FALSE;
  302. Invalidate();
  303. } // if
  304. } // if
  305. } // End of CancelHover
  306. void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
  307. {
  308. CWnd* wndUnderMouse = NULL;
  309. CWnd* wndActive = this;
  310. TRACKMOUSEEVENT csTME;
  311. CButton::OnMouseMove(nFlags, point);
  312. ClientToScreen(&point);
  313. wndUnderMouse = WindowFromPoint(point);
  314. // If the mouse enter the button with the left button pressed then do nothing
  315. if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
  316. // If our button is not flat then do nothing
  317. if (m_bIsFlat == FALSE) return;
  318. if (m_bAlwaysTrack == FALSE) wndActive = GetActiveWindow();
  319. if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
  320. {
  321. if (!m_bMouseOnButton)
  322. {
  323. m_bMouseOnButton = TRUE;
  324. Invalidate();
  325. #ifdef BTNST_USE_SOUND
  326. // Play sound ?
  327. if (m_csSounds[0].lpszSound)
  328. ::PlaySound(m_csSounds[0].lpszSound, m_csSounds[0].hMod, m_csSounds[0].dwFlags);
  329. #endif
  330. csTME.cbSize = sizeof(csTME);
  331. csTME.dwFlags = TME_LEAVE;
  332. csTME.hwndTrack = m_hWnd;
  333. ::_TrackMouseEvent(&csTME);
  334. } // if
  335. } else CancelHover();
  336. } // End of OnMouseMove
  337. // Handler for WM_MOUSELEAVE
  338. LRESULT CButtonST::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  339. {
  340. CancelHover();
  341. return 0;
  342. } // End of OnMouseLeave
  343. BOOL CButtonST::OnClicked()
  344. {
  345. SetFocus();
  346. #ifdef BTNST_USE_SOUND
  347. // Play sound ?
  348. if (m_csSounds[1].lpszSound)
  349. ::PlaySound(m_csSounds[1].lpszSound, m_csSounds[1].hMod, m_csSounds[1].dwFlags);
  350. #endif
  351. if (m_bIsCheckBox)
  352. {
  353. m_nCheck = !m_nCheck;
  354. Invalidate();
  355. } // if
  356. else
  357. {
  358. // Handle the menu (if any)
  359. #ifdef BTNST_USE_BCMENU
  360. if (m_menuPopup.m_hMenu)
  361. #else
  362. if (m_hMenu)
  363. #endif
  364. {
  365. CRect rWnd;
  366. GetWindowRect(rWnd);
  367. m_bMenuDisplayed = TRUE;
  368. Invalidate();
  369. #ifdef BTNST_USE_BCMENU
  370. BCMenu* psub = (BCMenu*)m_menuPopup.GetSubMenu(0);
  371. if (m_csCallbacks.hWnd) ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)psub, m_csCallbacks.lParam);
  372. DWORD dwRetValue = psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, this, NULL);
  373. #else
  374. HMENU hSubMenu = ::GetSubMenu(m_hMenu, 0);
  375. if (m_csCallbacks.hWnd) ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)hSubMenu, m_csCallbacks.lParam);
  376. DWORD dwRetValue = ::TrackPopupMenuEx(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);
  377. #endif
  378. m_bMenuDisplayed = FALSE;
  379. Invalidate();
  380. if (dwRetValue)
  381. ::PostMessage(m_hParentWndMenu, WM_COMMAND, MAKEWPARAM(dwRetValue, 0), (LPARAM)NULL);
  382. } // if
  383. else
  384. {
  385. // Handle the URL (if any)
  386. if (_tcslen(m_szURL) > 0)
  387. {
  388. SHELLEXECUTEINFO csSEI;
  389. memset(&csSEI, 0, sizeof(csSEI));
  390. csSEI.cbSize = sizeof(SHELLEXECUTEINFO);
  391. csSEI.fMask = SEE_MASK_FLAG_NO_UI;
  392. csSEI.lpVerb = _T("open");
  393. csSEI.lpFile = m_szURL;
  394. csSEI.nShow = SW_SHOWMAXIMIZED;
  395. ::ShellExecuteEx(&csSEI);
  396. } // if
  397. } // else
  398. } // else
  399. return FALSE;
  400. } // End of OnClicked
  401. void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  402. {
  403. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  404. // Checkbox?
  405. if (m_bIsCheckBox)
  406. {
  407. m_bIsPressed = (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
  408. } // if
  409. else // Normal button OR other button style ...
  410. {
  411. m_bIsPressed = (lpDIS->itemState & ODS_SELECTED);
  412. // If there is a menu and it's displayed, draw the button as pressed
  413. if (
  414. #ifdef BTNST_USE_BCMENU
  415. m_menuPopup.m_hMenu
  416. #else
  417. m_hMenu
  418. #endif
  419. && m_bMenuDisplayed) m_bIsPressed = TRUE;
  420. } // else
  421. m_bIsFocused = (lpDIS->itemState & ODS_FOCUS);
  422. m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
  423. CRect itemRect = lpDIS->rcItem;
  424. pDC->SetBkMode(TRANSPARENT);
  425. // Prepare draw... paint button background
  426. // Draw transparent?
  427. if (m_bDrawTransparent)
  428. PaintBk(pDC);
  429. else
  430. OnDrawBackground(pDC, &itemRect);
  431. // Draw button border
  432. OnDrawBorder(pDC, &itemRect);
  433. // Read the button's title
  434. CString sTitle;
  435. GetWindowText(sTitle);
  436. CRect captionRect = lpDIS->rcItem;
  437. // Draw the icon
  438. if (m_csIcons[0].hIcon)
  439. {
  440. DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
  441. } // if
  442. if (m_csBitmaps[0].hBitmap)
  443. {
  444. pDC->SetBkColor(RGB(255,255,255));
  445. DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
  446. } // if
  447. // Write the button title (if any)
  448. if (sTitle.IsEmpty() == FALSE)
  449. {
  450. DrawTheText(pDC, (LPCTSTR)sTitle, &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
  451. } // if
  452. if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
  453. {
  454. // Draw the focus rect
  455. if (m_bIsFocused)
  456. {
  457. CRect focusRect = itemRect;
  458. focusRect.DeflateRect(3, 3);
  459. pDC->DrawFocusRect(&focusRect);
  460. } // if
  461. } // if
  462. } // End of DrawItem
  463. void CButtonST::PaintBk(CDC* pDC)
  464. {
  465. CClientDC clDC(GetParent());
  466. CRect rect;
  467. CRect rect1;
  468. GetClientRect(rect);
  469. GetWindowRect(rect1);
  470. GetParent()->ScreenToClient(rect1);
  471. if (m_dcBk.m_hDC == NULL)
  472. {
  473. m_dcBk.CreateCompatibleDC(&clDC);
  474. m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
  475. m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
  476. m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
  477. } // if
  478. pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
  479. } // End of PaintBk
  480. HBITMAP CButtonST::CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor)
  481. {
  482. HBITMAP hMask = NULL;
  483. HDC hdcSrc = NULL;
  484. HDC hdcDest = NULL;
  485. HBITMAP hbmSrcT = NULL;
  486. HBITMAP hbmDestT = NULL;
  487. COLORREF crSaveBk;
  488. COLORREF crSaveDestText;
  489. hMask = ::CreateBitmap(dwWidth, dwHeight, 1, 1, NULL);
  490. if (hMask == NULL) return NULL;
  491. hdcSrc = ::CreateCompatibleDC(NULL);
  492. hdcDest = ::CreateCompatibleDC(NULL);
  493. hbmSrcT = (HBITMAP)::SelectObject(hdcSrc, hSourceBitmap);
  494. hbmDestT = (HBITMAP)::SelectObject(hdcDest, hMask);
  495. crSaveBk = ::SetBkColor(hdcSrc, crTransColor);
  496. ::BitBlt(hdcDest, 0, 0, dwWidth, dwHeight, hdcSrc, 0, 0, SRCCOPY);
  497. crSaveDestText = ::SetTextColor(hdcSrc, RGB(255, 255, 255));
  498. ::SetBkColor(hdcSrc,RGB(0, 0, 0));
  499. ::BitBlt(hdcSrc, 0, 0, dwWidth, dwHeight, hdcDest, 0, 0, SRCAND);
  500. SetTextColor(hdcDest, crSaveDestText);
  501. ::SetBkColor(hdcSrc, crSaveBk);
  502. ::SelectObject(hdcSrc, hbmSrcT);
  503. ::SelectObject(hdcDest, hbmDestT);
  504. ::DeleteDC(hdcSrc);
  505. ::DeleteDC(hdcDest);
  506. return hMask;
  507. } // End of CreateBitmapMask
  508. //
  509. // Parameters:
  510. // [IN] bHasTitle
  511. // TRUE if the button has a text
  512. // [IN] rpItem
  513. // A pointer to a RECT structure indicating the allowed paint area
  514. // [IN/OUT]rpTitle
  515. // A pointer to a CRect object indicating the paint area reserved for the
  516. // text. This structure will be modified if necessary.
  517. // [IN] bIsPressed
  518. // TRUE if the button is currently pressed
  519. // [IN] dwWidth
  520. // Width of the image (icon or bitmap)
  521. // [IN] dwHeight
  522. // Height of the image (icon or bitmap)
  523. // [OUT] rpImage
  524. // A pointer to a CRect object that will receive the area available to the image
  525. //
  526. void CButtonST::PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage)
  527. {
  528. CRect rBtn;
  529. rpImage->CopyRect(rpItem);
  530. switch (m_byAlign)
  531. {
  532. case ST_ALIGN_HORIZ:
  533. if (bHasTitle == FALSE)
  534. {
  535. // Center image horizontally
  536. rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
  537. }
  538. else
  539. {
  540. // Image must be placed just inside the focus rect
  541. rpImage->left += m_ptImageOrg.x;
  542. rpTitle->left += dwWidth + m_ptImageOrg.x;
  543. }
  544. // Center image vertically
  545. rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
  546. break;
  547. case ST_ALIGN_HORIZ_RIGHT:
  548. GetClientRect(&rBtn);
  549. if (bHasTitle == FALSE)
  550. {
  551. // Center image horizontally
  552. rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
  553. }
  554. else
  555. {
  556. // Image must be placed just inside the focus rect
  557. rpTitle->right = rpTitle->Width() - dwWidth - m_ptImageOrg.x;
  558. rpTitle->left = m_ptImageOrg.x;
  559. rpImage->left = rBtn.right - dwWidth - m_ptImageOrg.x;
  560. // Center image vertically
  561. rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
  562. }
  563. break;
  564. case ST_ALIGN_VERT:
  565. // Center image horizontally
  566. rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
  567. if (bHasTitle == FALSE)
  568. {
  569. // Center image vertically
  570. rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
  571. }
  572. else
  573. {
  574. rpImage->top = m_ptImageOrg.y;
  575. rpTitle->top += dwHeight;
  576. }
  577. break;
  578. case ST_ALIGN_OVERLAP:
  579. break;
  580. } // switch
  581. // If button is pressed then press image also
  582. if (bIsPressed && m_bIsCheckBox == FALSE)
  583. rpImage->OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
  584. } // End of PrepareImageRect
  585. void CButtonST::DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
  586. {
  587. BYTE byIndex = 0;
  588. // Select the icon to use
  589. if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
  590. byIndex = 0;
  591. else
  592. byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
  593. CRect rImage;
  594. PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);
  595. // Ole'!
  596. pDC->DrawState( rImage.TopLeft(),
  597. rImage.Size(),
  598. m_csIcons[byIndex].hIcon,
  599. (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
  600. (CBrush*)NULL);
  601. } // End of DrawTheIcon
  602. void CButtonST::DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
  603. {
  604. HDC hdcBmpMem = NULL;
  605. HBITMAP hbmOldBmp = NULL;
  606. HDC hdcMem = NULL;
  607. HBITMAP hbmT = NULL;
  608. BYTE byIndex = 0;
  609. // Select the bitmap to use
  610. if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
  611. byIndex = 0;
  612. else
  613. byIndex = (m_csBitmaps[1].hBitmap == NULL ? 0 : 1);
  614. CRect rImage;
  615. PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, &rImage);
  616. hdcBmpMem = ::CreateCompatibleDC(pDC->m_hDC);
  617. hbmOldBmp = (HBITMAP)::SelectObject(hdcBmpMem, m_csBitmaps[byIndex].hBitmap);
  618. hdcMem = ::CreateCompatibleDC(NULL);
  619. hbmT = (HBITMAP)::SelectObject(hdcMem, m_csBitmaps[byIndex].hMask);
  620. if (bIsDisabled && m_bShowDisabledBitmap)
  621. {
  622. HDC hDC = NULL;
  623. HBITMAP hBitmap = NULL;
  624. hDC = ::CreateCompatibleDC(pDC->m_hDC);
  625. hBitmap = ::CreateCompatibleBitmap(pDC->m_hDC, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight);
  626. HBITMAP hOldBmp2 = (HBITMAP)::SelectObject(hDC, hBitmap);
  627. RECT rRect;
  628. rRect.left = 0;
  629. rRect.top = 0;
  630. rRect.right = rImage.right + 1;
  631. rRect.bottom = rImage.bottom + 1;
  632. ::FillRect(hDC, &rRect, (HBRUSH)RGB(255, 255, 255));
  633. COLORREF crOldColor = ::SetBkColor(hDC, RGB(255,255,255));
  634. ::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
  635. ::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
  636. ::SetBkColor(hDC, crOldColor);
  637. ::SelectObject(hDC, hOldBmp2);
  638. ::DeleteDC(hDC);
  639. pDC->DrawState( CPoint(rImage.left/*+1*/, rImage.top),
  640. CSize(m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight),
  641. hBitmap, DST_BITMAP | DSS_DISABLED);
  642. ::DeleteObject(hBitmap);
  643. } // if
  644. else
  645. {
  646. ::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
  647. ::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
  648. } // else
  649. ::SelectObject(hdcMem, hbmT);
  650. ::DeleteDC(hdcMem);
  651. ::SelectObject(hdcBmpMem, hbmOldBmp);
  652. ::DeleteDC(hdcBmpMem);
  653. } // End of DrawTheBitmap
  654. void CButtonST::DrawTheText(CDC* pDC, LPCTSTR lpszText, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
  655. {
  656. // Draw the button's title
  657. // If button is pressed then "press" title also
  658. if (m_bIsPressed && m_bIsCheckBox == FALSE)
  659. rpCaption->OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
  660. // ONLY FOR DEBUG
  661. //CBrush brBtnShadow(RGB(255, 0, 0));
  662. //pDC->FrameRect(rCaption, &brBtnShadow);
  663. // Center text
  664. CRect centerRect = rpCaption;
  665. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
  666. rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())/2, (centerRect.Height() - rpCaption->Height())/2);
  667. /* RFU
  668. rpCaption->OffsetRect(0, (centerRect.Height() - rpCaption->Height())/2);
  669. rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())-4, (centerRect.Height() - rpCaption->Height())/2);
  670. */
  671. pDC->SetBkMode(TRANSPARENT);
  672. /*
  673. pDC->DrawState(rCaption->TopLeft(), rCaption->Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
  674. TRUE, 0, (CBrush*)NULL);
  675. */
  676. if (m_bIsDisabled)
  677. {
  678. rpCaption->OffsetRect(1, 1);
  679. pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
  680. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
  681. rpCaption->OffsetRect(-1, -1);
  682. pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
  683. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
  684. } // if
  685. else
  686. {
  687. if (m_bMouseOnButton || m_bIsPressed)
  688. {
  689. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
  690. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
  691. } // if
  692. else
  693. {
  694. if (m_bIsFocused)
  695. {
  696. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_FOCUS]);
  697. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_FOCUS]);
  698. } // if
  699. else
  700. {
  701. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
  702. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
  703. } // else
  704. } // else
  705. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
  706. } // if
  707. } // End of DrawTheText
  708. // This function creates a grayscale bitmap starting from a given bitmap.
  709. // The resulting bitmap will have the same size of the original one.
  710. //
  711. // Parameters:
  712. // [IN] hBitmap
  713. // Handle to the original bitmap.
  714. // [IN] dwWidth
  715. // Specifies the bitmap width, in pixels.
  716. // [IN] dwHeight
  717. // Specifies the bitmap height, in pixels.
  718. // [IN] crTrans
  719. // Color to be used as transparent color. This color will be left unchanged.
  720. //
  721. // Return value:
  722. // If the function succeeds, the return value is the handle to the newly created
  723. // grayscale bitmap.
  724. // If the function fails, the return value is NULL.
  725. //
  726. HBITMAP CButtonST::CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
  727. {
  728. HBITMAP hGrayBitmap = NULL;
  729. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  730. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  731. hMainDC = ::GetDC(NULL);
  732. if (hMainDC == NULL) return NULL;
  733. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  734. if (hMemDC1 == NULL)
  735. {
  736. ::ReleaseDC(NULL, hMainDC);
  737. return NULL;
  738. } // if
  739. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  740. if (hMemDC2 == NULL)
  741. {
  742. ::DeleteDC(hMemDC1);
  743. ::ReleaseDC(NULL, hMainDC);
  744. return NULL;
  745. } // if
  746. hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
  747. if (hGrayBitmap)
  748. {
  749. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
  750. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
  751. //::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
  752. DWORD dwLoopY = 0, dwLoopX = 0;
  753. COLORREF crPixel = 0;
  754. BYTE byNewPixel = 0;
  755. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  756. {
  757. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  758. {
  759. crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
  760. byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
  761. if (crPixel != crTrans)
  762. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
  763. else
  764. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
  765. } // for
  766. } // for
  767. ::SelectObject(hMemDC1, hOldBmp1);
  768. ::SelectObject(hMemDC2, hOldBmp2);
  769. } // if
  770. ::DeleteDC(hMemDC1);
  771. ::DeleteDC(hMemDC2);
  772. ::ReleaseDC(NULL, hMainDC);
  773. return hGrayBitmap;
  774. } // End of CreateGrayscaleBitmap
  775. // This function creates a bitmap that is 25% darker than the original.
  776. // The resulting bitmap will have the same size of the original one.
  777. //
  778. // Parameters:
  779. // [IN] hBitmap
  780. // Handle to the original bitmap.
  781. // [IN] dwWidth
  782. // Specifies the bitmap width, in pixels.
  783. // [IN] dwHeight
  784. // Specifies the bitmap height, in pixels.
  785. // [IN] crTrans
  786. // Color to be used as transparent color. This color will be left unchanged.
  787. //
  788. // Return value:
  789. // If the function succeeds, the return value is the handle to the newly created
  790. // darker bitmap.
  791. // If the function fails, the return value is NULL.
  792. //
  793. HBITMAP CButtonST::CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
  794. {
  795. HBITMAP hGrayBitmap = NULL;
  796. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  797. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  798. hMainDC = ::GetDC(NULL);
  799. if (hMainDC == NULL) return NULL;
  800. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  801. if (hMemDC1 == NULL)
  802. {
  803. ::ReleaseDC(NULL, hMainDC);
  804. return NULL;
  805. } // if
  806. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  807. if (hMemDC2 == NULL)
  808. {
  809. ::DeleteDC(hMemDC1);
  810. ::ReleaseDC(NULL, hMainDC);
  811. return NULL;
  812. } // if
  813. hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
  814. if (hGrayBitmap)
  815. {
  816. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
  817. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
  818. //::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
  819. DWORD dwLoopY = 0, dwLoopX = 0;
  820. COLORREF crPixel = 0;
  821. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  822. {
  823. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  824. {
  825. crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
  826. if (crPixel != crTrans)
  827. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
  828. else
  829. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
  830. } // for
  831. } // for
  832. ::SelectObject(hMemDC1, hOldBmp1);
  833. ::SelectObject(hMemDC2, hOldBmp2);
  834. } // if
  835. ::DeleteDC(hMemDC1);
  836. ::DeleteDC(hMemDC2);
  837. ::ReleaseDC(NULL, hMainDC);
  838. return hGrayBitmap;
  839. } // End of CreateDarkerBitmap
  840. // This function creates a grayscale icon starting from a given icon.
  841. // The resulting icon will have the same size of the original one.
  842. //
  843. // Parameters:
  844. // [IN] hIcon
  845. // Handle to the original icon.
  846. //
  847. // Return value:
  848. // If the function succeeds, the return value is the handle to the newly created
  849. // grayscale icon.
  850. // If the function fails, the return value is NULL.
  851. //
  852. // Updates:
  853. // 26/Nov/2002 Restored 1 BitBlt operation
  854. // 03/May/2002 Removed dependancy from m_hWnd
  855. // Removed 1 BitBlt operation
  856. //
  857. HICON CButtonST::CreateGrayscaleIcon(HICON hIcon)
  858. {
  859. HICON hGrayIcon = NULL;
  860. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  861. BITMAP bmp;
  862. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  863. ICONINFO csII, csGrayII;
  864. BOOL bRetValue = FALSE;
  865. bRetValue = ::GetIconInfo(hIcon, &csII);
  866. if (bRetValue == FALSE) return NULL;
  867. hMainDC = ::GetDC(NULL);
  868. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  869. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  870. if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  871. if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
  872. {
  873. DWORD dwWidth = csII.xHotspot*2;
  874. DWORD dwHeight = csII.yHotspot*2;
  875. csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
  876. if (csGrayII.hbmColor)
  877. {
  878. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
  879. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
  880. //::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
  881. DWORD dwLoopY = 0, dwLoopX = 0;
  882. COLORREF crPixel = 0;
  883. BYTE byNewPixel = 0;
  884. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  885. {
  886. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  887. {
  888. crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
  889. byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
  890. if (crPixel)
  891. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
  892. else
  893. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
  894. } // for
  895. } // for
  896. ::SelectObject(hMemDC1, hOldBmp1);
  897. ::SelectObject(hMemDC2, hOldBmp2);
  898. csGrayII.hbmMask = csII.hbmMask;
  899. csGrayII.fIcon = TRUE;
  900. hGrayIcon = ::CreateIconIndirect(&csGrayII);
  901. } // if
  902. ::DeleteObject(csGrayII.hbmColor);
  903. //::DeleteObject(csGrayII.hbmMask);
  904. } // if
  905. ::DeleteObject(csII.hbmColor);
  906. ::DeleteObject(csII.hbmMask);
  907. ::DeleteDC(hMemDC1);
  908. ::DeleteDC(hMemDC2);
  909. ::ReleaseDC(NULL, hMainDC);
  910. return hGrayIcon;
  911. } // End of CreateGrayscaleIcon
  912. // This function creates a icon that is 25% darker than the original.
  913. // The resulting icon will have the same size of the original one.
  914. //
  915. // Parameters:
  916. // [IN] hIcon
  917. // Handle to the original icon.
  918. //
  919. // Return value:
  920. // If the function succeeds, the return value is the handle to the newly created
  921. // darker icon.
  922. // If the function fails, the return value is NULL.
  923. //
  924. HICON CButtonST::CreateDarkerIcon(HICON hIcon)
  925. {
  926. HICON hGrayIcon = NULL;
  927. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  928. BITMAP bmp;
  929. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  930. ICONINFO csII, csGrayII;
  931. BOOL bRetValue = FALSE;
  932. bRetValue = ::GetIconInfo(hIcon, &csII);
  933. if (bRetValue == FALSE) return NULL;
  934. hMainDC = ::GetDC(NULL);
  935. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  936. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  937. if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  938. if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
  939. {
  940. DWORD dwWidth = csII.xHotspot*2;
  941. DWORD dwHeight = csII.yHotspot*2;
  942. csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
  943. if (csGrayII.hbmColor)
  944. {
  945. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
  946. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
  947. //::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
  948. DWORD dwLoopY = 0, dwLoopX = 0;
  949. COLORREF crPixel = 0;
  950. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  951. {
  952. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  953. {
  954. crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
  955. if (crPixel)
  956. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
  957. else
  958. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
  959. } // for
  960. } // for
  961. ::SelectObject(hMemDC1, hOldBmp1);
  962. ::SelectObject(hMemDC2, hOldBmp2);
  963. csGrayII.hbmMask = csII.hbmMask;
  964. csGrayII.fIcon = TRUE;
  965. hGrayIcon = ::CreateIconIndirect(&csGrayII);
  966. } // if
  967. ::DeleteObject(csGrayII.hbmColor);
  968. //::DeleteObject(csGrayII.hbmMask);
  969. } // if
  970. ::DeleteObject(csII.hbmColor);
  971. ::DeleteObject(csII.hbmMask);
  972. ::DeleteDC(hMemDC1);
  973. ::DeleteDC(hMemDC2);
  974. ::ReleaseDC(NULL, hMainDC);
  975. return hGrayIcon;
  976. } // End of CreateDarkerIcon
  977. COLORREF CButtonST::DarkenColor(COLORREF crColor, double dFactor)
  978. {
  979. if (dFactor > 0.0 && dFactor <= 1.0)
  980. {
  981. BYTE red,green,blue,lightred,lightgreen,lightblue;
  982. red = GetRValue(crColor);
  983. green = GetGValue(crColor);
  984. blue = GetBValue(crColor);
  985. lightred = (BYTE)(red-(dFactor * red));
  986. lightgreen = (BYTE)(green-(dFactor * green));
  987. lightblue = (BYTE)(blue-(dFactor * blue));
  988. crColor = RGB(lightred,lightgreen,lightblue);
  989. } // if
  990. return crColor;
  991. } // End of DarkenColor
  992. // This function assigns icons to the button.
  993. // Any previous icon or bitmap will be removed.
  994. //
  995. // Parameters:
  996. // [IN] nIconIn
  997. // ID number of the icon resource to show when the mouse is over the button.
  998. // Pass NULL to remove any icon from the button.
  999. // [IN] nCxDesiredIn
  1000. // Specifies the width, in pixels, of the icon to load.
  1001. // [IN] nCyDesiredIn
  1002. // Specifies the height, in pixels, of the icon to load.
  1003. // [IN] nIconOut
  1004. // ID number of the icon resource to show when the mouse is outside the button.
  1005. // Can be NULL.
  1006. // If this parameter is the special value BTNST_AUTO_GRAY (cast to int) the second
  1007. // icon will be automatically created starting from nIconIn and converted to grayscale.
  1008. // If this parameter is the special value BTNST_AUTO_DARKER (cast to int) the second
  1009. // icon will be automatically created 25% darker starting from nIconIn.
  1010. // [IN] nCxDesiredOut
  1011. // Specifies the width, in pixels, of the icon to load.
  1012. // [IN] nCyDesiredOut
  1013. // Specifies the height, in pixels, of the icon to load.
  1014. //
  1015. // Return value:
  1016. // BTNST_OK
  1017. // Function executed successfully.
  1018. // BTNST_INVALIDRESOURCE
  1019. // Failed loading the specified resource.
  1020. //
  1021. DWORD CButtonST::SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut, int nCxDesiredOut, int nCyDesiredOut)
  1022. {
  1023. HICON hIconIn = NULL;
  1024. HICON hIconOut = NULL;
  1025. HINSTANCE hInstResource = NULL;
  1026. // Find correct resource handle
  1027. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
  1028. // Set icon when the mouse is IN the button
  1029. hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, nCxDesiredIn, nCyDesiredIn, 0);
  1030. // Set icon when the mouse is OUT the button
  1031. switch (nIconOut)
  1032. {
  1033. case NULL:
  1034. break;
  1035. case (int)BTNST_AUTO_GRAY:
  1036. hIconOut = BTNST_AUTO_GRAY;
  1037. break;
  1038. case (int)BTNST_AUTO_DARKER:
  1039. hIconOut = BTNST_AUTO_DARKER;
  1040. break;
  1041. default:
  1042. hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, nCxDesiredOut, nCyDesiredOut, 0);
  1043. break;
  1044. } // switch
  1045. return SetIcon(hIconIn, hIconOut);
  1046. } // End of SetIcon
  1047. // This function assigns icons to the button.
  1048. // Any previous icon or bitmap will be removed.
  1049. //
  1050. // Parameters:
  1051. // [IN] nIconIn
  1052. // ID number of the icon resource to show when the mouse is over the button.
  1053. // Pass NULL to remove any icon from the button.
  1054. // [IN] nIconOut
  1055. // ID number of the icon resource to show when the mouse is outside the button.
  1056. // Can be NULL.
  1057. // If this parameter is the special value BTNST_AUTO_GRAY (cast to int) the second
  1058. // icon will be automatically created starting from nIconIn and converted to grayscale.
  1059. // If this parameter is the special value BTNST_AUTO_DARKER (cast to int) the second
  1060. // icon will be automatically created 25% darker starting from nIconIn.
  1061. //
  1062. // Return value:
  1063. // BTNST_OK
  1064. // Function executed successfully.
  1065. // BTNST_INVALIDRESOURCE
  1066. // Failed loading the specified resource.
  1067. //
  1068. DWORD CButtonST::SetIcon(int nIconIn, int nIconOut)
  1069. {
  1070. return SetIcon(nIconIn, 0, 0, nIconOut, 0, 0);
  1071. } // End of SetIcon
  1072. // This function assigns icons to the button.
  1073. // Any previous icon or bitmap will be removed.
  1074. //
  1075. // Parameters:
  1076. // [IN] hIconIn
  1077. // Handle fo the icon to show when the mouse is over the button.
  1078. // Pass NULL to remove any icon from the button.
  1079. // [IN] hIconOut
  1080. // Handle to the icon to show when the mouse is outside the button.
  1081. // Can be NULL.
  1082. // If this parameter is the special value BTNST_AUTO_GRAY the second
  1083. // icon will be automatically created starting from hIconIn and converted to grayscale.
  1084. // If this parameter is the special value BTNST_AUTO_DARKER the second
  1085. // icon will be automatically created 25% darker starting from hIconIn.
  1086. //
  1087. // Return value:
  1088. // BTNST_OK
  1089. // Function executed successfully.
  1090. // BTNST_INVALIDRESOURCE
  1091. // Failed loading the specified resource.
  1092. //
  1093. DWORD CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
  1094. {
  1095. BOOL bRetValue;
  1096. ICONINFO ii;
  1097. // Free any loaded resource
  1098. FreeResources();
  1099. if (hIconIn)
  1100. {
  1101. // Icon when mouse over button?
  1102. m_csIcons[0].hIcon = hIconIn;
  1103. // Get icon dimension
  1104. ::ZeroMemory(&ii, sizeof(ICONINFO));
  1105. bRetValue = ::GetIconInfo(hIconIn, &ii);
  1106. if (bRetValue == FALSE)
  1107. {
  1108. FreeResources();
  1109. return BTNST_INVALIDRESOURCE;
  1110. } // if
  1111. m_csIcons[0].dwWidth = (DWORD)(ii.xHotspot * 2);
  1112. m_csIcons[0].dwHeight = (DWORD)(ii.yHotspot * 2);
  1113. ::DeleteObject(ii.hbmMask);
  1114. ::DeleteObject(ii.hbmColor);
  1115. // Icon when mouse outside button?
  1116. if (hIconOut)
  1117. {
  1118. switch ((int)hIconOut)
  1119. {
  1120. case (int)BTNST_AUTO_GRAY:
  1121. hIconOut = CreateGrayscaleIcon(hIconIn);
  1122. break;
  1123. case (int)BTNST_AUTO_DARKER:
  1124. hIconOut = CreateDarkerIcon(hIconIn);
  1125. break;
  1126. } // switch
  1127. m_csIcons[1].hIcon = hIconOut;
  1128. // Get icon dimension
  1129. ::ZeroMemory(&ii, sizeof(ICONINFO));
  1130. bRetValue = ::GetIconInfo(hIconOut, &ii);
  1131. if (bRetValue == FALSE)
  1132. {
  1133. FreeResources();
  1134. return BTNST_INVALIDRESOURCE;
  1135. } // if
  1136. m_csIcons[1].dwWidth = (DWORD)(ii.xHotspot * 2);
  1137. m_csIcons[1].dwHeight = (DWORD)(ii.yHotspot * 2);
  1138. ::DeleteObject(ii.hbmMask);
  1139. ::DeleteObject(ii.hbmColor);
  1140. } // if
  1141. } // if
  1142. Invalidate();
  1143. return BTNST_OK;
  1144. } // End of SetIcon
  1145. // This function assigns bitmaps to the button.
  1146. // Any previous icon or bitmap will be removed.
  1147. //
  1148. // Parameters:
  1149. // [IN] nBitmapIn
  1150. // ID number of the bitmap resource to show when the mouse is over the button.
  1151. // Pass NULL to remove any bitmap from the button.
  1152. // [IN] crTransColorIn
  1153. // Color (inside nBitmapIn) to be used as transparent color.
  1154. // [IN] nBitmapOut
  1155. // ID number of the bitmap resource to show when the mouse is outside the button.
  1156. // Can be NULL.
  1157. // [IN] crTransColorOut
  1158. // Color (inside nBitmapOut) to be used as transparent color.
  1159. //
  1160. // Return value:
  1161. // BTNST_OK
  1162. // Function executed successfully.
  1163. // BTNST_INVALIDRESOURCE
  1164. // Failed loading the specified resource.
  1165. // BTNST_FAILEDMASK
  1166. // Failed creating mask bitmap.
  1167. //
  1168. DWORD CButtonST::SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut, COLORREF crTransColorOut)
  1169. {
  1170. HBITMAP hBitmapIn = NULL;
  1171. HBITMAP hBitmapOut = NULL;
  1172. HINSTANCE hInstResource = NULL;
  1173. // Find correct resource handle
  1174. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapIn), RT_BITMAP);
  1175. // Load bitmap In
  1176. hBitmapIn = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
  1177. // Load bitmap Out
  1178. switch (nBitmapOut)
  1179. {
  1180. case NULL:
  1181. break;
  1182. case (int)BTNST_AUTO_GRAY:
  1183. hBitmapOut = (HBITMAP)BTNST_AUTO_GRAY;
  1184. break;
  1185. case (int)BTNST_AUTO_DARKER:
  1186. hBitmapOut = (HBITMAP)BTNST_AUTO_DARKER;
  1187. break;
  1188. default:
  1189. hBitmapOut = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
  1190. break;
  1191. } // if
  1192. return SetBitmaps(hBitmapIn, crTransColorIn, hBitmapOut, crTransColorOut);
  1193. } // End of SetBitmaps
  1194. // This function assigns bitmaps to the button.
  1195. // Any previous icon or bitmap will be removed.
  1196. //
  1197. // Parameters:
  1198. // [IN] hBitmapIn
  1199. // Handle fo the bitmap to show when the mouse is over the button.
  1200. // Pass NULL to remove any bitmap from the button.
  1201. // [IN] crTransColorIn
  1202. // Color (inside hBitmapIn) to be used as transparent color.
  1203. // [IN] hBitmapOut
  1204. // Handle to the bitmap to show when the mouse is outside the button.
  1205. // Can be NULL.
  1206. // [IN] crTransColorOut
  1207. // Color (inside hBitmapOut) to be used as transparent color.
  1208. //
  1209. // Return value:
  1210. // BTNST_OK
  1211. // Function executed successfully.
  1212. // BTNST_INVALIDRESOURCE
  1213. // Failed loading the specified resource.
  1214. // BTNST_FAILEDMASK
  1215. // Failed creating mask bitmap.
  1216. //
  1217. DWORD CButtonST::SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut, COLORREF crTransColorOut)
  1218. {
  1219. int nRetValue = 0;
  1220. BITMAP csBitmapSize;
  1221. // Free any loaded resource
  1222. FreeResources();
  1223. if (hBitmapIn)
  1224. {
  1225. m_csBitmaps[0].hBitmap = hBitmapIn;
  1226. m_csBitmaps[0].crTransparent = crTransColorIn;
  1227. // Get bitmap size
  1228. nRetValue = ::GetObject(hBitmapIn, sizeof(csBitmapSize), &csBitmapSize);
  1229. if (nRetValue == 0)
  1230. {
  1231. FreeResources();
  1232. return BTNST_INVALIDRESOURCE;
  1233. } // if
  1234. m_csBitmaps[0].dwWidth = (DWORD)csBitmapSize.bmWidth;
  1235. m_csBitmaps[0].dwHeight = (DWORD)csBitmapSize.bmHeight;
  1236. // Create grayscale/darker bitmap BEFORE mask (of hBitmapIn)
  1237. switch ((int)hBitmapOut)
  1238. {
  1239. case (int)BTNST_AUTO_GRAY:
  1240. hBitmapOut = CreateGrayscaleBitmap(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  1241. m_csBitmaps[1].hBitmap = hBitmapOut;
  1242. crTransColorOut = crTransColorIn;
  1243. break;
  1244. case (int)BTNST_AUTO_DARKER:
  1245. hBitmapOut = CreateDarkerBitmap(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  1246. m_csBitmaps[1].hBitmap = hBitmapOut;
  1247. crTransColorOut = crTransColorIn;
  1248. break;
  1249. } // switch
  1250. // Create mask for bitmap In
  1251. m_csBitmaps[0].hMask = CreateBitmapMask(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  1252. if (m_csBitmaps[0].hMask == NULL)
  1253. {
  1254. FreeResources();
  1255. return BTNST_FAILEDMASK;
  1256. } // if
  1257. if (hBitmapOut)
  1258. {
  1259. m_csBitmaps[1].hBitmap = hBitmapOut;
  1260. m_csBitmaps[1].crTransparent = crTransColorOut;
  1261. // Get bitmap size
  1262. nRetValue = ::GetObject(hBitmapOut, sizeof(csBitmapSize), &csBitmapSize);
  1263. if (nRetValue == 0)
  1264. {
  1265. FreeResources();
  1266. return BTNST_INVALIDRESOURCE;
  1267. } // if
  1268. m_csBitmaps[1].dwWidth = (DWORD)csBitmapSize.bmWidth;
  1269. m_csBitmaps[1].dwHeight = (DWORD)csBitmapSize.bmHeight;
  1270. // Create mask for bitmap Out
  1271. m_csBitmaps[1].hMask = CreateBitmapMask(hBitmapOut, m_csBitmaps[1].dwWidth, m_csBitmaps[1].dwHeight, crTransColorOut);
  1272. if (m_csBitmaps[1].hMask == NULL)
  1273. {
  1274. FreeResources();
  1275. return BTNST_FAILEDMASK;
  1276. } // if
  1277. } // if
  1278. } // if
  1279. Invalidate();
  1280. return BTNST_OK;
  1281. } // End of SetBitmaps
  1282. // This functions sets the button to have a standard or flat style.
  1283. //
  1284. // Parameters:
  1285. // [IN] bFlat
  1286. // If TRUE the button will have a flat style, else
  1287. // will have a standard style.
  1288. // By default, CButtonST buttons are flat.
  1289. // [IN] bRepaint
  1290. // If TRUE the control will be repainted.
  1291. //
  1292. // Return value:
  1293. // BTNST_OK
  1294. // Function executed successfully.
  1295. //
  1296. DWORD CButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
  1297. {
  1298. m_bIsFlat = bFlat;
  1299. if (bRepaint) Invalidate();
  1300. return BTNST_OK;
  1301. } // End of SetFlat
  1302. // This function sets the alignment type between icon/bitmap and text.
  1303. //
  1304. // Parameters:
  1305. // [IN] byAlign
  1306. // Alignment type. Can be one of the following values:
  1307. // ST_ALIGN_HORIZ Icon/bitmap on the left, text on the right
  1308. // ST_ALIGN_VERT Icon/bitmap on the top, text on the bottom
  1309. // ST_ALIGN_HORIZ_RIGHT Icon/bitmap on the right, text on the left
  1310. // ST_ALIGN_OVERLAP Icon/bitmap on the same space as text
  1311. // By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.
  1312. // [IN] bRepaint
  1313. // If TRUE the control will be repainted.
  1314. //
  1315. // Return value:
  1316. // BTNST_OK
  1317. // Function executed successfully.
  1318. // BTNST_INVALIDALIGN
  1319. // Alignment type not supported.
  1320. //
  1321. DWORD CButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
  1322. {
  1323. switch (byAlign)
  1324. {
  1325. case ST_ALIGN_HORIZ:
  1326. case ST_ALIGN_HORIZ_RIGHT:
  1327. case ST_ALIGN_VERT:
  1328. case ST_ALIGN_OVERLAP:
  1329. m_byAlign = byAlign;
  1330. if (bRepaint) Invalidate();
  1331. return BTNST_OK;
  1332. break;
  1333. } // switch
  1334. return BTNST_INVALIDALIGN;
  1335. } // End of SetAlign
  1336. // This function sets the pressed style.
  1337. //
  1338. // Parameters:
  1339. // [IN] byStyle
  1340. // Pressed style. Can be one of the following values:
  1341. // BTNST_PRESSED_LEFTRIGHT Pressed style from left to right (as usual)
  1342. // BTNST_PRESSED_TOPBOTTOM Pressed style from top to bottom
  1343. // By default, CButtonST buttons have BTNST_PRESSED_LEFTRIGHT style.
  1344. // [IN] bRepaint
  1345. // If TRUE the control will be repainted.
  1346. //
  1347. // Return value:
  1348. // BTNST_OK
  1349. // Function executed successfully.
  1350. // BTNST_INVALIDPRESSEDSTYLE
  1351. // Pressed style not supported.
  1352. //
  1353. DWORD CButtonST::SetPressedStyle(BYTE byStyle, BOOL bRepaint)
  1354. {
  1355. switch (byStyle)
  1356. {
  1357. case BTNST_PRESSED_LEFTRIGHT:
  1358. m_ptPressedOffset.x = 1;
  1359. m_ptPressedOffset.y = 1;
  1360. break;
  1361. case BTNST_PRESSED_TOPBOTTOM:
  1362. m_ptPressedOffset.x = 0;
  1363. m_ptPressedOffset.y = 2;
  1364. break;
  1365. default:
  1366. return BTNST_INVALIDPRESSEDSTYLE;
  1367. } // switch
  1368. if (bRepaint) Invalidate();
  1369. return BTNST_OK;
  1370. } // End of SetPressedStyle
  1371. // This function sets the state of the checkbox.
  1372. // If the button is not a checkbox, this function has no meaning.
  1373. //
  1374. // Parameters:
  1375. // [IN] nCheck
  1376. // 1 to check the checkbox.
  1377. // 0 to un-check the checkbox.
  1378. // [IN] bRepaint
  1379. // If TRUE the control will be repainted.
  1380. //
  1381. // Return value:
  1382. // BTNST_OK
  1383. // Function executed successfully.
  1384. //
  1385. DWORD CButtonST::SetCheck(int nCheck, BOOL bRepaint)
  1386. {
  1387. if (m_bIsCheckBox)
  1388. {
  1389. if (nCheck == 0) m_nCheck = 0;
  1390. else m_nCheck = 1;
  1391. if (bRepaint) Invalidate();
  1392. } // if
  1393. return BTNST_OK;
  1394. } // End of SetCheck
  1395. // This function returns the current state of the checkbox.
  1396. // If the button is not a checkbox, this function has no meaning.
  1397. //
  1398. // Return value:
  1399. // The current state of the checkbox.
  1400. // 1 if checked.
  1401. // 0 if not checked or the button is not a checkbox.
  1402. //
  1403. int CButtonST::GetCheck()
  1404. {
  1405. return m_nCheck;
  1406. } // End of GetCheck
  1407. // This function sets all colors to a default value.
  1408. //
  1409. // Parameters:
  1410. // [IN] bRepaint
  1411. // If TRUE the control will be repainted.
  1412. //
  1413. // Return value:
  1414. // BTNST_OK
  1415. // Function executed successfully.
  1416. //
  1417. DWORD CButtonST::SetDefaultColors(BOOL bRepaint)
  1418. {
  1419. m_crColors[BTNST_COLOR_BK_IN] = ::GetSysColor(COLOR_BTNFACE);
  1420. m_crColors[BTNST_COLOR_FG_IN] = ::GetSysColor(COLOR_BTNTEXT);
  1421. m_crColors[BTNST_COLOR_BK_OUT] = ::GetSysColor(COLOR_BTNFACE);
  1422. m_crColors[BTNST_COLOR_FG_OUT] = ::GetSysColor(COLOR_BTNTEXT);
  1423. m_crColors[BTNST_COLOR_BK_FOCUS] = ::GetSysColor(COLOR_BTNFACE);
  1424. m_crColors[BTNST_COLOR_FG_FOCUS] = ::GetSysColor(COLOR_BTNTEXT);
  1425. if (bRepaint) Invalidate();
  1426. return BTNST_OK;
  1427. } // End of SetDefaultColors
  1428. // This function sets the color to use for a particular state.
  1429. //
  1430. // Parameters:
  1431. // [IN] byColorIndex
  1432. // Index of the color to set. Can be one of the following values:
  1433. // BTNST_COLOR_BK_IN Background color when mouse is over the button
  1434. // BTNST_COLOR_FG_IN Text color when mouse is over the button
  1435. // BTNST_COLOR_BK_OUT Background color when mouse is outside the button
  1436. // BTNST_COLOR_FG_OUT Text color when mouse is outside the button
  1437. // BTNST_COLOR_BK_FOCUS Background color when the button is focused
  1438. // BTNST_COLOR_FG_FOCUS Text color when the button is focused
  1439. // [IN] crColor
  1440. // New color.
  1441. // [IN] bRepaint
  1442. // If TRUE the control will be repainted.
  1443. //
  1444. // Return value:
  1445. // BTNST_OK
  1446. // Function executed successfully.
  1447. // BTNST_INVALIDINDEX
  1448. // Invalid color index.
  1449. //
  1450. DWORD CButtonST::SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint)
  1451. {
  1452. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1453. // Set new color
  1454. m_crColors[byColorIndex] = crColor;
  1455. if (bRepaint) Invalidate();
  1456. return BTNST_OK;
  1457. } // End of SetColor
  1458. // This functions returns the color used for a particular state.
  1459. //
  1460. // Parameters:
  1461. // [IN] byColorIndex
  1462. // Index of the color to get.
  1463. // See SetColor for the list of available colors.
  1464. // [OUT] crpColor
  1465. // A pointer to a COLORREF that will receive the color.
  1466. //
  1467. // Return value:
  1468. // BTNST_OK
  1469. // Function executed successfully.
  1470. // BTNST_INVALIDINDEX
  1471. // Invalid color index.
  1472. //
  1473. DWORD CButtonST::GetColor(BYTE byColorIndex, COLORREF* crpColor)
  1474. {
  1475. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1476. // Get color
  1477. *crpColor = m_crColors[byColorIndex];
  1478. return BTNST_OK;
  1479. } // End of GetColor
  1480. // This function applies an offset to the RGB components of the specified color.
  1481. // This function can be seen as an easy way to make a color darker or lighter than
  1482. // its default value.
  1483. //
  1484. // Parameters:
  1485. // [IN] byColorIndex
  1486. // Index of the color to set.
  1487. // See SetColor for the list of available colors.
  1488. // [IN] shOffsetColor
  1489. // A short value indicating the offset to apply to the color.
  1490. // This value must be between -255 and 255.
  1491. // [IN] bRepaint
  1492. // If TRUE the control will be repainted.
  1493. //
  1494. // Return value:
  1495. // BTNST_OK
  1496. // Function executed successfully.
  1497. // BTNST_INVALIDINDEX
  1498. // Invalid color index.
  1499. // BTNST_BADPARAM
  1500. // The specified offset is out of range.
  1501. //
  1502. DWORD CButtonST::OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint)
  1503. {
  1504. BYTE byRed = 0;
  1505. BYTE byGreen = 0;
  1506. BYTE byBlue = 0;
  1507. short shOffsetR = shOffset;
  1508. short shOffsetG = shOffset;
  1509. short shOffsetB = shOffset;
  1510. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1511. if (shOffset < -255 || shOffset > 255) return BTNST_BADPARAM;
  1512. // Get RGB components of specified color
  1513. byRed = GetRValue(m_crColors[byColorIndex]);
  1514. byGreen = GetGValue(m_crColors[byColorIndex]);
  1515. byBlue = GetBValue(m_crColors[byColorIndex]);
  1516. // Calculate max. allowed real offset
  1517. if (shOffset > 0)
  1518. {
  1519. if (byRed + shOffset > 255) shOffsetR = 255 - byRed;
  1520. if (byGreen + shOffset > 255) shOffsetG = 255 - byGreen;
  1521. if (byBlue + shOffset > 255) shOffsetB = 255 - byBlue;
  1522. shOffset = min(min(shOffsetR, shOffsetG), shOffsetB);
  1523. } // if
  1524. else
  1525. {
  1526. if (byRed + shOffset < 0) shOffsetR = -byRed;
  1527. if (byGreen + shOffset < 0) shOffsetG = -byGreen;
  1528. if (byBlue + shOffset < 0) shOffsetB = -byBlue;
  1529. shOffset = max(max(shOffsetR, shOffsetG), shOffsetB);
  1530. } // else
  1531. // Set new color
  1532. m_crColors[byColorIndex] = RGB(byRed + shOffset, byGreen + shOffset, byBlue + shOffset);
  1533. if (bRepaint) Invalidate();
  1534. return BTNST_OK;
  1535. } // End of OffsetColor
  1536. // This function sets the hilight logic for the button.
  1537. // Applies only to flat buttons.
  1538. //
  1539. // Parameters:
  1540. // [IN] bAlwaysTrack
  1541. // If TRUE the button will be hilighted even if the window that owns it, is
  1542. // not the active window.
  1543. // If FALSE the button will be hilighted only if the window that owns it,
  1544. // is the active window.
  1545. //
  1546. // Return value:
  1547. // BTNST_OK
  1548. // Function executed successfully.
  1549. //
  1550. DWORD CButtonST::SetAlwaysTrack(BOOL bAlwaysTrack)
  1551. {
  1552. m_bAlwaysTrack = bAlwaysTrack;
  1553. return BTNST_OK;
  1554. } // End of SetAlwaysTrack
  1555. // This function sets the cursor to be used when the mouse is over the button.
  1556. //
  1557. // Parameters:
  1558. // [IN] nCursorId
  1559. // ID number of the cursor resource.
  1560. // Pass NULL to remove a previously loaded cursor.
  1561. // [IN] bRepaint
  1562. // If TRUE the control will be repainted.
  1563. //
  1564. // Return value:
  1565. // BTNST_OK
  1566. // Function executed successfully.
  1567. // BTNST_INVALIDRESOURCE
  1568. // Failed loading the specified resource.
  1569. //
  1570. DWORD CButtonST::SetBtnCursor(int nCursorId, BOOL bRepaint)
  1571. {
  1572. HINSTANCE hInstResource = NULL;
  1573. // Destroy any previous cursor
  1574. if (m_hCursor)
  1575. {
  1576. ::DestroyCursor(m_hCursor);
  1577. m_hCursor = NULL;
  1578. } // if
  1579. // Load cursor
  1580. if (nCursorId)
  1581. {
  1582. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR);
  1583. // Load cursor resource
  1584. m_hCursor = (HCURSOR)::LoadImage(hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
  1585. // Repaint the button
  1586. if (bRepaint) Invalidate();
  1587. // If something wrong
  1588. if (m_hCursor == NULL) return BTNST_INVALIDRESOURCE;
  1589. } // if
  1590. return BTNST_OK;
  1591. } // End of SetBtnCursor
  1592. // This function sets if the button border must be drawn.
  1593. // Applies only to flat buttons.
  1594. //
  1595. // Parameters:
  1596. // [IN] bDrawBorder
  1597. // If TRUE the border will be drawn.
  1598. // [IN] bRepaint
  1599. // If TRUE the control will be repainted.
  1600. //
  1601. // Return value:
  1602. // BTNST_OK
  1603. // Function executed successfully.
  1604. //
  1605. DWORD CButtonST::DrawBorder(BOOL bDrawBorder, BOOL bRepaint)
  1606. {
  1607. m_bDrawBorder = bDrawBorder;
  1608. // Repaint the button
  1609. if (bRepaint) Invalidate();
  1610. return BTNST_OK;
  1611. } // End of DrawBorder
  1612. // This function sets if the focus rectangle must be drawn for flat buttons.
  1613. //
  1614. // Parameters:
  1615. // [IN] bDrawFlatFocus
  1616. // If TRUE the focus rectangle will be drawn also for flat buttons.
  1617. // [IN] bRepaint
  1618. // If TRUE the control will be repainted.
  1619. //
  1620. // Return value:
  1621. // BTNST_OK
  1622. // Function executed successfully.
  1623. //
  1624. DWORD CButtonST::DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
  1625. {
  1626. m_bDrawFlatFocus = bDrawFlatFocus;
  1627. // Repaint the button
  1628. if (bRepaint) Invalidate();
  1629. return BTNST_OK;
  1630. } // End of DrawFlatFocus
  1631. void CButtonST::InitToolTip()
  1632. {
  1633. if (m_ToolTip.m_hWnd == NULL)
  1634. {
  1635. // Create ToolTip control
  1636. m_ToolTip.Create(this, m_dwToolTipStyle);
  1637. // Create inactive
  1638. m_ToolTip.Activate(FALSE);
  1639. // Enable multiline
  1640. m_ToolTip.SendMessage(TTM_SETMAXTIPWIDTH, 0, 400);
  1641. //m_ToolTip.SendMessage(TTM_SETTITLE, TTI_INFO, (LPARAM)_T("Title"));
  1642. } // if
  1643. } // End of InitToolTip
  1644. // This function sets the text to show in the button tooltip.
  1645. //
  1646. // Parameters:
  1647. // [IN] nText
  1648. // ID number of the string resource containing the text to show.
  1649. // [IN] bActivate
  1650. // If TRUE the tooltip will be created active.
  1651. //
  1652. void CButtonST::SetTooltipText(int nText, BOOL bActivate)
  1653. {
  1654. CString sText;
  1655. // Load string resource
  1656. sText.LoadString(nText);
  1657. // If string resource is not empty
  1658. if (sText.IsEmpty() == FALSE) SetTooltipText((LPCTSTR)sText, bActivate);
  1659. } // End of SetTooltipText
  1660. // This function sets the text to show in the button tooltip.
  1661. //
  1662. // Parameters:
  1663. // [IN] lpszText
  1664. // Pointer to a null-terminated string containing the text to show.
  1665. // [IN] bActivate
  1666. // If TRUE the tooltip will be created active.
  1667. //
  1668. void CButtonST::SetTooltipText(LPCTSTR lpszText, BOOL bActivate)
  1669. {
  1670. // We cannot accept NULL pointer
  1671. if (lpszText == NULL) return;
  1672. // Initialize ToolTip
  1673. InitToolTip();
  1674. // If there is no tooltip defined then add it
  1675. if (m_ToolTip.GetToolCount() == 0)
  1676. {
  1677. CRect rectBtn;
  1678. GetClientRect(rectBtn);
  1679. m_ToolTip.AddTool(this, lpszText, rectBtn, 1);
  1680. } // if
  1681. // Set text for tooltip
  1682. m_ToolTip.UpdateTipText(lpszText, this, 1);
  1683. m_ToolTip.Activate(bActivate);
  1684. } // End of SetTooltipText
  1685. // This function enables or disables the button tooltip.
  1686. //
  1687. // Parameters:
  1688. // [IN] bActivate
  1689. // If TRUE the tooltip will be activated.
  1690. //
  1691. void CButtonST::ActivateTooltip(BOOL bActivate)
  1692. {
  1693. // If there is no tooltip then do nothing
  1694. if (m_ToolTip.GetToolCount() == 0) return;
  1695. // Activate tooltip
  1696. m_ToolTip.Activate(bActivate);
  1697. } // End of EnableTooltip
  1698. // This function enables the tooltip to be displayed using the balloon style.
  1699. // This function must be called before any call to SetTooltipText is made.
  1700. //
  1701. // Return value:
  1702. // BTNST_OK
  1703. // Function executed successfully.
  1704. //
  1705. DWORD CButtonST::EnableBalloonTooltip()
  1706. {
  1707. m_dwToolTipStyle |= TTS_BALLOON;
  1708. return BTNST_OK;
  1709. } // End of EnableBalloonTooltip
  1710. // This function returns if the button is the default button.
  1711. //
  1712. // Return value:
  1713. // TRUE
  1714. // The button is the default button.
  1715. // FALSE
  1716. // The button is not the default button.
  1717. //
  1718. BOOL CButtonST::GetDefault()
  1719. {
  1720. return m_bIsDefault;
  1721. } // End of GetDefault
  1722. // This function enables the transparent mode.
  1723. // Note: this operation is not reversible.
  1724. // DrawTransparent should be called just after the button is created.
  1725. // Do not use trasparent buttons until you really need it (you have a bitmapped
  1726. // background) since each transparent button makes a copy in memory of its background.
  1727. // This may bring unnecessary memory use and execution overload.
  1728. //
  1729. // Parameters:
  1730. // [IN] bRepaint
  1731. // If TRUE the control will be repainted.
  1732. //
  1733. void CButtonST::DrawTransparent(BOOL bRepaint)
  1734. {
  1735. m_bDrawTransparent = TRUE;
  1736. // Restore old bitmap (if any)
  1737. if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
  1738. {
  1739. m_dcBk.SelectObject(m_pbmpOldBk);
  1740. } // if
  1741. m_bmpBk.DeleteObject();
  1742. m_dcBk.DeleteDC();
  1743. // Repaint the button
  1744. if (bRepaint) Invalidate();
  1745. } // End of DrawTransparent
  1746. DWORD CButtonST::SetBk(CDC* pDC)
  1747. {
  1748. if (m_bDrawTransparent && pDC)
  1749. {
  1750. // Restore old bitmap (if any)
  1751. if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
  1752. {
  1753. m_dcBk.SelectObject(m_pbmpOldBk);
  1754. } // if
  1755. m_bmpBk.DeleteObject();
  1756. m_dcBk.DeleteDC();
  1757. CRect rect;
  1758. CRect rect1;
  1759. GetClientRect(rect);
  1760. GetWindowRect(rect1);
  1761. GetParent()->ScreenToClient(rect1);
  1762. m_dcBk.CreateCompatibleDC(pDC);
  1763. m_bmpBk.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  1764. m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
  1765. m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, rect1.left, rect1.top, SRCCOPY);
  1766. return BTNST_OK;
  1767. } // if
  1768. return BTNST_BADPARAM;
  1769. } // End of SetBk
  1770. // This function sets the URL that will be opened when the button is clicked.
  1771. //
  1772. // Parameters:
  1773. // [IN] lpszURL
  1774. // Pointer to a null-terminated string that contains the URL.
  1775. // Pass NULL to removed any previously specified URL.
  1776. //
  1777. // Return value:
  1778. // BTNST_OK
  1779. // Function executed successfully.
  1780. //
  1781. DWORD CButtonST::SetURL(LPCTSTR lpszURL)
  1782. {
  1783. // Remove any existing URL
  1784. memset(m_szURL, 0, sizeof(m_szURL));
  1785. if (lpszURL)
  1786. {
  1787. // Store the URL
  1788. _tcsncpy_s(m_szURL, lpszURL, _MAX_PATH);
  1789. } // if
  1790. return BTNST_OK;
  1791. } // End of SetURL
  1792. // This function associates a menu to the button.
  1793. // The menu will be displayed clicking the button.
  1794. //
  1795. // Parameters:
  1796. // [IN] nMenu
  1797. // ID number of the menu resource.
  1798. // Pass NULL to remove any menu from the button.
  1799. // [IN] hParentWnd
  1800. // Handle to the window that owns the menu.
  1801. // This window receives all messages from the menu.
  1802. // [IN] bRepaint
  1803. // If TRUE the control will be repainted.
  1804. //
  1805. // Return value:
  1806. // BTNST_OK
  1807. // Function executed successfully.
  1808. // BTNST_INVALIDRESOURCE
  1809. // Failed loading the specified resource.
  1810. //
  1811. #ifndef BTNST_USE_BCMENU
  1812. DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint)
  1813. {
  1814. HINSTANCE hInstResource = NULL;
  1815. // Destroy any previous menu
  1816. if (m_hMenu)
  1817. {
  1818. ::DestroyMenu(m_hMenu);
  1819. m_hMenu = NULL;
  1820. m_hParentWndMenu = NULL;
  1821. m_bMenuDisplayed = FALSE;
  1822. } // if
  1823. // Load menu
  1824. if (nMenu)
  1825. {
  1826. // Find correct resource handle
  1827. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nMenu), RT_MENU);
  1828. // Load menu resource
  1829. m_hMenu = ::LoadMenu(hInstResource, MAKEINTRESOURCE(nMenu));
  1830. m_hParentWndMenu = hParentWnd;
  1831. // If something wrong
  1832. if (m_hMenu == NULL) return BTNST_INVALIDRESOURCE;
  1833. } // if
  1834. // Repaint the button
  1835. if (bRepaint) Invalidate();
  1836. return BTNST_OK;
  1837. } // End of SetMenu
  1838. #endif
  1839. // This function associates a menu to the button.
  1840. // The menu will be displayed clicking the button.
  1841. // The menu will be handled by the BCMenu class.
  1842. //
  1843. // Parameters:
  1844. // [IN] nMenu
  1845. // ID number of the menu resource.
  1846. // Pass NULL to remove any menu from the button.
  1847. // [IN] hParentWnd
  1848. // Handle to the window that owns the menu.
  1849. // This window receives all messages from the menu.
  1850. // [IN] bWinXPStyle
  1851. // If TRUE the menu will be displayed using the new Windows XP style.
  1852. // If FALSE the menu will be displayed using the standard style.
  1853. // [IN] nToolbarID
  1854. // Resource ID of the toolbar to be associated to the menu.
  1855. // [IN] sizeToolbarIcon
  1856. // A CSize object indicating the size (in pixels) of each icon into the toolbar.
  1857. // All icons into the toolbar must have the same size.
  1858. // [IN] crToolbarBk
  1859. // A COLORREF value indicating the color to use as background for the icons into the toolbar.
  1860. // This color will be used as the "transparent" color.
  1861. // [IN] bRepaint
  1862. // If TRUE the control will be repainted.
  1863. //
  1864. // Return value:
  1865. // BTNST_OK
  1866. // Function executed successfully.
  1867. // BTNST_INVALIDRESOURCE
  1868. // Failed loading the specified resource.
  1869. //
  1870. #ifdef BTNST_USE_BCMENU
  1871. DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bWinXPStyle, UINT nToolbarID, CSize sizeToolbarIcon, COLORREF crToolbarBk, BOOL bRepaint)
  1872. {
  1873. BOOL bRetValue = FALSE;
  1874. // Destroy any previous menu
  1875. if (m_menuPopup.m_hMenu)
  1876. {
  1877. m_menuPopup.DestroyMenu();
  1878. m_hParentWndMenu = NULL;
  1879. m_bMenuDisplayed = FALSE;
  1880. } // if
  1881. // Load menu
  1882. if (nMenu)
  1883. {
  1884. m_menuPopup.SetMenuDrawMode(bWinXPStyle);
  1885. // Load menu
  1886. bRetValue = m_menuPopup.LoadMenu(nMenu);
  1887. // If something wrong
  1888. if (bRetValue == FALSE) return BTNST_INVALIDRESOURCE;
  1889. // Load toolbar
  1890. if (nToolbarID)
  1891. {
  1892. m_menuPopup.SetBitmapBackground(crToolbarBk);
  1893. m_menuPopup.SetIconSize(sizeToolbarIcon.cx, sizeToolbarIcon.cy);
  1894. bRetValue = m_menuPopup.LoadToolbar(nToolbarID);
  1895. // If something wrong
  1896. if (bRetValue == FALSE)
  1897. {
  1898. m_menuPopup.DestroyMenu();
  1899. return BTNST_INVALIDRESOURCE;
  1900. } // if
  1901. } // if
  1902. m_hParentWndMenu = hParentWnd;
  1903. } // if
  1904. // Repaint the button
  1905. if (bRepaint) Invalidate();
  1906. return BTNST_OK;
  1907. } // End of SetMenu
  1908. #endif
  1909. // This function sets the callback message that will be sent to the
  1910. // specified window just before the menu associated to the button is displayed.
  1911. //
  1912. // Parameters:
  1913. // [IN] hWnd
  1914. // Handle of the window that will receive the callback message.
  1915. // Pass NULL to remove any previously specified callback message.
  1916. // [IN] nMessage
  1917. // Callback message to send to window.
  1918. // [IN] lParam
  1919. // A 32 bits user specified value that will be passed to the callback function.
  1920. //
  1921. // Remarks:
  1922. // the callback function must be in the form:
  1923. // LRESULT On_MenuCallback(WPARAM wParam, LPARAM lParam)
  1924. // Where:
  1925. // [IN] wParam
  1926. // If support for BCMenu is enabled: a pointer to BCMenu
  1927. // else a HMENU handle to the menu that is being to be displayed.
  1928. // [IN] lParam
  1929. // The 32 bits user specified value.
  1930. //
  1931. // Return value:
  1932. // BTNST_OK
  1933. // Function executed successfully.
  1934. //
  1935. DWORD CButtonST::SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam)
  1936. {
  1937. m_csCallbacks.hWnd = hWnd;
  1938. m_csCallbacks.nMessage = nMessage;
  1939. m_csCallbacks.lParam = lParam;
  1940. return BTNST_OK;
  1941. } // End of SetMenuCallback
  1942. // This function resizes the button to the same size of the image.
  1943. // To get good results both the IN and OUT images should have the same size.
  1944. //
  1945. void CButtonST::SizeToContent()
  1946. {
  1947. if (m_csIcons[0].hIcon)
  1948. {
  1949. m_ptImageOrg.x = 0;
  1950. m_ptImageOrg.y = 0;
  1951. SetWindowPos( NULL, -1, -1, m_csIcons[0].dwWidth, m_csIcons[0].dwHeight,
  1952. SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);
  1953. } // if
  1954. else
  1955. if (m_csBitmaps[0].hBitmap)
  1956. {
  1957. m_ptImageOrg.x = 0;
  1958. m_ptImageOrg.y = 0;
  1959. SetWindowPos( NULL, -1, -1, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight,
  1960. SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);
  1961. } // if
  1962. } // End of SizeToContent
  1963. // This function sets the sound that must be played on particular button states.
  1964. //
  1965. // Parameters:
  1966. // [IN] lpszSound
  1967. // A string that specifies the sound to play.
  1968. // If hMod is NULL this string is interpreted as a filename, else it
  1969. // is interpreted as a resource identifier.
  1970. // Pass NULL to remove any previously specified sound.
  1971. // [IN] hMod
  1972. // Handle to the executable file that contains the resource to be loaded.
  1973. // This parameter must be NULL unless lpszSound specifies a resource identifier.
  1974. // [IN] bPlayOnClick
  1975. // TRUE if the sound must be played when the button is clicked.
  1976. // FALSE if the sound must be played when the mouse is moved over the button.
  1977. // [IN] bPlayAsync
  1978. // TRUE if the sound must be played asynchronously.
  1979. // FALSE if the sound must be played synchronously. The application takes control
  1980. // when the sound is completely played.
  1981. //
  1982. // Return value:
  1983. // BTNST_OK
  1984. // Function executed successfully.
  1985. //
  1986. #ifdef BTNST_USE_SOUND
  1987. DWORD CButtonST::SetSound(LPCTSTR lpszSound, HMODULE hMod, BOOL bPlayOnClick, BOOL bPlayAsync)
  1988. {
  1989. BYTE byIndex = bPlayOnClick ? 1 : 0;
  1990. // Store new sound
  1991. if (lpszSound)
  1992. {
  1993. if (hMod) // From resource identifier ?
  1994. {
  1995. m_csSounds[byIndex].lpszSound = lpszSound;
  1996. } // if
  1997. else
  1998. {
  1999. _tcscpy_s(m_csSounds[byIndex].szSound, lpszSound);
  2000. m_csSounds[byIndex].lpszSound = m_csSounds[byIndex].szSound;
  2001. } // else
  2002. m_csSounds[byIndex].hMod = hMod;
  2003. m_csSounds[byIndex].dwFlags = SND_NODEFAULT | SND_NOWAIT;
  2004. m_csSounds[byIndex].dwFlags |= hMod ? SND_RESOURCE : SND_FILENAME;
  2005. m_csSounds[byIndex].dwFlags |= bPlayAsync ? SND_ASYNC : SND_SYNC;
  2006. } // if
  2007. else
  2008. {
  2009. // Or remove any existing
  2010. ::ZeroMemory(&m_csSounds[byIndex], sizeof(STRUCT_SOUND));
  2011. } // else
  2012. return BTNST_OK;
  2013. } // End of SetSound
  2014. #endif
  2015. // This function is called every time the button background needs to be painted.
  2016. // If the button is in transparent mode this function will NOT be called.
  2017. // This is a virtual function that can be rewritten in CButtonST-derived classes
  2018. // to produce a whole range of buttons not available by default.
  2019. //
  2020. // Parameters:
  2021. // [IN] pDC
  2022. // Pointer to a CDC object that indicates the device context.
  2023. // [IN] pRect
  2024. // Pointer to a CRect object that indicates the bounds of the
  2025. // area to be painted.
  2026. //
  2027. // Return value:
  2028. // BTNST_OK
  2029. // Function executed successfully.
  2030. //
  2031. DWORD CButtonST::OnDrawBackground(CDC* pDC, CRect* pRect)
  2032. {
  2033. COLORREF crColor;
  2034. if (m_bIsFlat == FALSE)
  2035. {
  2036. if (m_bIsFocused || m_bIsDefault)
  2037. {
  2038. CBrush br(RGB(0,0,0));
  2039. pDC->FrameRect(pRect, &br);
  2040. pRect->DeflateRect(1, 1);
  2041. } // if
  2042. } // if
  2043. if (m_bMouseOnButton || m_bIsPressed)
  2044. crColor = m_crColors[BTNST_COLOR_BK_IN];
  2045. else
  2046. {
  2047. if (m_bIsFocused)
  2048. crColor = m_crColors[BTNST_COLOR_BK_FOCUS];
  2049. else
  2050. crColor = m_crColors[BTNST_COLOR_BK_OUT];
  2051. } // else
  2052. CBrush brBackground(crColor);
  2053. pDC->FillRect(pRect, &brBackground);
  2054. return BTNST_OK;
  2055. } // End of OnDrawBackground
  2056. // This function is called every time the button border needs to be painted.
  2057. // This is a virtual function that can be rewritten in CButtonST-derived classes
  2058. // to produce a whole range of buttons not available by default.
  2059. //
  2060. // Parameters:
  2061. // [IN] pDC
  2062. // Pointer to a CDC object that indicates the device context.
  2063. // [IN] pRect
  2064. // Pointer to a CRect object that indicates the bounds of the
  2065. // area to be painted.
  2066. //
  2067. // Return value:
  2068. // BTNST_OK
  2069. // Function executed successfully.
  2070. //
  2071. DWORD CButtonST::OnDrawBorder(CDC* pDC, CRect* pRect)
  2072. {
  2073. // Draw pressed button
  2074. if (m_bIsPressed)
  2075. {
  2076. if (m_bIsFlat)
  2077. {
  2078. if (m_bDrawBorder)
  2079. pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
  2080. }
  2081. else
  2082. {
  2083. CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
  2084. pDC->FrameRect(pRect, &brBtnShadow);
  2085. }
  2086. }
  2087. else // ...else draw non pressed button
  2088. {
  2089. CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
  2090. CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
  2091. CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
  2092. CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
  2093. if (m_bIsFlat)
  2094. {
  2095. if (m_bMouseOnButton && m_bDrawBorder)
  2096. pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
  2097. }
  2098. else
  2099. {
  2100. // Draw top-left borders
  2101. // White line
  2102. CPen* pOldPen = pDC->SelectObject(&penBtnHiLight);
  2103. pDC->MoveTo(pRect->left, pRect->bottom-1);
  2104. pDC->LineTo(pRect->left, pRect->top);
  2105. pDC->LineTo(pRect->right, pRect->top);
  2106. // Light gray line
  2107. pDC->SelectObject(pen3DLight);
  2108. pDC->MoveTo(pRect->left+1, pRect->bottom-1);
  2109. pDC->LineTo(pRect->left+1, pRect->top+1);
  2110. pDC->LineTo(pRect->right, pRect->top+1);
  2111. // Draw bottom-right borders
  2112. // Black line
  2113. pDC->SelectObject(pen3DDKShadow);
  2114. pDC->MoveTo(pRect->left, pRect->bottom-1);
  2115. pDC->LineTo(pRect->right-1, pRect->bottom-1);
  2116. pDC->LineTo(pRect->right-1, pRect->top-1);
  2117. // Dark gray line
  2118. pDC->SelectObject(penBtnShadow);
  2119. pDC->MoveTo(pRect->left+1, pRect->bottom-2);
  2120. pDC->LineTo(pRect->right-2, pRect->bottom-2);
  2121. pDC->LineTo(pRect->right-2, pRect->top);
  2122. //
  2123. pDC->SelectObject(pOldPen);
  2124. } // else
  2125. } // else
  2126. return BTNST_OK;
  2127. } // End of OnDrawBorder
  2128. #undef BTS_TYPEMASK