Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Custom Treeview with Overridden OnPaint & WndProc

Custom Treeview with Overridden OnPaint & WndProc

Scheduled Pinned Locked Moved C#
graphicsgame-dev
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VengefulSakhmet
    wrote on last edited by
    #1

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

    R 1 Reply Last reply
    0
    • V VengefulSakhmet

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

      R Offline
      R Offline
      Richard Blythe
      wrote on last edited by
      #2

      As obvious as it seems, have you set the DrawMode to: TreeViewDrawMode.OwnerDrawAll?

      The hurrier I go, the behinder I get.

      V 1 Reply Last reply
      0
      • R Richard Blythe

        As obvious as it seems, have you set the DrawMode to: TreeViewDrawMode.OwnerDrawAll?

        The hurrier I go, the behinder I get.

        V Offline
        V Offline
        VengefulSakhmet
        wrote on last edited by
        #3

        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.

        R 1 Reply Last reply
        0
        • V VengefulSakhmet

          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.

          R Offline
          R Offline
          Richard Blythe
          wrote on last edited by
          #4

          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.

          V 1 Reply Last reply
          0
          • R Richard Blythe

            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.

            V Offline
            V Offline
            VengefulSakhmet
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups