中间件标准版5.1git,去除基础模块

BtnST.cpp 67KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  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. // 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 & BS_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(BS_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 & BS_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 & ~BS_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. /////////////////////////////////////////////////////////// add by zsn 2013.8.20
  342. if (m_ToolTip.m_hWnd != NULL)
  343. {
  344. m_ToolTip.DelTool(this, 1);
  345. if (m_strTipText.IsEmpty() != TRUE)
  346. {
  347. CRect rectBtn;
  348. GetClientRect(rectBtn);
  349. m_ToolTip.AddTool(this, m_strTipText, rectBtn, 1);
  350. m_ToolTip.Activate(TRUE);
  351. }
  352. }
  353. ///////////////////////////////////////////////////////////
  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. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
  679. rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())/2, (centerRect.Height() - rpCaption->Height())/2);
  680. /* RFU
  681. rpCaption->OffsetRect(0, (centerRect.Height() - rpCaption->Height())/2);
  682. rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())-4, (centerRect.Height() - rpCaption->Height())/2);
  683. */
  684. pDC->SetBkMode(TRANSPARENT);
  685. /*
  686. pDC->DrawState(rCaption->TopLeft(), rCaption->Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
  687. TRUE, 0, (CBrush*)NULL);
  688. */
  689. if (m_bIsDisabled)
  690. {
  691. rpCaption->OffsetRect(1, 1);
  692. pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
  693. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
  694. rpCaption->OffsetRect(-1, -1);
  695. pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
  696. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
  697. } // if
  698. else
  699. {
  700. if (m_bMouseOnButton || m_bIsPressed)
  701. {
  702. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
  703. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
  704. } // if
  705. else
  706. {
  707. if (m_bIsFocused)
  708. {
  709. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_FOCUS]);
  710. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_FOCUS]);
  711. } // if
  712. else
  713. {
  714. pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
  715. pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
  716. } // else
  717. } // else
  718. pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
  719. } // if
  720. } // End of DrawTheText
  721. // This function creates a grayscale bitmap starting from a given bitmap.
  722. // The resulting bitmap will have the same size of the original one.
  723. //
  724. // Parameters:
  725. // [IN] hBitmap
  726. // Handle to the original bitmap.
  727. // [IN] dwWidth
  728. // Specifies the bitmap width, in pixels.
  729. // [IN] dwHeight
  730. // Specifies the bitmap height, in pixels.
  731. // [IN] crTrans
  732. // Color to be used as transparent color. This color will be left unchanged.
  733. //
  734. // Return value:
  735. // If the function succeeds, the return value is the handle to the newly created
  736. // grayscale bitmap.
  737. // If the function fails, the return value is NULL.
  738. //
  739. HBITMAP CButtonST::CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
  740. {
  741. HBITMAP hGrayBitmap = NULL;
  742. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  743. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  744. hMainDC = ::GetDC(NULL);
  745. if (hMainDC == NULL) return NULL;
  746. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  747. if (hMemDC1 == NULL)
  748. {
  749. ::ReleaseDC(NULL, hMainDC);
  750. return NULL;
  751. } // if
  752. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  753. if (hMemDC2 == NULL)
  754. {
  755. ::DeleteDC(hMemDC1);
  756. ::ReleaseDC(NULL, hMainDC);
  757. return NULL;
  758. } // if
  759. hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
  760. if (hGrayBitmap)
  761. {
  762. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
  763. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
  764. //::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
  765. DWORD dwLoopY = 0, dwLoopX = 0;
  766. COLORREF crPixel = 0;
  767. BYTE byNewPixel = 0;
  768. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  769. {
  770. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  771. {
  772. crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
  773. byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
  774. if (crPixel != crTrans)
  775. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
  776. else
  777. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
  778. } // for
  779. } // for
  780. ::SelectObject(hMemDC1, hOldBmp1);
  781. ::SelectObject(hMemDC2, hOldBmp2);
  782. } // if
  783. ::DeleteDC(hMemDC1);
  784. ::DeleteDC(hMemDC2);
  785. ::ReleaseDC(NULL, hMainDC);
  786. return hGrayBitmap;
  787. } // End of CreateGrayscaleBitmap
  788. // This function creates a bitmap that is 25% darker than the original.
  789. // The resulting bitmap will have the same size of the original one.
  790. //
  791. // Parameters:
  792. // [IN] hBitmap
  793. // Handle to the original bitmap.
  794. // [IN] dwWidth
  795. // Specifies the bitmap width, in pixels.
  796. // [IN] dwHeight
  797. // Specifies the bitmap height, in pixels.
  798. // [IN] crTrans
  799. // Color to be used as transparent color. This color will be left unchanged.
  800. //
  801. // Return value:
  802. // If the function succeeds, the return value is the handle to the newly created
  803. // darker bitmap.
  804. // If the function fails, the return value is NULL.
  805. //
  806. HBITMAP CButtonST::CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
  807. {
  808. HBITMAP hGrayBitmap = NULL;
  809. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  810. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  811. hMainDC = ::GetDC(NULL);
  812. if (hMainDC == NULL) return NULL;
  813. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  814. if (hMemDC1 == NULL)
  815. {
  816. ::ReleaseDC(NULL, hMainDC);
  817. return NULL;
  818. } // if
  819. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  820. if (hMemDC2 == NULL)
  821. {
  822. ::DeleteDC(hMemDC1);
  823. ::ReleaseDC(NULL, hMainDC);
  824. return NULL;
  825. } // if
  826. hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
  827. if (hGrayBitmap)
  828. {
  829. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
  830. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
  831. //::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
  832. DWORD dwLoopY = 0, dwLoopX = 0;
  833. COLORREF crPixel = 0;
  834. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  835. {
  836. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  837. {
  838. crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
  839. if (crPixel != crTrans)
  840. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
  841. else
  842. ::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
  843. } // for
  844. } // for
  845. ::SelectObject(hMemDC1, hOldBmp1);
  846. ::SelectObject(hMemDC2, hOldBmp2);
  847. } // if
  848. ::DeleteDC(hMemDC1);
  849. ::DeleteDC(hMemDC2);
  850. ::ReleaseDC(NULL, hMainDC);
  851. return hGrayBitmap;
  852. } // End of CreateDarkerBitmap
  853. // This function creates a grayscale icon starting from a given icon.
  854. // The resulting icon will have the same size of the original one.
  855. //
  856. // Parameters:
  857. // [IN] hIcon
  858. // Handle to the original icon.
  859. //
  860. // Return value:
  861. // If the function succeeds, the return value is the handle to the newly created
  862. // grayscale icon.
  863. // If the function fails, the return value is NULL.
  864. //
  865. // Updates:
  866. // 26/Nov/2002 Restored 1 BitBlt operation
  867. // 03/May/2002 Removed dependancy from m_hWnd
  868. // Removed 1 BitBlt operation
  869. //
  870. HICON CButtonST::CreateGrayscaleIcon(HICON hIcon)
  871. {
  872. HICON hGrayIcon = NULL;
  873. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  874. BITMAP bmp;
  875. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  876. ICONINFO csII, csGrayII;
  877. BOOL bRetValue = FALSE;
  878. bRetValue = ::GetIconInfo(hIcon, &csII);
  879. if (bRetValue == FALSE) return NULL;
  880. hMainDC = ::GetDC(NULL);
  881. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  882. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  883. if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  884. if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
  885. {
  886. DWORD dwWidth = csII.xHotspot*2;
  887. DWORD dwHeight = csII.yHotspot*2;
  888. csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
  889. if (csGrayII.hbmColor)
  890. {
  891. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
  892. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
  893. //::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
  894. DWORD dwLoopY = 0, dwLoopX = 0;
  895. COLORREF crPixel = 0;
  896. BYTE byNewPixel = 0;
  897. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  898. {
  899. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  900. {
  901. crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
  902. byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
  903. if (crPixel)
  904. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
  905. else
  906. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
  907. } // for
  908. } // for
  909. ::SelectObject(hMemDC1, hOldBmp1);
  910. ::SelectObject(hMemDC2, hOldBmp2);
  911. csGrayII.hbmMask = csII.hbmMask;
  912. csGrayII.fIcon = TRUE;
  913. hGrayIcon = ::CreateIconIndirect(&csGrayII);
  914. } // if
  915. ::DeleteObject(csGrayII.hbmColor);
  916. //::DeleteObject(csGrayII.hbmMask);
  917. } // if
  918. ::DeleteObject(csII.hbmColor);
  919. ::DeleteObject(csII.hbmMask);
  920. ::DeleteDC(hMemDC1);
  921. ::DeleteDC(hMemDC2);
  922. ::ReleaseDC(NULL, hMainDC);
  923. return hGrayIcon;
  924. } // End of CreateGrayscaleIcon
  925. // This function creates a icon that is 25% darker than the original.
  926. // The resulting icon will have the same size of the original one.
  927. //
  928. // Parameters:
  929. // [IN] hIcon
  930. // Handle to the original icon.
  931. //
  932. // Return value:
  933. // If the function succeeds, the return value is the handle to the newly created
  934. // darker icon.
  935. // If the function fails, the return value is NULL.
  936. //
  937. HICON CButtonST::CreateDarkerIcon(HICON hIcon)
  938. {
  939. HICON hGrayIcon = NULL;
  940. HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
  941. BITMAP bmp;
  942. HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
  943. ICONINFO csII, csGrayII;
  944. BOOL bRetValue = FALSE;
  945. bRetValue = ::GetIconInfo(hIcon, &csII);
  946. if (bRetValue == FALSE) return NULL;
  947. hMainDC = ::GetDC(NULL);
  948. hMemDC1 = ::CreateCompatibleDC(hMainDC);
  949. hMemDC2 = ::CreateCompatibleDC(hMainDC);
  950. if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  951. if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
  952. {
  953. DWORD dwWidth = csII.xHotspot*2;
  954. DWORD dwHeight = csII.yHotspot*2;
  955. csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
  956. if (csGrayII.hbmColor)
  957. {
  958. hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
  959. hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
  960. //::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
  961. DWORD dwLoopY = 0, dwLoopX = 0;
  962. COLORREF crPixel = 0;
  963. for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
  964. {
  965. for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
  966. {
  967. crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
  968. if (crPixel)
  969. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
  970. else
  971. ::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
  972. } // for
  973. } // for
  974. ::SelectObject(hMemDC1, hOldBmp1);
  975. ::SelectObject(hMemDC2, hOldBmp2);
  976. csGrayII.hbmMask = csII.hbmMask;
  977. csGrayII.fIcon = TRUE;
  978. hGrayIcon = ::CreateIconIndirect(&csGrayII);
  979. } // if
  980. ::DeleteObject(csGrayII.hbmColor);
  981. //::DeleteObject(csGrayII.hbmMask);
  982. } // if
  983. ::DeleteObject(csII.hbmColor);
  984. ::DeleteObject(csII.hbmMask);
  985. ::DeleteDC(hMemDC1);
  986. ::DeleteDC(hMemDC2);
  987. ::ReleaseDC(NULL, hMainDC);
  988. return hGrayIcon;
  989. } // End of CreateDarkerIcon
  990. COLORREF CButtonST::DarkenColor(COLORREF crColor, double dFactor)
  991. {
  992. if (dFactor > 0.0 && dFactor <= 1.0)
  993. {
  994. BYTE red,green,blue,lightred,lightgreen,lightblue;
  995. red = GetRValue(crColor);
  996. green = GetGValue(crColor);
  997. blue = GetBValue(crColor);
  998. lightred = (BYTE)(red-(dFactor * red));
  999. lightgreen = (BYTE)(green-(dFactor * green));
  1000. lightblue = (BYTE)(blue-(dFactor * blue));
  1001. crColor = RGB(lightred,lightgreen,lightblue);
  1002. } // if
  1003. return crColor;
  1004. } // End of DarkenColor
  1005. // This function assigns icons to the button.
  1006. // Any previous icon or bitmap will be removed.
  1007. //
  1008. // Parameters:
  1009. // [IN] nIconIn
  1010. // ID number of the icon resource to show when the mouse is over the button.
  1011. // Pass NULL to remove any icon from the button.
  1012. // [IN] nCxDesiredIn
  1013. // Specifies the width, in pixels, of the icon to load.
  1014. // [IN] nCyDesiredIn
  1015. // Specifies the height, in pixels, of the icon to load.
  1016. // [IN] nIconOut
  1017. // ID number of the icon resource to show when the mouse is outside the button.
  1018. // Can be NULL.
  1019. // If this parameter is the special value BTNST_AUTO_GRAY (cast to int) the second
  1020. // icon will be automatically created starting from nIconIn and converted to grayscale.
  1021. // If this parameter is the special value BTNST_AUTO_DARKER (cast to int) the second
  1022. // icon will be automatically created 25% darker starting from nIconIn.
  1023. // [IN] nCxDesiredOut
  1024. // Specifies the width, in pixels, of the icon to load.
  1025. // [IN] nCyDesiredOut
  1026. // Specifies the height, in pixels, of the icon to load.
  1027. //
  1028. // Return value:
  1029. // BTNST_OK
  1030. // Function executed successfully.
  1031. // BTNST_INVALIDRESOURCE
  1032. // Failed loading the specified resource.
  1033. //
  1034. DWORD CButtonST::SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut, int nCxDesiredOut, int nCyDesiredOut)
  1035. {
  1036. HICON hIconIn = NULL;
  1037. HICON hIconOut = NULL;
  1038. HINSTANCE hInstResource = NULL;
  1039. // Find correct resource handle
  1040. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
  1041. // Set icon when the mouse is IN the button
  1042. hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, nCxDesiredIn, nCyDesiredIn, 0);
  1043. // Set icon when the mouse is OUT the button
  1044. switch (nIconOut)
  1045. {
  1046. case NULL:
  1047. break;
  1048. case (int)BTNST_AUTO_GRAY:
  1049. hIconOut = BTNST_AUTO_GRAY;
  1050. break;
  1051. case (int)BTNST_AUTO_DARKER:
  1052. hIconOut = BTNST_AUTO_DARKER;
  1053. break;
  1054. default:
  1055. hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, nCxDesiredOut, nCyDesiredOut, 0);
  1056. break;
  1057. } // switch
  1058. return SetIcon(hIconIn, hIconOut);
  1059. } // End of SetIcon
  1060. // This function assigns icons to the button.
  1061. // Any previous icon or bitmap will be removed.
  1062. //
  1063. // Parameters:
  1064. // [IN] nIconIn
  1065. // ID number of the icon resource to show when the mouse is over the button.
  1066. // Pass NULL to remove any icon from the button.
  1067. // [IN] nIconOut
  1068. // ID number of the icon resource to show when the mouse is outside the button.
  1069. // Can be NULL.
  1070. // If this parameter is the special value BTNST_AUTO_GRAY (cast to int) the second
  1071. // icon will be automatically created starting from nIconIn and converted to grayscale.
  1072. // If this parameter is the special value BTNST_AUTO_DARKER (cast to int) the second
  1073. // icon will be automatically created 25% darker starting from nIconIn.
  1074. //
  1075. // Return value:
  1076. // BTNST_OK
  1077. // Function executed successfully.
  1078. // BTNST_INVALIDRESOURCE
  1079. // Failed loading the specified resource.
  1080. //
  1081. DWORD CButtonST::SetIcon(int nIconIn, int nIconOut)
  1082. {
  1083. return SetIcon(nIconIn, 0, 0, nIconOut, 0, 0);
  1084. } // End of SetIcon
  1085. // This function assigns icons to the button.
  1086. // Any previous icon or bitmap will be removed.
  1087. //
  1088. // Parameters:
  1089. // [IN] hIconIn
  1090. // Handle fo the icon to show when the mouse is over the button.
  1091. // Pass NULL to remove any icon from the button.
  1092. // [IN] hIconOut
  1093. // Handle to the icon to show when the mouse is outside the button.
  1094. // Can be NULL.
  1095. // If this parameter is the special value BTNST_AUTO_GRAY the second
  1096. // icon will be automatically created starting from hIconIn and converted to grayscale.
  1097. // If this parameter is the special value BTNST_AUTO_DARKER the second
  1098. // icon will be automatically created 25% darker starting from hIconIn.
  1099. //
  1100. // Return value:
  1101. // BTNST_OK
  1102. // Function executed successfully.
  1103. // BTNST_INVALIDRESOURCE
  1104. // Failed loading the specified resource.
  1105. //
  1106. DWORD CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
  1107. {
  1108. BOOL bRetValue;
  1109. ICONINFO ii;
  1110. // Free any loaded resource
  1111. FreeResources();
  1112. if (hIconIn)
  1113. {
  1114. // Icon when mouse over button?
  1115. m_csIcons[0].hIcon = hIconIn;
  1116. // Get icon dimension
  1117. ::ZeroMemory(&ii, sizeof(ICONINFO));
  1118. bRetValue = ::GetIconInfo(hIconIn, &ii);
  1119. if (bRetValue == FALSE)
  1120. {
  1121. FreeResources();
  1122. return BTNST_INVALIDRESOURCE;
  1123. } // if
  1124. m_csIcons[0].dwWidth = (DWORD)(ii.xHotspot * 2);
  1125. m_csIcons[0].dwHeight = (DWORD)(ii.yHotspot * 2);
  1126. ::DeleteObject(ii.hbmMask);
  1127. ::DeleteObject(ii.hbmColor);
  1128. // Icon when mouse outside button?
  1129. if (hIconOut)
  1130. {
  1131. switch ((int)hIconOut)
  1132. {
  1133. case (int)BTNST_AUTO_GRAY:
  1134. hIconOut = CreateGrayscaleIcon(hIconIn);
  1135. break;
  1136. case (int)BTNST_AUTO_DARKER:
  1137. hIconOut = CreateDarkerIcon(hIconIn);
  1138. break;
  1139. } // switch
  1140. m_csIcons[1].hIcon = hIconOut;
  1141. // Get icon dimension
  1142. ::ZeroMemory(&ii, sizeof(ICONINFO));
  1143. bRetValue = ::GetIconInfo(hIconOut, &ii);
  1144. if (bRetValue == FALSE)
  1145. {
  1146. FreeResources();
  1147. return BTNST_INVALIDRESOURCE;
  1148. } // if
  1149. m_csIcons[1].dwWidth = (DWORD)(ii.xHotspot * 2);
  1150. m_csIcons[1].dwHeight = (DWORD)(ii.yHotspot * 2);
  1151. ::DeleteObject(ii.hbmMask);
  1152. ::DeleteObject(ii.hbmColor);
  1153. } // if
  1154. } // if
  1155. Invalidate();
  1156. return BTNST_OK;
  1157. } // End of SetIcon
  1158. // This function assigns bitmaps to the button.
  1159. // Any previous icon or bitmap will be removed.
  1160. //
  1161. // Parameters:
  1162. // [IN] nBitmapIn
  1163. // ID number of the bitmap resource to show when the mouse is over the button.
  1164. // Pass NULL to remove any bitmap from the button.
  1165. // [IN] crTransColorIn
  1166. // Color (inside nBitmapIn) to be used as transparent color.
  1167. // [IN] nBitmapOut
  1168. // ID number of the bitmap resource to show when the mouse is outside the button.
  1169. // Can be NULL.
  1170. // [IN] crTransColorOut
  1171. // Color (inside nBitmapOut) to be used as transparent color.
  1172. //
  1173. // Return value:
  1174. // BTNST_OK
  1175. // Function executed successfully.
  1176. // BTNST_INVALIDRESOURCE
  1177. // Failed loading the specified resource.
  1178. // BTNST_FAILEDMASK
  1179. // Failed creating mask bitmap.
  1180. //
  1181. DWORD CButtonST::SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut, COLORREF crTransColorOut)
  1182. {
  1183. HBITMAP hBitmapIn = NULL;
  1184. HBITMAP hBitmapOut = NULL;
  1185. HINSTANCE hInstResource = NULL;
  1186. // Find correct resource handle
  1187. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapIn), RT_BITMAP);
  1188. // Load bitmap In
  1189. hBitmapIn = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
  1190. // Load bitmap Out
  1191. switch (nBitmapOut)
  1192. {
  1193. case NULL:
  1194. break;
  1195. case (int)BTNST_AUTO_GRAY:
  1196. hBitmapOut = (HBITMAP)BTNST_AUTO_GRAY;
  1197. break;
  1198. case (int)BTNST_AUTO_DARKER:
  1199. hBitmapOut = (HBITMAP)BTNST_AUTO_DARKER;
  1200. break;
  1201. default:
  1202. hBitmapOut = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
  1203. break;
  1204. } // if
  1205. return SetBitmaps(hBitmapIn, crTransColorIn, hBitmapOut, crTransColorOut);
  1206. } // End of SetBitmaps
  1207. DWORD CButtonST::SetBitmaps(HINSTANCE Module, int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut, COLORREF crTransColorOut)
  1208. {
  1209. HBITMAP hBitmapIn = NULL;
  1210. HBITMAP hBitmapOut = NULL;
  1211. // Load bitmap In
  1212. hBitmapIn = (HBITMAP)::LoadImage(Module, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
  1213. // Load bitmap Out
  1214. switch (nBitmapOut)
  1215. {
  1216. case NULL:
  1217. break;
  1218. case (int)BTNST_AUTO_GRAY:
  1219. hBitmapOut = (HBITMAP)BTNST_AUTO_GRAY;
  1220. break;
  1221. case (int)BTNST_AUTO_DARKER:
  1222. hBitmapOut = (HBITMAP)BTNST_AUTO_DARKER;
  1223. break;
  1224. default:
  1225. hBitmapOut = (HBITMAP)::LoadImage(Module, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
  1226. break;
  1227. } // if
  1228. return SetBitmaps(hBitmapIn, crTransColorIn, hBitmapOut, crTransColorOut);
  1229. }
  1230. // This function assigns bitmaps to the button.
  1231. // Any previous icon or bitmap will be removed.
  1232. //
  1233. // Parameters:
  1234. // [IN] hBitmapIn
  1235. // Handle fo the bitmap to show when the mouse is over the button.
  1236. // Pass NULL to remove any bitmap from the button.
  1237. // [IN] crTransColorIn
  1238. // Color (inside hBitmapIn) to be used as transparent color.
  1239. // [IN] hBitmapOut
  1240. // Handle to the bitmap to show when the mouse is outside the button.
  1241. // Can be NULL.
  1242. // [IN] crTransColorOut
  1243. // Color (inside hBitmapOut) to be used as transparent color.
  1244. //
  1245. // Return value:
  1246. // BTNST_OK
  1247. // Function executed successfully.
  1248. // BTNST_INVALIDRESOURCE
  1249. // Failed loading the specified resource.
  1250. // BTNST_FAILEDMASK
  1251. // Failed creating mask bitmap.
  1252. //
  1253. DWORD CButtonST::SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut, COLORREF crTransColorOut)
  1254. {
  1255. int nRetValue = 0;
  1256. BITMAP csBitmapSize;
  1257. // Free any loaded resource
  1258. FreeResources();
  1259. if (hBitmapIn)
  1260. {
  1261. m_csBitmaps[0].hBitmap = hBitmapIn;
  1262. m_csBitmaps[0].crTransparent = crTransColorIn;
  1263. // Get bitmap size
  1264. nRetValue = ::GetObject(hBitmapIn, sizeof(csBitmapSize), &csBitmapSize);
  1265. if (nRetValue == 0)
  1266. {
  1267. FreeResources();
  1268. return BTNST_INVALIDRESOURCE;
  1269. } // if
  1270. m_csBitmaps[0].dwWidth = (DWORD)csBitmapSize.bmWidth;
  1271. m_csBitmaps[0].dwHeight = (DWORD)csBitmapSize.bmHeight;
  1272. // Create grayscale/darker bitmap BEFORE mask (of hBitmapIn)
  1273. switch ((int)hBitmapOut)
  1274. {
  1275. case (int)BTNST_AUTO_GRAY:
  1276. hBitmapOut = CreateGrayscaleBitmap(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  1277. m_csBitmaps[1].hBitmap = hBitmapOut;
  1278. crTransColorOut = crTransColorIn;
  1279. break;
  1280. case (int)BTNST_AUTO_DARKER:
  1281. hBitmapOut = CreateDarkerBitmap(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  1282. m_csBitmaps[1].hBitmap = hBitmapOut;
  1283. crTransColorOut = crTransColorIn;
  1284. break;
  1285. } // switch
  1286. // Create mask for bitmap In
  1287. m_csBitmaps[0].hMask = CreateBitmapMask(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
  1288. if (m_csBitmaps[0].hMask == NULL)
  1289. {
  1290. FreeResources();
  1291. return BTNST_FAILEDMASK;
  1292. } // if
  1293. if (hBitmapOut)
  1294. {
  1295. m_csBitmaps[1].hBitmap = hBitmapOut;
  1296. m_csBitmaps[1].crTransparent = crTransColorOut;
  1297. // Get bitmap size
  1298. nRetValue = ::GetObject(hBitmapOut, sizeof(csBitmapSize), &csBitmapSize);
  1299. if (nRetValue == 0)
  1300. {
  1301. FreeResources();
  1302. return BTNST_INVALIDRESOURCE;
  1303. } // if
  1304. m_csBitmaps[1].dwWidth = (DWORD)csBitmapSize.bmWidth;
  1305. m_csBitmaps[1].dwHeight = (DWORD)csBitmapSize.bmHeight;
  1306. // Create mask for bitmap Out
  1307. m_csBitmaps[1].hMask = CreateBitmapMask(hBitmapOut, m_csBitmaps[1].dwWidth, m_csBitmaps[1].dwHeight, crTransColorOut);
  1308. if (m_csBitmaps[1].hMask == NULL)
  1309. {
  1310. FreeResources();
  1311. return BTNST_FAILEDMASK;
  1312. } // if
  1313. } // if
  1314. } // if
  1315. Invalidate();
  1316. return BTNST_OK;
  1317. } // End of SetBitmaps
  1318. // This functions sets the button to have a standard or flat style.
  1319. //
  1320. // Parameters:
  1321. // [IN] bFlat
  1322. // If TRUE the button will have a flat style, else
  1323. // will have a standard style.
  1324. // By default, CButtonST buttons are flat.
  1325. // [IN] bRepaint
  1326. // If TRUE the control will be repainted.
  1327. //
  1328. // Return value:
  1329. // BTNST_OK
  1330. // Function executed successfully.
  1331. //
  1332. DWORD CButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
  1333. {
  1334. m_bIsFlat = bFlat;
  1335. if (bRepaint) Invalidate();
  1336. return BTNST_OK;
  1337. } // End of SetFlat
  1338. // This function sets the alignment type between icon/bitmap and text.
  1339. //
  1340. // Parameters:
  1341. // [IN] byAlign
  1342. // Alignment type. Can be one of the following values:
  1343. // ST_ALIGN_HORIZ Icon/bitmap on the left, text on the right
  1344. // ST_ALIGN_VERT Icon/bitmap on the top, text on the bottom
  1345. // ST_ALIGN_HORIZ_RIGHT Icon/bitmap on the right, text on the left
  1346. // ST_ALIGN_OVERLAP Icon/bitmap on the same space as text
  1347. // By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.
  1348. // [IN] bRepaint
  1349. // If TRUE the control will be repainted.
  1350. //
  1351. // Return value:
  1352. // BTNST_OK
  1353. // Function executed successfully.
  1354. // BTNST_INVALIDALIGN
  1355. // Alignment type not supported.
  1356. //
  1357. DWORD CButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
  1358. {
  1359. switch (byAlign)
  1360. {
  1361. case ST_ALIGN_HORIZ:
  1362. case ST_ALIGN_HORIZ_RIGHT:
  1363. case ST_ALIGN_VERT:
  1364. case ST_ALIGN_OVERLAP:
  1365. m_byAlign = byAlign;
  1366. if (bRepaint) Invalidate();
  1367. return BTNST_OK;
  1368. break;
  1369. } // switch
  1370. return BTNST_INVALIDALIGN;
  1371. } // End of SetAlign
  1372. // This function sets the pressed style.
  1373. //
  1374. // Parameters:
  1375. // [IN] byStyle
  1376. // Pressed style. Can be one of the following values:
  1377. // BTNST_PRESSED_LEFTRIGHT Pressed style from left to right (as usual)
  1378. // BTNST_PRESSED_TOPBOTTOM Pressed style from top to bottom
  1379. // By default, CButtonST buttons have BTNST_PRESSED_LEFTRIGHT style.
  1380. // [IN] bRepaint
  1381. // If TRUE the control will be repainted.
  1382. //
  1383. // Return value:
  1384. // BTNST_OK
  1385. // Function executed successfully.
  1386. // BTNST_INVALIDPRESSEDSTYLE
  1387. // Pressed style not supported.
  1388. //
  1389. DWORD CButtonST::SetPressedStyle(BYTE byStyle, BOOL bRepaint)
  1390. {
  1391. switch (byStyle)
  1392. {
  1393. case BTNST_PRESSED_LEFTRIGHT:
  1394. m_ptPressedOffset.x = 1;
  1395. m_ptPressedOffset.y = 1;
  1396. break;
  1397. case BTNST_PRESSED_TOPBOTTOM:
  1398. m_ptPressedOffset.x = 0;
  1399. m_ptPressedOffset.y = 2;
  1400. break;
  1401. default:
  1402. return BTNST_INVALIDPRESSEDSTYLE;
  1403. } // switch
  1404. if (bRepaint) Invalidate();
  1405. return BTNST_OK;
  1406. } // End of SetPressedStyle
  1407. // This function sets the state of the checkbox.
  1408. // If the button is not a checkbox, this function has no meaning.
  1409. //
  1410. // Parameters:
  1411. // [IN] nCheck
  1412. // 1 to check the checkbox.
  1413. // 0 to un-check the checkbox.
  1414. // [IN] bRepaint
  1415. // If TRUE the control will be repainted.
  1416. //
  1417. // Return value:
  1418. // BTNST_OK
  1419. // Function executed successfully.
  1420. //
  1421. DWORD CButtonST::SetCheck(int nCheck, BOOL bRepaint)
  1422. {
  1423. if (m_bIsCheckBox)
  1424. {
  1425. if (nCheck == 0) m_nCheck = 0;
  1426. else m_nCheck = 1;
  1427. if (bRepaint) Invalidate();
  1428. } // if
  1429. return BTNST_OK;
  1430. } // End of SetCheck
  1431. // This function returns the current state of the checkbox.
  1432. // If the button is not a checkbox, this function has no meaning.
  1433. //
  1434. // Return value:
  1435. // The current state of the checkbox.
  1436. // 1 if checked.
  1437. // 0 if not checked or the button is not a checkbox.
  1438. //
  1439. int CButtonST::GetCheck()
  1440. {
  1441. return m_nCheck;
  1442. } // End of GetCheck
  1443. // This function sets all colors to a default value.
  1444. //
  1445. // Parameters:
  1446. // [IN] bRepaint
  1447. // If TRUE the control will be repainted.
  1448. //
  1449. // Return value:
  1450. // BTNST_OK
  1451. // Function executed successfully.
  1452. //
  1453. DWORD CButtonST::SetDefaultColors(BOOL bRepaint)
  1454. {
  1455. m_crColors[BTNST_COLOR_BK_IN] = ::GetSysColor(COLOR_BTNFACE);
  1456. m_crColors[BTNST_COLOR_FG_IN] = ::GetSysColor(COLOR_BTNTEXT);
  1457. m_crColors[BTNST_COLOR_BK_OUT] = ::GetSysColor(COLOR_BTNFACE);
  1458. m_crColors[BTNST_COLOR_FG_OUT] = ::GetSysColor(COLOR_BTNTEXT);
  1459. m_crColors[BTNST_COLOR_BK_FOCUS] = ::GetSysColor(COLOR_BTNFACE);
  1460. m_crColors[BTNST_COLOR_FG_FOCUS] = ::GetSysColor(COLOR_BTNTEXT);
  1461. if (bRepaint) Invalidate();
  1462. return BTNST_OK;
  1463. } // End of SetDefaultColors
  1464. // This function sets the color to use for a particular state.
  1465. //
  1466. // Parameters:
  1467. // [IN] byColorIndex
  1468. // Index of the color to set. Can be one of the following values:
  1469. // BTNST_COLOR_BK_IN Background color when mouse is over the button
  1470. // BTNST_COLOR_FG_IN Text color when mouse is over the button
  1471. // BTNST_COLOR_BK_OUT Background color when mouse is outside the button
  1472. // BTNST_COLOR_FG_OUT Text color when mouse is outside the button
  1473. // BTNST_COLOR_BK_FOCUS Background color when the button is focused
  1474. // BTNST_COLOR_FG_FOCUS Text color when the button is focused
  1475. // [IN] crColor
  1476. // New color.
  1477. // [IN] bRepaint
  1478. // If TRUE the control will be repainted.
  1479. //
  1480. // Return value:
  1481. // BTNST_OK
  1482. // Function executed successfully.
  1483. // BTNST_INVALIDINDEX
  1484. // Invalid color index.
  1485. //
  1486. DWORD CButtonST::SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint)
  1487. {
  1488. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1489. // Set new color
  1490. m_crColors[byColorIndex] = crColor;
  1491. if (bRepaint) Invalidate();
  1492. return BTNST_OK;
  1493. } // End of SetColor
  1494. // This functions returns the color used for a particular state.
  1495. //
  1496. // Parameters:
  1497. // [IN] byColorIndex
  1498. // Index of the color to get.
  1499. // See SetColor for the list of available colors.
  1500. // [OUT] crpColor
  1501. // A pointer to a COLORREF that will receive the color.
  1502. //
  1503. // Return value:
  1504. // BTNST_OK
  1505. // Function executed successfully.
  1506. // BTNST_INVALIDINDEX
  1507. // Invalid color index.
  1508. //
  1509. DWORD CButtonST::GetColor(BYTE byColorIndex, COLORREF* crpColor)
  1510. {
  1511. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1512. // Get color
  1513. *crpColor = m_crColors[byColorIndex];
  1514. return BTNST_OK;
  1515. } // End of GetColor
  1516. // This function applies an offset to the RGB components of the specified color.
  1517. // This function can be seen as an easy way to make a color darker or lighter than
  1518. // its default value.
  1519. //
  1520. // Parameters:
  1521. // [IN] byColorIndex
  1522. // Index of the color to set.
  1523. // See SetColor for the list of available colors.
  1524. // [IN] shOffsetColor
  1525. // A short value indicating the offset to apply to the color.
  1526. // This value must be between -255 and 255.
  1527. // [IN] bRepaint
  1528. // If TRUE the control will be repainted.
  1529. //
  1530. // Return value:
  1531. // BTNST_OK
  1532. // Function executed successfully.
  1533. // BTNST_INVALIDINDEX
  1534. // Invalid color index.
  1535. // BTNST_BADPARAM
  1536. // The specified offset is out of range.
  1537. //
  1538. DWORD CButtonST::OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint)
  1539. {
  1540. BYTE byRed = 0;
  1541. BYTE byGreen = 0;
  1542. BYTE byBlue = 0;
  1543. short shOffsetR = shOffset;
  1544. short shOffsetG = shOffset;
  1545. short shOffsetB = shOffset;
  1546. if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
  1547. if (shOffset < -255 || shOffset > 255) return BTNST_BADPARAM;
  1548. // Get RGB components of specified color
  1549. byRed = GetRValue(m_crColors[byColorIndex]);
  1550. byGreen = GetGValue(m_crColors[byColorIndex]);
  1551. byBlue = GetBValue(m_crColors[byColorIndex]);
  1552. // Calculate max. allowed real offset
  1553. if (shOffset > 0)
  1554. {
  1555. if (byRed + shOffset > 255) shOffsetR = 255 - byRed;
  1556. if (byGreen + shOffset > 255) shOffsetG = 255 - byGreen;
  1557. if (byBlue + shOffset > 255) shOffsetB = 255 - byBlue;
  1558. shOffset = min(min(shOffsetR, shOffsetG), shOffsetB);
  1559. } // if
  1560. else
  1561. {
  1562. if (byRed + shOffset < 0) shOffsetR = -byRed;
  1563. if (byGreen + shOffset < 0) shOffsetG = -byGreen;
  1564. if (byBlue + shOffset < 0) shOffsetB = -byBlue;
  1565. shOffset = max(max(shOffsetR, shOffsetG), shOffsetB);
  1566. } // else
  1567. // Set new color
  1568. m_crColors[byColorIndex] = RGB(byRed + shOffset, byGreen + shOffset, byBlue + shOffset);
  1569. if (bRepaint) Invalidate();
  1570. return BTNST_OK;
  1571. } // End of OffsetColor
  1572. // This function sets the hilight logic for the button.
  1573. // Applies only to flat buttons.
  1574. //
  1575. // Parameters:
  1576. // [IN] bAlwaysTrack
  1577. // If TRUE the button will be hilighted even if the window that owns it, is
  1578. // not the active window.
  1579. // If FALSE the button will be hilighted only if the window that owns it,
  1580. // is the active window.
  1581. //
  1582. // Return value:
  1583. // BTNST_OK
  1584. // Function executed successfully.
  1585. //
  1586. DWORD CButtonST::SetAlwaysTrack(BOOL bAlwaysTrack)
  1587. {
  1588. m_bAlwaysTrack = bAlwaysTrack;
  1589. return BTNST_OK;
  1590. } // End of SetAlwaysTrack
  1591. // This function sets the cursor to be used when the mouse is over the button.
  1592. //
  1593. // Parameters:
  1594. // [IN] nCursorId
  1595. // ID number of the cursor resource.
  1596. // Pass NULL to remove a previously loaded cursor.
  1597. // [IN] bRepaint
  1598. // If TRUE the control will be repainted.
  1599. //
  1600. // Return value:
  1601. // BTNST_OK
  1602. // Function executed successfully.
  1603. // BTNST_INVALIDRESOURCE
  1604. // Failed loading the specified resource.
  1605. //
  1606. DWORD CButtonST::SetBtnCursor(int nCursorId, BOOL bRepaint)
  1607. {
  1608. HINSTANCE hInstResource = NULL;
  1609. // Destroy any previous cursor
  1610. if (m_hCursor)
  1611. {
  1612. ::DestroyCursor(m_hCursor);
  1613. m_hCursor = NULL;
  1614. } // if
  1615. // Load cursor
  1616. if (nCursorId)
  1617. {
  1618. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR);
  1619. // Load cursor resource
  1620. m_hCursor = (HCURSOR)::LoadImage(hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
  1621. // Repaint the button
  1622. if (bRepaint) Invalidate();
  1623. // If something wrong
  1624. if (m_hCursor == NULL) return BTNST_INVALIDRESOURCE;
  1625. } // if
  1626. return BTNST_OK;
  1627. } // End of SetBtnCursor
  1628. // This function sets if the button border must be drawn.
  1629. // Applies only to flat buttons.
  1630. //
  1631. // Parameters:
  1632. // [IN] bDrawBorder
  1633. // If TRUE the border will be drawn.
  1634. // [IN] bRepaint
  1635. // If TRUE the control will be repainted.
  1636. //
  1637. // Return value:
  1638. // BTNST_OK
  1639. // Function executed successfully.
  1640. //
  1641. DWORD CButtonST::DrawBorder(BOOL bDrawBorder, BOOL bRepaint)
  1642. {
  1643. m_bDrawBorder = bDrawBorder;
  1644. // Repaint the button
  1645. if (bRepaint) Invalidate();
  1646. return BTNST_OK;
  1647. } // End of DrawBorder
  1648. // This function sets if the focus rectangle must be drawn for flat buttons.
  1649. //
  1650. // Parameters:
  1651. // [IN] bDrawFlatFocus
  1652. // If TRUE the focus rectangle will be drawn also for flat buttons.
  1653. // [IN] bRepaint
  1654. // If TRUE the control will be repainted.
  1655. //
  1656. // Return value:
  1657. // BTNST_OK
  1658. // Function executed successfully.
  1659. //
  1660. DWORD CButtonST::DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
  1661. {
  1662. m_bDrawFlatFocus = bDrawFlatFocus;
  1663. // Repaint the button
  1664. if (bRepaint) Invalidate();
  1665. return BTNST_OK;
  1666. } // End of DrawFlatFocus
  1667. void CButtonST::InitToolTip()
  1668. {
  1669. if (m_ToolTip.m_hWnd == NULL)
  1670. {
  1671. // Create ToolTip control
  1672. m_ToolTip.Create(this, m_dwToolTipStyle);
  1673. // Create inactive
  1674. m_ToolTip.Activate(FALSE);
  1675. // Enable multiline
  1676. m_ToolTip.SendMessage(TTM_SETMAXTIPWIDTH, 0, 400);
  1677. //m_ToolTip.SendMessage(TTM_SETTITLE, TTI_INFO, (LPARAM)_T("Title"));
  1678. } // if
  1679. } // End of InitToolTip
  1680. // This function sets the text to show in the button tooltip.
  1681. //
  1682. // Parameters:
  1683. // [IN] nText
  1684. // ID number of the string resource containing the text to show.
  1685. // [IN] bActivate
  1686. // If TRUE the tooltip will be created active.
  1687. //
  1688. void CButtonST::SetTooltipText(int nText, BOOL bActivate)
  1689. {
  1690. CString sText;
  1691. // Load string resource
  1692. sText.LoadString(nText);
  1693. // If string resource is not empty
  1694. if (sText.IsEmpty() == FALSE) SetTooltipText((LPCTSTR)sText, bActivate);
  1695. } // End of SetTooltipText
  1696. // This function sets the text to show in the button tooltip.
  1697. //
  1698. // Parameters:
  1699. // [IN] lpszText
  1700. // Pointer to a null-terminated string containing the text to show.
  1701. // [IN] bActivate
  1702. // If TRUE the tooltip will be created active.
  1703. //
  1704. void CButtonST::SetTooltipText(LPCTSTR lpszText, BOOL bActivate)
  1705. {
  1706. // We cannot accept NULL pointer
  1707. if (lpszText == NULL) return;
  1708. ////////////////////////////////////////// add by zsn 2013.8.20
  1709. m_strTipText = lpszText;
  1710. ///////////////////////////////////////////
  1711. // Initialize ToolTip
  1712. InitToolTip();
  1713. // If there is no tooltip defined then add it
  1714. if (m_ToolTip.GetToolCount() == 0)
  1715. {
  1716. CRect rectBtn;
  1717. GetClientRect(rectBtn);
  1718. m_ToolTip.AddTool(this, lpszText, rectBtn, 1);
  1719. } // if
  1720. // Set text for tooltip
  1721. m_ToolTip.UpdateTipText(lpszText, this, 1);
  1722. m_ToolTip.Activate(bActivate);
  1723. } // End of SetTooltipText
  1724. // This function enables or disables the button tooltip.
  1725. //
  1726. // Parameters:
  1727. // [IN] bActivate
  1728. // If TRUE the tooltip will be activated.
  1729. //
  1730. void CButtonST::ActivateTooltip(BOOL bActivate)
  1731. {
  1732. // If there is no tooltip then do nothing
  1733. if (m_ToolTip.GetToolCount() == 0) return;
  1734. // Activate tooltip
  1735. m_ToolTip.Activate(bActivate);
  1736. } // End of EnableTooltip
  1737. // This function enables the tooltip to be displayed using the balloon style.
  1738. // This function must be called before any call to SetTooltipText is made.
  1739. //
  1740. // Return value:
  1741. // BTNST_OK
  1742. // Function executed successfully.
  1743. //
  1744. DWORD CButtonST::EnableBalloonTooltip()
  1745. {
  1746. m_dwToolTipStyle |= TTS_BALLOON;
  1747. return BTNST_OK;
  1748. } // End of EnableBalloonTooltip
  1749. // This function returns if the button is the default button.
  1750. //
  1751. // Return value:
  1752. // TRUE
  1753. // The button is the default button.
  1754. // FALSE
  1755. // The button is not the default button.
  1756. //
  1757. BOOL CButtonST::GetDefault()
  1758. {
  1759. return m_bIsDefault;
  1760. } // End of GetDefault
  1761. // This function enables the transparent mode.
  1762. // Note: this operation is not reversible.
  1763. // DrawTransparent should be called just after the button is created.
  1764. // Do not use trasparent buttons until you really need it (you have a bitmapped
  1765. // background) since each transparent button makes a copy in memory of its background.
  1766. // This may bring unnecessary memory use and execution overload.
  1767. //
  1768. // Parameters:
  1769. // [IN] bRepaint
  1770. // If TRUE the control will be repainted.
  1771. //
  1772. void CButtonST::DrawTransparent(BOOL bRepaint)
  1773. {
  1774. m_bDrawTransparent = TRUE;
  1775. // Restore old bitmap (if any)
  1776. if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
  1777. {
  1778. m_dcBk.SelectObject(m_pbmpOldBk);
  1779. } // if
  1780. m_bmpBk.DeleteObject();
  1781. m_dcBk.DeleteDC();
  1782. // Repaint the button
  1783. if (bRepaint) Invalidate();
  1784. } // End of DrawTransparent
  1785. DWORD CButtonST::SetBk(CDC* pDC)
  1786. {
  1787. if (m_bDrawTransparent && pDC)
  1788. {
  1789. // Restore old bitmap (if any)
  1790. if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
  1791. {
  1792. m_dcBk.SelectObject(m_pbmpOldBk);
  1793. } // if
  1794. m_bmpBk.DeleteObject();
  1795. m_dcBk.DeleteDC();
  1796. CRect rect;
  1797. CRect rect1;
  1798. GetClientRect(rect);
  1799. GetWindowRect(rect1);
  1800. GetParent()->ScreenToClient(rect1);
  1801. m_dcBk.CreateCompatibleDC(pDC);
  1802. m_bmpBk.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
  1803. m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
  1804. m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, rect1.left, rect1.top, SRCCOPY);
  1805. return BTNST_OK;
  1806. } // if
  1807. return BTNST_BADPARAM;
  1808. } // End of SetBk
  1809. // This function sets the URL that will be opened when the button is clicked.
  1810. //
  1811. // Parameters:
  1812. // [IN] lpszURL
  1813. // Pointer to a null-terminated string that contains the URL.
  1814. // Pass NULL to removed any previously specified URL.
  1815. //
  1816. // Return value:
  1817. // BTNST_OK
  1818. // Function executed successfully.
  1819. //
  1820. DWORD CButtonST::SetURL(LPCTSTR lpszURL)
  1821. {
  1822. // Remove any existing URL
  1823. memset(m_szURL, 0, sizeof(m_szURL));
  1824. if (lpszURL)
  1825. {
  1826. // Store the URL
  1827. _tcsncpy_s(m_szURL, MAX_PATH, lpszURL, _MAX_PATH);
  1828. } // if
  1829. return BTNST_OK;
  1830. } // End of SetURL
  1831. // This function associates a menu to the button.
  1832. // The menu will be displayed clicking the button.
  1833. //
  1834. // Parameters:
  1835. // [IN] nMenu
  1836. // ID number of the menu resource.
  1837. // Pass NULL to remove any menu from the button.
  1838. // [IN] hParentWnd
  1839. // Handle to the window that owns the menu.
  1840. // This window receives all messages from the menu.
  1841. // [IN] bRepaint
  1842. // If TRUE the control will be repainted.
  1843. //
  1844. // Return value:
  1845. // BTNST_OK
  1846. // Function executed successfully.
  1847. // BTNST_INVALIDRESOURCE
  1848. // Failed loading the specified resource.
  1849. //
  1850. #ifndef BTNST_USE_BCMENU
  1851. DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint)
  1852. {
  1853. HINSTANCE hInstResource = NULL;
  1854. // Destroy any previous menu
  1855. if (m_hMenu)
  1856. {
  1857. ::DestroyMenu(m_hMenu);
  1858. m_hMenu = NULL;
  1859. m_hParentWndMenu = NULL;
  1860. m_bMenuDisplayed = FALSE;
  1861. } // if
  1862. // Load menu
  1863. if (nMenu)
  1864. {
  1865. // Find correct resource handle
  1866. hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nMenu), RT_MENU);
  1867. // Load menu resource
  1868. m_hMenu = ::LoadMenu(hInstResource, MAKEINTRESOURCE(nMenu));
  1869. m_hParentWndMenu = hParentWnd;
  1870. // If something wrong
  1871. if (m_hMenu == NULL) return BTNST_INVALIDRESOURCE;
  1872. } // if
  1873. // Repaint the button
  1874. if (bRepaint) Invalidate();
  1875. return BTNST_OK;
  1876. } // End of SetMenu
  1877. #endif
  1878. // This function associates a menu to the button.
  1879. // The menu will be displayed clicking the button.
  1880. // The menu will be handled by the BCMenu class.
  1881. //
  1882. // Parameters:
  1883. // [IN] nMenu
  1884. // ID number of the menu resource.
  1885. // Pass NULL to remove any menu from the button.
  1886. // [IN] hParentWnd
  1887. // Handle to the window that owns the menu.
  1888. // This window receives all messages from the menu.
  1889. // [IN] bWinXPStyle
  1890. // If TRUE the menu will be displayed using the new Windows XP style.
  1891. // If FALSE the menu will be displayed using the standard style.
  1892. // [IN] nToolbarID
  1893. // Resource ID of the toolbar to be associated to the menu.
  1894. // [IN] sizeToolbarIcon
  1895. // A CSize object indicating the size (in pixels) of each icon into the toolbar.
  1896. // All icons into the toolbar must have the same size.
  1897. // [IN] crToolbarBk
  1898. // A COLORREF value indicating the color to use as background for the icons into the toolbar.
  1899. // This color will be used as the "transparent" color.
  1900. // [IN] bRepaint
  1901. // If TRUE the control will be repainted.
  1902. //
  1903. // Return value:
  1904. // BTNST_OK
  1905. // Function executed successfully.
  1906. // BTNST_INVALIDRESOURCE
  1907. // Failed loading the specified resource.
  1908. //
  1909. #ifdef BTNST_USE_BCMENU
  1910. DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bWinXPStyle, UINT nToolbarID, CSize sizeToolbarIcon, COLORREF crToolbarBk, BOOL bRepaint)
  1911. {
  1912. BOOL bRetValue = FALSE;
  1913. // Destroy any previous menu
  1914. if (m_menuPopup.m_hMenu)
  1915. {
  1916. m_menuPopup.DestroyMenu();
  1917. m_hParentWndMenu = NULL;
  1918. m_bMenuDisplayed = FALSE;
  1919. } // if
  1920. // Load menu
  1921. if (nMenu)
  1922. {
  1923. m_menuPopup.SetMenuDrawMode(bWinXPStyle);
  1924. // Load menu
  1925. bRetValue = m_menuPopup.LoadMenu(nMenu);
  1926. // If something wrong
  1927. if (bRetValue == FALSE) return BTNST_INVALIDRESOURCE;
  1928. // Load toolbar
  1929. if (nToolbarID)
  1930. {
  1931. m_menuPopup.SetBitmapBackground(crToolbarBk);
  1932. m_menuPopup.SetIconSize(sizeToolbarIcon.cx, sizeToolbarIcon.cy);
  1933. bRetValue = m_menuPopup.LoadToolbar(nToolbarID);
  1934. // If something wrong
  1935. if (bRetValue == FALSE)
  1936. {
  1937. m_menuPopup.DestroyMenu();
  1938. return BTNST_INVALIDRESOURCE;
  1939. } // if
  1940. } // if
  1941. m_hParentWndMenu = hParentWnd;
  1942. } // if
  1943. // Repaint the button
  1944. if (bRepaint) Invalidate();
  1945. return BTNST_OK;
  1946. } // End of SetMenu
  1947. #endif
  1948. // This function sets the callback message that will be sent to the
  1949. // specified window just before the menu associated to the button is displayed.
  1950. //
  1951. // Parameters:
  1952. // [IN] hWnd
  1953. // Handle of the window that will receive the callback message.
  1954. // Pass NULL to remove any previously specified callback message.
  1955. // [IN] nMessage
  1956. // Callback message to send to window.
  1957. // [IN] lParam
  1958. // A 32 bits user specified value that will be passed to the callback function.
  1959. //
  1960. // Remarks:
  1961. // the callback function must be in the form:
  1962. // LRESULT On_MenuCallback(WPARAM wParam, LPARAM lParam)
  1963. // Where:
  1964. // [IN] wParam
  1965. // If support for BCMenu is enabled: a pointer to BCMenu
  1966. // else a HMENU handle to the menu that is being to be displayed.
  1967. // [IN] lParam
  1968. // The 32 bits user specified value.
  1969. //
  1970. // Return value:
  1971. // BTNST_OK
  1972. // Function executed successfully.
  1973. //
  1974. DWORD CButtonST::SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam)
  1975. {
  1976. m_csCallbacks.hWnd = hWnd;
  1977. m_csCallbacks.nMessage = nMessage;
  1978. m_csCallbacks.lParam = lParam;
  1979. return BTNST_OK;
  1980. } // End of SetMenuCallback
  1981. // This function resizes the button to the same size of the image.
  1982. // To get good results both the IN and OUT images should have the same size.
  1983. //
  1984. void CButtonST::SizeToContent()
  1985. {
  1986. if (m_csIcons[0].hIcon)
  1987. {
  1988. m_ptImageOrg.x = 0;
  1989. m_ptImageOrg.y = 0;
  1990. SetWindowPos( NULL, -1, -1, m_csIcons[0].dwWidth, m_csIcons[0].dwHeight,
  1991. SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);
  1992. } // if
  1993. else
  1994. if (m_csBitmaps[0].hBitmap)
  1995. {
  1996. m_ptImageOrg.x = 0;
  1997. m_ptImageOrg.y = 0;
  1998. SetWindowPos( NULL, -1, -1, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight,
  1999. SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);
  2000. } // if
  2001. } // End of SizeToContent
  2002. // This function sets the sound that must be played on particular button states.
  2003. //
  2004. // Parameters:
  2005. // [IN] lpszSound
  2006. // A string that specifies the sound to play.
  2007. // If hMod is NULL this string is interpreted as a filename, else it
  2008. // is interpreted as a resource identifier.
  2009. // Pass NULL to remove any previously specified sound.
  2010. // [IN] hMod
  2011. // Handle to the executable file that contains the resource to be loaded.
  2012. // This parameter must be NULL unless lpszSound specifies a resource identifier.
  2013. // [IN] bPlayOnClick
  2014. // TRUE if the sound must be played when the button is clicked.
  2015. // FALSE if the sound must be played when the mouse is moved over the button.
  2016. // [IN] bPlayAsync
  2017. // TRUE if the sound must be played asynchronously.
  2018. // FALSE if the sound must be played synchronously. The application takes control
  2019. // when the sound is completely played.
  2020. //
  2021. // Return value:
  2022. // BTNST_OK
  2023. // Function executed successfully.
  2024. //
  2025. #ifdef BTNST_USE_SOUND
  2026. DWORD CButtonST::SetSound(LPCTSTR lpszSound, HMODULE hMod, BOOL bPlayOnClick, BOOL bPlayAsync)
  2027. {
  2028. BYTE byIndex = bPlayOnClick ? 1 : 0;
  2029. // Store new sound
  2030. if (lpszSound)
  2031. {
  2032. if (hMod) // From resource identifier ?
  2033. {
  2034. m_csSounds[byIndex].lpszSound = lpszSound;
  2035. } // if
  2036. else
  2037. {
  2038. _tcscpy(m_csSounds[byIndex].szSound, lpszSound);
  2039. m_csSounds[byIndex].lpszSound = m_csSounds[byIndex].szSound;
  2040. } // else
  2041. m_csSounds[byIndex].hMod = hMod;
  2042. m_csSounds[byIndex].dwFlags = SND_NODEFAULT | SND_NOWAIT;
  2043. m_csSounds[byIndex].dwFlags |= hMod ? SND_RESOURCE : SND_FILENAME;
  2044. m_csSounds[byIndex].dwFlags |= bPlayAsync ? SND_ASYNC : SND_SYNC;
  2045. } // if
  2046. else
  2047. {
  2048. // Or remove any existing
  2049. ::ZeroMemory(&m_csSounds[byIndex], sizeof(STRUCT_SOUND));
  2050. } // else
  2051. return BTNST_OK;
  2052. } // End of SetSound
  2053. #endif
  2054. // This function is called every time the button background needs to be painted.
  2055. // If the button is in transparent mode this function will NOT be called.
  2056. // This is a virtual function that can be rewritten in CButtonST-derived classes
  2057. // to produce a whole range of buttons not available by default.
  2058. //
  2059. // Parameters:
  2060. // [IN] pDC
  2061. // Pointer to a CDC object that indicates the device context.
  2062. // [IN] pRect
  2063. // Pointer to a CRect object that indicates the bounds of the
  2064. // area to be painted.
  2065. //
  2066. // Return value:
  2067. // BTNST_OK
  2068. // Function executed successfully.
  2069. //
  2070. DWORD CButtonST::OnDrawBackground(CDC* pDC, CRect* pRect)
  2071. {
  2072. COLORREF crColor;
  2073. if (m_bIsFlat == FALSE)
  2074. {
  2075. if (m_bIsFocused || m_bIsDefault)
  2076. {
  2077. CBrush br(RGB(0,0,0));
  2078. pDC->FrameRect(pRect, &br);
  2079. pRect->DeflateRect(1, 1);
  2080. } // if
  2081. } // if
  2082. if (m_bMouseOnButton || m_bIsPressed)
  2083. crColor = m_crColors[BTNST_COLOR_BK_IN];
  2084. else
  2085. {
  2086. if (m_bIsFocused)
  2087. crColor = m_crColors[BTNST_COLOR_BK_FOCUS];
  2088. else
  2089. crColor = m_crColors[BTNST_COLOR_BK_OUT];
  2090. } // else
  2091. CBrush brBackground(crColor);
  2092. pDC->FillRect(pRect, &brBackground);
  2093. return BTNST_OK;
  2094. } // End of OnDrawBackground
  2095. // This function is called every time the button border needs to be painted.
  2096. // This is a virtual function that can be rewritten in CButtonST-derived classes
  2097. // to produce a whole range of buttons not available by default.
  2098. //
  2099. // Parameters:
  2100. // [IN] pDC
  2101. // Pointer to a CDC object that indicates the device context.
  2102. // [IN] pRect
  2103. // Pointer to a CRect object that indicates the bounds of the
  2104. // area to be painted.
  2105. //
  2106. // Return value:
  2107. // BTNST_OK
  2108. // Function executed successfully.
  2109. //
  2110. DWORD CButtonST::OnDrawBorder(CDC* pDC, CRect* pRect)
  2111. {
  2112. // Draw pressed button
  2113. if (m_bIsPressed)
  2114. {
  2115. if (m_bIsFlat)
  2116. {
  2117. if (m_bDrawBorder)
  2118. pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
  2119. }
  2120. else
  2121. {
  2122. CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
  2123. pDC->FrameRect(pRect, &brBtnShadow);
  2124. }
  2125. }
  2126. else // ...else draw non pressed button
  2127. {
  2128. CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
  2129. CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
  2130. CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
  2131. CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
  2132. if (m_bIsFlat)
  2133. {
  2134. if (m_bMouseOnButton && m_bDrawBorder)
  2135. pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
  2136. }
  2137. else
  2138. {
  2139. // Draw top-left borders
  2140. // White line
  2141. CPen* pOldPen = pDC->SelectObject(&penBtnHiLight);
  2142. pDC->MoveTo(pRect->left, pRect->bottom-1);
  2143. pDC->LineTo(pRect->left, pRect->top);
  2144. pDC->LineTo(pRect->right, pRect->top);
  2145. // Light gray line
  2146. pDC->SelectObject(pen3DLight);
  2147. pDC->MoveTo(pRect->left+1, pRect->bottom-1);
  2148. pDC->LineTo(pRect->left+1, pRect->top+1);
  2149. pDC->LineTo(pRect->right, pRect->top+1);
  2150. // Draw bottom-right borders
  2151. // Black line
  2152. pDC->SelectObject(pen3DDKShadow);
  2153. pDC->MoveTo(pRect->left, pRect->bottom-1);
  2154. pDC->LineTo(pRect->right-1, pRect->bottom-1);
  2155. pDC->LineTo(pRect->right-1, pRect->top-1);
  2156. // Dark gray line
  2157. pDC->SelectObject(penBtnShadow);
  2158. pDC->MoveTo(pRect->left+1, pRect->bottom-2);
  2159. pDC->LineTo(pRect->right-2, pRect->bottom-2);
  2160. pDC->LineTo(pRect->right-2, pRect->top);
  2161. //
  2162. pDC->SelectObject(pOldPen);
  2163. } // else
  2164. } // else
  2165. return BTNST_OK;
  2166. } // End of OnDrawBorder
  2167. #undef BS_TYPEMASK