ctxhookmenu.ctl

来自「非常漂亮的VB控件」· CTL 代码 · 共 1,494 行 · 第 1/5 页

CTL
1,494 行
字号
                    End If
                    '--- draw text
                    If UBound(vSplit) >= 0 Then
                        If (lType And MFT_RIGHTJUSTIFY) = 0 Then
                            .DrawText vSplit(0), dis.rcItem.Left + m_lMenuHeight + 10, dis.rcItem.Top, dis.rcItem.Right - m_lMenuHeight, dis.rcItem.Bottom - 1, DT_LEFT Or DT_SINGLELINE Or DT_VCENTER
                        Else
                            .DrawText vSplit(0), dis.rcItem.Left + m_lMenuHeight, dis.rcItem.Top, dis.rcItem.Right - 1 - m_lMenuHeight - 10, dis.rcItem.Bottom - 1, DT_RIGHT Or DT_SINGLELINE Or DT_VCENTER
                        End If
                    End If
                    '--- draw shortcut keys
                    If UBound(vSplit) > 0 Then
                        If (lType And MFT_RIGHTJUSTIFY) = 0 Then
                            .DrawText vSplit(1), dis.rcItem.Left + m_lMenuHeight + 10, dis.rcItem.Top, dis.rcItem.Right - m_lMenuHeight, dis.rcItem.Bottom - 1, DT_RIGHT Or DT_SINGLELINE Or DT_VCENTER
                        Else
                            .DrawText vSplit(1), dis.rcItem.Left + m_lMenuHeight, dis.rcItem.Top, dis.rcItem.Right - 1 - m_lMenuHeight - 10, dis.rcItem.Bottom - 1, DT_LEFT Or DT_SINGLELINE Or DT_VCENTER
                        End If
                    End If
                    '--- draw submenu arrow (if necessary)
                    If (lState And MF_POPUP) <> 0 Then
                        lI = (dis.rcItem.Top + dis.rcItem.Bottom - 1) \ 2
                        If (lType And MFT_RIGHTJUSTIFY) = 0 Then
                            lJ = dis.rcItem.Right - m_lMenuHeight \ 3 - 4
                        Else
                            lJ = dis.rcItem.Left + m_lMenuHeight * 2 \ 3 - 4
                        End If
                        For lK = (m_lTextHeight + 3) \ 6 To 0 Step -1
                            If (lType And MFT_RIGHTJUSTIFY) = 0 Then
                                .DrawLine lJ - lK, lI - lK, lJ - lK, lI + lK + 1, .ForeColor
                            Else
                                .DrawLine lJ + lK, lI - lK, lJ + lK, lI + lK + 1, .ForeColor
                            End If
                        Next
                    End If
                End If
            End If
            If .IsMemoryDC Then
                SetViewportOrgEx .hDC, 0, 0, 0
                .BitBlt dis.hDC, dis.rcItem.Left, dis.rcItem.Top
            End If
        End With
        '--- prevent further drawing (esp sub-menu arrow) and reduce flicker
        ExcludeClipRect dis.hDC, dis.rcItem.Left, dis.rcItem.Top, dis.rcItem.Right, dis.rcItem.Bottom
        '--- handled
        pvDrawItem = True
    End If
End Function

Private Sub pvWritePictureProperty( _
            oBag As PropertyBag, _
            sPropName As String, _
            ByVal oPic As StdPicture, _
            Optional DefaultValue As Variant)
    Dim ii              As ICONINFO
    Dim hr              As Long
    Dim oMemDC          As cMemDC
    
    If Not oPic Is Nothing Then
        If oPic.Type = vbPicTypeIcon Then
            Set oMemDC = New cMemDC
            hr = GetIconInfo(oPic.handle, ii)
            With New PropertyBag
                Call .WriteProperty("c", oMemDC.BitmapToPicture(ii.hbmColor))
                Call .WriteProperty("m", oMemDC.BitmapToPicture(ii.hbmMask))
                Call oBag.WriteProperty(sPropName, .Contents, DefaultValue)
            End With
            Exit Sub
        End If
    End If
    '--- else
    Call oBag.WriteProperty(sPropName, oPic, DefaultValue)
End Sub

Private Function pvReadPictureProperty( _
            oBag As PropertyBag, _
            sPropName As String, _
            Optional DefaultValue As Variant) As StdPicture
    Dim ii              As ICONINFO
    Dim hr              As Long
    Dim imgColor        As StdPicture
    Dim imgMask         As StdPicture
    
    If IsArray(oBag.ReadProperty(sPropName, DefaultValue)) Then
        With New PropertyBag
            .Contents = oBag.ReadProperty(sPropName, DefaultValue)
            Set imgColor = .ReadProperty("c")
            Set imgMask = .ReadProperty("m")
        End With
        ii.fIcon = 1
        ii.hbmColor = imgColor.handle
        ii.hbmMask = imgMask.handle
        With New cMemDC
            Set pvReadPictureProperty = .IconToPicture(CreateIconIndirect(ii))
        End With
    Else
        Set pvReadPictureProperty = oBag.ReadProperty(sPropName, DefaultValue)
    End If
End Function

Private Function pvGetBackground(ByVal hwnd As Long) As cMemDC
    Dim oMemDC          As cMemDC
    Dim rc              As RECT
    Dim rcItem          As RECT
    Dim rcPopup         As RECT
    Dim rcPopupBtm      As RECT
    Dim lI              As Long
    Dim lJ              As Long
    Dim lWidth          As Long
    Dim lHeight         As Long
    Dim v               As Variant
    Dim hWndFrm         As Long
    Dim lHorShadowStart As Long
    Dim lHorShadowEnd   As Long
    
    On Error Resume Next
    Set oMemDC = m_cMemDC("#" & hwnd)
    If oMemDC Is Nothing Then
        GetWindowRect hwnd, rc
        Set oMemDC = New cMemDC
        oMemDC.Init rc.Right - rc.Left, rc.Bottom - rc.Top
        With New cMemDC
            .Init , , , GetWindowDC(GetDesktopWindow())
            .BitBlt oMemDC.hDC, 0, 0, rc.Right - rc.Left, rc.Bottom - rc.Top, m_ptLast.X, m_ptLast.Y
            Call ReleaseDC(GetDesktopWindow(), .hDC)
        End With
        lWidth = rc.Right - rc.Left - 2 * m_lFrameWidth + 1
        lHeight = rc.Bottom - rc.Top - 2 * m_lFrameWidth + 3
        lHorShadowEnd = lWidth - 1 + 3
        With oMemDC
            .Rectangle 0, 0, lWidth, lHeight, vbWindowBackground, , m_clrMenuBorder
            '--- visually improves performance to clear the left band here
            .FillRect 1, 2, m_lMenuHeight + 4, lHeight - 2, m_clrMenuBack
            Call GetMenuItemRect(m_hFormHwnd, m_hLastMenu, 0, rcItem)
            '--- fix the line right below the main menu
            For lI = 0 To GetMenuItemCount(m_hFormMenu) - 1
                '--- find opened main menu item
                If (GetMenuState(m_hFormMenu, lI, MF_BYPOSITION) And MF_HILITE) <> 0 Then
                    If m_hLastMenu = GetSubMenu(m_hFormMenu, lI) Then
                        '--- get its popup menu dimensions
                        hWndFrm = IIf(m_hParentHwnd <> 0, m_hParentHwnd, m_hFormHwnd)
                        '--- win98: can't pass NULL for hwnd (so use hWndFrm)
                        Call GetMenuItemRect(hWndFrm, m_hLastMenu, 0, rcPopup)
                        Call GetMenuItemRect(hWndFrm, m_hLastMenu, GetMenuItemCount(m_hLastMenu) - 1, rcPopupBtm)
                        '--- get main menu item dimensions
                        Call GetMenuItemRect(hWndFrm, m_hFormMenu, lI, rcItem)
                        '--- if popup below main menu fix border
                        If rcItem.Bottom + rcPopupBtm.Bottom - rcPopup.Top + 2 * m_lFrameWidth <= GetSystemMetrics(SM_CYSCREEN) Then
                            If Not m_bLastSelMenuRightAlign Then
                                .FillRect 1, 0, rcItem.Right - rcItem.Left - 1, 1, m_clrMenuBack
                            Else
                                .FillRect lWidth - (rcItem.Right - rcItem.Left - 1), 0, lWidth - 1, 1, m_clrMenuBack
                            End If
                        ElseIf rcPopupBtm.Bottom > rcItem.Top Then
                            If Not m_bLastSelMenuRightAlign Then
                                .FillRect 1, lHeight - 1, rcItem.Right - rcItem.Left - 1, lHeight, m_clrMenuBack
                                lHorShadowStart = rcItem.Right - rcItem.Left - 3
                            Else
                                .FillRect lWidth - (rcItem.Right - rcItem.Left - 1), lHeight - 1, lWidth - 1, lHeight, m_clrMenuBack
                                lHorShadowEnd = lHorShadowEnd - (rcItem.Right - rcItem.Left + 3)
                            End If
                        End If
                    End If
                End If
            Next
            '--- shadow
            If m_bConstrainedColors Then
                .FillRect lHorShadowStart + 3, lHeight, lHorShadowEnd, lHeight + 2, vbButtonShadow
                .FillRect lWidth, 3, lWidth + 2, lHeight, vbButtonShadow
            Else
                For lJ = 0 To 2
                    For lI = lHorShadowStart + 3 To lHorShadowEnd
                        .SetPixel lI, lHeight + lJ, pvAlphaBlend(vbBlack, .GetPixel(lI, lHeight + lJ), (&H40 - lJ * (&H40 / 3)) * (IIf(lI <= 6, lI - 2, 4) / 4) * (IIf(lI >= lWidth, lWidth + 3 - lI, 4) / 4))
                    Next
                    For lI = 3 To lHeight - 1
                        .SetPixel lWidth + lJ, lI, pvAlphaBlend(vbBlack, .GetPixel(lWidth + lJ, lI), (&H40 - lJ * (&H40 / 3)) * (IIf(lI <= 6, lI - 2, 4) / 4))
                    Next
                Next
            End If
        End With
        m_cMemDC.Add oMemDC, "#" & hwnd
    End If
QH:
    Set pvGetBackground = oMemDC
End Function

'==============================================================================
' Base class events
'==============================================================================

Private Sub UserControl_Initialize()
    Set m_cMenuSubclass = New Collection
    Set m_cBmps = New Collection
    Set m_cMemDC = New Collection
    Set m_cMenuInfo = New Collection
    Set m_oFont = New StdFont
    If g_oMenuHookImpl Is Nothing Then
        Set g_oMenuHookImpl = New cMenuHook
    End If
    #If DebugMode Then
        DebugInit m_sDebugID, MODULE_NAME
    #End If
End Sub

Private Sub UserControl_Terminate()
    If g_oCurrentMenu Is Me Then
        #If WEAK_REF_CURRENTMENU Then
            CopyMemory VarPtr(g_oCurrentMenu), VarPtr(0), 4
        #Else
            Set g_oCurrentMenu = Nothing
        #End If
    End If
    #If DebugMode Then
        DebugTerm m_sDebugID
    #End If
End Sub

Private Sub UserControl_InitProperties()
    SelectDisabled = DEF_SELECTDISABLED
    BitmapSize = DEF_BITMAPSIZE
    UseSystemFont = DEF_USESYSTEMFONT
    Set Font = DEF_FONT
    Init UserControl.ContainerHwnd
    
    If g_oMenuHookImpl Is Nothing Then
        Set g_oMenuHookImpl = New cMenuHook
    End If
    #If DebugMode Then
        DebugInit m_sDebugID, MODULE_NAME
    #End If
    
'    m_UseSystemColor = m_def_UseSystemColor
    m_UseSystemColor = m_def_UseSystemColor
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Dim lIdx            As Long
    Dim vElem           As Variant
    
    On Error Resume Next
    ReDim vElem(0 To 2)
    With PropBag
        SelectDisabled = .ReadProperty("SelectDisabled", DEF_SELECTDISABLED)
        BitmapSize = .ReadProperty("BitmapSize", DEF_BITMAPSIZE)
        For lIdx = 1 To .ReadProperty("BmpCount", 0)
            Set vElem(0) = pvReadPictureProperty(PropBag, "Bmp:" & lIdx, Nothing)
            vElem(1) = .ReadProperty("Mask:" & lIdx, 0)
            vElem(2) = .ReadProperty("Key:" & lIdx, "#" & lIdx)
            m_cBmps.Add vElem, vElem(2)
        Next
        UseSystemFont = .ReadProperty("UseSystemFont", DEF_USESYSTEMFONT)
        Set Font = .ReadProperty("Font", DEF_FONT)
    End With
    Init UserControl.ContainerHwnd
    m_SelMenuBorder = PropBag.ReadProperty("SelMenuBorder", m_def_SelMenuBorder)
    m_SelMenuBack = PropBag.ReadProperty("SelMenuBack", m_def_SelMenuBack)
    m_SelMenuFore = PropBag.ReadProperty("SelMenuFore", m_def_SelMenuFore)
    m_SelCheckBack = PropBag.ReadProperty("SelCheckBack", m_def_SelCheckBack)
    m_MenuBorder = PropBag.ReadProperty("MenuBorder", m_def_MenuBorder)
    m_MenuBack = PropBag.ReadProperty("MenuBack", m_def_MenuBack)
    m_MenuFore = PropBag.ReadProperty("MenuFore", m_def_MenuFore)
    m_CheckBack = PropBag.ReadProperty("CheckBack", m_def_CheckBack)
    m_CheckFore = PropBag.ReadProperty("CheckFore", m_def_CheckFore)
    m_DisabledMenuBorder = PropBag.ReadProperty("DisabledMenuBorder", m_def_DisabledMenuBorder)
    m_DisabledMenuBack = PropBag.ReadProperty("DisabledMenuBack", m_def_DisabledMenuBack)
    m_DisabledMenuFore = PropBag.ReadProperty("DisabledMenuFore", m_def_DisabledMenuFore)
    m_MenuBarBack = PropBag.ReadProperty("MenuBarBack", m_def_MenuBarBack)
    m_MenuPopupBack = PropBag.ReadProperty("MenuPopupBack", m_def_MenuPopupBack)
'    m_UseSystemColor = PropBag.ReadProperty("UseSystemColor", m_def_UseSystemColor)
    m_UseSystemColor = PropBag.ReadProperty("UseSystemColor", m_def_UseSystemColor)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Dim lIdx            As Long
    
    On Error Resume Next
    With PropBag
        Call .WriteProperty("SelectDisabled", SelectDisabled, DEF_SELECTDISABLED)
        Call .WriteProperty("BitmapSize", BitmapSize, DEF_BITMAPSIZE)
        Call .WriteProperty("BmpCount", m_cBmps.Count)
        For lIdx = 1 To m_cBmps.Count
            Call pvWritePictureProperty(PropBag, "Bmp:" & lIdx, m_cBmps(lIdx)(0), Nothing)
            Call .WriteProperty("Mask:" & lIdx, m_cBmps(lIdx)(1), 0)
            Call .WriteProperty("Key:" & lIdx, m_cBmps(lIdx)(2), "#" & lIdx)
        Next
        Call .WriteProperty("UseSystemFont", UseSystemFont, DEF_USESYSTEMFONT)
        Call .WriteProperty("Font", Font, DEF_FONT)
    End With
    Call PropBag.WriteProperty("SelMenuBorder", m_SelMenuBorder, m_def_SelMenuBorder)
    Call PropBag.WriteProperty("SelMenuBack", m_SelMenuBack, m_def_SelMenuBack)
    Call PropBag.WriteProperty("SelMenuFore", m_SelMenuFore, m_def_SelMenuFore)
    Call PropBag.WriteProperty("SelCheckBack", m_SelCheckBack, m_def_SelCheckBack)
    Call PropBag.WriteProperty("MenuBorder", m_MenuBorder, m_def_MenuBorder)
    Call PropBag.WriteProperty("MenuBack", m_MenuBack, m_def_MenuBack)
    Call PropBag.WriteProperty("MenuFore", m_MenuFore, m_def_MenuFore)
    Call PropBag.WriteProperty("CheckBack", m_CheckBack, m_def_CheckBack)
    Call PropBag.WriteProperty("CheckFore", m_CheckFore, m_def_CheckFore)
    Call PropBag.WriteProperty("DisabledMenuBorder", m_DisabledMenuBorder, m_def_DisabledMenuBorder)
    Call PropBag.WriteProperty("DisabledMenuBack", m_DisabledMenuBack, m_def_DisabledMenuBack)
    Call PropBag.Wr

⌨️ 快捷键说明

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