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. Owner draw TreeView?

Owner draw TreeView?

Scheduled Pinned Locked Moved C#
questiondata-structuresjsonhelp
3 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.
  • M Offline
    M Offline
    misterbear
    wrote on last edited by
    #1

    I use an ordinary tree view inside a dialog, but I wish to write a text saying "No data available" if the treeview (which is filled with items loaded from files) is empty. Instead of just adding a node saying "No Data Available", which doesn't look very good, I'd like to set a flag in the control (The control is a very thin wrapper for the API's treeview) and when that flag is set draw the string centered in the treeview instead. I found somewhere on google that this was only possible using Win32 messages with p/invoke. My question is, how do I do this? I get the handle of the control and send some kind of paint message? I haven't been into painting in WIN32 very much... can anyone help me?

    N 1 Reply Last reply
    0
    • M misterbear

      I use an ordinary tree view inside a dialog, but I wish to write a text saying "No data available" if the treeview (which is filled with items loaded from files) is empty. Instead of just adding a node saying "No Data Available", which doesn't look very good, I'd like to set a flag in the control (The control is a very thin wrapper for the API's treeview) and when that flag is set draw the string centered in the treeview instead. I found somewhere on google that this was only possible using Win32 messages with p/invoke. My question is, how do I do this? I get the handle of the control and send some kind of paint message? I haven't been into painting in WIN32 very much... can anyone help me?

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      The TreeView class is simply a wrapper around the Win32 TreeView control. You could simply subclass the TreeView and add your P/Invoke statements there. Depending on what you want to do, you could do something as simple as the Win32 function DrawText. The P/Invoke signature looks like this:

      [DllImport("user32.dll")]
      static extern int DrawText(IntPtr hDC, string lpString, int nCount,
      ref RECT lpRect, uint uFormat);

      Also, you could just use the Graphics object and call the DrawString method. It all depends on what you are doing, this should get you started. - Nick Parker
      My Blog | My Articles

      M 1 Reply Last reply
      0
      • N Nick Parker

        The TreeView class is simply a wrapper around the Win32 TreeView control. You could simply subclass the TreeView and add your P/Invoke statements there. Depending on what you want to do, you could do something as simple as the Win32 function DrawText. The P/Invoke signature looks like this:

        [DllImport("user32.dll")]
        static extern int DrawText(IntPtr hDC, string lpString, int nCount,
        ref RECT lpRect, uint uFormat);

        Also, you could just use the Graphics object and call the DrawString method. It all depends on what you are doing, this should get you started. - Nick Parker
        My Blog | My Articles

        M Offline
        M Offline
        misterbear
        wrote on last edited by
        #3

        Thanks for the advice, got it working. What I did was to override the wndproc and catch the WM_PAINT message, upon which a call to CreateGraphics() let me paint what I want. What I tried originally was overriding the OnPaint method, but that didn't work, never got called I think..? (why?)

        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