C#:트 소유자 그리 ownerdrawtext 과 이상한 까만 강조를 클릭하면 노드

StackOverflow https://stackoverflow.com/questions/1003459

  •  05-07-2019
  •  | 
  •  

문제

나는 설정을 쓰기/켭 OwnerDrawText 및 식에 DrawNode 이벤트,추가 코드를 그리는 텍스트 방법은 내가 원하는 모든 작품이 잘 저장을위 검은 선택을 강조하면 노드를 선택합니다.

문제 없습니다,나는 추가 논리를 확인하는 경우 노드의 상태를 강조했다고 그는 내 자신의 강조 표시 제외한 까만 강조하는 추가된 경우 노드를 클릭하면,단지 선택한...하이라이트를 얻을 통해 그려하여 내 사각형되면 마우스 단추를 눌렀다 놓지 않을 얻을 그린으로 깜박입니다...그것이다.:/

분명히 나는 잊을 실제로 요청 나의 질문을...방법 중 하나로 이동 제거하기에 관하여 선지 않고 완전하게 처리하는가?

도움이 되었습니까?

해결책

내 경험에서 당신은 일반적으로 할 수 없습니다.하나 당신이 그 품목 또는 당신은하지 않습니다.당신이 시도하는 경우를 종합신에서 그래픽의 정상들에 의해 그려진 제어,당신은 끝날 것이다.

그것은 약간의 고통을 해야 하기 때문에 손잡이 사각형을 초점,선택을 강조,그리고 그 모든 문자 자신입니다.

Visual 스타일 을 수행하는 데 사용될 수 있습니다 대부분의 작업을 수행합니다.

여기에 몇 가지는 코드에 당신을 얻을 것이 대부분을 도울 수 있습니다.(이전에서 사용하는 몇 가지 방법이 포함되어 있지 않고,그것은 렌더링되지 않는 정확하게 정상적인 트 않기 때문에 그것을 지원하는 대학원 작성 항목 및 열어야 한 편리한 참조)

    protected virtual void OnDrawTreeNode(object sender, DrawTreeNodeEventArgs e)
    {
        string text = e.Node.Text;
        Rectangle itemRect = e.Bounds;
        if (e.Bounds.Height < 1 || e.Bounds.Width < 1)
            return;

        int cIndentBy   = 19;       // TODO - support Indent value
        int cMargin     = 6;        // TODO - this is a bit random, it's slaved off the Indent in some way
        int cTwoMargins = cMargin * 2;

        int indent = (e.Node.Level * cIndentBy) + cMargin;
        int iconLeft = indent;                      // Where to draw parentage lines & icon/checkbox
        int textLeft = iconLeft + 16;               // Where to draw text

        Color leftColour = e.Node.BackColor;
        Color textColour = e.Node.ForeColor;

        if (Bitfield.IsBitSet(e.State, TreeNodeStates.Grayed))
            textColour = Color.FromArgb(255,128,128,128);

        // Grad-fill the background
        Brush backBrush = new SolidBrush(leftColour);
        e.Graphics.FillRectangle(backBrush, itemRect);

        // Faint underline along the bottom of each item
        Color separatorColor = ColourUtils.Mix(leftColour, Color.FromArgb(255,0,0,0), 0.02);
        Pen separatorPen = new Pen(separatorColor);
        e.Graphics.DrawLine(separatorPen, itemRect.Left, itemRect.Bottom-1, itemRect.Right, itemRect.Bottom-1);

        // Bodged to use Button styles as Treeview styles not available on my laptop...
        if (!HideSelection)
        {
            if (Bitfield.IsBitSet(e.State, TreeNodeStates.Selected) || Bitfield.IsBitSet(e.State, TreeNodeStates.Hot))
            {
                Rectangle selRect = new Rectangle(textLeft, itemRect.Top, itemRect.Right - textLeft, itemRect.Height);
                VisualStyleRenderer renderer = new VisualStyleRenderer((ContainsFocus) ? VisualStyleElement.Button.PushButton.Hot
                                                                                       : VisualStyleElement.Button.PushButton.Normal);
                renderer.DrawBackground(e.Graphics, selRect);

                // Bodge to make VisualStyle look like Explorer selections - overdraw with alpha'd white rectangle to fade the colour a lot
                Brush bodge = new SolidBrush(Color.FromArgb((Bitfield.IsBitSet(e.State, TreeNodeStates.Hot)) ? 224 : 128,255,255,255));
                e.Graphics.FillRectangle(bodge, selRect);
            }
        }

        Pen dotPen = new Pen(Color.FromArgb(128,128,128));
        dotPen.DashStyle = DashStyle.Dot;

        int midY = (itemRect.Top + itemRect.Bottom) / 2;

        // Draw parentage lines
        if (ShowLines)
        {
            int x    = cMargin * 2;

            if (e.Node.Level == 0 && e.Node.PrevNode == null)
            {
                // The very first node in the tree has a half-height line
                e.Graphics.DrawLine(dotPen, x, midY, x, itemRect.Bottom);
            }
            else
            {
                TreeNode testNode = e.Node;         // Used to only draw lines to nodes with Next Siblings, as in normal TreeViews
                for (int iLine = e.Node.Level; iLine >= 0; iLine--)
                {
                    if (testNode.NextNode != null)
                    {
                        x = (iLine * cIndentBy) + (cMargin * 2);
                        e.Graphics.DrawLine(dotPen, x, itemRect.Top, x, itemRect.Bottom);
                    }

                    testNode = testNode.Parent;
                }

                x = (e.Node.Level * cIndentBy) + cTwoMargins;
                e.Graphics.DrawLine(dotPen,  x, itemRect.Top, x, midY);
            }

            e.Graphics.DrawLine(dotPen, iconLeft + cMargin, midY, iconLeft + cMargin + 10, midY);
        }

        // Draw Expand (plus/minus) icon if required
        if (ShowPlusMinus && e.Node.Nodes.Count > 0)
        {
            // Use the VisualStyles renderer to use the proper OS-defined glyphs
            Rectangle expandRect = new Rectangle(iconLeft-1, midY - 7, 16, 16);

            VisualStyleElement element = (e.Node.IsExpanded) ? VisualStyleElement.TreeView.Glyph.Opened
                                                             : VisualStyleElement.TreeView.Glyph.Closed;

            VisualStyleRenderer renderer = new VisualStyleRenderer(element);
            renderer.DrawBackground(e.Graphics, expandRect);
        }

        // Draw the text, which is separated into columns by | characters
        Point textStartPos = new Point(itemRect.Left + textLeft, itemRect.Top);
        Point textPos = new Point(textStartPos.X, textStartPos.Y);

        Font textFont = e.Node.NodeFont;    // Get the font for the item, or failing that, for this control
        if (textFont == null)
            textFont = Font;

        StringFormat drawFormat = new StringFormat();
        drawFormat.Alignment = StringAlignment.Near;
        drawFormat.LineAlignment = StringAlignment.Center;
        drawFormat.FormatFlags = StringFormatFlags.NoWrap;

        string [] columnTextList = text.Split('|');
        for (int iCol = 0; iCol < columnTextList.GetLength(0); iCol++)
        {
            Rectangle textRect = new Rectangle(textPos.X, textPos.Y, itemRect.Right - textPos.X, itemRect.Bottom - textPos.Y);
            if (mColumnImageList != null && mColumnImageList[iCol] != null)
            {
                // This column has an imagelist assigned, so we use the column text as an integer zero-based index
                // into the imagelist to indicate the icon to display
                int iImage = 0;
                try
                {
                    iImage = MathUtils.Clamp(Convert.ToInt32(columnTextList[iCol]), 0, mColumnImageList[iCol].Images.Count);
                }
                catch(Exception)
                {
                    iImage = 0;
                }

                e.Graphics.DrawImageUnscaled(mColumnImageList[iCol].Images[iImage], textRect.Left, textRect.Top);
            }
            else
                e.Graphics.DrawString(columnTextList[iCol], textFont, new SolidBrush(textColour), textRect, drawFormat);

            textPos.X += mColumnWidthList[iCol];
        }

        // Draw Focussing box around the text
        if (e.State == TreeNodeStates.Focused)
        {
            SizeF size = e.Graphics.MeasureString(text, textFont);
            size.Width = (ClientRectangle.Width - 2) - textStartPos.X;
            size.Height += 1;
            Rectangle rect = new Rectangle(textStartPos, size.ToSize());
            e.Graphics.DrawRectangle(dotPen, rect);
//          ControlPaint.DrawFocusRectangle(e.Graphics, Rect);
        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top