当前位置:编程学习 > C#/ASP.NET >>

ToolBar的模样自己画(五)

答案: 

'最后一部分,也是最核心的消息处理代码与主绘图过程

Friend Function MsgProc(lParam As Long, MouseDown As Boolean) As Long
    Dim tHDR As NMHDR
    Dim className As String * 32
    Dim retval As Long
    CopyMemory tHDR, ByVal lParam, Len(tHDR)
    If tHDR.hwndFrom <> 0 Then
        retval = GetClassName(tHDR.hwndFrom, className, 33)
        If retval > 0 Then
            If Left$(className, retval) = "msvb_lib_易做图" Then
                MsgProc = OnCustomDraw(lParam, MouseDown)
            End If
        End If
    End If
End Function
Private Function OnCustomDraw(lParam As Long, MouseDown As Boolean) As Long
    Dim tTBCD As NMTBCUSTOMDRAW
    Dim hBrush As Long
    CopyMemory tTBCD, ByVal lParam, Len(tTBCD)
    With tTBCD.nmcd
        Select Case .dwDrawStage
            Case CDDS_ITEMPREPAINT
                OnCustomDraw = CDRF_SKIPDEFAULT
                DrawToolbarButton .hdr.hwndFrom, .hdc, .dwItemSpec, .uItemState, .rc, MouseDown
            Case CDDS_PREPAINT
                OnCustomDraw = CDRF_NOTIFYITEMDRAW
                GetClientRect .hdr.hwndFrom, .rc
                If mpicBk Is Nothing Then
                    hBrush = CreateSolidBrush(m_lngBackColor)
                Else
                    hBrush = CreatePatternBrush(mpicBk)
                End If
                FillRect .hdc, .rc, hBrush
                DeleteObject hBrush
        End Select
    End With
End Function
Private Sub DrawToolbarButton(ByVal hWnd As Long, ByVal hdc As Long, itemSpec As Long, ByVal itemState As Long, tR As RECT, MouseDown As Boolean)
    Dim i As Long
    Dim bPushed As Boolean, bDropDown As Boolean, bHover As Boolean
    Dim bDisabled As Boolean, bChecked As Boolean
    Dim bSkipped As Boolean, bBottomText As Boolean, bNoDsbIcon As Boolean
    Dim hIcon As Long, hImageList As Long
    Dim tTB As TBBUTTON
    Dim szText As Size, rcDrop As RECT, rcIcon As RECT
    Dim hOldPen As Long, hPen As Long
    Dim hFont As Long, hOldFont As Long
    Dim sCaption As String, bFirstSetBk As Boolean
    Dim lDropWidth As Long, lTxtColor As Long
    sCaption = String$(128, vbNullChar)
    i = SendMessage(hWnd, TB_GETBUTTONTEXTA, itemSpec, ByVal sCaption)
    If i > 0 Then
        sCaption = Left$(sCaption, i)
    Else
        sCaption = ""
    End If
    i = GetWindowLong(hWnd, GWL_STYLE)
    bBottomText = ((i And TBSTYLE_LIST) = 0)
    i = SendMessage(hWnd, TB_COMMANDTOINDEX, itemSpec, ByVal 0)
    SendMessage hWnd, TB_GETBUTTON, i, tTB
   
    bDisabled = (itemState And CDIS_DISABLED)
    bChecked = (itemState And CDIS_CHECKED)
    bHover = (itemState And CDIS_HOT)
    bPushed = (itemState And CDIS_SELECTED)
   
    If tTB.fsStyle And TBSTYLE_SEP Then '分隔线按钮
        hPen = CreatePen(PS_SOLID, 1, vb3DShadow)
        hOldPen = SelectObject(hdc, hPen)
        MoveToEx hdc, tR.Left + 2&, tR.Top + 1&, ByVal 0
        LineTo hdc, tR.Left + 2&, tR.Bottom - 1&
        SelectObject hdc, hOldPen
        DeleteObject hPen
        Exit Sub
    Else
        hImageList = SendMessage(hWnd, TB_GETIMAGELIST, 0, ByVal 0)
        If hImageList <> 0 Then '取得主图像列表
            If mlngImgList <> hImageList Then
                mlngImgList = hImageList
                bFirstSetBk = True
                mlngIconWidth = 0
            End If
            If bDisabled Then   '取得禁用图像列表
                i = SendMessage(hWnd, TB_GETDISABLEDIMAGELIST, 0, ByVal 0)
                If i <> 0 And i <> hImageList Then
                    hImageList = i
                    If mlngDsbImgList <> i Then
                        mlngDsbImgList = i
                        bFirstSetBk = True
                    End If
                Else
                    bNoDsbIcon = True
                End If
            ElseIf bHover Then  '取得热图像列表
                i = SendMessage(hWnd, TB_GETHOTIMAGELIST, 0, ByVal 0)

上一个:复制文件夹所有内容 和 删除整个文件夹的2个函数
下一个:ToolBar的模样自己画(四)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,