Custom Treeview with Overridden OnPaint & WndProc
-
I am attempting to do a custom OnPaint on a custom Treeview with a lot of bumps along the way. At this point in the game, I'm just trying to get a purple rectangle to print out anywhere involving this TreeView. I'll work on the bounds afterwards. I found out the long way that WndProc turns off OnPaint and am trying to now to override WndProc, but I have absolutely no idea what to pass to OnPaint seeing this is my first attempt at GDI and from all the examples I've been looking at OnPaint appears to be called implicitly by other Win Forms. Please point me in the right direction. public class myProfileTree : System.Windows.Forms.TreeView { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush brush = new SolidBrush(Color.MediumOrchid); Rectangle blueRectangle = new Rectangle(50, 50, 200, 100); g.FillRectangle(brush, blueRectangle); } protected override void WndProc(ref Message m) { switch (m.Msg) { case(15): { OnPaint( :confused: ); break; } default: { break; } } base.WndProc(ref m); } }
-
I am attempting to do a custom OnPaint on a custom Treeview with a lot of bumps along the way. At this point in the game, I'm just trying to get a purple rectangle to print out anywhere involving this TreeView. I'll work on the bounds afterwards. I found out the long way that WndProc turns off OnPaint and am trying to now to override WndProc, but I have absolutely no idea what to pass to OnPaint seeing this is my first attempt at GDI and from all the examples I've been looking at OnPaint appears to be called implicitly by other Win Forms. Please point me in the right direction. public class myProfileTree : System.Windows.Forms.TreeView { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush brush = new SolidBrush(Color.MediumOrchid); Rectangle blueRectangle = new Rectangle(50, 50, 200, 100); g.FillRectangle(brush, blueRectangle); } protected override void WndProc(ref Message m) { switch (m.Msg) { case(15): { OnPaint( :confused: ); break; } default: { break; } } base.WndProc(ref m); } }
As obvious as it seems, have you set the DrawMode to: TreeViewDrawMode.OwnerDrawAll?
The hurrier I go, the behinder I get.
-
As obvious as it seems, have you set the DrawMode to: TreeViewDrawMode.OwnerDrawAll?
The hurrier I go, the behinder I get.
Not entirely sure that is the direction I want to be taking. This just appears to erase my entire custom treeview with no call to OnPaint. I'm trying to keep the way the rest of the nodes aside from a selected node to look the norm. I need to keep the selected node the focus color as buttons are pressed even though this goes against standard convention. The thought was to make a custom treeview with custom onpaint to repaint this one node with a rectangle using onpaint and then putting back the original text on top. Then I read this: http://www.codeproject.com/Messages/685886/Re-Custom-Tree-View-Painting.aspx and came to the overriding WndProc, but was unsure on how to procede with the call to OnPaint.
-
Not entirely sure that is the direction I want to be taking. This just appears to erase my entire custom treeview with no call to OnPaint. I'm trying to keep the way the rest of the nodes aside from a selected node to look the norm. I need to keep the selected node the focus color as buttons are pressed even though this goes against standard convention. The thought was to make a custom treeview with custom onpaint to repaint this one node with a rectangle using onpaint and then putting back the original text on top. Then I read this: http://www.codeproject.com/Messages/685886/Re-Custom-Tree-View-Painting.aspx and came to the overriding WndProc, but was unsure on how to procede with the call to OnPaint.
I need to keep the selected node the focus color
If that's all your after then set the: "HideSelection" property to false. This setting allows you to retain visibility of the selected node once the treeview has lost focus. If you still want your own paint, just manualy set the node's properties: tv.SelectedNode.BackColor = SystemColors.Highlight; ...
The hurrier I go, the behinder I get.
-
I need to keep the selected node the focus color
If that's all your after then set the: "HideSelection" property to false. This setting allows you to retain visibility of the selected node once the treeview has lost focus. If you still want your own paint, just manualy set the node's properties: tv.SelectedNode.BackColor = SystemColors.Highlight; ...
The hurrier I go, the behinder I get.
HideSelection will not work for this instance and changing the BackColor will not address the issue I have. My button moves the selected node up and down. Since it is no longer the focus, it turns from medium blue to light gray. I did however figure out how to override WndProc: protected override void WndProc(ref Message m) { base.WndProc(ref m); switch (m.Msg) { case (15): { PaintEventArgs pae = new PaintEventArgs( Graphics.FromHwnd((m.HWnd)), new Rectangle(0, 0, this.Width, this.Height)); OnPaint(pae); break; } default: { break; } } } Thanks for trying!