TreeView DrawNode problem

  • Thread starter Thread starter John
  • Start date Start date
J

John

I have overridden the default DrawNode (OwnerDrawText mode) to allow me to
draw an extra icon at the front of the text block, while still using the
built-in icon list for another icon. The problem I am having is that when a
node is selected it snips the text to the length of the text minus the
length of the extra icon.

Is there a way to redefine the selection box size for each node?

The DrawNode code is as follows :

private void tvParts_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Brush fontbrush;
Icon ic;
Font font;

if (ilItemTypeIcons.Images.ContainsKey((e.Node as
itemTreeNode).itemtype_name))
ic = Icon.FromHandle(((Bitmap)ilItemTypeIcons.Images[(e.Node as
itemTreeNode).itemtype_name]).GetHicon());
else
ic =
Icon.FromHandle(((Bitmap)ilItemTypeIcons.Images["Default"]).GetHicon());

if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
fontbrush = System.Drawing.Brushes.White;
else
fontbrush = System.Drawing.Brushes.Black;

font = e.Node.NodeFont;
if (font == null)
font = e.Node.TreeView.Font;

e.Graphics.DrawIcon(ic, e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawString(e.Node.Text, font, fontbrush, ic.Width +
e.Bounds.X, e.Bounds.Y);

}
 
I think your problem is with the override of Graphics.DrawString you're
using. It would appear that since you only passing an x and y location it
uses the current clip region to clip the text.

Try calculating a rectangle and using the DrawString override that takes a
rectangle rather than an x and y.

See the example with the TreeView.DrawNode Event [1] docs on msdn for an
example of calculating that bounds rectangle.

[1]
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.drawnode.aspx
 
How about using a StateImageList instead? That way you can have two images
for every node

/claes
 

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

ComboBox Drawing Problem 3

Back
Top