如何使TreeNode的图像显示更小?

时间:2014-03-17 09:51:39

标签: c# winforms treeview

在我的Windows窗体中,我有以下Treeview控件,其中有四个节点代表大学课程的学年。

enter image description here

正如您所看到的,我已经向节点添加了小图像(对它们进行颜色编码)图像的大小为8 X 8像素png图像。我已使用ImageList集合将它们内联存储。

我对此有两个问题

  1. 节点之间没有间隙,因此图像(颜色代码)看起来不专业
  2. 第二个问题请参考下图,了解单击节点时发生的情况

    enter image description here

    2.你可以看到绿色突然变成橙色

    有没有办法解决这些问题?

    treeview控件的当前配置中,我已将showlines设置为false,将hideselection设置为false所有其他属性均为默认设置。

    这是代码

    在表单的load()中我将此myImageList集合变量全局声明

        private void FrmNewProgram_Load(object sender, EventArgs e)
        {
    
            myImageList = new ImageList();
            myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year1tagx16));
            myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year12tagx16));
            myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year3x16));
            myImageList.Images.Add(new Bitmap(SMSV100.Properties.Resources.year4x16));
            tvcAcdYears.ImageList = myImageList;
       }
    

    我没有提到的组合框但是它在这里做了什么,它有1,2,3,4个值分别意味着第1,2,3,4个,所以在我添加的组合框的SelectedIndexChanged()事件中这段代码所以它将以下列方式添加节点。如果用户选择1,如果用户选择2,则仅将年份#1添加到TreeView,将年份#1和年份#2添加到树视图中

     private void cmbProgDuration_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbProgDuration.SelectedIndex > -1)
            {
                if (cmbProgDuration.SelectedIndex == 0)
                {
                    tvcAcdYears.Nodes.Clear();
                    TreeNode tnOneYear = new TreeNode("Year #1");
    
    
                    tnOneYear.ImageIndex = 0;
                    tvcAcdYears.Nodes.Add(tnOneYear);
    
    
                }
                else if (cmbProgDuration.SelectedIndex == 1)
                {
                    tvcAcdYears.Nodes.Clear();
                    TreeNode tnOneYear = new TreeNode("Year #1");
                    tnOneYear.ImageIndex = 0;
    
    
                    TreeNode tn2ndYear = new TreeNode("Year #2");
                    tn2ndYear.ImageIndex = 1;
    
    
                    tvcAcdYears.Nodes.Add(tnOneYear);
                    tvcAcdYears.Nodes.Add(tn2ndYear);
                }
                else if (cmbProgDuration.SelectedIndex == 2)
                {
                    tvcAcdYears.Nodes.Clear();
                    TreeNode tnOneYear = new TreeNode("Year #1");
                    tnOneYear.ImageIndex = 0;
    
    
                    TreeNode tn2ndYear = new TreeNode("Year #2");
                    tn2ndYear.ImageIndex = 1;
    
    
                    TreeNode tn3rdYear = new TreeNode("Year #3");
                    tn3rdYear.ImageIndex = 2;
    
    
                    tvcAcdYears.Nodes.Add(tnOneYear);
                    tvcAcdYears.Nodes.Add(tn2ndYear);
                    tvcAcdYears.Nodes.Add(tn3rdYear);
                }
                else if (cmbProgDuration.SelectedIndex == 3)
                {
                    tvcAcdYears.Nodes.Clear();
                    TreeNode tnOneYear = new TreeNode("Year #1");
                    tnOneYear.ImageIndex = 0;                    
    
    
                    TreeNode tn2ndYear = new TreeNode("Year #2");
                    tn2ndYear.ImageIndex = 1;
    
    
                    TreeNode tn3rdYear = new TreeNode("Year #3");
                    tn3rdYear.ImageIndex = 2;
    
    
                    TreeNode tn4thYear = new TreeNode("Year #4");
                    tn4thYear.ImageIndex = 3;
    
    
                    tvcAcdYears.Nodes.Add(tnOneYear);
                    tvcAcdYears.Nodes.Add(tn2ndYear);
                    tvcAcdYears.Nodes.Add(tn3rdYear);
                    tvcAcdYears.Nodes.Add(tn4thYear);
                }
    
                tvcAcdYears.SelectedNode = tvcAcdYears.Nodes[0];
    
            }
        }
    

    感谢

1 个答案:

答案 0 :(得分:3)

  1. 将图像更改为16x16尺寸,并为每个图像将原始8x8图像放在中央,保持边缘透明。
  2. 确保每个树视图节点的SelectedImageIndex属性(以及设置StateImageIndex属性的StateImage)设置为与{相同的值} {1}} property。