gusucode.com > VC++模仿QQ界面风格源码程序 > VC++模仿QQ界面风格/IConBtn.cpp
#include "stdafx.h" #include "IConBtn.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CIConBtn CIConBtn::CIConBtn() { m_MouseOnButton = FALSE; m_hIconIn = NULL; m_hIconOut = NULL; m_hBmpIn = NULL; m_hBmpOut = NULL; m_cxIcon = 0; m_cyIcon = 0; m_cxBmp = 0; m_cyBmp = 0; m_hCursor = NULL; // Default type is "flat" button m_bIsFlat = TRUE; // By default draw border in "flat" button m_bDrawBorder = TRUE; // By default icon is aligned horizontally m_nAlign = ST_ALIGN_HORIZ; // By default show the text button m_bShowText = TRUE; // By default, for "flat" button, don't draw the focus rect m_bDrawFlatFocus = FALSE; // By default the button is not the default button m_bIsDefault = FALSE; SetDefaultInactiveBgColor(); SetDefaultInactiveFgColor(); SetDefaultActiveBgColor(); SetDefaultActiveFgColor(); // No tooltip created m_ToolTip.m_hWnd = NULL; // Do not draw as a transparent button m_bDrawTransparent = TRUE; m_pbmpOldBk = NULL; } // End of CIConBtn CIConBtn::~CIConBtn() { // Restore old bitmap (if any) if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL) { m_dcBk.SelectObject(m_pbmpOldBk); } // Destroy the icons (if any) // Note: the following two lines MUST be here! even if // BoundChecker says they are unnecessary! if (m_hIconIn != NULL) ::DestroyIcon(m_hIconIn); if (m_hIconOut != NULL) ::DestroyIcon(m_hIconOut); // Destroy the cursor (if any) if (m_hCursor != NULL) ::DestroyCursor(m_hCursor); } // End of ~CIConBtn BEGIN_MESSAGE_MAP(CIConBtn, CButton) //{{AFX_MSG_MAP(CIConBtn) ON_WM_CAPTURECHANGED() ON_WM_SETCURSOR() ON_WM_KILLFOCUS() ON_WM_MOUSEMOVE() ON_WM_CTLCOLOR_REFLECT() ON_WM_SYSCOLORCHANGE() //}}AFX_MSG_MAP END_MESSAGE_MAP() void CIConBtn::SetIcon(int nIconInId, int nIconOutId) { HICON hIconIn; HICON hIconOut; HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId), RT_GROUP_ICON); // Set icon when the mouse is IN the button hIconIn = (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0); // Set icon when the mouse is OUT the button hIconOut = (nIconOutId == NULL) ? NULL : (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0); SetIcon(hIconIn, hIconOut); /* // Note: the following two lines MUST be here! even if // BoundChecker says they are unnecessary! if (m_hIconIn != NULL) ::DestroyIcon(m_hIconIn); if (m_hIconOut != NULL) ::DestroyIcon(m_hIconOut); // Set icon when the mouse is IN the button m_hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0); // Set icon when the mouse is OUT the button m_hIconOut = (nIconOutId == NULL) ? m_hIconIn : (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0); ICONINFO ii; // Get icon dimension ZeroMemory(&ii, sizeof(ICONINFO)); ::GetIconInfo(m_hIconIn, &ii); m_cxIcon = (BYTE)(ii.xHotspot * 2); m_cyIcon = (BYTE)(ii.yHotspot * 2); ::DeleteObject(ii.hbmMask); ::DeleteObject(ii.hbmColor); RedrawWindow(); */ } // End of SetIcon void CIConBtn::SetIcon(HICON hIconIn, HICON hIconOut) { // Note: the following two lines MUST be here! even if // BoundChecker says they are unnecessary! if (m_hIconIn != NULL) ::DestroyIcon(m_hIconIn); if (m_hIconOut != NULL) ::DestroyIcon(m_hIconOut); // Set icon when the mouse is IN the button m_hIconIn = hIconIn; // Set icon when the mouse is OUT the button m_hIconOut = (hIconOut == NULL) ? m_hIconIn : hIconOut; ICONINFO ii; // Get icon dimension ZeroMemory(&ii, sizeof(ICONINFO)); ::GetIconInfo(m_hIconIn, &ii); m_cxIcon = (BYTE)(ii.xHotspot * 2); m_cyIcon = (BYTE)(ii.yHotspot * 2); ::DeleteObject(ii.hbmMask); ::DeleteObject(ii.hbmColor); RedrawWindow(); } // End of SetIcon BOOL CIConBtn::SetBtnCursor(int nCursorId) { HINSTANCE hInstResource; // Destroy any previous cursor if (m_hCursor != NULL) ::DestroyCursor(m_hCursor); m_hCursor = NULL; // If we want a cursor if (nCursorId != -1) { hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR); // Load icon resource m_hCursor = (HCURSOR)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0); // If something wrong then return FALSE if (m_hCursor == NULL) return FALSE; } return TRUE; } // End of SetBtnCursor void CIConBtn::SetFlat(BOOL bState) { m_bIsFlat = bState; Invalidate(); } // End of SetFlat BOOL CIConBtn::GetFlat() { return m_bIsFlat; } // End of GetFlat void CIConBtn::SetAlign(int nAlign) { switch (nAlign) { case ST_ALIGN_HORIZ: m_nAlign = ST_ALIGN_HORIZ; break; case ST_ALIGN_HORIZ_RIGHT: m_nAlign = ST_ALIGN_HORIZ_RIGHT; break; case ST_ALIGN_VERT: m_nAlign = ST_ALIGN_VERT; break; } Invalidate(); } // End of SetAlign int CIConBtn::GetAlign() { return m_nAlign; } // End of GetAlign void CIConBtn::DrawBorder(BOOL bEnable) { m_bDrawBorder = bEnable; } // End of DrawBorder const char* CIConBtn::GetVersionC() { return "2.6"; } // End of GetVersionC const short CIConBtn::GetVersionI() { return 26; // Divide by 10 to get actual version } // End of GetVersionI void CIConBtn::SetShowText(BOOL bShow) { m_bShowText = bShow; Invalidate(); } // End of SetShowText BOOL CIConBtn::GetShowText() { return m_bShowText; } // End of GetShowText void CIConBtn::OnMouseMove(UINT nFlags, CPoint point) { CWnd* pWnd; // Finestra attiva CWnd* pParent; // Finestra che contiene il bottone CButton::OnMouseMove(nFlags, point); // If the mouse enter the button with the left button pressed // then do nothing if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return; // If our button is not flat then do nothing if (m_bIsFlat == FALSE) return; pWnd = GetActiveWindow(); pParent = GetOwner(); if ((GetCapture() != this) && ( #ifndef ST_LIKEIE pWnd != NULL && #endif pParent != NULL)) { m_MouseOnButton = TRUE; //SetFocus(); // Thanks Ralph! SetCapture(); Invalidate(); } else { /* CRect rc; GetClientRect(&rc); if (!rc.PtInRect(point)) { */ POINT p2 = point; ClientToScreen(&p2); CWnd* wndUnderMouse = WindowFromPoint(p2); // if (wndUnderMouse != this) if (wndUnderMouse && wndUnderMouse->m_hWnd != this->m_hWnd) { // Redraw only if mouse goes out if (m_MouseOnButton == TRUE) { m_MouseOnButton = FALSE; Invalidate(); } // If user is NOT pressing left button then release capture! if (!(nFlags & MK_LBUTTON)) ReleaseCapture(); } } Invalidate(); } // End of OnMouseMove void CIConBtn::OnKillFocus(CWnd * pNewWnd) { CButton::OnKillFocus(pNewWnd); // If our button is not flat then do nothing if (m_bIsFlat == FALSE) return; if (m_MouseOnButton == TRUE) { m_MouseOnButton = FALSE; Invalidate(); } } // End of OnKillFocus void CIConBtn::OnCaptureChanged(CWnd *pWnd) { if (m_MouseOnButton == TRUE) { ReleaseCapture(); Invalidate(); } // Call base message handler CButton::OnCaptureChanged(pWnd); } // End of OnCaptureChanged void CIConBtn::DrawItem(LPDRAWITEMSTRUCT lpDIS) { #ifdef ST_USE_MEMDC CDC *pdrawDC = CDC::FromHandle(lpDIS->hDC); CMemDC memDC(pdrawDC); CDC *pDC = &memDC; #else CDC* pDC = CDC::FromHandle(lpDIS->hDC); #endif CPen *pOldPen; BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED); BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS); BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED); CRect itemRect = lpDIS->rcItem; pDC->SetBkMode(TRANSPARENT); if (m_bIsFlat == FALSE) { if (bIsFocused || (GetDefault() == TRUE)) { CBrush br(RGB(0,0,0)); pDC->FrameRect(&itemRect, &br); itemRect.DeflateRect(1, 1); } } // Prepare draw... paint button's area with background color COLORREF bgColor; if ((m_MouseOnButton == TRUE) || (bIsPressed)) bgColor = GetActiveBgColor(); else bgColor = GetInactiveBgColor(); CBrush br(bgColor); // Draw transparent? if (m_bDrawTransparent == TRUE) { PaintBk(pDC); } else { pDC->FillRect(&itemRect, &br); } // Disegno lo sfondo del bottone //CBrush br(GetSysColor(COLOR_BTNFACE)); //pDC->FillRect(&itemRect, &br); // Draw pressed button if (bIsPressed) { if (m_bIsFlat == TRUE) { if (m_bDrawBorder == TRUE) { pDC->Draw3dRect(itemRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT)); } } else { //draw norm rect around the btn CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW)); pDC->FrameRect(&itemRect, &brBtnShadow); } } else // ...else draw non pressed button { CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black if (m_bIsFlat == TRUE) { if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE) { pDC->Draw3dRect(itemRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW)); } } else { // Disegno i bordi a sinistra e in alto // White line pOldPen = pDC->SelectObject(&penBtnHiLight); pDC->MoveTo(itemRect.left, itemRect.bottom-1); pDC->LineTo(itemRect.left, itemRect.top); pDC->LineTo(itemRect.right, itemRect.top); // Light gray line pDC->SelectObject(pen3DLight); pDC->MoveTo(itemRect.left+1, itemRect.bottom-1); pDC->LineTo(itemRect.left+1, itemRect.top+1); pDC->LineTo(itemRect.right, itemRect.top+1); // Disegno i bordi a destra e in basso // Black line pDC->SelectObject(pen3DDKShadow); pDC->MoveTo(itemRect.left, itemRect.bottom-1); pDC->LineTo(itemRect.right-1, itemRect.bottom-1); pDC->LineTo(itemRect.right-1, itemRect.top-1); // Dark gray line pDC->SelectObject(penBtnShadow); pDC->MoveTo(itemRect.left+1, itemRect.bottom-2); pDC->LineTo(itemRect.right-2, itemRect.bottom-2); pDC->LineTo(itemRect.right-2, itemRect.top); // pDC->SelectObject(pOldPen); } } // Read the button's title CString sTitle; if (m_szText.IsEmpty()) GetWindowText(sTitle); else sTitle=m_szText; // If we don't want the title displayed if (m_bShowText == FALSE) sTitle.Empty(); CRect captionRect = lpDIS->rcItem; // Draw the icon if (m_hIconIn != NULL) { DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled); } else if (m_hBmpIn!=NULL) { DrawTheBmp(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled); } // Write the button title (if any) if (sTitle.IsEmpty() == FALSE) { // Disegno la caption del bottone // Se il bottone e' premuto muovo la captionRect di conseguenza if (bIsPressed) captionRect.OffsetRect(1, 1); // ONLY FOR DEBUG // Evidenzia il rettangolo in cui verra' centrata la caption //CBrush brBtnShadow(RGB(255, 0, 0)); //pDC->FrameRect(&captionRect, &brBtnShadow); #ifdef ST_USE_MEMDC // Get dialog's font CFont *pCurrentFont = GetFont(); CFont *pOldFont = pDC->SelectObject(pCurrentFont); #endif if ((m_MouseOnButton == TRUE) || (bIsPressed)) { pDC->SetTextColor(GetActiveFgColor()); pDC->SetBkColor(GetActiveBgColor()); } else { pDC->SetTextColor(GetInactiveFgColor()); pDC->SetBkColor(GetInactiveBgColor()); } // Center text CRect centerRect = captionRect; pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT); captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2); /* RFU captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2); captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2); */ pDC->SetBkMode(TRANSPARENT); pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL), TRUE, 0, (CBrush*)NULL); #ifdef ST_USE_MEMDC pDC->SelectObject(pOldFont); #endif } if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE)) { // Draw the focus rect if (bIsFocused) { CRect focusRect = itemRect; focusRect.DeflateRect(3, 3); pDC->DrawFocusRect(&focusRect); } } } // End of DrawItem void CIConBtn::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled) { CRect iconRect = rcItem; CRect btnRect; switch (m_nAlign) { case ST_ALIGN_HORIZ: if (title->IsEmpty()) { // Center the icon horizontally iconRect.left += ((iconRect.Width() - m_cxIcon)/2); } else { // L'icona deve vedersi subito dentro il focus rect iconRect.left += 3; captionRect->left += m_cxIcon + 3; } // Center the icon vertically iconRect.top += ((iconRect.Height() - m_cyIcon)/2); break; case ST_ALIGN_HORIZ_RIGHT: GetClientRect(&btnRect); if (title->IsEmpty()) { // Center the icon horizontally iconRect.left += ((iconRect.Width() - m_cxIcon)/2); } else { // L'icona deve vedersi subito dentro il focus rect captionRect->right = captionRect->Width() - m_cxIcon - 3; captionRect->left = 3; iconRect.left = btnRect.right - m_cxIcon - 3; // Center the icon vertically iconRect.top += ((iconRect.Height() - m_cyIcon)/2); } break; case ST_ALIGN_VERT: // Center the icon horizontally iconRect.left += ((iconRect.Width() - m_cxIcon)/2); if (title->IsEmpty()) { // Center the icon vertically iconRect.top += ((iconRect.Height() - m_cyIcon)/2); } else { captionRect->top += m_cyIcon; } break; } // If button is pressed then press the icon also if (IsPressed) iconRect.OffsetRect(1, 1); // Ole'! pDC->DrawState( iconRect.TopLeft(), iconRect.Size(), (m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut, (IsDisabled ? DSS_DISABLED : DSS_NORMAL), (CBrush*)NULL); } // End of DrawTheIcon void CIConBtn::PreSubclassWindow() { UINT nBS; nBS = GetButtonStyle(); // Check if this is the default button if (nBS & BS_DEFPUSHBUTTON) m_bIsDefault = TRUE; // Add BS_OWNERDRAW style SetButtonStyle(nBS | BS_OWNERDRAW); CButton::PreSubclassWindow(); } // End of PreSubclassWindow BOOL CIConBtn::PreTranslateMessage(MSG* pMsg) { InitToolTip(); m_ToolTip.RelayEvent(pMsg); return CButton::PreTranslateMessage(pMsg); } // End of PreTranslateMessage LRESULT CIConBtn::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_LBUTTONDBLCLK) { message = WM_LBUTTONDOWN; } return CButton::DefWindowProc(message, wParam, lParam); } // End of DefWindowProc void CIConBtn::SetDefaultInactiveBgColor(BOOL bRepaint) { m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE); if (bRepaint == TRUE) Invalidate(); } // End of SetDefaultInactiveBgColor void CIConBtn::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint) { m_crInactiveBg = crNew; if (bRepaint == TRUE) Invalidate(); } // End of SetInactiveBgColor const COLORREF CIConBtn::GetInactiveBgColor() { return m_crInactiveBg; } // End of GetInactiveBgColor void CIConBtn::SetDefaultInactiveFgColor(BOOL bRepaint) { m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT); if (bRepaint == TRUE) Invalidate(); } // End of SetDefaultInactiveFgColor void CIConBtn::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint) { m_crInactiveFg = crNew; if (bRepaint == TRUE) Invalidate(); } // End of SetInactiveFgColor const COLORREF CIConBtn::GetInactiveFgColor() { return m_crInactiveFg; } // End of GetInactiveFgColor void CIConBtn::SetDefaultActiveBgColor(BOOL bRepaint) { m_crActiveBg = ::GetSysColor(COLOR_BTNFACE); if (bRepaint == TRUE) Invalidate(); } // End of SetDefaultActiveBgColor void CIConBtn::SetActiveBgColor(COLORREF crNew, BOOL bRepaint) { m_crActiveBg = crNew; if (bRepaint == TRUE) Invalidate(); } // End of SetActiveBgColor const COLORREF CIConBtn::GetActiveBgColor() { return m_crActiveBg; } // End of GetActiveBgColor void CIConBtn::SetDefaultActiveFgColor(BOOL bRepaint) { m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT); if (bRepaint == TRUE) Invalidate(); } // End of SetDefaultActiveFgColor void CIConBtn::SetActiveFgColor(COLORREF crNew, BOOL bRepaint) { m_crActiveFg = crNew; if (bRepaint == TRUE) Invalidate(); } // End of SetActiveFgColor const COLORREF CIConBtn::GetActiveFgColor() { return m_crActiveFg; } // End of GetActiveFgColor void CIConBtn::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint) { m_bDrawFlatFocus = bDrawFlatFocus; // Repaint the button if (bRepaint == TRUE) Invalidate(); } // End of SetFlatFocus BOOL CIConBtn::GetFlatFocus() { return m_bDrawFlatFocus; } // End of GetFlatFocus BOOL CIConBtn::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { // If a cursor was specified then use it! if (m_hCursor != NULL) { ::SetCursor(m_hCursor); return TRUE; } return CButton::OnSetCursor(pWnd, nHitTest, message); } // End of OnSetCursor void CIConBtn::SetTooltipText(CString* spText, BOOL bActivate) { // We cannot accept NULL pointer if (spText == NULL) return; // Initialize ToolTip InitToolTip(); // If there is no tooltip defined then add it if (m_ToolTip.GetToolCount() == 0) { CRect rectBtn; GetClientRect(rectBtn); m_ToolTip.AddTool(this, (LPCTSTR)*spText, rectBtn, 1); } // Set text for tooltip m_ToolTip.UpdateTipText((LPCTSTR)*spText, this, 1); m_ToolTip.Activate(bActivate); } // End of SetTooltipText void CIConBtn::SetTooltipText(int nId, BOOL bActivate) { CString sText; // load string resource sText.LoadString(nId); // If string resource is not empty if (sText.IsEmpty() == FALSE) SetTooltipText(&sText, bActivate); } // End of SetTooltipText void CIConBtn::ActivateTooltip(BOOL bActivate) { // If there is no tooltip then do nothing if (m_ToolTip.GetToolCount() == 0) return; // Activate tooltip m_ToolTip.Activate(bActivate); } // End of EnableTooltip BOOL CIConBtn::GetDefault() { return m_bIsDefault; } // End of GetDefault void CIConBtn::DrawTransparent(BOOL bRepaint) { m_bDrawTransparent = TRUE; // Restore old bitmap (if any) if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL) { m_dcBk.SelectObject(m_pbmpOldBk); } m_bmpBk.Detach(); m_dcBk.Detach(); // Repaint the button if (bRepaint == TRUE) Invalidate(); } // End of DrawTransparent void CIConBtn::InitToolTip() { if (m_ToolTip.m_hWnd == NULL) { // Create ToolTip control m_ToolTip.Create(this); // Create inactive m_ToolTip.Activate(FALSE); } } // End of InitToolTip void CIConBtn::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); } // End of PaintBk HBRUSH CIConBtn::CtlColor(CDC* pDC, UINT nCtlColor) { return (HBRUSH)::GetStockObject(NULL_BRUSH); } // End of CtlColor void CIConBtn::OnSysColorChange() { CButton::OnSysColorChange(); m_dcBk.DeleteDC(); m_bmpBk.DeleteObject(); } // End of OnSysColorChange void CIConBtn::DrawTheBmp(CDC *pDC, CString *title, RECT *rcItem, CRect *captionRect, BOOL IsPressed, BOOL IsDisabled) { CRect bmpRect = rcItem; CRect btnRect; switch (m_nAlign) { case ST_ALIGN_HORIZ: if (title->IsEmpty()) { // Center the bmp horizontally bmpRect.left += ((bmpRect.Width() - m_cxBmp)/2); } else { // L'bmpa deve vedersi subito dentro il focus rect bmpRect.left += 3; captionRect->left += m_cxBmp + 3; } // Center the bmp vertically bmpRect.top += ((bmpRect.Height() - m_cyBmp)/2); break; case ST_ALIGN_HORIZ_RIGHT: GetClientRect(&btnRect); if (title->IsEmpty()) { // Center the bmp horizontally bmpRect.left += ((bmpRect.Width() - m_cxBmp)/2); } else { // L'bmpa deve vedersi subito dentro il focus rect captionRect->right = captionRect->Width() - m_cxBmp - 3; captionRect->left = 3; bmpRect.left = btnRect.right - m_cxBmp - 3; // Center the bmp vertically bmpRect.top += ((bmpRect.Height() - m_cyBmp)/2); } break; case ST_ALIGN_VERT: // Center the bmp horizontally bmpRect.left += ((bmpRect.Width() - m_cxBmp)/2); if (title->IsEmpty()) { // Center the bmp vertically bmpRect.top += ((bmpRect.Height() - m_cyBmp)/2); } else { captionRect->top += m_cyBmp; } break; } // If button is pressed then press the bmp also if (IsPressed) bmpRect.OffsetRect(1, 1); // Ole'! pDC->DrawState( bmpRect.TopLeft(), bmpRect.Size(), (m_MouseOnButton == TRUE || IsPressed) ? m_hBmpIn : m_hBmpOut, (IsDisabled ? DSS_DISABLED : DSS_NORMAL), NULL); } #undef ST_USE_MEMDC #undef ST_LIKEIE void CIConBtn::SetBitmap(int idIn, int idOut) { if (idIn==0) {//Add and Modify by Coolboy 2000-1-10 m_hBmpIn=NULL; m_hBmpOut=NULL; return; } m_hBmpIn = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(idIn), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); idOut=(idOut==0)?idIn:idOut; m_hBmpOut = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(idOut), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); // Get the bitmap parameters, because we will need width and height BITMAP bmpval; ::GetObject(m_hBmpIn, sizeof(BITMAP), &bmpval); m_cxBmp=(BYTE)bmpval.bmWidth; m_cyBmp=(BYTE)bmpval.bmHeight; } void CIConBtn::SetText(LPCTSTR szText) { m_szText=szText; }