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);
}
}
}
}