树状视图和滚动 [英] treeview and scrolling

查看:62
本文介绍了树状视图和滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!!!
我使用树视图,其中的节点高度不同.为此,我在添加节点时使用调用的函数HeiNode(如果需要)(函数代码如下所示).但是在这种情况下,如果您滚动到底部,则最后一个节点不是全部可见,并且滚动也不能滚动到末尾(它将自动跳"高一点).
如果我增加表格的高度-一切正常.
你能告诉我如何解决这个问题吗?
在下面的示例中,为简单起见,所有节点的高度增加了5倍.
主要形式,其中添加了节点:

Hello!!!
I use treeview, which nodes are different in height. For this, I use function HeiNode that call (if necessary), when I add nodes (function code is shown below). But in this case, if you do scroll to the bottom, the last node will be visible not all and scrolling can not be scroll to the end (it will automatically "jump" a little higher).
If I increase the height of the form - that everything is normal.
Can you please tell how to solve this problem!
In the example below, for simplicity, the height of all nodes increases 5 times.
The main form, where the addition of nodes:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    My_tree_view mtv = new My_tree_view();
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      
     mtv.Visible = true;
     this.Controls.Add(mtv);
     mtv.BeginUpdate();
     for (int i = 0; i < 20; i++)
     {
       TreeNode tn = new TreeNode("node" + i.ToString());
       tn.Name = "node" + i.ToString();
       mtv.Nodes.Add(tn);

       mtv.HeiNode(tn.Handle.ToInt32());



     }
     mtv.EndUpdate();
  
    }
    

   
  }
}



我的树状视图:



My treeview:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
  class My_tree_view : System.Windows.Forms.TreeView
  {
    public My_tree_view():base()
    {
    this.Visible = true;
     
      this.Scrollable = true;
      this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
      this.BorderStyle = BorderStyle.Fixed3D;
      this.FullRowSelect = true;
      this.HideSelection = false;
      this.Dock=DockStyle.Fill;
      this.ShowPlusMinus = false;
      this.ShowRootLines = false;
      this.ItemHeight = 17;
    
    }
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct TVITEM
    {
      public uint mask;
      public IntPtr hItem;
      public uint state;
      public uint stateMask;
      public IntPtr pszText;
      public int cchTextMax;
      public int iImage;
      public int iSelectedImage;
      public int cChildren;
      public IntPtr lParam;
      public int iIntegral;

    }
    public enum TVIF : int
    {
      TVIF_TEXT = 0x0001,
      TVIF_IMAGE = 0x0002,
      TVIF_PARAM = 0x0004,
      TVIF_STATE = 0x0008,
      TVIF_HANDLE = 0x0010,
      TVIF_SELECTEDIMAGE = 0x0020,
      TVIF_CHILDREN = 0x0040,
      TVIF_INTEGRAL = 0x0080
    }
    public enum TreeViewMessages : int
    {
      TV_FIRST = 0x1100,   // TreeView messages
      TVM_SETITEM = (TV_FIRST + 13),
      TVM_SETITEMHEIGHT = (TV_FIRST + 27)
    }
    public void HeiNode(int hItem)
    {
      if (hItem > 0)
      {
        TVITEM tvi = new TVITEM();
        tvi.mask = (uint)TVIF.TVIF_HANDLE | (uint)TVIF.TVIF_INTEGRAL;
        tvi.hItem = new IntPtr(hItem);
        tvi.stateMask = (uint)TVIF.TVIF_INTEGRAL;
        tvi.state = 0;

        tvi.iIntegral = 5;

        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(tvi));

        try
        {
          Marshal.StructureToPtr(tvi, ptr, false);
          Message msg = Message.Create(this.Handle, (int)TreeViewMessages.TVM_SETITEM, IntPtr.Zero, ptr);
          DefWndProc(ref msg);
        }
        finally
        {
          Marshal.FreeHGlobal(ptr);
        }
      }
    }
    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // My_tree_view
      // 
       this.ResumeLayout(false);

    }
    
    
    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
      Rectangle rec = e.Bounds;
      e.Graphics.FillRectangle(Brushes.Aqua, rec);
      e.Graphics.DrawRectangle(Pens.Black, rec.X, rec.Y, rec.Width, rec.Height - 2);
      e.Graphics.DrawString(e.Node.Name, new Font("Times", 12f,FontStyle.Bold),Brushes.Black,new PointF(rec.X+10,rec.Y+5));
      base.OnDrawNode(e);
      
    }
    
  }
}



谢谢!)



Thanks!)

推荐答案

以每个人都能理解的语言发布您的问题,以便更多人可以为您提供帮助.首选英语.
Post your question in a language everyone can understand so that more people can help you. English is the preferred choice.


这篇关于树状视图和滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆