2014-02-21 115 views
0

我希望將複選框添加到根節點,而不是Treeview控件的子節點。 Sample Image在樹狀視圖C中顯示覆選框到根節點#

- [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 

要做到這一點我已經做了變化,我經常TreeView控件

tvreport是樹視圖控制

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; 
    } 

後運行的代碼已發現的根節點顯示覆選框,並且childnodes沒有複選框[這是我想要的]。

但問題是顯示層次的「線條」消失了。現在我想填充這些LINES。這怎麼能實現。

回答

0

藉助上述鏈接,我可以達到我的要求。在下面的代碼中,只有在樹視圖中填充節點後才應調用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); 
      }