BtnST.cpp 68 KB

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