TreeView Nonsense

J

Jon Slaughter

I'm trying to override the default paiting of a treeview but I get bad
results:

My code(well, atleast this is the most simple that reproduces the result) is
this
private void TVDrawNode(Object sender, DrawTreeNodeEventArgs e)
{
e.Graphics.DrawString(e.Node.Text, e.Node.TreeView.Font, Brushes.Black,
e.Bounds.Left, e.Bounds.Top);
}

My code to turn this on is
DirTView.DrawMode = TreeViewDrawMode.OwnerDrawAll;
DirTView.DrawNode += new
DrawTreeNodeEventHandler((DrawTreeNodeEventHandler)TVDrawNode);

Now the problem is that the method TVDrawNode gets called many times to
print the nodes more than once. Sometimes its called with the correct Bounds
and other times Bounds is 0 resulting the the Text for each node being
printed at the upper left corner resulting in a mishmash of text. It does
print correctly though the "second" time through and it only seems to do
this on grandchildren. (I have a parent, a child and an several
grandchildren.)


When I expand the child is when it occures and the first row contains all
the text's for all the nodes while the rest of the nodes are fine. This
means its getting called to draw the node more than once and one of those
times the Bounds is not set properly.

What the heck is going on? Its very frustrating doing this stuff because I
can't seem to find any help on the subject ;/ MSDN is pitiful at describing
how these things work... atleast I can't find anything there that goes into
any more detail than just the descriptions of methods and fields.

Is there something I'm obviously doing wrong?

(I'm not creating a custom treeview control either but just overriding the
"paint" method for the nodes in my app)

Thanks,
Jon
 
J

Jon Slaughter

Jon Slaughter said:
I'm trying to override the default paiting of a treeview but I get bad
results:

My code(well, atleast this is the most simple that reproduces the result)
is this
private void TVDrawNode(Object sender, DrawTreeNodeEventArgs e)
{
e.Graphics.DrawString(e.Node.Text, e.Node.TreeView.Font, Brushes.Black,
e.Bounds.Left, e.Bounds.Top);
}

My code to turn this on is
DirTView.DrawMode = TreeViewDrawMode.OwnerDrawAll;
DirTView.DrawNode += new
DrawTreeNodeEventHandler((DrawTreeNodeEventHandler)TVDrawNode);

Now the problem is that the method TVDrawNode gets called many times to
print the nodes more than once. Sometimes its called with the correct
Bounds and other times Bounds is 0 resulting the the Text for each node
being printed at the upper left corner resulting in a mishmash of text. It
does print correctly though the "second" time through and it only seems to
do this on grandchildren. (I have a parent, a child and an several
grandchildren.)


When I expand the child is when it occures and the first row contains all
the text's for all the nodes while the rest of the nodes are fine. This
means its getting called to draw the node more than once and one of those
times the Bounds is not set properly.

What the heck is going on? Its very frustrating doing this stuff because I
can't seem to find any help on the subject ;/ MSDN is pitiful at
describing how these things work... atleast I can't find anything there
that goes into any more detail than just the descriptions of methods and
fields.

Is there something I'm obviously doing wrong?

(I'm not creating a custom treeview control either but just overriding the
"paint" method for the nodes in my app)

Thanks,
Jon

Strange thing that when I minimize the window and bring it back up
everything works fine ;/
 
P

PS

Jon Slaughter said:
I'm trying to override the default paiting of a treeview but I get bad
results:

My code(well, atleast this is the most simple that reproduces the result)
is this
private void TVDrawNode(Object sender, DrawTreeNodeEventArgs e)
{
e.Graphics.DrawString(e.Node.Text, e.Node.TreeView.Font, Brushes.Black,
e.Bounds.Left, e.Bounds.Top);
}

Does this help?

When using OwnerDrawAll, however, the DrawTreeNodeEventArgs.Bounds property
encompasses the entire width of the TreeView. In this case, you can access
the hit test region by getting the DrawTreeNodeEventArgs.Node value and
accessing its TreeNode.Bounds property.
 
J

Jon Slaughter

PS said:
Does this help?

When using OwnerDrawAll, however, the DrawTreeNodeEventArgs.Bounds
property encompasses the entire width of the TreeView. In this case, you
can access the hit test region by getting the DrawTreeNodeEventArgs.Node
value and accessing its TreeNode.Bounds property.

I've tried the TreeNode.Bounds on the e.Nodes and its the same thing except
the indention changes.

i.e., using e.Node.Bounds instead of e.Bounds. Same result except
indentation is alreadly done for me. I think that is what you are getting at
though. Remember, it works fine once I hide the window and bring it back up
;/

Thanks,
Jon
 
P

PS

Jon Slaughter said:
I've tried the TreeNode.Bounds on the e.Nodes and its the same thing
except the indention changes.

i.e., using e.Node.Bounds instead of e.Bounds. Same result except
indentation is alreadly done for me. I think that is what you are getting
at though. Remember, it works fine once I hide the window and bring it
back up

It seems that some hidden nodes upon scrolling or expanding into view
initially have e.Node.Bounds equal to 0 for X, Y, Height and Width. You
should check that these values are not zero before drawing the node.

PS
 
J

Jon Slaughter

PS said:
It seems that some hidden nodes upon scrolling or expanding into view
initially have e.Node.Bounds equal to 0 for X, Y, Height and Width. You
should check that these values are not zero before drawing the node.

Yes, they are sometimes zero but why? Why would the draw routine be called
like that? It would be nice if there was some information on the behavior of
these controls. MSDN does a piss-poor job of describing the workings of
them.

I suppose what might be happening is that the very first time the control is
displayed the TreeView control is not setting up the Bounds properly or
something like that? I really have no idea though ;/ I will try what you
sugggest and see if that fixes(it definately will but might break something
else).

Thanks,
Jon
 
P

PS

Yes, they are sometimes zero but why? Why would the draw routine be called
like that? It would be nice if there was some information on the behavior
of these controls. MSDN does a piss-poor job of describing the workings of
them.

I suppose what might be happening is that the very first time the control
is displayed the TreeView control is not setting up the Bounds properly or
something like that? I really have no idea though ;/ I will try what you
sugggest and see if that fixes(it definately will but might break
something else).

I don't think it will break anything. When you do something like expand a
node it raises an event to say "this node needs to be drawn". The node may
not of determined it's bounds yet hence the zero values. It could be a race
condition with different events. Sometimes the node has determined it's
bounds when the draw node event fires, sometimes it hasn't.

PS
 
J

Jon Slaughter

PS said:
I don't think it will break anything. When you do something like expand a
node it raises an event to say "this node needs to be drawn". The node may
not of determined it's bounds yet hence the zero values. It could be a
race condition with different events. Sometimes the node has determined
it's bounds when the draw node event fires, sometimes it hasn't.

yeah, it works.

I agree with that but I don't understand why it it would generate those
messages in the first place? Its basically telling us to redraw the nodes
twice but that seems like a waste of messages? I just don't like the idea of
not knowing whats going on for sure. I would wish for good documentation by
those that actually implemented it but that won't happen ;/

If the whole reason I was doing this is because there is no way to have a
checkbox on a per node basis ;/ So if MS screwed up here they could screw up
in other places ;/ (and knowing MS's past history ;/

The old MS-help has very good descriptions of these processes and describes
almost all the inner workings but the newer helps don't give anything like
this.

Thanks,
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top