123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460 |
- #include "stdafx.h"
- #include "BtnST.h"
- #ifdef BTNST_USE_SOUND
- #pragma comment(lib, "winmm.lib")
- #include <Mmsystem.h>
- #endif
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define BTS_TYPEMASK SS_TYPEMASK
- #ifndef TTM_SETTITLE
- #define TTM_SETTITLEA (WM_USER + 32)
- #define TTM_SETTITLEW (WM_USER + 33)
- #ifdef UNICODE
- #define TTM_SETTITLE TTM_SETTITLEW
- #else
- #define TTM_SETTITLE TTM_SETTITLEA
- #endif
- #endif
- #ifndef TTS_BALLOON
- #define TTS_BALLOON 0x40
- #endif
- CButtonST::CButtonST()
- {
- m_bIsPressed = FALSE;
- m_bIsFocused = FALSE;
- m_bIsDisabled = FALSE;
- m_bMouseOnButton = FALSE;
- FreeResources(FALSE);
-
- m_bIsFlat = TRUE;
-
- m_bAlwaysTrack = TRUE;
-
-
- m_bDrawBorder = TRUE;
-
-
- m_byAlign = ST_ALIGN_HORIZ;
-
- SetPressedStyle(BTNST_PRESSED_LEFTRIGHT, FALSE);
-
-
- m_bDrawFlatFocus = FALSE;
-
- m_bIsDefault = FALSE;
-
- m_nTypeStyle = BTS_TYPEMASK;
-
- m_bIsCheckBox = FALSE;
- m_nCheck = 0;
-
- SetDefaultColors(FALSE);
-
- m_ToolTip.m_hWnd = NULL;
- m_dwToolTipStyle = 0;
-
- m_bDrawTransparent = FALSE;
- m_pbmpOldBk = NULL;
-
- SetURL(NULL);
-
- m_hCursor = NULL;
-
- #ifndef BTNST_USE_BCMENU
- m_hMenu = NULL;
- #endif
- m_hParentWndMenu = NULL;
- m_bMenuDisplayed = FALSE;
- m_bShowDisabledBitmap = TRUE;
- m_ptImageOrg.x = 3;
- m_ptImageOrg.y = 3;
-
- ::ZeroMemory(&m_csCallbacks, sizeof(m_csCallbacks));
- #ifdef BTNST_USE_SOUND
-
- ::ZeroMemory(&m_csSounds, sizeof(m_csSounds));
- #endif
- }
- CButtonST::~CButtonST()
- {
-
- if (m_dcBk.m_hDC && m_pbmpOldBk)
- {
- m_dcBk.SelectObject(m_pbmpOldBk);
- }
- FreeResources();
-
- if (m_hCursor) ::DestroyCursor(m_hCursor);
-
- #ifdef BTNST_USE_BCMENU
- if (m_menuPopup.m_hMenu) m_menuPopup.DestroyMenu();
- #else
- if (m_hMenu) ::DestroyMenu(m_hMenu);
- #endif
- }
- BEGIN_MESSAGE_MAP(CButtonST, CButton)
-
- ON_WM_SETCURSOR()
- ON_WM_KILLFOCUS()
- ON_WM_MOUSEMOVE()
- ON_WM_SYSCOLORCHANGE()
- ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
- ON_WM_ACTIVATE()
- ON_WM_ENABLE()
- ON_WM_CANCELMODE()
- ON_WM_GETDLGCODE()
- ON_WM_CTLCOLOR_REFLECT()
-
- #ifdef BTNST_USE_BCMENU
- ON_WM_MENUCHAR()
- ON_WM_MEASUREITEM()
- #endif
- ON_MESSAGE(BM_SETSTYLE, OnSetStyle)
- ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
- ON_MESSAGE(BM_SETCHECK, OnSetCheck)
- ON_MESSAGE(BM_GETCHECK, OnGetCheck)
- END_MESSAGE_MAP()
- void CButtonST::FreeResources(BOOL bCheckForNULL)
- {
- if (bCheckForNULL)
- {
-
-
-
- if (m_csIcons[0].hIcon) ::DestroyIcon(m_csIcons[0].hIcon);
- if (m_csIcons[1].hIcon) ::DestroyIcon(m_csIcons[1].hIcon);
-
- if (m_csBitmaps[0].hBitmap) ::DeleteObject(m_csBitmaps[0].hBitmap);
- if (m_csBitmaps[1].hBitmap) ::DeleteObject(m_csBitmaps[1].hBitmap);
-
- if (m_csBitmaps[0].hMask) ::DeleteObject(m_csBitmaps[0].hMask);
- if (m_csBitmaps[1].hMask) ::DeleteObject(m_csBitmaps[1].hMask);
- }
- ::ZeroMemory(&m_csIcons, sizeof(m_csIcons));
- ::ZeroMemory(&m_csBitmaps, sizeof(m_csBitmaps));
- }
- void CButtonST::PreSubclassWindow()
- {
- UINT nBS;
- nBS = GetButtonStyle();
-
- m_nTypeStyle = nBS & BTS_TYPEMASK;
-
- if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
-
- if (m_nTypeStyle == BS_DEFPUSHBUTTON)
- {
-
- m_bIsDefault = TRUE;
-
- m_nTypeStyle = BS_PUSHBUTTON;
- }
-
-
-
- ASSERT(m_nTypeStyle != BS_OWNERDRAW);
-
- ModifyStyle(BTS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
- CButton::PreSubclassWindow();
- }
- UINT CButtonST::OnGetDlgCode()
- {
- UINT nCode = CButton::OnGetDlgCode();
-
-
- nCode |= (m_bIsDefault ? DLGC_DEFPUSHBUTTON : DLGC_UNDEFPUSHBUTTON);
- return nCode;
- }
- BOOL CButtonST::PreTranslateMessage(MSG* pMsg)
- {
- InitToolTip();
- m_ToolTip.RelayEvent(pMsg);
-
- if (pMsg->message == WM_LBUTTONDBLCLK)
- pMsg->message = WM_LBUTTONDOWN;
- return CButton::PreTranslateMessage(pMsg);
- }
- HBRUSH CButtonST::CtlColor(CDC* pDC, UINT nCtlColor)
- {
- return (HBRUSH)::GetStockObject(NULL_BRUSH);
- }
- void CButtonST::OnSysColorChange()
- {
- CButton::OnSysColorChange();
- m_dcBk.DeleteDC();
- m_bmpBk.DeleteObject();
- SetDefaultColors();
- }
- LRESULT CButtonST::OnSetStyle(WPARAM wParam, LPARAM lParam)
- {
- UINT nNewType = (wParam & BTS_TYPEMASK);
-
- if (nNewType == BS_DEFPUSHBUTTON)
- {
- m_bIsDefault = TRUE;
- }
- else if (nNewType == BS_PUSHBUTTON)
- {
-
- m_bIsDefault = FALSE;
- }
-
-
-
- return DefWindowProc(BM_SETSTYLE,
- (wParam & ~BTS_TYPEMASK) | BS_OWNERDRAW, lParam);
- }
- LRESULT CButtonST::OnSetCheck(WPARAM wParam, LPARAM lParam)
- {
- ASSERT(m_bIsCheckBox);
- switch (wParam)
- {
- case BST_CHECKED:
- case BST_INDETERMINATE:
- SetCheck(1);
- break;
- default:
- SetCheck(0);
- break;
- }
- return 0;
- }
- LRESULT CButtonST::OnGetCheck(WPARAM wParam, LPARAM lParam)
- {
- ASSERT(m_bIsCheckBox);
- return GetCheck();
- }
- #ifdef BTNST_USE_BCMENU
- LRESULT CButtonST::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu)
- {
- LRESULT lResult;
- if (BCMenu::IsMenu(pMenu))
- lResult = BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
- else
- lResult = CButton::OnMenuChar(nChar, nFlags, pMenu);
- return lResult;
- }
- #endif
- #ifdef BTNST_USE_BCMENU
- void CButtonST::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
- {
- BOOL bSetFlag = FALSE;
- if (lpMeasureItemStruct->CtlType == ODT_MENU)
- {
- if (IsMenu((HMENU)lpMeasureItemStruct->itemID) && BCMenu::IsMenu((HMENU)lpMeasureItemStruct->itemID))
- {
- m_menuPopup.MeasureItem(lpMeasureItemStruct);
- bSetFlag = TRUE;
- }
- }
- if (!bSetFlag) CButton::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
- }
- #endif
- void CButtonST::OnEnable(BOOL bEnable)
- {
- CButton::OnEnable(bEnable);
-
- if (bEnable == FALSE)
- {
- CWnd* pWnd = GetParent()->GetNextDlgTabItem(this);
- if (pWnd)
- pWnd->SetFocus();
- else
- GetParent()->SetFocus();
- CancelHover();
- }
- }
- void CButtonST::OnKillFocus(CWnd * pNewWnd)
- {
- CButton::OnKillFocus(pNewWnd);
- CancelHover();
- }
- void CButtonST::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
- {
- CButton::OnActivate(nState, pWndOther, bMinimized);
- if (nState == WA_INACTIVE) CancelHover();
- }
- void CButtonST::OnCancelMode()
- {
- CButton::OnCancelMode();
- CancelHover();
- }
- BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
-
- if (m_hCursor != NULL)
- {
- ::SetCursor(m_hCursor);
- return TRUE;
- }
- return CButton::OnSetCursor(pWnd, nHitTest, message);
- }
- void CButtonST::CancelHover()
- {
-
- if (m_bIsFlat)
- {
- if (m_bMouseOnButton)
- {
- m_bMouseOnButton = FALSE;
- Invalidate();
- }
- }
- }
- void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
- {
- CWnd* wndUnderMouse = NULL;
- CWnd* wndActive = this;
- TRACKMOUSEEVENT csTME;
- CButton::OnMouseMove(nFlags, point);
- ClientToScreen(&point);
- wndUnderMouse = WindowFromPoint(point);
-
- if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
-
- if (m_bIsFlat == FALSE) return;
- if (m_bAlwaysTrack == FALSE) wndActive = GetActiveWindow();
- if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
- {
- if (!m_bMouseOnButton)
- {
- m_bMouseOnButton = TRUE;
- Invalidate();
- #ifdef BTNST_USE_SOUND
-
- if (m_csSounds[0].lpszSound)
- ::PlaySound(m_csSounds[0].lpszSound, m_csSounds[0].hMod, m_csSounds[0].dwFlags);
- #endif
- csTME.cbSize = sizeof(csTME);
- csTME.dwFlags = TME_LEAVE;
- csTME.hwndTrack = m_hWnd;
- ::_TrackMouseEvent(&csTME);
- }
- } else CancelHover();
- }
- LRESULT CButtonST::OnMouseLeave(WPARAM wParam, LPARAM lParam)
- {
- CancelHover();
- return 0;
- }
- BOOL CButtonST::OnClicked()
- {
- SetFocus();
- #ifdef BTNST_USE_SOUND
-
- if (m_csSounds[1].lpszSound)
- ::PlaySound(m_csSounds[1].lpszSound, m_csSounds[1].hMod, m_csSounds[1].dwFlags);
- #endif
- if (m_bIsCheckBox)
- {
- m_nCheck = !m_nCheck;
- Invalidate();
- }
- else
- {
-
- #ifdef BTNST_USE_BCMENU
- if (m_menuPopup.m_hMenu)
- #else
- if (m_hMenu)
- #endif
- {
- CRect rWnd;
- GetWindowRect(rWnd);
- m_bMenuDisplayed = TRUE;
- Invalidate();
- #ifdef BTNST_USE_BCMENU
- BCMenu* psub = (BCMenu*)m_menuPopup.GetSubMenu(0);
- if (m_csCallbacks.hWnd) ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)psub, m_csCallbacks.lParam);
- DWORD dwRetValue = psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, this, NULL);
- #else
- HMENU hSubMenu = ::GetSubMenu(m_hMenu, 0);
- if (m_csCallbacks.hWnd) ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)hSubMenu, m_csCallbacks.lParam);
- DWORD dwRetValue = ::TrackPopupMenuEx(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);
- #endif
- m_bMenuDisplayed = FALSE;
- Invalidate();
- if (dwRetValue)
- ::PostMessage(m_hParentWndMenu, WM_COMMAND, MAKEWPARAM(dwRetValue, 0), (LPARAM)NULL);
- }
- else
- {
-
- if (_tcslen(m_szURL) > 0)
- {
- SHELLEXECUTEINFO csSEI;
- memset(&csSEI, 0, sizeof(csSEI));
- csSEI.cbSize = sizeof(SHELLEXECUTEINFO);
- csSEI.fMask = SEE_MASK_FLAG_NO_UI;
- csSEI.lpVerb = _T("open");
- csSEI.lpFile = m_szURL;
- csSEI.nShow = SW_SHOWMAXIMIZED;
- ::ShellExecuteEx(&csSEI);
- }
- }
- }
- return FALSE;
- }
- void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- CDC* pDC = CDC::FromHandle(lpDIS->hDC);
-
- if (m_bIsCheckBox)
- {
- m_bIsPressed = (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
- }
- else
- {
- m_bIsPressed = (lpDIS->itemState & ODS_SELECTED);
-
- if (
- #ifdef BTNST_USE_BCMENU
- m_menuPopup.m_hMenu
- #else
- m_hMenu
- #endif
- && m_bMenuDisplayed) m_bIsPressed = TRUE;
- }
- m_bIsFocused = (lpDIS->itemState & ODS_FOCUS);
- m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
- CRect itemRect = lpDIS->rcItem;
- pDC->SetBkMode(TRANSPARENT);
-
-
- if (m_bDrawTransparent)
- PaintBk(pDC);
- else
- OnDrawBackground(pDC, &itemRect);
-
- OnDrawBorder(pDC, &itemRect);
-
- CString sTitle;
- GetWindowText(sTitle);
- CRect captionRect = lpDIS->rcItem;
-
- if (m_csIcons[0].hIcon)
- {
- DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
- }
- if (m_csBitmaps[0].hBitmap)
- {
- pDC->SetBkColor(RGB(255,255,255));
- DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
- }
-
- if (sTitle.IsEmpty() == FALSE)
- {
- DrawTheText(pDC, (LPCTSTR)sTitle, &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
- }
- if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
- {
-
- if (m_bIsFocused)
- {
- CRect focusRect = itemRect;
- focusRect.DeflateRect(3, 3);
- pDC->DrawFocusRect(&focusRect);
- }
- }
- }
- void CButtonST::PaintBk(CDC* pDC)
- {
- CClientDC clDC(GetParent());
- CRect rect;
- CRect rect1;
- GetClientRect(rect);
- GetWindowRect(rect1);
- GetParent()->ScreenToClient(rect1);
- if (m_dcBk.m_hDC == NULL)
- {
- m_dcBk.CreateCompatibleDC(&clDC);
- m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
- m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
- m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
- }
- pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
- }
- HBITMAP CButtonST::CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor)
- {
- HBITMAP hMask = NULL;
- HDC hdcSrc = NULL;
- HDC hdcDest = NULL;
- HBITMAP hbmSrcT = NULL;
- HBITMAP hbmDestT = NULL;
- COLORREF crSaveBk;
- COLORREF crSaveDestText;
- hMask = ::CreateBitmap(dwWidth, dwHeight, 1, 1, NULL);
- if (hMask == NULL) return NULL;
- hdcSrc = ::CreateCompatibleDC(NULL);
- hdcDest = ::CreateCompatibleDC(NULL);
- hbmSrcT = (HBITMAP)::SelectObject(hdcSrc, hSourceBitmap);
- hbmDestT = (HBITMAP)::SelectObject(hdcDest, hMask);
- crSaveBk = ::SetBkColor(hdcSrc, crTransColor);
- ::BitBlt(hdcDest, 0, 0, dwWidth, dwHeight, hdcSrc, 0, 0, SRCCOPY);
- crSaveDestText = ::SetTextColor(hdcSrc, RGB(255, 255, 255));
- ::SetBkColor(hdcSrc,RGB(0, 0, 0));
- ::BitBlt(hdcSrc, 0, 0, dwWidth, dwHeight, hdcDest, 0, 0, SRCAND);
- SetTextColor(hdcDest, crSaveDestText);
- ::SetBkColor(hdcSrc, crSaveBk);
- ::SelectObject(hdcSrc, hbmSrcT);
- ::SelectObject(hdcDest, hbmDestT);
- ::DeleteDC(hdcSrc);
- ::DeleteDC(hdcDest);
- return hMask;
- }
- void CButtonST::PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage)
- {
- CRect rBtn;
- rpImage->CopyRect(rpItem);
- switch (m_byAlign)
- {
- case ST_ALIGN_HORIZ:
- if (bHasTitle == FALSE)
- {
-
- rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
- }
- else
- {
-
- rpImage->left += m_ptImageOrg.x;
- rpTitle->left += dwWidth + m_ptImageOrg.x;
- }
-
- rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
- break;
- case ST_ALIGN_HORIZ_RIGHT:
- GetClientRect(&rBtn);
- if (bHasTitle == FALSE)
- {
-
- rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
- }
- else
- {
-
- rpTitle->right = rpTitle->Width() - dwWidth - m_ptImageOrg.x;
- rpTitle->left = m_ptImageOrg.x;
- rpImage->left = rBtn.right - dwWidth - m_ptImageOrg.x;
-
- rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
- }
- break;
-
- case ST_ALIGN_VERT:
-
- rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
- if (bHasTitle == FALSE)
- {
-
- rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
- }
- else
- {
- rpImage->top = m_ptImageOrg.y;
- rpTitle->top += dwHeight;
- }
- break;
- case ST_ALIGN_OVERLAP:
- break;
- }
-
-
- if (bIsPressed && m_bIsCheckBox == FALSE)
- rpImage->OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
- }
- void CButtonST::DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
- {
- BYTE byIndex = 0;
-
- if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
- byIndex = 0;
- else
- byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
- CRect rImage;
- PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);
-
- pDC->DrawState( rImage.TopLeft(),
- rImage.Size(),
- m_csIcons[byIndex].hIcon,
- (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
- (CBrush*)NULL);
- }
- void CButtonST::DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
- {
- HDC hdcBmpMem = NULL;
- HBITMAP hbmOldBmp = NULL;
- HDC hdcMem = NULL;
- HBITMAP hbmT = NULL;
- BYTE byIndex = 0;
-
- if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
- byIndex = 0;
- else
- byIndex = (m_csBitmaps[1].hBitmap == NULL ? 0 : 1);
- CRect rImage;
- PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, &rImage);
- hdcBmpMem = ::CreateCompatibleDC(pDC->m_hDC);
- hbmOldBmp = (HBITMAP)::SelectObject(hdcBmpMem, m_csBitmaps[byIndex].hBitmap);
- hdcMem = ::CreateCompatibleDC(NULL);
- hbmT = (HBITMAP)::SelectObject(hdcMem, m_csBitmaps[byIndex].hMask);
- if (bIsDisabled && m_bShowDisabledBitmap)
- {
- HDC hDC = NULL;
- HBITMAP hBitmap = NULL;
- hDC = ::CreateCompatibleDC(pDC->m_hDC);
- hBitmap = ::CreateCompatibleBitmap(pDC->m_hDC, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight);
- HBITMAP hOldBmp2 = (HBITMAP)::SelectObject(hDC, hBitmap);
- RECT rRect;
- rRect.left = 0;
- rRect.top = 0;
- rRect.right = rImage.right + 1;
- rRect.bottom = rImage.bottom + 1;
- ::FillRect(hDC, &rRect, (HBRUSH)RGB(255, 255, 255));
- COLORREF crOldColor = ::SetBkColor(hDC, RGB(255,255,255));
- ::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
- ::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
- ::SetBkColor(hDC, crOldColor);
- ::SelectObject(hDC, hOldBmp2);
- ::DeleteDC(hDC);
- pDC->DrawState( CPoint(rImage.left, rImage.top),
- CSize(m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight),
- hBitmap, DST_BITMAP | DSS_DISABLED);
- ::DeleteObject(hBitmap);
- }
- else
- {
- ::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
- ::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
- }
- ::SelectObject(hdcMem, hbmT);
- ::DeleteDC(hdcMem);
- ::SelectObject(hdcBmpMem, hbmOldBmp);
- ::DeleteDC(hdcBmpMem);
- }
- void CButtonST::DrawTheText(CDC* pDC, LPCTSTR lpszText, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
- {
-
-
- if (m_bIsPressed && m_bIsCheckBox == FALSE)
- rpCaption->OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
-
-
-
-
- CRect centerRect = rpCaption;
- pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
- rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())/2, (centerRect.Height() - rpCaption->Height())/2);
-
- pDC->SetBkMode(TRANSPARENT);
-
- if (m_bIsDisabled)
- {
- rpCaption->OffsetRect(1, 1);
- pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
- pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
- rpCaption->OffsetRect(-1, -1);
- pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
- pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
- }
- else
- {
- if (m_bMouseOnButton || m_bIsPressed)
- {
- pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
- pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
- }
- else
- {
- if (m_bIsFocused)
- {
- pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_FOCUS]);
- pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_FOCUS]);
- }
- else
- {
- pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
- pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
- }
- }
- pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
- }
- }
- HBITMAP CButtonST::CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
- {
- HBITMAP hGrayBitmap = NULL;
- HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
- HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
- hMainDC = ::GetDC(NULL);
- if (hMainDC == NULL) return NULL;
- hMemDC1 = ::CreateCompatibleDC(hMainDC);
- if (hMemDC1 == NULL)
- {
- ::ReleaseDC(NULL, hMainDC);
- return NULL;
- }
- hMemDC2 = ::CreateCompatibleDC(hMainDC);
- if (hMemDC2 == NULL)
- {
- ::DeleteDC(hMemDC1);
- ::ReleaseDC(NULL, hMainDC);
- return NULL;
- }
- hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
- if (hGrayBitmap)
- {
- hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
- hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
-
- DWORD dwLoopY = 0, dwLoopX = 0;
- COLORREF crPixel = 0;
- BYTE byNewPixel = 0;
- for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
- {
- for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
- {
- crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
- byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
- if (crPixel != crTrans)
- ::SetPixel(hMemDC1, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
- else
- ::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
- }
- }
- ::SelectObject(hMemDC1, hOldBmp1);
- ::SelectObject(hMemDC2, hOldBmp2);
- }
- ::DeleteDC(hMemDC1);
- ::DeleteDC(hMemDC2);
- ::ReleaseDC(NULL, hMainDC);
- return hGrayBitmap;
- }
- HBITMAP CButtonST::CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
- {
- HBITMAP hGrayBitmap = NULL;
- HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
- HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
- hMainDC = ::GetDC(NULL);
- if (hMainDC == NULL) return NULL;
- hMemDC1 = ::CreateCompatibleDC(hMainDC);
- if (hMemDC1 == NULL)
- {
- ::ReleaseDC(NULL, hMainDC);
- return NULL;
- }
- hMemDC2 = ::CreateCompatibleDC(hMainDC);
- if (hMemDC2 == NULL)
- {
- ::DeleteDC(hMemDC1);
- ::ReleaseDC(NULL, hMainDC);
- return NULL;
- }
- hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
- if (hGrayBitmap)
- {
- hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
- hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
-
- DWORD dwLoopY = 0, dwLoopX = 0;
- COLORREF crPixel = 0;
- for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
- {
- for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
- {
- crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
- if (crPixel != crTrans)
- ::SetPixel(hMemDC1, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
- else
- ::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
- }
- }
- ::SelectObject(hMemDC1, hOldBmp1);
- ::SelectObject(hMemDC2, hOldBmp2);
- }
- ::DeleteDC(hMemDC1);
- ::DeleteDC(hMemDC2);
- ::ReleaseDC(NULL, hMainDC);
- return hGrayBitmap;
- }
- HICON CButtonST::CreateGrayscaleIcon(HICON hIcon)
- {
- HICON hGrayIcon = NULL;
- HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
- BITMAP bmp;
- HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
- ICONINFO csII, csGrayII;
- BOOL bRetValue = FALSE;
- bRetValue = ::GetIconInfo(hIcon, &csII);
- if (bRetValue == FALSE) return NULL;
- hMainDC = ::GetDC(NULL);
- hMemDC1 = ::CreateCompatibleDC(hMainDC);
- hMemDC2 = ::CreateCompatibleDC(hMainDC);
- if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
-
- if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
- {
- DWORD dwWidth = csII.xHotspot*2;
- DWORD dwHeight = csII.yHotspot*2;
- csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
- if (csGrayII.hbmColor)
- {
- hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
- hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
-
- DWORD dwLoopY = 0, dwLoopX = 0;
- COLORREF crPixel = 0;
- BYTE byNewPixel = 0;
- for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
- {
- for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
- {
- crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
- byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
- if (crPixel)
- ::SetPixel(hMemDC2, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
- else
- ::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
- }
- }
- ::SelectObject(hMemDC1, hOldBmp1);
- ::SelectObject(hMemDC2, hOldBmp2);
- csGrayII.hbmMask = csII.hbmMask;
- csGrayII.fIcon = TRUE;
- hGrayIcon = ::CreateIconIndirect(&csGrayII);
- }
- ::DeleteObject(csGrayII.hbmColor);
-
- }
- ::DeleteObject(csII.hbmColor);
- ::DeleteObject(csII.hbmMask);
- ::DeleteDC(hMemDC1);
- ::DeleteDC(hMemDC2);
- ::ReleaseDC(NULL, hMainDC);
- return hGrayIcon;
- }
- HICON CButtonST::CreateDarkerIcon(HICON hIcon)
- {
- HICON hGrayIcon = NULL;
- HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
- BITMAP bmp;
- HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
- ICONINFO csII, csGrayII;
- BOOL bRetValue = FALSE;
- bRetValue = ::GetIconInfo(hIcon, &csII);
- if (bRetValue == FALSE) return NULL;
- hMainDC = ::GetDC(NULL);
- hMemDC1 = ::CreateCompatibleDC(hMainDC);
- hMemDC2 = ::CreateCompatibleDC(hMainDC);
- if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
-
- if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
- {
- DWORD dwWidth = csII.xHotspot*2;
- DWORD dwHeight = csII.yHotspot*2;
- csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
- if (csGrayII.hbmColor)
- {
- hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
- hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
-
- DWORD dwLoopY = 0, dwLoopX = 0;
- COLORREF crPixel = 0;
- for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
- {
- for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
- {
- crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
- if (crPixel)
- ::SetPixel(hMemDC2, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
- else
- ::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
- }
- }
- ::SelectObject(hMemDC1, hOldBmp1);
- ::SelectObject(hMemDC2, hOldBmp2);
- csGrayII.hbmMask = csII.hbmMask;
- csGrayII.fIcon = TRUE;
- hGrayIcon = ::CreateIconIndirect(&csGrayII);
- }
- ::DeleteObject(csGrayII.hbmColor);
-
- }
- ::DeleteObject(csII.hbmColor);
- ::DeleteObject(csII.hbmMask);
- ::DeleteDC(hMemDC1);
- ::DeleteDC(hMemDC2);
- ::ReleaseDC(NULL, hMainDC);
- return hGrayIcon;
- }
- COLORREF CButtonST::DarkenColor(COLORREF crColor, double dFactor)
- {
- if (dFactor > 0.0 && dFactor <= 1.0)
- {
- BYTE red,green,blue,lightred,lightgreen,lightblue;
- red = GetRValue(crColor);
- green = GetGValue(crColor);
- blue = GetBValue(crColor);
- lightred = (BYTE)(red-(dFactor * red));
- lightgreen = (BYTE)(green-(dFactor * green));
- lightblue = (BYTE)(blue-(dFactor * blue));
- crColor = RGB(lightred,lightgreen,lightblue);
- }
- return crColor;
- }
- DWORD CButtonST::SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut, int nCxDesiredOut, int nCyDesiredOut)
- {
- HICON hIconIn = NULL;
- HICON hIconOut = NULL;
- HINSTANCE hInstResource = NULL;
-
- hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
-
- hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, nCxDesiredIn, nCyDesiredIn, 0);
-
- switch (nIconOut)
- {
- case NULL:
- break;
- case (int)BTNST_AUTO_GRAY:
- hIconOut = BTNST_AUTO_GRAY;
- break;
- case (int)BTNST_AUTO_DARKER:
- hIconOut = BTNST_AUTO_DARKER;
- break;
- default:
- hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, nCxDesiredOut, nCyDesiredOut, 0);
- break;
- }
- return SetIcon(hIconIn, hIconOut);
- }
- DWORD CButtonST::SetIcon(int nIconIn, int nIconOut)
- {
- return SetIcon(nIconIn, 0, 0, nIconOut, 0, 0);
- }
- DWORD CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
- {
- BOOL bRetValue;
- ICONINFO ii;
-
- FreeResources();
- if (hIconIn)
- {
-
- m_csIcons[0].hIcon = hIconIn;
-
- ::ZeroMemory(&ii, sizeof(ICONINFO));
- bRetValue = ::GetIconInfo(hIconIn, &ii);
- if (bRetValue == FALSE)
- {
- FreeResources();
- return BTNST_INVALIDRESOURCE;
- }
- m_csIcons[0].dwWidth = (DWORD)(ii.xHotspot * 2);
- m_csIcons[0].dwHeight = (DWORD)(ii.yHotspot * 2);
- ::DeleteObject(ii.hbmMask);
- ::DeleteObject(ii.hbmColor);
-
- if (hIconOut)
- {
- switch ((int)hIconOut)
- {
- case (int)BTNST_AUTO_GRAY:
- hIconOut = CreateGrayscaleIcon(hIconIn);
- break;
- case (int)BTNST_AUTO_DARKER:
- hIconOut = CreateDarkerIcon(hIconIn);
- break;
- }
- m_csIcons[1].hIcon = hIconOut;
-
- ::ZeroMemory(&ii, sizeof(ICONINFO));
- bRetValue = ::GetIconInfo(hIconOut, &ii);
- if (bRetValue == FALSE)
- {
- FreeResources();
- return BTNST_INVALIDRESOURCE;
- }
- m_csIcons[1].dwWidth = (DWORD)(ii.xHotspot * 2);
- m_csIcons[1].dwHeight = (DWORD)(ii.yHotspot * 2);
- ::DeleteObject(ii.hbmMask);
- ::DeleteObject(ii.hbmColor);
- }
- }
- Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut, COLORREF crTransColorOut)
- {
- HBITMAP hBitmapIn = NULL;
- HBITMAP hBitmapOut = NULL;
- HINSTANCE hInstResource = NULL;
-
-
- hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapIn), RT_BITMAP);
-
- hBitmapIn = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
-
- switch (nBitmapOut)
- {
- case NULL:
- break;
- case (int)BTNST_AUTO_GRAY:
- hBitmapOut = (HBITMAP)BTNST_AUTO_GRAY;
- break;
- case (int)BTNST_AUTO_DARKER:
- hBitmapOut = (HBITMAP)BTNST_AUTO_DARKER;
- break;
- default:
- hBitmapOut = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
- break;
- }
- return SetBitmaps(hBitmapIn, crTransColorIn, hBitmapOut, crTransColorOut);
- }
- DWORD CButtonST::SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut, COLORREF crTransColorOut)
- {
- int nRetValue = 0;
- BITMAP csBitmapSize;
-
- FreeResources();
- if (hBitmapIn)
- {
- m_csBitmaps[0].hBitmap = hBitmapIn;
- m_csBitmaps[0].crTransparent = crTransColorIn;
-
- nRetValue = ::GetObject(hBitmapIn, sizeof(csBitmapSize), &csBitmapSize);
- if (nRetValue == 0)
- {
- FreeResources();
- return BTNST_INVALIDRESOURCE;
- }
- m_csBitmaps[0].dwWidth = (DWORD)csBitmapSize.bmWidth;
- m_csBitmaps[0].dwHeight = (DWORD)csBitmapSize.bmHeight;
-
- switch ((int)hBitmapOut)
- {
- case (int)BTNST_AUTO_GRAY:
- hBitmapOut = CreateGrayscaleBitmap(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
- m_csBitmaps[1].hBitmap = hBitmapOut;
- crTransColorOut = crTransColorIn;
- break;
- case (int)BTNST_AUTO_DARKER:
- hBitmapOut = CreateDarkerBitmap(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
- m_csBitmaps[1].hBitmap = hBitmapOut;
- crTransColorOut = crTransColorIn;
- break;
- }
-
- m_csBitmaps[0].hMask = CreateBitmapMask(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
- if (m_csBitmaps[0].hMask == NULL)
- {
- FreeResources();
- return BTNST_FAILEDMASK;
- }
- if (hBitmapOut)
- {
- m_csBitmaps[1].hBitmap = hBitmapOut;
- m_csBitmaps[1].crTransparent = crTransColorOut;
-
- nRetValue = ::GetObject(hBitmapOut, sizeof(csBitmapSize), &csBitmapSize);
- if (nRetValue == 0)
- {
- FreeResources();
- return BTNST_INVALIDRESOURCE;
- }
- m_csBitmaps[1].dwWidth = (DWORD)csBitmapSize.bmWidth;
- m_csBitmaps[1].dwHeight = (DWORD)csBitmapSize.bmHeight;
-
- m_csBitmaps[1].hMask = CreateBitmapMask(hBitmapOut, m_csBitmaps[1].dwWidth, m_csBitmaps[1].dwHeight, crTransColorOut);
- if (m_csBitmaps[1].hMask == NULL)
- {
- FreeResources();
- return BTNST_FAILEDMASK;
- }
- }
- }
- Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
- {
- m_bIsFlat = bFlat;
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
- {
- switch (byAlign)
- {
- case ST_ALIGN_HORIZ:
- case ST_ALIGN_HORIZ_RIGHT:
- case ST_ALIGN_VERT:
- case ST_ALIGN_OVERLAP:
- m_byAlign = byAlign;
- if (bRepaint) Invalidate();
- return BTNST_OK;
- break;
- }
- return BTNST_INVALIDALIGN;
- }
- DWORD CButtonST::SetPressedStyle(BYTE byStyle, BOOL bRepaint)
- {
- switch (byStyle)
- {
- case BTNST_PRESSED_LEFTRIGHT:
- m_ptPressedOffset.x = 1;
- m_ptPressedOffset.y = 1;
- break;
- case BTNST_PRESSED_TOPBOTTOM:
- m_ptPressedOffset.x = 0;
- m_ptPressedOffset.y = 2;
- break;
- default:
- return BTNST_INVALIDPRESSEDSTYLE;
- }
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::SetCheck(int nCheck, BOOL bRepaint)
- {
- if (m_bIsCheckBox)
- {
- if (nCheck == 0) m_nCheck = 0;
- else m_nCheck = 1;
- if (bRepaint) Invalidate();
- }
- return BTNST_OK;
- }
- int CButtonST::GetCheck()
- {
- return m_nCheck;
- }
- DWORD CButtonST::SetDefaultColors(BOOL bRepaint)
- {
- m_crColors[BTNST_COLOR_BK_IN] = ::GetSysColor(COLOR_BTNFACE);
- m_crColors[BTNST_COLOR_FG_IN] = ::GetSysColor(COLOR_BTNTEXT);
- m_crColors[BTNST_COLOR_BK_OUT] = ::GetSysColor(COLOR_BTNFACE);
- m_crColors[BTNST_COLOR_FG_OUT] = ::GetSysColor(COLOR_BTNTEXT);
- m_crColors[BTNST_COLOR_BK_FOCUS] = ::GetSysColor(COLOR_BTNFACE);
- m_crColors[BTNST_COLOR_FG_FOCUS] = ::GetSysColor(COLOR_BTNTEXT);
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint)
- {
- if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
-
- m_crColors[byColorIndex] = crColor;
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::GetColor(BYTE byColorIndex, COLORREF* crpColor)
- {
- if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
-
- *crpColor = m_crColors[byColorIndex];
- return BTNST_OK;
- }
- DWORD CButtonST::OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint)
- {
- BYTE byRed = 0;
- BYTE byGreen = 0;
- BYTE byBlue = 0;
- short shOffsetR = shOffset;
- short shOffsetG = shOffset;
- short shOffsetB = shOffset;
- if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
- if (shOffset < -255 || shOffset > 255) return BTNST_BADPARAM;
-
- byRed = GetRValue(m_crColors[byColorIndex]);
- byGreen = GetGValue(m_crColors[byColorIndex]);
- byBlue = GetBValue(m_crColors[byColorIndex]);
-
- if (shOffset > 0)
- {
- if (byRed + shOffset > 255) shOffsetR = 255 - byRed;
- if (byGreen + shOffset > 255) shOffsetG = 255 - byGreen;
- if (byBlue + shOffset > 255) shOffsetB = 255 - byBlue;
- shOffset = min(min(shOffsetR, shOffsetG), shOffsetB);
- }
- else
- {
- if (byRed + shOffset < 0) shOffsetR = -byRed;
- if (byGreen + shOffset < 0) shOffsetG = -byGreen;
- if (byBlue + shOffset < 0) shOffsetB = -byBlue;
- shOffset = max(max(shOffsetR, shOffsetG), shOffsetB);
- }
-
- m_crColors[byColorIndex] = RGB(byRed + shOffset, byGreen + shOffset, byBlue + shOffset);
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::SetAlwaysTrack(BOOL bAlwaysTrack)
- {
- m_bAlwaysTrack = bAlwaysTrack;
- return BTNST_OK;
- }
- DWORD CButtonST::SetBtnCursor(int nCursorId, BOOL bRepaint)
- {
- HINSTANCE hInstResource = NULL;
-
- if (m_hCursor)
- {
- ::DestroyCursor(m_hCursor);
- m_hCursor = NULL;
- }
-
- if (nCursorId)
- {
- hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR);
-
- m_hCursor = (HCURSOR)::LoadImage(hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
-
- if (bRepaint) Invalidate();
-
- if (m_hCursor == NULL) return BTNST_INVALIDRESOURCE;
- }
- return BTNST_OK;
- }
- DWORD CButtonST::DrawBorder(BOOL bDrawBorder, BOOL bRepaint)
- {
- m_bDrawBorder = bDrawBorder;
-
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- DWORD CButtonST::DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
- {
- m_bDrawFlatFocus = bDrawFlatFocus;
-
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- void CButtonST::InitToolTip()
- {
- if (m_ToolTip.m_hWnd == NULL)
- {
-
- m_ToolTip.Create(this, m_dwToolTipStyle);
-
- m_ToolTip.Activate(FALSE);
-
- m_ToolTip.SendMessage(TTM_SETMAXTIPWIDTH, 0, 400);
-
- }
- }
- void CButtonST::SetTooltipText(int nText, BOOL bActivate)
- {
- CString sText;
-
- sText.LoadString(nText);
-
- if (sText.IsEmpty() == FALSE) SetTooltipText((LPCTSTR)sText, bActivate);
- }
- void CButtonST::SetTooltipText(LPCTSTR lpszText, BOOL bActivate)
- {
-
- if (lpszText == NULL) return;
-
- InitToolTip();
-
- if (m_ToolTip.GetToolCount() == 0)
- {
- CRect rectBtn;
- GetClientRect(rectBtn);
- m_ToolTip.AddTool(this, lpszText, rectBtn, 1);
- }
-
- m_ToolTip.UpdateTipText(lpszText, this, 1);
- m_ToolTip.Activate(bActivate);
- }
- void CButtonST::ActivateTooltip(BOOL bActivate)
- {
-
- if (m_ToolTip.GetToolCount() == 0) return;
-
- m_ToolTip.Activate(bActivate);
- }
- DWORD CButtonST::EnableBalloonTooltip()
- {
- m_dwToolTipStyle |= TTS_BALLOON;
- return BTNST_OK;
- }
- BOOL CButtonST::GetDefault()
- {
- return m_bIsDefault;
- }
- void CButtonST::DrawTransparent(BOOL bRepaint)
- {
- m_bDrawTransparent = TRUE;
-
- if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
- {
- m_dcBk.SelectObject(m_pbmpOldBk);
- }
- m_bmpBk.DeleteObject();
- m_dcBk.DeleteDC();
-
- if (bRepaint) Invalidate();
- }
- DWORD CButtonST::SetBk(CDC* pDC)
- {
- if (m_bDrawTransparent && pDC)
- {
-
- if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
- {
- m_dcBk.SelectObject(m_pbmpOldBk);
- }
- m_bmpBk.DeleteObject();
- m_dcBk.DeleteDC();
- CRect rect;
- CRect rect1;
- GetClientRect(rect);
- GetWindowRect(rect1);
- GetParent()->ScreenToClient(rect1);
- m_dcBk.CreateCompatibleDC(pDC);
- m_bmpBk.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
- m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
- m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, rect1.left, rect1.top, SRCCOPY);
- return BTNST_OK;
- }
- return BTNST_BADPARAM;
- }
- DWORD CButtonST::SetURL(LPCTSTR lpszURL)
- {
-
- memset(m_szURL, 0, sizeof(m_szURL));
- if (lpszURL)
- {
-
- _tcsncpy_s(m_szURL, lpszURL, _MAX_PATH);
- }
- return BTNST_OK;
- }
- #ifndef BTNST_USE_BCMENU
- DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint)
- {
- HINSTANCE hInstResource = NULL;
-
- if (m_hMenu)
- {
- ::DestroyMenu(m_hMenu);
- m_hMenu = NULL;
- m_hParentWndMenu = NULL;
- m_bMenuDisplayed = FALSE;
- }
-
- if (nMenu)
- {
-
- hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nMenu), RT_MENU);
-
- m_hMenu = ::LoadMenu(hInstResource, MAKEINTRESOURCE(nMenu));
- m_hParentWndMenu = hParentWnd;
-
- if (m_hMenu == NULL) return BTNST_INVALIDRESOURCE;
- }
-
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- #endif
- #ifdef BTNST_USE_BCMENU
- DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bWinXPStyle, UINT nToolbarID, CSize sizeToolbarIcon, COLORREF crToolbarBk, BOOL bRepaint)
- {
- BOOL bRetValue = FALSE;
-
- if (m_menuPopup.m_hMenu)
- {
- m_menuPopup.DestroyMenu();
- m_hParentWndMenu = NULL;
- m_bMenuDisplayed = FALSE;
- }
-
- if (nMenu)
- {
- m_menuPopup.SetMenuDrawMode(bWinXPStyle);
-
- bRetValue = m_menuPopup.LoadMenu(nMenu);
-
- if (bRetValue == FALSE) return BTNST_INVALIDRESOURCE;
-
- if (nToolbarID)
- {
- m_menuPopup.SetBitmapBackground(crToolbarBk);
- m_menuPopup.SetIconSize(sizeToolbarIcon.cx, sizeToolbarIcon.cy);
- bRetValue = m_menuPopup.LoadToolbar(nToolbarID);
-
- if (bRetValue == FALSE)
- {
- m_menuPopup.DestroyMenu();
- return BTNST_INVALIDRESOURCE;
- }
- }
- m_hParentWndMenu = hParentWnd;
- }
-
- if (bRepaint) Invalidate();
- return BTNST_OK;
- }
- #endif
- DWORD CButtonST::SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam)
- {
- m_csCallbacks.hWnd = hWnd;
- m_csCallbacks.nMessage = nMessage;
- m_csCallbacks.lParam = lParam;
- return BTNST_OK;
- }
- void CButtonST::SizeToContent()
- {
- if (m_csIcons[0].hIcon)
- {
- m_ptImageOrg.x = 0;
- m_ptImageOrg.y = 0;
- SetWindowPos( NULL, -1, -1, m_csIcons[0].dwWidth, m_csIcons[0].dwHeight,
- SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);
- }
- else
- if (m_csBitmaps[0].hBitmap)
- {
- m_ptImageOrg.x = 0;
- m_ptImageOrg.y = 0;
- SetWindowPos( NULL, -1, -1, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight,
- SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW | SWP_NOACTIVATE);
- }
- }
- #ifdef BTNST_USE_SOUND
- DWORD CButtonST::SetSound(LPCTSTR lpszSound, HMODULE hMod, BOOL bPlayOnClick, BOOL bPlayAsync)
- {
- BYTE byIndex = bPlayOnClick ? 1 : 0;
-
- if (lpszSound)
- {
- if (hMod)
- {
- m_csSounds[byIndex].lpszSound = lpszSound;
- }
- else
- {
- _tcscpy_s(m_csSounds[byIndex].szSound, lpszSound);
- m_csSounds[byIndex].lpszSound = m_csSounds[byIndex].szSound;
- }
- m_csSounds[byIndex].hMod = hMod;
- m_csSounds[byIndex].dwFlags = SND_NODEFAULT | SND_NOWAIT;
- m_csSounds[byIndex].dwFlags |= hMod ? SND_RESOURCE : SND_FILENAME;
- m_csSounds[byIndex].dwFlags |= bPlayAsync ? SND_ASYNC : SND_SYNC;
- }
- else
- {
-
- ::ZeroMemory(&m_csSounds[byIndex], sizeof(STRUCT_SOUND));
- }
- return BTNST_OK;
- }
- #endif
- DWORD CButtonST::OnDrawBackground(CDC* pDC, CRect* pRect)
- {
- COLORREF crColor;
- if (m_bIsFlat == FALSE)
- {
- if (m_bIsFocused || m_bIsDefault)
- {
- CBrush br(RGB(0,0,0));
- pDC->FrameRect(pRect, &br);
- pRect->DeflateRect(1, 1);
- }
- }
- if (m_bMouseOnButton || m_bIsPressed)
- crColor = m_crColors[BTNST_COLOR_BK_IN];
- else
- {
- if (m_bIsFocused)
- crColor = m_crColors[BTNST_COLOR_BK_FOCUS];
- else
- crColor = m_crColors[BTNST_COLOR_BK_OUT];
- }
- CBrush brBackground(crColor);
- pDC->FillRect(pRect, &brBackground);
- return BTNST_OK;
- }
- DWORD CButtonST::OnDrawBorder(CDC* pDC, CRect* pRect)
- {
-
- if (m_bIsPressed)
- {
- if (m_bIsFlat)
- {
- if (m_bDrawBorder)
- pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
- }
- else
- {
- CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
- pDC->FrameRect(pRect, &brBtnShadow);
- }
- }
- else
- {
- CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT));
- CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
- CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
- CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
- if (m_bIsFlat)
- {
- if (m_bMouseOnButton && m_bDrawBorder)
- pDC->Draw3dRect(pRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
- }
- else
- {
-
-
- CPen* pOldPen = pDC->SelectObject(&penBtnHiLight);
- pDC->MoveTo(pRect->left, pRect->bottom-1);
- pDC->LineTo(pRect->left, pRect->top);
- pDC->LineTo(pRect->right, pRect->top);
-
- pDC->SelectObject(pen3DLight);
- pDC->MoveTo(pRect->left+1, pRect->bottom-1);
- pDC->LineTo(pRect->left+1, pRect->top+1);
- pDC->LineTo(pRect->right, pRect->top+1);
-
-
- pDC->SelectObject(pen3DDKShadow);
- pDC->MoveTo(pRect->left, pRect->bottom-1);
- pDC->LineTo(pRect->right-1, pRect->bottom-1);
- pDC->LineTo(pRect->right-1, pRect->top-1);
-
- pDC->SelectObject(penBtnShadow);
- pDC->MoveTo(pRect->left+1, pRect->bottom-2);
- pDC->LineTo(pRect->right-2, pRect->bottom-2);
- pDC->LineTo(pRect->right-2, pRect->top);
-
- pDC->SelectObject(pOldPen);
- }
- }
- return BTNST_OK;
- }
- #undef BTS_TYPEMASK
|