⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cebtnst.cpp

📁 EVC6下非常好的按钮贴图
💻 CPP
📖 第 1 页 / 共 3 页
字号:

    // Prepare draw... paint button background
    OnDrawBackground(pDC, &itemRect);

    // Paint button border
    if (!m_bIsFlat || (m_bIsFlat && m_bDrawBorder))
        OnDrawBorder(pDC, &itemRect);

    // Read the button's title
    TCHAR szTitle[256];
    GetWindowText(szTitle, sizeof(szTitle) / sizeof(TCHAR));

    CRect captionRect = lpDIS->rcItem;

    // Draw the icon
    if (m_csIcons[0].hIcon)
    {
        DrawTheIcon(pDC, _tcslen(szTitle), &lpDIS->rcItem, &captionRect,
            m_bIsPressed, m_bIsDisabled);
    } // if

    // Write the button title (if any)
    if (_tcslen(szTitle))
    {
        // Draw the button's title
        // If button is pressed then "press" title also
        if (m_bIsPressed && m_bIsCheckBox == FALSE)
            captionRect.OffsetRect(1, 1);

        // ONLY FOR DEBUG 
        //CBrush brBtnShadow(RGB(255, 0, 0));
        //pDC->FrameRect(&captionRect, &brBtnShadow);

        // Center text
        CRect centerRect = captionRect;
        pDC->DrawText(szTitle,  - 1, captionRect, DT_WORDBREAK | DT_CENTER |
            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(), szTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL), 
        TRUE, 0, (CBrush*)NULL);
         */
        if (m_bIsDisabled)
        {
            captionRect.OffsetRect(1, 1);
            pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
            pDC->DrawText(szTitle,  - 1, captionRect, DT_WORDBREAK | DT_CENTER);
            captionRect.OffsetRect( - 1,  - 1);
            pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
            pDC->DrawText(szTitle,  - 1, captionRect, DT_WORDBREAK | DT_CENTER);
        } // if
        else
        {
            if (m_bMouseOnButton || m_bIsPressed)
            {
                pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
                pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
            } // if
            else
            {
                pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
                pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
            } // else
            pDC->DrawText(szTitle,  - 1, captionRect, DT_WORDBREAK | DT_CENTER);
        } // if
    } // if

    if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
    {
        // Draw the focus rect
        if (m_bIsFocused)
        {
            CRect focusRect = itemRect;
            focusRect.DeflateRect(3, 3);
            pDC->DrawFocusRect(&focusRect);
        } // if
    } // if
} // End of DrawItem

//
// Parameters:
//		[IN]	bHasTitle
//				TRUE if the button has a text
//		[IN]	rpItem
//				A pointer to a RECT structure indicating the allowed paint area
//		[IN/OUT]rpTitle
//				A pointer to a CRect object indicating the paint area reserved for the
//				text. This structure will be modified if necessary.
//		[IN]	bIsPressed
//				TRUE if the button is currently pressed
//		[IN]	dwWidth
//				Width of the image (icon or bitmap)
//		[IN]	dwHeight
//				Height of the image (icon or bitmap)
//		[OUT]	rpImage
//				A pointer to a CRect object that will receive the area available to the image
//
void CCeButtonST::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)
            {
                // Center image horizontally
                rpImage->left += ((rpImage->Width() - dwWidth) / 2);
            }
            else
            {
                // Image must be placed just inside the focus rect
                rpImage->left += 3;
                rpTitle->left += dwWidth + 3;
            }
            // Center image vertically
            rpImage->top += ((rpImage->Height() - dwHeight) / 2);
            break;

        case ST_ALIGN_HORIZ_RIGHT:
            GetClientRect(&rBtn);
            if (bHasTitle == FALSE)
            {
                // Center image horizontally
                rpImage->left += ((rpImage->Width() - dwWidth) / 2);
            }
            else
            {
                // Image must be placed just inside the focus rect
                rpTitle->right = rpTitle->Width() - dwWidth - 3;
                rpTitle->left = 3;
                rpImage->left = rBtn.right - dwWidth - 3;
                // Center image vertically
                rpImage->top += ((rpImage->Height() - dwHeight) / 2);
            }
            break;

        case ST_ALIGN_VERT:
            // Center image horizontally
            rpImage->left += ((rpImage->Width() - dwWidth) / 2);
            if (bHasTitle == FALSE)
            {
                // Center image vertically
                rpImage->top += ((rpImage->Height() - dwHeight) / 2);
            }
            else
            {
                rpImage->top = 3;
                rpTitle->top += dwHeight;
            }
            break;
    }

    // If button is pressed then press image also
    if (bIsPressed && m_bIsCheckBox == FALSE)
        rpImage->OffsetRect(1, 1);
} // End of PrepareImageRect

void CCeButtonST::DrawTheIcon(CDC *pDC, BOOL bHasTitle, RECT *rpItem, CRect
    *rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
{
    BYTE byIndex = 0;

    // Select the icon to use
    if (bIsDisabled)
        byIndex = (m_csIcons[2].hIcon == NULL ? 0 : 2);
    else
    {
        if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed ||
            m_bMouseOnButton)))
            byIndex = 0;
        else
            byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
    } // else

    CRect rImage;
    PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed,
        m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);

    // Ole'!
    pDC->DrawIcon(rImage.TopLeft(), m_csIcons[byIndex].hIcon);
} // End of DrawTheIcon

// This function assigns icons to the button.
// Any previous icon will be removed.
//
// Parameters:
//		[IN]	nIconIn
//				ID number of the icon resource to show when the mouse is over the button.
//				Pass NULL to remove any icon from the button.
//		[IN]	sizeIn
//				Size of the icon.
//		[IN]	nIconOut
//				ID number of the icon resource to show when the mouse is outside the button.
//				Can be NULL.
//		[IN]	sizeOut
//				Size of the icon.
//		[IN]	nIconDis
//				ID number of the icon resource to show when the button is disabled.
//				Can be NULL.
//		[IN]	sizeDis
//				Size of the icon.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCeButtonST::SetIcon(int nIconIn, CSize sizeIn, int nIconOut, CSize
    sizeOut, int nIconDis, CSize sizeDis)
{
    HICON hIconIn = NULL;
    HICON hIconOut = NULL;
    HICON hIconDis = NULL;
    HINSTANCE hInstResource = NULL;

    // Find correct resource handle
    hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn),
        RT_GROUP_ICON);

    // Set icon when the mouse is IN the button
    hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn),
        IMAGE_ICON, 0, 0, 0);
    // Set icon when the mouse is OUT the button
    if (nIconOut)
        hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut),
            IMAGE_ICON, 0, 0, 0);
    // Set icon when the mouse is DISABLED
    if (nIconDis)
        hIconDis = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconDis),
            IMAGE_ICON, 0, 0, 0);

    return SetIcon(hIconIn, sizeIn, hIconOut, sizeOut, hIconDis, sizeDis);
} // End of SetIcon

// This function assigns icons to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
//		[IN]	hIconIn
//				Handle fo the icon to show when the mouse is over the button.
//				Pass NULL to remove any icon from the button.
//		[IN]	sizeIn
//				Size of the icon.
//		[IN]	hIconOut
//				Handle to the icon to show when the mouse is outside the button.
//				Can be NULL.
//		[IN]	sizeOut
//				Size of the icon.
//		[IN]	hIconDis
//				ID number of the icon resource to show when the button is disabled.
//				Can be NULL.
//		[IN]	sizeDis
//				Size of the icon.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCeButtonST::SetIcon(HICON hIconIn, CSize sizeIn, HICON hIconOut, CSize
    sizeOut, HICON hIconDis, CSize sizeDis)
{
    // Free any loaded resource
    FreeResources();

    if (hIconIn)
    {
        // Icon when mouse over button?
        m_csIcons[0].hIcon = hIconIn;
        // Get icon dimension
        m_csIcons[0].dwWidth = (DWORD)sizeIn.cx;
        m_csIcons[0].dwHeight = (DWORD)sizeIn.cy;

        // Icon when mouse outside button?
        if (hIconOut)
        {
            m_csIcons[1].hIcon = hIconOut;
            // Get icon dimension
            m_csIcons[1].dwWidth = (DWORD)sizeOut.cx;
            m_csIcons[1].dwHeight = (DWORD)sizeOut.cy;
        } // if

        // Icon when button disabled?
        if (hIconDis)
        {
            m_csIcons[2].hIcon = hIconDis;
            // Get icon dimension
            m_csIcons[2].dwWidth = (DWORD)sizeDis.cx;
            m_csIcons[2].dwHeight = (DWORD)sizeDis.cy;
        } // if
    } // if

    Invalidate();

    return BTNST_OK;
} // End of SetIcon

// This functions sets the button to have a standard or flat style.
//
// Parameters:
//		[IN]	bFlat
//				If TRUE the button will have a flat style, else
//				will have a standard style.
//		[IN]	bRepaint
//				If TRUE the control will be repainted.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCeButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
{
    m_bIsFlat = bFlat;
    if (bRepaint)
        Invalidate();

    return BTNST_OK;
} // End of SetFlat

// This function sets the alignment type between icon/bitmap and text.
//
// Parameters:
//		[IN]	byAlign
//				Alignment type. Can be one of the following values:
//				ST_ALIGN_HORIZ			Icon/bitmap on the left, text on the right
//				ST_ALIGN_VERT			Icon/bitmap on the top, text on the bottom
//				ST_ALIGN_HORIZ_RIGHT	Icon/bitmap on the right, text on the left
//				By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.
//		[IN]	bRepaint
//				If TRUE the control will be repainted.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//		BTNST_INVALIDALIGN
//			Alignment type not supported.
//
DWORD CCeButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
{
    switch (byAlign)
    {
        case ST_ALIGN_HORIZ:
        case ST_ALIGN_HORIZ_RIGHT:
        case ST_ALIGN_VERT:
            m_byAlign = byAlign;
            if (bRepaint)
                Invalidate();
            return BTNST_OK;
            break;
    } // switch

    return BTNST_INVALIDALIGN;
} // End of SetAlign

// This function sets the state of the checkbox.
// If the button is not a checkbox, this function has no meaning.
//
// Parameters:
//		[IN]	nCheck
//				1 to check the checkbox.
//				0 to un-check the checkbox.
//		[IN]	bRepaint
//				If TRUE the control will be repainted.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCeButtonST::SetCheck(int nCheck, BOOL bRepaint)
{
    if (m_bIsCheckBox)
    {
        if (nCheck == 0)
            m_nCheck = 0;
        else
            m_nCheck = 1;

        if (bRepaint)
            Invalidate();
    } // if

    return BTNST_OK;
} // End of SetCheck

// This function returns the current state of the checkbox.
// If the button is not a checkbox, this function has no meaning.
//
// Return value:
//		The current state of the checkbox.
//			1 if checked.
//			0 if not checked or the button is not a checkbox.
//
int CCeButtonST::GetCheck()
{
    return m_nCheck;
} // End of GetCheck

// This function sets all colors to a default value.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -