Owner draw TreeView?
-
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?
-
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?
The
TreeView
class is simply a wrapper around the Win32 TreeView control. You could simply subclass theTreeView
and add your P/Invoke statements there. Depending on what you want to do, you could do something as simple as the Win32 functionDrawText
. 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 theDrawString
method. It all depends on what you are doing, this should get you started. - Nick Parker
My Blog | My Articles -
The
TreeView
class is simply a wrapper around the Win32 TreeView control. You could simply subclass theTreeView
and add your P/Invoke statements there. Depending on what you want to do, you could do something as simple as the Win32 functionDrawText
. 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 theDrawString
method. It all depends on what you are doing, this should get you started. - Nick Parker
My Blog | My ArticlesThanks 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?)