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

📄 atlflatbutton.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
📖 第 1 页 / 共 2 页
字号:
  {
    // If there is no tooltip then do nothing
    if (m_ToolTip.GetToolCount() == 0)
      return;
    
    // Activate tooltip
    m_ToolTip.Activate(bActivate);
  } // End of EnableTooltip
  
  
  BOOL GetDefault()
  {
    return m_bIsDefault;
  } // End of GetDefault
  
  
  void DrawTransparent(BOOL bRepaint)
  {
    m_bDrawTransparent = TRUE;
    
    // Restore old bitmap (if any)
    if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
    {
      m_dcBk.SelectBitmap(m_pbmpOldBk);
    }
    
    m_bmpBk.Detach();
    m_dcBk.Detach();
    
    // Repaint the button
    if (bRepaint) 
      Invalidate();
  } // End of DrawTransparent
  
  
  void InitToolTip()
  {
    if (m_ToolTip.m_hWnd == NULL)
    {
      // Create ToolTip control
      m_ToolTip.Create(m_hWnd);
      // Create inactive
      m_ToolTip.Activate(FALSE);
    }
  } // End of InitToolTip
  
protected:
  LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    WORD x, y;
    x = LOWORD(lParam);
    y = HIWORD(lParam);
    CPoint point(x, y);
    UINT nFlags = (UINT)wParam;
    
    CWindow* pWnd = NULL;  
    CWindow* pParent = NULL; 
    
    // If the mouse enter the button with the left button pressed
    // then do nothing
    if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) 
      return 0;
    
    // If our button is not flat then do nothing
    if (m_bIsFlat == FALSE) 
      return 0;
    
    CWindow Wnd(GetActiveWindow());
    CWindow ParentWnd(GetParent());
    pWnd = &Wnd;
    pParent = &ParentWnd;
    
    if ((CWindow(GetCapture()).m_hWnd != *this) && 
      (
#ifndef ST_LIKEIE
      pWnd != NULL && 
#endif
      pParent != NULL)) 
    {
      m_MouseOnButton = TRUE;
      SetCapture();
      Invalidate();
    }
    else
    {
      POINT p2 = point;
      ClientToScreen(&p2);
      CWindow tempwndUnderMouse(WindowFromPoint(p2));
      CWindow* wndUnderMouse = &tempwndUnderMouse;
      if (wndUnderMouse && wndUnderMouse->m_hWnd != this->m_hWnd)
      {
        // Redraw only if mouse goes out
        if (m_MouseOnButton)
        {
          m_MouseOnButton = FALSE;
          Invalidate();
        }
        // If user is NOT pressing left button then release capture!
        if (!(nFlags & MK_LBUTTON))
          ReleaseCapture();
      }
    }
    
    return 0;
  }
  
  LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    // If our button is not flat then do nothing
    if (!m_bIsFlat) 
      return 0;
    
    if (m_MouseOnButton)
    {
      m_MouseOnButton = FALSE;
      Invalidate();
    }
    
    return 0;
  }
  
  LRESULT OnCaptureChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    if (m_MouseOnButton)
    {
      ReleaseCapture();
      Invalidate();
    }
    return 0;
  }
  
  LRESULT OnDrawItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    DRAWITEMSTRUCT* lpDIS = (DRAWITEMSTRUCT*)lParam;
    
    CBitmap bmp;
    CRect rc;
    GetClientRect(rc);
    HBITMAP hOldBitmap;
    
    // Set up for double buffering...	
    CDC* pDCMem;
    pDCMem = new CDC;
    pDCMem->CreateCompatibleDC(lpDIS->hDC);
    bmp.CreateCompatibleBitmap(lpDIS->hDC, rc.Width(), rc.Height());
    hOldBitmap = pDCMem->SelectBitmap(bmp);
    CDC *pDC = pDCMem;
    
    HPEN hOldPen;
    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 &&(bIsFocused || GetDefault()))
    {
      pDC->FrameRect(&itemRect, CBrush().CreateSolidBrush(RGB(0, 0, 0)));
      itemRect.DeflateRect(1, 1);
    }
    
    // Prepare draw... paint button's area with background color
    COLORREF bgColor;
    if (m_MouseOnButton || bIsPressed)
      bgColor = GetActiveBgColor();
    else
      bgColor = GetInactiveBgColor();
    
    // Draw transparent?
    if (m_bDrawTransparent)
      PaintBk(pDC);
    else
      pDC->FillRect(&itemRect, CBrush().CreateSolidBrush(bgColor));
    
    // Draw pressed button
    if (bIsPressed)
    {
      if (m_bIsFlat)
      {
        if (m_bDrawBorder)
          pDC->Draw3dRect(itemRect, m_crShadow, m_crHighLight);
      }
      else    
        pDC->FrameRect(&itemRect, CBrush().CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW)));
    }
    else // ...else draw non pressed button
    {
      CPen penBtnHiLight;
      penBtnHiLight.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
      CPen pen3DLight;
      pen3DLight.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
      CPen penBtnShadow;
      penBtnShadow.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
      CPen pen3DDKShadow;
      pen3DDKShadow.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
      
      if (m_bIsFlat)
      {
        if (m_MouseOnButton && m_bDrawBorder)
          pDC->Draw3dRect(itemRect, m_crHighLight, m_crShadow);
      }
      else
      {
        // White line
        hOldPen = pDC->SelectPen(penBtnHiLight);
        pDC->MoveTo(itemRect.left, itemRect.bottom - 1);
        pDC->LineTo(itemRect.left, itemRect.top);
        pDC->LineTo(itemRect.right, itemRect.top);
        // Light gray line
        pDC->SelectPen(pen3DLight);
        pDC->MoveTo(itemRect.left + 1, itemRect.bottom - 1);
        pDC->LineTo(itemRect.left + 1, itemRect.top + 1);
        pDC->LineTo(itemRect.right, itemRect.top + 1);
        // Black line
        pDC->SelectPen(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->SelectPen(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->SelectPen(hOldPen);
      }
    }
    
    // Read the button's title
    TCHAR cValue[2048];
    GetWindowText(cValue, sizeof(cValue));
    CString sTitle(cValue);
    
    // If we don't want the title displayed
    if (!m_bShowText) 
      sTitle.Empty();
    
    CRect captionRect = lpDIS->rcItem;
    
    // Draw the icon
    if (m_hIconIn != NULL)
      DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
    
    // Write the button title (if any)
    if (!sTitle.IsEmpty())
    {
      if (bIsPressed)
        captionRect.OffsetRect(1, 1);
      
      // Get dialog's font
      HFONT pOldFont = pDC->SelectFont(GetFont());
      pDC->SetTextColor(bIsDisabled ? ::GetSysColor(COLOR_GRAYTEXT):m_crTextColor);
      
      if (m_MouseOnButton || bIsPressed) 
        pDC->SetBkColor(GetActiveBgColor());
      else 
        pDC->SetBkColor(GetInactiveBgColor());
      
      // Center text
      CRect centerRect = captionRect;
      pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE | DT_CALCRECT);
      
      int nWidth = captionRect.Width();
      if (m_bAddArrow)
        nWidth += 5+3;

      captionRect.OffsetRect((centerRect.Width() - nWidth)/2, (centerRect.Height() - captionRect.Height())/2);
      
      pDC->SetBkMode(TRANSPARENT);
      pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL), TRUE, 0, NULL);
      pDC->SelectFont(pOldFont);
    }
    
    if (!m_bIsFlat ||(m_bIsFlat && m_bDrawFlatFocus))
    {
      // Draw the focus rect
      if (bIsFocused)
      {
        CRect focusRect = itemRect;
        focusRect.DeflateRect(3, 3);
        pDC->DrawFocusRect(&focusRect);
      }
    }
    
    // Set up pen to use for drawing the triangle
    CPen pen;
    pen.CreatePen(PS_SOLID, 1, m_crTextColor);
    pDC->SelectPen(pen);
    
    // Create the triangle at the bottom right
    if (m_bAddArrow)
    {
      int nxBorder = 5;
      int nyBorder = 5;
      int nArrowWidth = 5;
      if (nArrowWidth % 2 == 0)					// It looks better if odd
        nArrowWidth--;
      int nArrowHeight = 3;
      CPoint pointStart(rc.right - nArrowWidth - nxBorder, rc.bottom - nArrowHeight - nyBorder);
      
      pDC->BeginPath();
      
      // draw the triangle.
      pDC->MoveTo(pointStart);
      // Move Horizontal
      pDC->LineTo(rc.right - nxBorder, rc.bottom - nArrowHeight - nyBorder);
      // Move Diagonally
      pDC->LineTo(rc.right - nxBorder - ((nArrowWidth + 1)/2), rc.bottom - nyBorder);
      // back to original point
      pDC->LineTo(rc.right - nxBorder - nArrowWidth - 1, rc.bottom - nArrowHeight - nyBorder);
      
      pDC->EndPath();
      
      CBrush solidBrush;
      solidBrush.CreateSolidBrush(m_crTextColor);
      pDC->SelectBrush(solidBrush);
      pDC->StrokeAndFillPath();
    }
    
    // This avoid flickering
    CDC(lpDIS->hDC).BitBlt(0, 0, rc.Width(), rc.Height(), pDC->m_hDC, 0, 0, SRCCOPY);
    pDCMem->SelectBitmap(hOldBitmap);
    if (pDCMem)
      delete pDCMem;
    
    return 0;
  }
  
  void 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), 
      NULL);
  } // End of DrawTheIcon
  
  
  LRESULT OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    // If a cursor was specified then use it!
    if (m_hCursor != NULL)
    {
      ::SetCursor(m_hCursor);
      bHandled = TRUE;
    }
    
    return 0;
  }
  
  void PaintBk(CDC * pDC)
  {
    CClientDC clDC(GetParent());
    CRect rect;
    CRect rect1;
    
    GetClientRect(rect);
    
    GetWindowRect(rect1);
    CWindow(GetParent()).ScreenToClient(rect1);
    
    if (m_dcBk.m_hDC == NULL)
    {
      m_dcBk.CreateCompatibleDC(clDC.m_hDC);
      m_bmpBk.CreateCompatibleBitmap(clDC.m_hDC, rect.Width(), rect.Height());
      m_pbmpOldBk = m_dcBk.SelectBitmap(m_bmpBk);
      m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), clDC.m_hDC, rect1.left, rect1.top, SRCCOPY);
    }
    
    pDC->BitBlt(0, 0, rect.Width(), rect.Height(), m_dcBk, 0, 0, SRCCOPY);
  } // End of PaintBk
  
  LRESULT OnSysColorChange(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
    m_dcBk.DeleteDC();
    m_bmpBk.DeleteObject();	
    
    return 0;
  }
	LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// avoid flickering
		return 1;
	}
  
  int m_nAlign;
  BOOL m_bShowText;
  BOOL m_bDrawBorder;
  BOOL m_bIsFlat;
  BOOL m_MouseOnButton;
  BOOL m_bDrawFlatFocus;
  
  HCURSOR m_hCursor;
  CToolTipCtrl m_ToolTip;
  
  HICON m_hIconIn;
  HICON m_hIconOut;
  BYTE m_cyIcon;
  BYTE m_cxIcon;
  
  CDC m_dcBk;
  CBitmap m_bmpBk;
  HBITMAP m_pbmpOldBk;
  BOOL m_bDrawTransparent;
  
  BOOL m_bIsDefault;
  
  COLORREF  m_crInactiveBg;
  COLORREF  m_crInactiveFg;
  COLORREF  m_crActiveBg;
  COLORREF  m_crActiveFg;
  COLORREF  m_crTextColor;
  COLORREF  m_crHighLight;
  COLORREF  m_crShadow;
  
  BOOL m_bAddArrow;
};


#endif // ATL_FLAT_BUTTON_H__

⌨️ 快捷键说明

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