|
-
Aug 12th, 2000, 04:34 AM
#1
Thread Starter
Addicted Member
How do I change the background colour and foreground/text colour of a treeview?
I'm a newbie at TV's, and I have no idea what to do!
-Git
[Edited by git on 08-12-2000 at 05:41 AM]
-
Aug 12th, 2000, 04:40 AM
#2
Thread Starter
Addicted Member
Btw, how can you disable the keyboard in a TreeView, and how do you make a keyboard have an affect in a TreeView? Eg. when you click a node you use _Click, how do you do that so say if you use the arrows and enter/space or whatever it has an effect?
-
Aug 12th, 2000, 12:35 PM
#3
Here's how you can change the color of TreeView. Add Common Dialog control and 2 buttons: cmdBackColor and cmdForeColor, then copy this code:
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'treeview styles
Private Const TVS_HASLINES As Long = 2
Private Const TV_FIRST As Long = &H1100
Private Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29)
Private Const TVM_SETTEXTCOLOR As Long = (TV_FIRST + 30)
Public Enum enumTreePartColor
BACK_COLOR = 1
FORE_COLOR = 2
End Enum
Public Sub SetTreeColor(pTreeView As TreeView, pTreePart As enumTreePartColor)
Dim lngStyle As Long
Dim intTreePart As Integer
If pTreePart = BACK_COLOR Then
intTreePart = TVM_SETBKCOLOR
Else
intTreePart = TVM_SETTEXTCOLOR
End If
With pTreeView
Call SendMessage(.hwnd, intTreePart, 0, ByVal pColor)
lngStyle = GetWindowLong(.hwnd, GWL_STYLE)
If lngStyle And TVS_HASLINES Then
Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle Xor TVS_HASLINES)
Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle)
End If
End With
End Sub
Private Sub cmdBackColor_Click()
Dim lngColor As Long
On Error Resume Next
With CommonDialog1
.CancelError = True
.ShowColor
If Err.Number = cdlCancel Then Exit Sub
lngColor = .Color
End With
SetTreeColor TreeView1, BACK_COLOR
End Sub
Private Sub cmdForeColor_Click()
Dim lngColor As Long
Dim lngStyle As Long
On Error Resume Next
With CommonDialog1
.CancelError = True
.ShowColor
If Err.Number = cdlCancel Then Exit Sub
lngColor = .Color
End With
SetTreeColor TreeView1, FORE_COLOR
End Sub
Private Sub Form_Load()
Dim nodParent As Node
Dim intParent As Integer
Dim intChild As Integer
With TreeView1.Nodes
For intParent = 1 To 3
Set nodParent = .Add(, , "Parent" & intParent, "Parent" & intParent)
For intChild = 1 To 5
.Add nodParent, tvwChild, , "ChildNode " & intChild
Next
Next
End With
End Sub
-
Aug 13th, 2000, 12:06 AM
#4
Thread Starter
Addicted Member
It doesn't work. On this line :
Private Const TV_FIRST As Long = &H 1100
It says Expected: Expression and highlights the =.
Any idea what's wrong?
Thanks,
-Git
-
Aug 13th, 2000, 12:24 AM
#5
Vb-World's has bug: The 0's don't always come out correct, just put the 1100 by the H.
Just fix it and everything should be okay.
-
Aug 16th, 2000, 11:17 PM
#6
Thread Starter
Addicted Member
I finally got around to trying this out. There's one problem though, it says 'pColor' isn't defined.
Any ideas? Thanks.
Oh, and how would I pick a colour in the code (in RGB if possible), rather than getting a window pop up?
-Git
-
Aug 17th, 2000, 09:26 AM
#7
Oops, I forgot to include pColor as a parameter (this happens when I type directly into the post):
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'treeview styles
Private Const TVS_HASLINES As Long = 2
Private Const TV_FIRST As Long = &H1100
Private Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29)
Private Const TVM_SETTEXTCOLOR As Long = (TV_FIRST + 30)
Public Enum enumTreePartColor
BACK_COLOR = 1
FORE_COLOR = 2
End Enum
Public Sub SetTreeColor(pTreeView As TreeView, pTreePart As enumTreePartColor, pColor As OLE_COLOR)
Dim lngStyle As Long
Dim intTreePart As Integer
If pTreePart = BACK_COLOR Then
intTreePart = TVM_SETBKCOLOR
Else
intTreePart = TVM_SETTEXTCOLOR
End If
With pTreeView
Call SendMessage(.hwnd, intTreePart, 0, ByVal pColor)
lngStyle = GetWindowLong(.hwnd, GWL_STYLE)
If lngStyle And TVS_HASLINES Then
Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle Xor TVS_HASLINES)
Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle)
End If
End With
End Sub
Private Sub cmdBackColor_Click()
Dim lngColor As Long
On Error Resume Next
With CommonDialog1
.CancelError = True
.ShowColor
If Err.Number = cdlCancel Then Exit Sub
lngColor = .Color
End With
SetTreeColor TreeView1, BACK_COLOR, lngColor
End Sub
Private Sub cmdForeColor_Click()
Dim lngColor As Long
Dim lngStyle As Long
On Error Resume Next
With CommonDialog1
.CancelError = True
.ShowColor
If Err.Number = cdlCancel Then Exit Sub
lngColor = .Color
End With
SetTreeColor TreeView1, FORE_COLOR, lngColor
End Sub
Private Sub Form_Load()
Dim nodParent As Node
Dim intParent As Integer
Dim intChild As Integer
With TreeView1.Nodes
For intParent = 1 To 3
Set nodParent = .Add(, , "Parent" & intParent, "Parent" & intParent)
For intChild = 1 To 5
.Add nodParent, tvwChild, , "ChildNode " & intChild
Next
Next
End With
End Sub
-
Aug 18th, 2000, 12:48 AM
#8
Thread Starter
Addicted Member
Now it says GWL_STYLE isn't defined ... =/
Please help.
Thanks!
-Git
-
Aug 18th, 2000, 01:01 AM
#9
Guru
Put this somewhere in the General Declarations area:
Code:
Private Const GWL_STYLE = (-16)
You may get other non-defined things but come on, really...
This is a TreeView! And it requires going to the Components window (OH THE HORROR)!
So nobody really feels like actually trying out this code in VB. 
As for the no-popup-window and setting the color directly in the code, replace these subs:
Code:
Private Sub cmdBackColor_Click()
Dim lngColor As Long
On Error Resume Next
With CommonDialog1
.CancelError = True
.ShowColor
If Err.Number = cdlCancel Then Exit Sub
lngColor = .Color
End With
SetTreeColor TreeView1, BACK_COLOR, lngColor
End Sub
Private Sub cmdForeColor_Click()
Dim lngColor As Long
Dim lngStyle As Long
On Error Resume Next
With CommonDialog1
.CancelError = True
.ShowColor
If Err.Number = cdlCancel Then Exit Sub
lngColor = .Color
End With
SetTreeColor TreeView1, FORE_COLOR, lngColor
End Sub
With these:
Code:
Private Sub cmdBackColor_Click()
Dim lngColor As Long
lngColor = RGB(50, 100, 200) ' BackColor
SetTreeColor TreeView1, BACK_COLOR, lngColor
End Sub
Private Sub cmdForeColor_Click()
Dim lngColor As Long
lngColor = RGB(200, 100, 50) ' ForeColor
SetTreeColor TreeView1, FORE_COLOR, lngColor
End Sub
-
Aug 18th, 2000, 01:28 AM
#10
Thread Starter
Addicted Member
Thank you!!! =D
Works perfectly!
Cya.
-Git
-
May 31st, 2002, 09:17 PM
#11
Thread Starter
Addicted Member
-
May 31st, 2002, 10:55 PM
#12
Stuck in the 80s
Just goes to show you that in two years span, we're still asking the same questions
-
May 31st, 2002, 11:19 PM
#13
I couldn't understand why my foreground color wouldn't change so I searched the web and found some similar code on VBNet (which also allows bolding) . When I ran that code it had the same problem. When I read the article that went along with that code it explained what the problem is. Here is what the article says.
In developing the VB6 common controls, the VB development team stopped using comctl32.dll as its basis for functionality. Instead, the team took the unprecedented steps of removing the VB OCX dependency upon comctl32.dll, and opted (wrongly in this author's view) to rewrite the functionality available in comctl32.dll v4.72 as the new VB6 mscomctl.ocx. This redesigned OCX is not dependant on the original Windows' common control dll, and no longer relies on it for any functionality. And in creating the OCX, its implementation has been tweaked to the point where some methods that do work against the VB5 common control implementation (which uses comctl32.dll) no longer function as expected using the VB6 common control (mscomctl.ocx).
The TreeView BackColor and ForeColor methods detailed here do not work the same under the VB6 mscomctl.dll-version of the common controls. Specifically, the ForeColor method fails to recolor all nodes; only the node losing focus exhibits the changed colour, momentarily, then reverts to the default WindowText colour. The BackgroundColor method will not colour the background area of the icon and text. FullRowSelect is available in the VB6-based control as a property; the API implementation shown here will not work correctly against a coloured background. Font bolding, thankfully, still works as indicated.
So I'm still looking for code that does the foreground color of the VB6 control. Does anyone have any?
-
May 31st, 2002, 11:32 PM
#14
Stuck in the 80s
You could probably extract some code from this project:
http://www.planet-source-code.com/vb...25049&lngWId=1
But I've always done ForeColor this way:
VB Code:
Private Sub Form_Load()
Dim iNodes As Integer, nodX As Node
For iNodes = 1 To 10
Set nodX = TreeView1.Nodes.Add(, , , "Node #" & iNodes)
nodX.ForeColor = vbBlue
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|