制作有背景图案的TreeView

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MyTreeview1.BackgroundImage = Image.FromFile("d:/1.jpg")
            End Sub

   End Class

Public Class MyTreeview : Inherits treeview
    Dim myPicturebox As UPictureBox
    Dim myBitmap As Bitmap
    Dim myAlphaBitmap As Bitmap
    Dim myBackColor As Color = Color.Green
    Dim myBackAlpha As Integer = 0
    Dim myUpToDate As Boolean = False
    Dim myCaretUpToDate As Boolean = False
    Dim myPaintedFirstTime As Boolean = False
    Dim myCaretState As Boolean = True
    Dim myFontHeight As Integer = 10
    Dim myTimer1 As Timer


    Public Sub New()
        MyBase.New()
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        Me.BackColor = Me.myBackColor
        Me.myPicturebox = New UPictureBox()
        Me.Controls.Add(Me.myPicturebox)
        Me.myPicturebox.Dock = DockStyle.Fill
        'Me.myPicturebox.Visible = False
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = win32.WM_PAINT Then

            myPaintedFirstTime = True
            If Not (myUpToDate) Or Not (myCaretUpToDate) Then
                GetBitmap()
            End If
            myUpToDate = True
            myCaretUpToDate = True

            If (myPicturebox.Image IsNot Nothing) Then
                myPicturebox.Image.Dispose()
            End If
            myPicturebox.Image = myAlphaBitmap.Clone()
            'Me.BackgroundImage = myAlphaBitmap.Clone()
        End If
    End Sub

    Private Sub GetBitmap()
        If (myBitmap Is Nothing) OrElse (myAlphaBitmap Is Nothing) OrElse (myBitmap.Width <> Me.Width) OrElse (myBitmap.Height <> Me.Height) OrElse (myAlphaBitmap.Width <> Me.Width) OrElse (myAlphaBitmap.Height <> Height) Then
            myBitmap = Nothing
            myAlphaBitmap = Nothing
        End If


        If (myBitmap Is Nothing) Then
            myBitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
            myUpToDate = False
        End If


        If Not (myUpToDate) Then
            'Capture the TextBox control window
            Me.SetStyle(ControlStyles.UserPaint, False)
            win32.CaptureWindow(Me, myBitmap)


            Me.SetStyle(ControlStyles.UserPaint, True)
            Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            Me.BackColor = Color.FromArgb(myBackAlpha, myBackColor)
        End If

 

        Dim r2 As Rectangle = New Rectangle(0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        Dim tempImageAttr As Imaging.ImageAttributes = New Imaging.ImageAttributes()
        'Found the color map code in the MS Help
        Dim tempColorMap(0) As Imaging.ColorMap
        tempColorMap(0) = New Imaging.ColorMap()
        tempColorMap(0).OldColor = Color.FromArgb(255, myBackColor)
        tempColorMap(0).NewColor = Color.FromArgb(myBackAlpha, myBackColor)
        tempImageAttr.SetRemapTable(tempColorMap)

        If (myAlphaBitmap IsNot Nothing) Then
            myAlphaBitmap.Dispose()
        End If


        myAlphaBitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)

        Dim tempGraphics1 As Graphics = Graphics.FromImage(myAlphaBitmap)

        tempGraphics1.DrawImage(myBitmap, r2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, GraphicsUnit.Pixel, tempImageAttr)

        tempGraphics1.Dispose()

    End Sub

 

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        Me.myBitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        Me.myAlphaBitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyDown(e)
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyUp(e)
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(mevent)
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnGiveFeedback(ByVal gfbevent As System.Windows.Forms.GiveFeedbackEventArgs)
        MyBase.OnGiveFeedback(gfbevent)
        myUpToDate = False
        Me.Invalidate()
    End Sub
    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        Dim ptCursor As Point = Windows.Forms.Cursor.Position
        Dim f As Form = Me.FindForm
        ptCursor = f.PointToClient(ptCursor)
        If Not (Me.Bounds.Contains(ptCursor)) Then
            MyBase.OnMouseLeave(e)
        End If
    End Sub

    Protected Overrides Sub OnChangeUICues(ByVal e As System.Windows.Forms.UICuesEventArgs)
        MyBase.OnChangeUICues(e)
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
        MyBase.OnGotFocus(e)
        myUpToDate = False
        Me.Invalidate()
        myTimer1 = New Timer
        myTimer1.Interval = win32.GetCaretBlinkTime()
        AddHandler myTimer1.Tick, AddressOf mytick
        myTimer1.Enabled = True
    End Sub
    Private Sub mytick(ByVal send As Object, ByVal e As EventArgs)
        myCaretState = Not myCaretState
        myUpToDate = False
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
        MyBase.OnLostFocus(e)
        Me.myCaretUpToDate = False
        myUpToDate = False
        Me.Invalidate()

        myTimer1.Dispose()
    End Sub


    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(e)

        myUpToDate = False
        Me.Invalidate()
    End Sub
End Class

Public Class UPictureBox : Inherits PictureBox
    Public Sub New()
        'MyBase.New()
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        Me.Cursor = Nothing
        Me.Enabled = True
        Me.SizeMode = PictureBoxSizeMode.Normal
    End Sub
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = win32.WM_LBUTTONDOWN _
           Or m.Msg = win32.WM_RBUTTONDOWN _
           Or m.Msg = win32.WM_MOUSELEAVE _
           Or m.Msg = win32.WM_MOUSEMOVE Then
            win32.PostMessage(Me.Parent.Handle, m.Msg, m.WParam, m.LParam)
        ElseIf m.Msg = win32.WM_LBUTTONUP Then
            Me.Parent.Invalidate()
        End If
        MyBase.WndProc(m)
    End Sub
End Class


Public Class win32
    Public Const WM_MOUSEMOVE As Integer = &H200
    Public Const WM_LBUTTONDOWN As Integer = &H201
    Public Const WM_LBUTTONUP As Integer = &H202
    Public Const WM_RBUTTONDOWN As Integer = &H204
    Public Const WM_LBUTTONDBLCLK As Integer = &H203
    Public Const WM_MOUSELEAVE As Integer = &H2A3
    Public Const WM_PAINT As Integer = &HF
    Public Const WM_ERASEBKGND As Integer = &H14
    Public Const WM_PRINT As Integer = &H317
    Public Const WM_HSCROLL As Integer = &H114
    Public Const WM_VSCROLL As Integer = &H115
    Public Const EM_GETSEL As Integer = &HB0
    Public Const EM_LINEINDEX As Integer = &HBB
    Public Const EM_LINEFROMCHAR As Integer = &HC9
    Public Const EM_POSFROMCHAR As Integer = &HD6

    Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
      ByVal hwnd As Int32, _
      ByVal wMsg As Int32, _
      ByVal wParam As Int32, _
      ByVal lParam As Int32) As Int32
    Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" ( _
      ByVal hwnd As Int32, _
      ByVal wMsg As Int32, _
      ByVal wParam As Int32, _
      ByVal lParam As Int32) As Int32
    Public Declare Function GetCaretBlinkTime Lib "user32.dll" () As Int32

    Public Const WM_PRINTCLIENT As Integer = &H318
    Public Const PRF_CHECKVISIBLE = &H1L
    Public Const PRF_NONCLIENT = &H2L
    Public Const PRF_CLIENT = &H4L
    Public Const PRF_ERASEBKGND = &H8L
    Public Const PRF_CHILDREN = &H10L
    Public Const PRF_OWNED = &H20L


    Public Shared Function CaptureWindow(ByVal control As System.Windows.Forms.Control, ByRef bitmap As System.Drawing.Bitmap) As Boolean
        'This function captures the contents of a window or control
        Dim g2 As Graphics
        g2 = Graphics.FromImage(bitmap)
        Dim meint As Integer = CType((PRF_CLIENT + PRF_ERASEBKGND), Integer)
        Dim meptr As System.IntPtr = New System.IntPtr(meint)
        Dim hdc As System.IntPtr = g2.GetHdc()
        win32.SendMessage(control.Handle, win32.WM_PRINT, hdc, meptr)
        g2.ReleaseHdc(hdc)
        g2.Dispose()
        Return True
    End Function
End Class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值