BtnST.cpp 69 KB

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