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

📄 cchecksk.cpp

📁 这是一个比较好的实现较理想的界面的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        pDC->DrawFocusRect(&rectItem);
    rectItem.DeflateRect(1,1);
    
    //  .......................................................................
    //  DRAW THE ON/OFF LED
    //  .......................................................................
    //  calculate LED's and text's rectangle
    CRect rectLed;

    if (m_hIconOn.hIcon && m_hIconOff.hIcon)
        m_nLedSize = m_hIconOn.dwWidth;

    if (m_nStyle & 0x20)
    {
        //  for text left of led
        rectLed.left   = rectItem.right - m_nLedSize;
        rectLed.right  = rectLed.left + m_nLedSize;
        rectItem.right = rectLed.left - 3; // keep a gap of 3 px between text and LED
    }
    else
    {
        //  for text right of led
        rectLed.left   = rectItem.left;
        rectLed.right  = rectLed.left + m_nLedSize;
        rectItem.left  = rectLed.right + 3; // keep a gap of 3 px between text and LED
    }

    rectLed.top    = (rectItem.Height() - m_nLedSize)/2 + 2;
    rectLed.bottom = rectLed.top + m_nLedSize;

    //  If icon is given then use the icon
    if (m_hIconOn.hIcon && m_hIconOff.hIcon)
    {
        rectLed.top    = 0;
        rectLed.bottom = rectLed.top + m_hIconOn.dwWidth;

        //  if preseed then move the rectangle to give a button is pressed down look
        if (m_bIsPressed)
            rectLed.OffsetRect(1, 1);

        structIcon *pSIcon;
        //  Select the icon to use based on whether mouse is over the button or not
        if (m_bCheck && m_hIconOff.hIcon != NULL)
            pSIcon = &m_hIconOn;
        else
            pSIcon = &m_hIconOff;
   
        //  draw the icon on the button
        pDC->DrawState(	rectLed.TopLeft(), rectLed.Size(),  pSIcon->hIcon, 
            (m_bIsDisabled ? DSS_DISABLED : DSS_NORMAL), (CBrush*)NULL);
    }
    else
    {
        //  if preseed then move the rectangle to give a button is pressed down look
        if (m_bIsPressed)
            rectLed.OffsetRect(1, 1);

        //  create brush and pen
        CBrush brushLed;
        CPen penLed(PS_SOLID,1,RGB(0,0,0));
        if (m_bIsDisabled)
            brushLed.CreateSolidBrush (::GetSysColor(COLOR_3DSHADOW));
        else
        {
            if (m_bCheck)
                brushLed.CreateSolidBrush (m_colLedOn);
            else
                brushLed.CreateSolidBrush (m_colLedOff);
        }

        pDC->SelectObject(&brushLed);
        pDC->SelectObject(&penLed);
    
        //  draw the led
        pDC->Ellipse(&rectLed);
        CPen penLeadAntiAliase (PS_SOLID, 1, m_colLedOff);
        pDC->SelectObject(&penLeadAntiAliase);
        rectLed.DeflateRect(1,1);
        pDC->Ellipse(&rectLed);

        pDC->SelectObject(pOldPen);
        pDC->SelectObject(pOldBrush);
        brushLed.DeleteObject();
        penLed.DeleteObject();
    }

    //  .......................................................................
    //  GET & DISPLAY THE BUTTON TEXT
    //  .......................................................................
    //  get the text on the check box
    CString sTitle;
    GetWindowText(sTitle);
    
    if ( !sTitle.IsEmpty())
    {
        //  ...................................................................
        //  ALIGN TEXT HORIZONTALLY BASED ON THE STYLE 
        //  ...................................................................
        CRect centerRect = rectItem;
                
        UINT uFormat;

        //  center align
        if ((m_nStyle & 0x00000F00) == 0x300)
        {
            uFormat = DT_WORDBREAK | DT_CENTER | DT_VCENTER;
            pDC->DrawText(sTitle, -1, &rectItem, uFormat | DT_CALCRECT);
            rectItem.OffsetRect((centerRect.Width() - rectItem.Width())/2, 
                (centerRect.Height() - rectItem.Height())/2);
        }
        //  right align
        else if ((m_nStyle & 0x00000F00) == 0x200)
        {
            uFormat = DT_WORDBREAK | DT_RIGHT | DT_VCENTER;
            pDC->DrawText(sTitle, -1, &rectItem, uFormat | DT_CALCRECT);
            rectItem.OffsetRect(centerRect.Width() - rectItem.Width(), 
                (centerRect.Height() - rectItem.Height())/2);
        }
        //  by default left-align
        else // if ((m_nStyle & 0x00000F00) == 0x100)
        {
            uFormat = DT_WORDBREAK | DT_LEFT | DT_VCENTER;
            pDC->DrawText(sTitle, -1, &rectItem, uFormat | DT_CALCRECT);
            rectItem.OffsetRect(0, (centerRect.Height() - rectItem.Height())/2);
        }

        //  if preseed then move the rectangle to give a button is pressed down look
        if (m_bIsPressed)
            rectItem.OffsetRect(1, 1);

        pDC->SetBkMode(TRANSPARENT);

        //  ...................................................................
        //  Draw the text
        //  ...................................................................
        if (m_bIsDisabled)
        {
            //  for disabled button draw etched text
            rectItem.OffsetRect(1, 1);
            pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
            pDC->DrawText(sTitle, -1, &rectItem, uFormat);
            rectItem.OffsetRect(-1, -1);
            pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
            pDC->DrawText(sTitle, -1, &rectItem, uFormat);
        }
        else
        {
            //  for non-disabled button get the color for background and foreground
            if (m_bMouseOver || m_bIsPressed)
            {
                pDC->SetTextColor(::GetSysColor(COLOR_BTNTEXT));
                pDC->SetBkColor(::GetSysColor(COLOR_BTNFACE));
            }
            else 
            {
                pDC->SetTextColor(::GetSysColor(COLOR_BTNTEXT));
                pDC->SetBkColor(::GetSysColor(COLOR_BTNFACE));
            }
            //  draw the text
            pDC->DrawText(sTitle, -1, &rectItem, uFormat);
        }
    }

    //  .......................................................................
    //  Release resources
    //  .......................................................................
    pDC->SelectObject(pOldPen);
    pDC->SelectObject(pOldBrush);

    penLed.DeleteObject();
    brushForeGnd.DeleteObject();
    pen.DeleteObject();
    brushLed.DeleteObject();
}

//  ...........................................................................
//  WM_MOUSEMOVE message handler
//  ...........................................................................
void CCheckSK::OnMouseMove(UINT nFlags, CPoint point) 
{
    CWnd*               wndUnderMouse = NULL;
    CWnd*               wndActive = this;
    TRACKMOUSEEVENT     csTME;
    
    ClientToScreen(&point);
    wndUnderMouse = WindowFromPoint(point);
    
    // If the mouse enter the button with the left button pressed then do nothing
    if (nFlags & MK_LBUTTON && m_bMouseOver == FALSE) return;
    
    if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd)
    {
        if (!m_bMouseOver)
        {
            m_bMouseOver = true;
            
            Invalidate();
            
            csTME.cbSize = sizeof(csTME);
            csTME.dwFlags = TME_LEAVE;
            csTME.hwndTrack = m_hWnd;
            ::_TrackMouseEvent(&csTME);
        }
    } 
    else 
        CancelHover();

	CButton::OnMouseMove(nFlags, point);
}

void
CCheckSK::CancelHover()
{
    if (m_bMouseOver)
        m_bMouseOver = FALSE;

    Invalidate();
}


void
CCheckSK::OnClicked() 
{
	m_bCheck = !m_bCheck;

    Invalidate();
}

BOOL
CCheckSK::SetLedSize(int nSize)
{
    if (nSize < 0)
        return FALSE;
    else
    {
        m_nLedSize = nSize;
        Invalidate();
        return TRUE;
    }
}

BOOL
CCheckSK::SetLedColor(COLORREF colLedOn, COLORREF colLedOff)
{
    m_colLedOn  = colLedOn;
    m_colLedOff = colLedOff;
    Invalidate();
    return TRUE;
}

void
CCheckSK::SetToolTip(LPCTSTR lpszText)
{
    if (!lpszText)
        return;
    
    TOOLINFO ti;
    
    ti.cbSize   = sizeof(TOOLINFO);
    ti.lpszText = (LPTSTR)lpszText;
    ti.hinst    = AfxGetInstanceHandle();
    ti.hwnd     = this->GetParent()->GetSafeHwnd();
    ti.uFlags   = TTF_SUBCLASS | TTF_IDISHWND;
    ti.uId      = (UINT) this->GetSafeHwnd();
    
    m_tooltip.SendMessage(TTM_ADDTOOL, 0, (LPARAM) &ti);
}

void CCheckSK::FreeResources()
{
    if (m_hIconOn.hIcon)
        ::DeleteObject(m_hIconOn.hIcon);
    if (m_hIconOn.hIcon)
        ::DeleteObject(m_hIconOn.hIcon);

    m_hIconOn.hIcon  = NULL;
    m_hIconOn.hIcon = NULL;

}

⌨️ 快捷键说明

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