TreeNode bounds are 0,0,0,0

T

Tim_Mac

hi,
i am trying to draw an alpha effect on top of the icon for a treenode.
here is my code:

node.ImageIndex = 9;
ImageAttributes ia = new ImageAttributes();
ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = 0.5F; //50% transparency
ia.SetColorMatrix(cm);

using(Graphics g = CreateGraphics())
{
Bitmap icon = this.ImageList.Images[9] as Bitmap;
g.Clear(this.BackColor);
g.DrawImage(icon, node.Bounds, 0, 0, icon.Width, icon.Height,
GraphicsUnit.Pixel, ia);
}

nothing is different when i run this, and when i debug, node.Bounds has
X=0, Y=0, Height=0, Width=0.
does anyone know why? the 'node' variable is a valid treenode object.

thanks
tim
 
J

Jeffrey Tan[MSFT]

Hi Tim,

Thanks for your post.

I am not sure where does this code snippet executed. I have tried it in
TreeView.BeforeSelect and other methods, TreeView.Bounds will return the
correct values.

Can you provide a little sample project for us to reproduce your problem?
Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tim_Mac

hi Allen,
thanks for the reply.
it is attached to the tree, but i'm accessing the nodes recursively through
their parent TreeNode, so maybe the location context is not following on
from there.
i think i'm going to give up on the idea. i found out that it's a bad idea
to use CreateGraphics to do custom painting, because the Paint event is
going to override it. so i'll just have to stick with an imagelist
containing every combination of images, assigning them in a cumbersome
if/else statement.

thanks anyway
tim

--------------------------
blog: http://tim.mackey.ie

Allen Anderson said:
Did you attach the node to the tree before you tried to see the bounds
member? For instance, if you just create your own treenode without
adding it to the Nodes collection, it will of course not have any
bounds in and of itself because there is no context.

-Allen
http://www.glacialcomponents.com
http://allenanderson.blogspot.com

hi,
i am trying to draw an alpha effect on top of the icon for a treenode.
here is my code:

node.ImageIndex = 9;
ImageAttributes ia = new ImageAttributes();
ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = 0.5F; //50% transparency
ia.SetColorMatrix(cm);

using(Graphics g = CreateGraphics())
{
Bitmap icon = this.ImageList.Images[9] as Bitmap;
g.Clear(this.BackColor);
g.DrawImage(icon, node.Bounds, 0, 0, icon.Width, icon.Height,
GraphicsUnit.Pixel, ia);
}

nothing is different when i run this, and when i debug, node.Bounds has
X=0, Y=0, Height=0, Width=0.
does anyone know why? the 'node' variable is a valid treenode object.

thanks
tim
 
T

Tim_Mac

hi Jeffrey,
thanks for the reply. it seems to be to do with the way i build my
treenodes. i start with a root node, and add all the children to that. it
seems that unless the child nodes are added directly to the tree, they do
not get any bounds.

you can reproduce this with the following code:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web;

namespace Test
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.IContainer components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(232, 396);
this.treeView1.TabIndex = 0;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(232, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(304, 396);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(536, 396);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
TreeNode root = new TreeNode("Node1");
root.Nodes.Add(new TreeNode("Node2"));
root.Nodes.Add(new TreeNode("Node3"));
root.Nodes.Add(new TreeNode("Node4"));

this.treeView1.Nodes.Add(root);

foreach(TreeNode node in root.Nodes)
this.textBox1.Text += node.Bounds.ToString() + "\r\n";
}
}
}

i wouldn't have thought there was anything weird about my approach above
(which is simplified). recursion is used a lot with treeviews.

do i get a prize for finding a bug in the treeview? ;)

tim

--------------------------
blog: http://tim.mackey.ie

"Jeffrey Tan[MSFT]" said:
Hi Tim,

Thanks for your post.

I am not sure where does this code snippet executed. I have tried it in
TreeView.BeforeSelect and other methods, TreeView.Bounds will return the
correct values.

Can you provide a little sample project for us to reproduce your problem?
Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tim_Mac

p.s. note that if i reference the child nodes through the
treeview.Nodes collection instead of the local 'root' variable, the
result is the same, presumably because it is the same object.

foreach(TreeNode node in this.treeView1.Nodes[0].Nodes)
this.textBox1.Text += node.Bounds.ToString() + "\r\n";
// still shows 0,0,0,0 for each child node

tim
 
J

Jeffrey Tan[MSFT]

Hi Tim,

Thanks for your feedback.

Yes, with your sample code snippet, I can reproduce out your issue.
However, it is not a bug.

If we use Reflector to view System.Windows.Forms.TreeNode.get_Bounds, we
will see that it actually p/invoked SendMessage win32 API to send
TVM_GETITEMRECT(0x1104) message to TreeView control.

From the MSDN of "TVM_GETITEMRECT Message", we get this:
"If the item is visible and the bounding rectangle was successfully
retrieved, the return value is TRUE. Otherwise, the message returns FALSE
and does not retrieve the bounding rectangle."

So actually because the child nodes of the root nodes are all invisible,
this SendMessage call will fail with empty rectangle data returned.

If we called this.treeView1.ExpandAll() before TreeNode.Bounds checking
code, everything will work well with correct bound information returned.

private void Form1_Load(object sender, System.EventArgs e)
{
TreeNode root = new TreeNode("Node1");
root.Nodes.Add(new TreeNode("Node2"));
root.Nodes.Add(new TreeNode("Node3"));
root.Nodes.Add(new TreeNode("Node4"));

this.treeView1.Nodes.Add(root);

this.treeView1.ExpandAll();

foreach(TreeNode node in root.Nodes)
this.textBox1.Text += node.Bounds.ToString() + "\r\n";
}

Hope this helps.
=====================================

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Similar Threads


Top