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. Derived Treeview override not being called at all.

Derived Treeview override not being called at all.

Scheduled Pinned Locked Moved C#
graphicsdebugging
7 Posts 4 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.
  • P Offline
    P Offline
    pr1mem0ver
    wrote on last edited by
    #1

    I have the following treeview class whose purpose is to draw an overlay on the icons in a node. Notice it overrides "OnDrawNode":

    /// /// A method that is supplied to the OverlayTreeView class to retrieve the overlay for a particular node
    ///
    /// The node being drawn
    ///
    public delegate Image GetTreeIconOverlayMethod(TreeNode node);

    public class OverlayTreeView : TreeView
    {
    bool drawOverlay;
    int iconHeight;
    int iconWidth;

    public GetTreeIconOverlayMethod GetIconOverlay { get; set; }
    
    public OverlayTreeView() { }
    protected override void OnHandleCreated(EventArgs e)
    {
    	base.OnHandleCreated(e);
    	if (ImageList != null)
    	{
    		drawOverlay = true;
    		iconHeight = ImageList.ImageSize.Height;
    		iconWidth = ImageList.ImageSize.Width;
    	}
    }
    
    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
    	base.OnDrawNode(e);
    
    	Image overlay = null;
    	if (drawOverlay)
    		if (GetIconOverlay != null)
    			if (!((e.Node.ImageIndex == -1) && (e.Node.ImageKey == null)))
    				overlay = GetIconOverlay(e.Node);
    	if (overlay != null)
    	{
    		int x = e.Node.Bounds.X + iconWidth - overlay.Width;
    		int y = e.Node.Bounds.Y + iconHeight - overlay.Height;
    		e.Graphics.DrawImage(overlay, x, y);
    	}
    }
    

    }

    The overlay is not being drawn and it seems this is because the override is not being called at all. Any breakpoint put in this method does NOT gte triggered even though a breakpoint in OnHandleCreated is. It doesn't matter where I put the breakpoint within the method. (And yes... symbols are loaded). I have no idea why this would occur...

    Richard DeemingR B 2 Replies Last reply
    0
    • P pr1mem0ver

      I have the following treeview class whose purpose is to draw an overlay on the icons in a node. Notice it overrides "OnDrawNode":

      /// /// A method that is supplied to the OverlayTreeView class to retrieve the overlay for a particular node
      ///
      /// The node being drawn
      ///
      public delegate Image GetTreeIconOverlayMethod(TreeNode node);

      public class OverlayTreeView : TreeView
      {
      bool drawOverlay;
      int iconHeight;
      int iconWidth;

      public GetTreeIconOverlayMethod GetIconOverlay { get; set; }
      
      public OverlayTreeView() { }
      protected override void OnHandleCreated(EventArgs e)
      {
      	base.OnHandleCreated(e);
      	if (ImageList != null)
      	{
      		drawOverlay = true;
      		iconHeight = ImageList.ImageSize.Height;
      		iconWidth = ImageList.ImageSize.Width;
      	}
      }
      
      protected override void OnDrawNode(DrawTreeNodeEventArgs e)
      {
      	base.OnDrawNode(e);
      
      	Image overlay = null;
      	if (drawOverlay)
      		if (GetIconOverlay != null)
      			if (!((e.Node.ImageIndex == -1) && (e.Node.ImageKey == null)))
      				overlay = GetIconOverlay(e.Node);
      	if (overlay != null)
      	{
      		int x = e.Node.Bounds.X + iconWidth - overlay.Width;
      		int y = e.Node.Bounds.Y + iconHeight - overlay.Height;
      		e.Graphics.DrawImage(overlay, x, y);
      	}
      }
      

      }

      The overlay is not being drawn and it seems this is because the override is not being called at all. Any breakpoint put in this method does NOT gte triggered even though a breakpoint in OnHandleCreated is. It doesn't matter where I put the breakpoint within the method. (And yes... symbols are loaded). I have no idea why this would occur...

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The OnDrawNode method is used by the base class to raise the DrawNode event. According to the documentation[^], that event is only raised if "the DrawMode property is set to a TreeViewDrawMode value other than Normal." It's therefore safe to assume that the OnDrawNode method won't be called if DrawMode is set to Normal.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The OnDrawNode method is used by the base class to raise the DrawNode event. According to the documentation[^], that event is only raised if "the DrawMode property is set to a TreeViewDrawMode value other than Normal." It's therefore safe to assume that the OnDrawNode method won't be called if DrawMode is set to Normal.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        P Offline
        P Offline
        primem0ver
        wrote on last edited by
        #3

        So what method would I override to add the overlay no matter what draw mode is being used? I can't find one, except perhaps OnPaint... but I would think Microsoft would have included a default drawnode method to use regardless of the draw method. Since the point of my class is to add the overlay, I wouldn't want a potential user of the class to have to specify a draw mode. (I know there are workarounds... but to me forcing us to use them doesn't make a whole lot of sense to me, especially considering the complexity of drawing a tree node).

        Richard DeemingR 1 Reply Last reply
        0
        • P primem0ver

          So what method would I override to add the overlay no matter what draw mode is being used? I can't find one, except perhaps OnPaint... but I would think Microsoft would have included a default drawnode method to use regardless of the draw method. Since the point of my class is to add the overlay, I wouldn't want a potential user of the class to have to specify a draw mode. (I know there are workarounds... but to me forcing us to use them doesn't make a whole lot of sense to me, especially considering the complexity of drawing a tree node).

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          There isn't one. The method is called in response to an unmanaged WM_ message, which will only be sent to the control if it's set to be owner-drawn. Windows Forms controls are thin wrappers around native Windows controls. They can't add functionality that the native controls don't support.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          P B 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            There isn't one. The method is called in response to an unmanaged WM_ message, which will only be sent to the control if it's set to be owner-drawn. Windows Forms controls are thin wrappers around native Windows controls. They can't add functionality that the native controls don't support.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            P Offline
            P Offline
            primem0ver
            wrote on last edited by
            #5

            Alright. Thanks

            1 Reply Last reply
            0
            • P pr1mem0ver

              I have the following treeview class whose purpose is to draw an overlay on the icons in a node. Notice it overrides "OnDrawNode":

              /// /// A method that is supplied to the OverlayTreeView class to retrieve the overlay for a particular node
              ///
              /// The node being drawn
              ///
              public delegate Image GetTreeIconOverlayMethod(TreeNode node);

              public class OverlayTreeView : TreeView
              {
              bool drawOverlay;
              int iconHeight;
              int iconWidth;

              public GetTreeIconOverlayMethod GetIconOverlay { get; set; }
              
              public OverlayTreeView() { }
              protected override void OnHandleCreated(EventArgs e)
              {
              	base.OnHandleCreated(e);
              	if (ImageList != null)
              	{
              		drawOverlay = true;
              		iconHeight = ImageList.ImageSize.Height;
              		iconWidth = ImageList.ImageSize.Width;
              	}
              }
              
              protected override void OnDrawNode(DrawTreeNodeEventArgs e)
              {
              	base.OnDrawNode(e);
              
              	Image overlay = null;
              	if (drawOverlay)
              		if (GetIconOverlay != null)
              			if (!((e.Node.ImageIndex == -1) && (e.Node.ImageKey == null)))
              				overlay = GetIconOverlay(e.Node);
              	if (overlay != null)
              	{
              		int x = e.Node.Bounds.X + iconWidth - overlay.Width;
              		int y = e.Node.Bounds.Y + iconHeight - overlay.Height;
              		e.Graphics.DrawImage(overlay, x, y);
              	}
              }
              

              }

              The overlay is not being drawn and it seems this is because the override is not being called at all. Any breakpoint put in this method does NOT gte triggered even though a breakpoint in OnHandleCreated is. It doesn't matter where I put the breakpoint within the method. (And yes... symbols are loaded). I have no idea why this would occur...

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              Works for me: Override 'OnCreateControl and set the 'TreeViewDrawMode Property ... with either 'DrawMode == 'OwnerDrawText or 'OwnerDrawAll: 'OnDrawNode will be called.

              protected override void OnCreateControl()
              {{

              if (DrawMode != TreeViewDrawMode.OwnerDrawText)
              {
                  DrawMode = TreeViewDrawMode.OwnerDrawText;
              }
              
              base.OnCreateControl();
              

              }

              «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                There isn't one. The method is called in response to an unmanaged WM_ message, which will only be sent to the control if it's set to be owner-drawn. Windows Forms controls are thin wrappers around native Windows controls. They can't add functionality that the native controls don't support.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                Richard Deeming wrote:

                There isn't one.

                See my answer below for a solution.

                «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

                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