【问题标题】:Show checkbox to root node in treeview C#在树视图 C# 中向根节点显示复选框
【发布时间】:2014-02-21 07:01:14
【问题描述】:

我希望对根节点而不是 Treeview 控件的子节点有复选框。

- [x]Pie Chart report 
    - Sales report
    - Sales Projection report
    - Linear Sales report

- [x]Surface Chart report
    - Sales report
    - Sales Projection report
    - Linear Sales report

- [x]Caligraph report
    - Sales report
    - Sales Projection report
    - Linear Sales report

为了实现这一点,我对常规树视图控件进行了更改

tvreport 是 Treeview 控件

this.tvreport.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(tvreport_DrawNode);
this.tvreport.ShowLines = true;
this.tvreport.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;


    private void tvreport_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        if (IsContactNode(e.Node))
        {
            Color backColor, foreColor;
            if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
            {
                backColor = SystemColors.Highlight;
                foreColor = SystemColors.HighlightText;
            }

            if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
            {
                backColor = SystemColors.HotTrack;
                foreColor = SystemColors.HighlightText;
            }
            else
            {
                backColor = e.Node.BackColor;
                foreColor = e.Node.ForeColor;
            }

            Rectangle newBounds = e.Node.Bounds;
            newBounds.X = 60;


            using (SolidBrush brush = new SolidBrush(backColor))
            {
                e.Graphics.FillRectangle(brush, e.Node.Bounds);
            }

            TextRenderer.DrawText(e.Graphics, e.Node.Text, this.tvreport.Font, e.Node.Bounds, foreColor, backColor);
            if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)
            {
                ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);
            }
            e.DrawDefault = false;
        }

        else
        {
            e.DrawDefault = true;
            tvContactList1.ShowRootLines = true;
            tvContactList1.ShowLines = true;
        }

    }


    private bool IsContactNode(TreeNode node)
    {
        return node.Parent != null;
    }

运行代码后发现根节点显示复选框,子节点没有复选框[这是我希望拥有的]。

但问题是显示层次结构的“线条”消失了。现在我想填充这些 LINES。这是如何实现的。

【问题讨论】:

    标签: c# treeview


    【解决方案1】:

    TreeView Remove CheckBox by some Nodes 是一个可能的解决方案。 此解决方案将 TVM_SETITEM-Message 发送到树视图。

    【讨论】:

      【解决方案2】:

      借助上述链接,我可以实现我的要求。在下面的代码中,只有在树视图中填充节点后才应调用 HideCheckBox() 函数。

                  private const int TVIF_STATE = 0x8;
                  private const int TVIS_STATEIMAGEMASK = 0xF000;
                  private const int TV_FIRST = 0x1100;
                  private const int TVM_SETITEM = TV_FIRST + 63;
      
                  [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
                  private struct TVITEM
                  {
                      public int mask;
                      public IntPtr hItem;
                      public int state;
                      public int stateMask;
                      [MarshalAs(UnmanagedType.LPTStr)]
                      public string lpszText;
                      public int cchTextMax;
                      public int iImage;
                      public int iSelectedImage;
                      public int cChildren;
                      public IntPtr lParam;
                  }
      
                  [DllImport("user32.dll", CharSet = CharSet.Auto)]
                  private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
                                                           ref TVITEM lParam);
      
                  /// <summary>
                  /// Hides the checkbox for the specified node on a TreeView control.
                  /// </summary>
                  private void HideCheckBox(TreeView tvw, TreeNode node)
                  {
                      TVITEM tvi = new TVITEM();
                      tvi.hItem = node.Handle;
                      tvi.mask = TVIF_STATE;
                      tvi.stateMask = TVIS_STATEIMAGEMASK;
                      tvi.state = 0;
                      SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
                  }
      

      【讨论】:

        猜你喜欢
        • 2011-11-10
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        相关资源
        最近更新 更多