Question

I have a treeview in a windows form. I recently added code to custom draw the text so that I can fiddle with placement, bold text, etc. Everything is great except it appears that when I make my changes the node bounds don't get updated so the horizontal scrollbar gets confused into thinking it doesn't need to be shown. I am guessing this is the case because when I click a node on tree I see a box that I think is the node bounds and it is too small.

In my OnDrawText method, I'd love to be able to change the Bounds on the Node member of DrawTreeNodeEventArgs, but it is readonly.

I've searched for a solution for a painfully long time now to no avail. Setting the Bounds of the treeview itself doesn't make the scrollbar appear. I am using TextRenderer.DrawText() in my OnDrawText method, if that makes a difference.

Any help greatly appreciated!

If anyone thinks seeing the code would help, I'll add it.

Was it helpful?

Solution

For the benefit of anyone else out there searching, I'll provide my hackorama (thanks Hans!) for reference.

When I build my tree initially, I now pad blanks onto the end of the .Text of the nodes that I will be modifying in my ...OnDrawText(object sender, DrawTreeNodeEventArgs e) method. In this method, I use TextRenderer.DrawText to customize the look of the node, however the rectangle that corresponds to the customized stuff doesn't get taken into account by whatever is figuring out my scrollbar size. The scrollbar size is still determined by the original text. Since the original text is (massively) padded with blanks, the scrollbar gets drawn appropriately.

I am not sure how this will hold up over time, but it works for now.

Edit on 11/12/12: To hide tooltips, ignore some WndProc messages. The WM_VSCROLL part is to reduce flicker when scrolling. The other cases were related to tooltips, but I don't recall exactly which one did what. I think that Notify might be the only one you need, but I thought I'd add the whole method just in case.

protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            //case WindowsConstants.WM_HSCROLL:
            case WindowsConstants.WM_VSCROLL:
                {
                    var nfy = m.WParam.ToInt32() & 0xFFFF;
                    if (nfy == WindowsConstants.SB_THUMBTRACK)
                    {
                        currentMsgCount++;
                        if (currentMsgCount % skipMsgCount == 0)
                            base.WndProc(ref m);
                        return;
                    }
                    if (nfy == WindowsConstants.SB_ENDSCROLL)
                        currentMsgCount = 0;

                    base.WndProc(ref m);
                }
                break;
            case WindowsConstants.MouseLeave:
            case WindowsConstants.NcMouseLeave:
            case WindowsConstants.MouseHover:
            case WindowsConstants.NcMouseHover:
            case WindowsConstants.Notify:
                break;
            default:
                base.WndProc(ref m);
                break;
        }

    }


    public const int NcMouseHover = 0x2a0;
    public const int MouseHover = 0x2a1;
    public const int NcMouseLeave = 0x2a2;
    public const int MouseLeave = 0x2a3;
    public const int Notify = 0x4e;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top