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

📄 如何改变treeview的背景色和前景色.txt

📁 以电子书的形式收集了VB一些常见问题解决方法,可以很方便的查找自己需要解决的问题.对一些VB初学者很用.
💻 TXT
字号:
背 景 色 可 以 通 过 API来 修 改 : 
    先 加 入 以 下 API声 明 : 
    Option Explicit 
     
    Public Const GWL_STYLE As Long = -16& 
    Public Const COLOR_WINDOW = 5 
    Public Const COLOR_WINDOWTEXT = 8 
     
    Public Const TVI_ROOT As Long = &HFFFF0000 
    Public Const TVI_FIRST As Long = &HFFFF0001 
    Public Const TVI_LAST As Long = &HFFFF0002 
    Public Const TVI_SORT As Long = &HFFFF0003 
     
    Public Const TVIF_STATE As Long = &H8 
     
    'treeview styles 
    Public Const TVS_HASLINES As Long = 2 
    Public Const TVS_FULLROWSELECT As Long = &H1000 
     
    'treeview style item states 
    Public Const TVIS_BOLD As Long = &H10 
     
    Public Const TV_FIRST As Long = &H1100 
    Public Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10) 
    Public Const TVM_GETITEM As Long = (TV_FIRST + 12) 
    Public Const TVM_SETITEM As Long = (TV_FIRST + 13) 
    Public Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29) 
    Public Const TVM_SETTEXTCOLOR As Long = (TV_FIRST + 30) 
    Public Const TVM_GETBKCOLOR As Long = (TV_FIRST + 31) 
    Public Const TVM_GETTEXTCOLOR As Long = (TV_FIRST + 32) 
     
    Public Const TVGN_ROOT As Long = &H0 
    Public Const TVGN_NEXT As Long = &H1 
    Public Const TVGN_PREVIOUS As Long = &H2 
    Public Const TVGN_PARENT As Long = &H3 
    Public Const TVGN_CHILD As Long = &H4 
    Public Const TVGN_FIRSTVISIBLE As Long = &H5 
    Public Const TVGN_NEXTVISIBLE As Long = &H6 
    Public Const TVGN_PREVIOUSVISIBLE As Long = &H7 
    Public Const TVGN_DROPHILITE As Long = &H8 
    Public Const TVGN_CARET As Long = &H9 
     
    Public Type TVITEM 
     mask As Long 
     hItem As Long 
     state As Long 
     stateMask As Long 
     pszText As String 
     cchTextMax As Long 
     iImage As Long 
     iSelectedImage As Long 
     cChildren As Long 
     lParam As Long 
    End Type 
     
    Public Declare Function SendMessageLong Lib "user32" _ 
     Alias "SendMessageA" _ 
     (ByVal hwnd As Long, _ 
     ByVal wMsg As Long, _ 
     ByVal wParam As Long, _ 
     ByVal lParam As Long) As Long 
     
    Public Declare Function SendMessageAny Lib "user32" _ 
     Alias "SendMessageA" _ 
     (ByVal hwnd As Long, _ 
     ByVal wMsg As Long, _ 
     ByVal wParam As Long, _ 
     lParam As Any) As Long 
     
    Public Declare Function GetWindowLong Lib "user32" _ 
     Alias "GetWindowLongA" _ 
     (ByVal hwnd As Long, _ 
     ByVal nIndex As Long) As Long 
     
    Public Declare Function SetWindowLong Lib "user32" _ 
     Alias "SetWindowLongA" _ 
     (ByVal hwnd As Long, _ 
     ByVal nIndex As Long, _ 
     ByVal dwNewLong As Long) As Long 
     
    Public Declare Function GetSysColor Lib "user32" _ 
     (ByVal nIndex As Long) As Long 
    '--end block--' 
    使 用 以 下 的 函 数 可 以 得 到 和 设 置 背 景 色 ( 假 定 TreeView控 件 叫 TV1) : 
    Private Function GetTVBackColour() As Long 
     Dim clrref As Long 
     Dim hwndTV As Long 
     
     hwndTV = TV1.hwnd 
     
     'try for the treeview backcolor 
     clrref = SendMessageLong(hwndTV, TVM_GETBKCOLOR, 0, 0) 
     
     'if clrref = -1, then the color is a system color. 
     'In theory, system colors need to be Or'd with &HFFFFFF 
     'to retrieve the actual RGB value, but not Or'ing 
     'seems to work for me. The default system colour for 
     'a treeview background is COLOR_WINDOW. 
     If clrref = -1 Then 
     clrref = GetSysColor(COLOR_WINDOW) ' Or &HFFFFFF 
     End If 
     
     'one way or another, pass it back 
     GetTVBackColour = clrref 
     
    End Function 
     
    Private Sub SetTVBackColour(clrref As Long) 
     
     Dim hwndTV As Long 
     Dim style As Long 
     
     hwndTV = TV1.hwnd 
     
     'Change the background 
     Call SendMessageLong(hwndTV, TVM_SETBKCOLOR, 0, clrref) 
     
     'reset the treeview style so the 
     'tree lines appear properly 
     style = GetWindowLong(TV1.hwnd, GWL_STYLE) 
     
     'if the treeview has lines, temporarily 
     'remove them so the back repaints to the 
     'selected colour, then restore 
     If style And TVS_HASLINES Then 
     Call SetWindowLong(hwndTV, GWL_STYLE, style Xor TVS_HASLINES) 
     Call SetWindowLong(hwndTV, GWL_STYLE, style) 
     End If 
     
    End Sub 
    也 有 类 似 的 办 法 可 以 设 置 前 景 色 , 但 这 种 方 法 似 乎 不 能 用 于 VB 6的 TreeView控 件 , 只 能 用 于 vb6的 控 件 。 如 果 你 是 从 vb6升 级 到 VB6的 , 你 的 机 器 中 将 同 时 又 有 两 个 版 本 的 控 件 。 
    Private Sub SetTVForeColour(clrref) 
     
     Dim hwndTV As Long 
     Dim style As Long 
     
     hwndTV = TV1.hwnd 
     
     'Change the background 
     Call SendMessageLong(hwndTV, TVM_SETTEXTCOLOR, 0, clrref) 
     
     'reset the treeview style so the 
     'tree lines appear properly 
     style = GetWindowLong(TV1.hwnd, GWL_STYLE) 
     
     'if the treeview has lines, temporarily 
     'remove them so the back repaints to the 
     'selected colour, then restore 
     If style And TVS_HASLINES Then 
     Call SetWindowLong(hwndTV, GWL_STYLE, style Xor TVS_HASLINES) 
     Call SetWindowLong(hwndTV, GWL_STYLE, style) 
     End If 
     
    End Sub 
     
    Private Function GetTVForeColour() As Long 
     
     Dim clrref As Long 
     Dim hwndTV As Long 
     
     hwndTV = TV1.hwnd 
     
     'try for the treeview text colour 
     clrref = SendMessageLong(hwndTV, TVM_GETTEXTCOLOR, 0, 0) 
     
     'if clrref = -1, then the color is a system color. 
     'In theory, system colors need to be Or'd with &HFFFFFF 
     'to retrieve the actual RGB value, but not Or'ing 
     'seems to work for me. The default system colour for 
     'treeview text is COLOR_WINDOWTEXT. 
     If clrref = -1 Then 
     clrref = GetSysColor(COLOR_WINDOWTEXT) ' Or &HFFFFFF 
     End If 
     
     'one way or another, pass it back 
     GetTVForeColour = clrref 
     
    End Function 
<END>    

⌨️ 快捷键说明

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