cmenubar.cls

来自「非常漂亮的VB控件」· CLS 代码 · 共 1,299 行 · 第 1/4 页

CLS
1,299 行
字号
      End If
      
      CopyMemory ByVal lParam, tMIS, LenB(tMIS)
      
   Else
      MeasureItem = CallOldWindowProc(m_hWnd, WM_MEASUREITEM, wParam, lParam)
   End If
End Function
Public Function DrawItem(ByVal wParam As Long, ByVal lParam As Long) As Long
Dim tDIS As DRAWITEMSTRUCT
Dim hBr As Long, ChkColor As Long
Dim tR As RECT, tTR As RECT, tWR As RECT
Dim lhDC As Long, mPDC As Long
Dim hFntOld As Long
Dim tMII As MENUITEMINFO
Dim bRadioCheck As Boolean, bDisabled As Boolean, bChecked As Boolean, bHighlighted As Boolean
Dim bUnchecked As Boolean, bUnhilite As Boolean, bEnabled As Boolean
Dim lID As Long
Dim hFntS As Long, hFntSOld As Long

   CopyMemory tDIS, ByVal lParam, LenB(tDIS)
   If tDIS.CtlType = ODT_MENU Then
      ' Todo
      ' tDIS.hWndItem is the menu containing the item, tDIS.itemID is the wID
      m_cMemDC.Width = tDIS.rcItem.right - tDIS.rcItem.left + 1
      m_cMemDC.Height = tDIS.rcItem.bottom - tDIS.rcItem.top + 1
      lhDC = m_cMemDC.hdc
      hFntOld = SelectObject(lhDC, hFont)
      
      LSet tR = tDIS.rcItem
      OffsetRect tR, -tR.left, -tR.top
      ' Fill background:
      
      tTR.right = m_cMemDC.Width
      tTR.bottom = m_cMemDC.Height
      If Mstyle = 0 Then
      'hBr = CreateSolidBrush(TranslateColor(m_oMenuBackgroundColor))  '这个是用来设置菜单的背景颜色
      hBr = CreatePatternBrush(LoadResPicture(100, 0)) '这个是用来设置菜单的背景图案
      Else
      hBr = CreatePatternBrush(LoadResPicture(Mstyle, 0)) '这个是用来设置菜单的背景图案
      End If
      FillRect lhDC, tTR, hBr
      DeleteObject hBr
      
      SetBkMode lhDC, TRANSPARENT
      
      ' Draw the text:
      tMII.cbSize = LenB(tMII)
      tMII.fMask = MIIM_TYPE Or MIIM_STATE Or MIIM_DATA
      GetMenuItemInfo tDIS.hwndItem, tDIS.itemID, False, tMII
      
      If (tMII.fType And MFT_SEPARATOR) = MFT_SEPARATOR Then
         ' Separator:
         LSet tWR = tR
         tWR.top = (tWR.bottom - tWR.top - 2) \ 2 + tWR.top
         tWR.bottom = tWR.top + 2
         InflateRect tWR, -2, 0
         
         '分割线的样式
         If Mstyle = 1 Or Mstyle = 2 Or Mstyle = 19 Then
            tWR.left = 10
            DrawEdge lhDC, tWR, &H5, BF_TOP Or BF_BOTTOM '默认
         ElseIf Mstyle = 7 Or Mstyle = 15 Or Mstyle = 14 Or Mstyle = 10 Or Mstyle = 11 Or Mstyle = 18 Or Mstyle = 20 Or Mstyle = 21 Or Mstyle = 22 Then
            tWR.left = 8
            DrawEdge lhDC, tWR, &H5, BF_TOP Or BF_BOTTOM '默认
         ElseIf Mstyle = 13 Then
            tWR.left = 21
            DrawEdge lhDC, tWR, &H5, BF_TOP Or BF_BOTTOM '分割线的样式
            tWR.top = tWR.top - 1
            DrawEdge lhDC, tWR, &H5, BF_TOP Or BF_BOTTOM '分割线的样式
         Else
            tWR.left = 25
            DrawEdge lhDC, tWR, &H2, BF_TOP Or BF_BOTTOM '分割线的样式
         End If
         
      Else
         ' Text item:
         bEnabled = ((tMII.fState And MFS_ENABLED) = MFS_ENABLED)
         bRadioCheck = ((tMII.fType And MFT_RADIOCHECK) = MFT_RADIOCHECK)
         bDisabled = ((tMII.fState And MFS_DISABLED) = MFS_DISABLED)
         bChecked = ((tMII.fState And MFS_CHECKED) = MFS_CHECKED)
         bHighlighted = ((tMII.fState And MFS_HILITE) = MFS_HILITE)
         bUnchecked = ((tMII.fState And MFS_UNCHECKED) = MFS_UNCHECKED)
         bUnhilite = ((tMII.fState And MFS_UNHILITE) = MFS_UNHILITE)
         If bHighlighted Then
            SetTextColor lhDC, TranslateColor(m_oActiveMenuColorOver)
         Else
            SetTextColor lhDC, TranslateColor(m_oActiveMenuColor)
         End If
         
         ' Check:
         If bChecked Then
            LSet tWR = tR
            tWR.top = tWR.top - 1
            InflateRect tWR, -4, -4
            tWR.right = tWR.left + (tWR.bottom - tWR.top)
            
            '复选菜单的边框样式
            If Mstyle = 1 Or Mstyle = 2 Or Mstyle = 7 Or Mstyle = 10 Or Mstyle = 15 Or Mstyle = 11 Or Mstyle = 18 Or Mstyle = 20 Or Mstyle = 21 Or Mstyle = 22 Then
                tWR.left = 9: tWR.right = 21
                DrawEdge lhDC, tWR, &H0, BF_RECT  '
                If Mstyle = 1 Then
                ChkColor = RGB(241, 197, 20)
                ElseIf Mstyle = 2 Or Mstyle = 18 Or Mstyle = 21 Then
                ChkColor = RGB(17, 70, 149)
                ElseIf Mstyle = 20 Then
                ChkColor = RGB(160, 5, 5)
                Else
                ChkColor = 0
                End If
            ElseIf Mstyle = 0 Or Mstyle = 3 Or Mstyle = 4 Or Mstyle = 5 Or Mstyle = 6 Or Mstyle = 9 Then
                tWR.left = 3: tWR.right = 16
                DrawEdge lhDC, tWR, &H0, BF_RECT  '
                If Mstyle = 22 Then
                ChkColor = RGB(17, 70, 149)
                Else
                ChkColor = RGB(214, 198, 3)
                End If
            ElseIf Mstyle = 9 Then
                tWR.left = 6: tWR.right = 19
                DrawEdge lhDC, tWR, &H0, BF_RECT  '
                ChkColor = RGB(214, 198, 3)
            ElseIf Mstyle = 13 Or Mstyle = 15 Then
                tWR.left = 2: tWR.right = 15
                DrawEdge lhDC, tWR, &H0, BF_RECT  '
                ChkColor = 0
            ElseIf Mstyle = 8 Or Mstyle = 12 Then
                tWR.left = 5: tWR.right = 17
                DrawEdge lhDC, tWR, &H0, BF_RECT  '
                ChkColor = RGB(201, 232, 142)
            ElseIf Mstyle = 19 Or Mstyle = 14 Then
                tWR.left = 13: tWR.right = 23
                DrawEdge lhDC, tWR, &H0, BF_RECT  '
                ChkColor = 0
            Else
                ChkColor = 0
            End If
            SelectObject lhDC, hFntOld
            hFntSOld = SelectObject(lhDC, hFontSymbol)
           ' If bHighlighted Then
             '  SetTextColor lhDC, TranslateColor(m_oActiveMenuColor)
             '  pDrawItem lhDC, "b", tWR, bDisabled, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
           ' Else
            tWR.top = tWR.top - 1
            SetTextColor lhDC, TranslateColor(vb3DDKShadow)
            pDrawItem lhDC, "b", tWR, bDisabled, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
            tWR.top = tWR.top + 2
            SetTextColor lhDC, TranslateColor(vb3DHighlight)
            pDrawItem lhDC, "b", tWR, bDisabled, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
            tWR.top = tWR.top - 1
            SetTextColor lhDC, TranslateColor(ChkColor)
            pDrawItem lhDC, "b", tWR, bDisabled, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER
            'End If
          '  SetTextColor lhDC, TranslateColor(m_oActiveMenuColorOver)
            SelectObject lhDC, hFntSOld
            hFntOld = SelectObject(lhDC, hFont)
         End If
         'Disabled

         
         ' Draw text:设置菜单文字样式
         LSet tWR = tR
         tWR.left = 25
         lID = tMII.dwItemData
         If lID > 0 And lID <= m_iRestore Then
            SetTextColor lhDC, vbWhite
            pDrawItem lhDC, m_sCaption(lID), tWR, bDisabled, DT_LEFT Or DT_SINGLELINE Or DT_VCENTER
            If Len(m_sShortCut(lID)) > 0 Then
               tWR.left = tWR.left + m_lMenuTextSize(lID) + 4 + 4
               SetTextColor lhDC, TranslateColor(m_oActiveMenuColor)
               pDrawItem lhDC, m_sShortCut(lID), tWR, bDisabled, DT_LEFT Or DT_SINGLELINE Or DT_VCENTER
            End If
            tWR.left = 25
            tWR.top = tWR.top - 1
            SetTextColor lhDC, TranslateColor(m_oActiveMenuColor)
            pDrawItem lhDC, m_sCaption(lID), tWR, bDisabled, DT_LEFT Or DT_SINGLELINE Or DT_VCENTER
         End If
         

         ' Highlighted:鼠标在菜单项上的样式
         If bHighlighted And Not (bDisabled) Then
            LSet tWR = tR
            InflateRect tWR, -1, -1
            tWR.top = tWR.top - 1
            If Mstyle = 1 Or Mstyle = 14 Or Mstyle = 15 Or Mstyle = 2 Or Mstyle = 7 Or Mstyle = 10 Or Mstyle = 11 Or Mstyle = 18 Or Mstyle = 19 Or Mstyle = 20 Or Mstyle = 21 Or Mstyle = 22 Then
                If Mstyle = 18 Or Mstyle = 20 Then
                tWR.left = 6
                ElseIf Mstyle = 19 Or Mstyle = 14 Then
                tWR.left = 10
                Else
                tWR.left = 8
                End If
                DrawEdge lhDC, tWR, &H6, BF_RECT  '
            ElseIf Mstyle = 3 Or Mstyle = 4 Or Mstyle = 5 Or Mstyle = 6 Or Mstyle = 8 Or Mstyle = 24 Or Mstyle = 25 Or Mstyle = 26 Then
                tWR.left = 22
                tWR.top = tWR.top
                DrawEdge lhDC, tWR, &H2, BF_RECT '
            ElseIf Mstyle = 12 Or Mstyle = 13 Or Mstyle = 17 Or Mstyle = 16 Or Mstyle = 15 Then
                tWR.left = 21
                tWR.top = tWR.top
                DrawEdge lhDC, tWR, &H4, BF_RECT '
            Else
                tWR.left = 22
                tWR.top = tWR.top + 1
                DrawEdge lhDC, tWR, &H6, BF_RECT '
            End If
            
        End If
         
         '可用的菜单项样式
         If bEnabled Then
           LSet tWR = tR
           InflateRect tWR, 0, 0
            tWR.top = tWR.top + 1
            If Mstyle = 0 Then
              DrawEdge lhDC, tWR, &H0, BF_RECT
            Else
              DrawEdge lhDC, tWR, &H0, BF_RECT
            End If
            
         End If
         
         '没有鼠标焦点的菜单项的样式
         'If bUnhilite Then
          '  LSet tWR = tR
        '    InflateRect tWR, 0, 0
        '    DrawEdge lhDC, tWR, &H2, BF_RECT  '
        ' End If
         
         '主菜单按下
         If bUnchecked Then
         
         End If

      End If
      SelectObject lhDC, hFntOld
      
      BitBlt tDIS.hdc, tDIS.rcItem.left, tDIS.rcItem.top, tDIS.rcItem.right - tDIS.rcItem.left + 1, tDIS.rcItem.bottom - tDIS.rcItem.top + 1, lhDC, 0, 0, vbSrcCopy
      
   Else
      DrawItem = CallOldWindowProc(m_hWnd, WM_DRAWITEM, wParam, lParam)
   End If
   
End Function
Friend Sub PressButton(ByVal iButton As Long, ByVal bState As Boolean)
   If bState Then
      m_iDownOn = iButton
   Else
      If m_iDownOn = iButton Then
         m_iDownOn = -1
      End If
   End If
   SendMessageLong m_hWnd, WM_NCPAINT, 0, 0
   
End Sub

Private Sub pDrawItem(ByVal lhDC As Long, ByVal sText As String, ByRef tR As RECT, ByVal bDisabled As Boolean, ByVal dtFlags As Long)
Dim tWR As RECT
   LSet tWR = tR
   If bDisabled Then '判断菜单项是否为无效
      SetTextColor lhDC, TranslateColor(vb3DHighlight)
      OffsetRect tWR, 0, 0
      tWR.left = tWR.left + 2: tWR.top = tWR.top + 2
      DrawText lhDC, sText, -1, tWR, dtFlags
      SetTextColor lhDC, TranslateColor(vb3DShadow)
      OffsetRect tWR, 0, 0
      tWR.left = tWR.left - 1: tWR.top = tWR.top - 1
      DrawText lhDC, sText, -1, tWR, dtFlags
   Else
      DrawText lhDC, sText, -1, tWR, dtFlags
   End If
End Sub
Private Sub pMouseMove()
Dim iMenu As Long
Dim iLastOver As Long
   iMenu = MenuHitTest()
   iLastOver = m_iOver
   m_iOver = iMenu
   'Debug.Print "Over:", m_iOver, iLastOver
   If m_iOver <> iLastOver Then
      ' !Repaint!
      'Debug.Print "Repaint"
      SendMessageLong m_hWnd, WM_NCPAINT, 0, 0
   End If
   If m_cTmr Is Nothing Then
      Set m_cTmr = New CTimer
   End If
   If m_iOver < 1 And m_iDownOn = 0 Then
      m_cTmr.Interval = 0
   Else
      If m_iDownOn > 0 Then
         If GetAsyncKeyState(vbLeftButton) = 0 Then
            m_iDownOn = 0
            SendMessageLong m_hWnd, WM_NCPAINT, 0, 0
         End If
      End If
      m_cTmr.Interval = 50
   End If
End Sub

Private Sub m_cTmr_ThatTime()
   pMouseMove
End Sub
' Convert Automation color to Windows color
Private Function TranslateColor(ByVal clr As OLE_COLOR, _
                        Optional hPal As Long = 0) As Long
    If OleTranslateColor(clr, hPal, TranslateColor) Then
        TranslateColor = CLR_INVALID
    End If
End Function













⌨️ 快捷键说明

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