views:

71

answers:

2

I have been doing some research and this seems to be not possible unless I start digging into InterOperabilty, i.e PInvoke and what not which is not really my kettle fish. I am re posting this question as I want to know if anyone has managed to do this yet?

I use .png for all my images and get a professional to provide my images so I know the images come with the best quality most appropriate formats.

It seems the standard tree view control does not support a background image directly a such neither does it allow its background colour to be set to transparent? Anyone got any ideas on these two?

+2  A: 

If you're willing to use a third-party library take a look at http://objectlistview.sourceforge.net/cs/index.html - note however that it's GPL. It's easy to set the background images there.

romkyns
this might very lazy of me but how much does it differ from the TreeView control ?? i.m can you quickley swap it out just for trialing purposes and see how it looks and feels and etc?
IbrarMumtaz
that looks like an amazing little control but I have spent countless hours and time building my application around the TreeView control.That bloke who built that control looks like he could have a small publicaiton published on it : | it's soo good but I can't afford to change the core feature of my application all because of one control. DAMN BACKGROUND IMAGE !!! Spent sooo long in photoshop building it also ????
IbrarMumtaz
@IbrarMumtaz - yeah, that's a shame. I'm afraid I don't know how else to set the background of a TreeView control.
romkyns
Chosen this as a solution, as the answer is to replace the control.
IbrarMumtaz
+1  A: 

This is possible by overriding WndProc() and catch the WM_ERASEBKGND message. The control shown below does this. You will however quickly find out why the Windows Forms TreeView class doesn't do this. With the "smooth scrolling" system option turned on, you get very ugly artifacts. Not mentioning the lack of node text transparency. No, there's no fix for that, only a complete replacement of the control that doesn't depend on the native Windows control can solve this problem. Not something you should normally consider, unless it is from a very reputable component vendor.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyTreeView : TreeView {
    private Image mImage;
    public Image Image {
        get { return mImage; }
        set { mImage = value; Invalidate(); }
    }
    protected override void OnAfterCollapse(TreeViewEventArgs e) {
        if (mImage != null) Invalidate();
        base.OnAfterCollapse(e);
    }
    protected override void OnAfterExpand(TreeViewEventArgs e) {
        if (mImage != null) Invalidate();
        base.OnAfterExpand(e);
    }
    protected override void WndProc(ref Message m) {
        base.WndProc(ref m);
        if (m.Msg == 0x14 && mImage != null) {
            using (var gr = Graphics.FromHdc(m.WParam)) {
                gr.DrawImage(mImage, Point.Empty);
            }
        }
    }
}
Hans Passant
thanks for this .... its well worth a look in if I get a chance.
IbrarMumtaz
but how to make the node background as some????