【问题标题】:How do I paint a single treenode checkbox state to indeterminate如何将单个树节点复选框状态绘制为不确定
【发布时间】:2016-11-20 09:38:11
【问题描述】:

带有复选框的树节点有 3 种状态,选中、未选中和不确定(正方形)。

我的问题是如何绘制不确定状态?

我有以下代码将 3 个复选框添加到树视图图像列表:

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

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();

        treeView1.StateImageList = new System.Windows.Forms.ImageList();

        // populate the image list, using images from the System.Windows.Forms.CheckBoxRenderer class
        for (int i = 0; i < 3; i++)
        {
            // Create a bitmap which holds the relevent check box style
            // see http://msdn.microsoft.com/en-us/library/ms404307.aspx and http://msdn.microsoft.com/en-us/library/system.windows.forms.checkboxrenderer.aspx

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(16, 16);
            System.Drawing.Graphics chkGraphics = System.Drawing.Graphics.FromImage(bmp);
            switch (i)
            {
                // 0,1 - offset the checkbox slightly so it positions in the correct place
                case 0:
                    System.Windows.Forms.CheckBoxRenderer.DrawCheckBox(chkGraphics, new System.Drawing.Point(0, 1), System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                    break;
                case 1:
                    System.Windows.Forms.CheckBoxRenderer.DrawCheckBox(chkGraphics, new System.Drawing.Point(0, 1), System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal);
                    break;
                case 2:
                    System.Windows.Forms.CheckBoxRenderer.DrawCheckBox(chkGraphics, new System.Drawing.Point(0, 1), System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal);
                    break;
            }

            treeView1.StateImageList.Images.Add(bmp);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TreeNode n = new TreeNode("Root");
        n.SelectedImageIndex = 2;
        n.Nodes.Add("asd");
        treeView1.Nodes.Add(n);
    }
  }
}

但是当我设置一个treenode的SelectedImageIndex = 2

什么都没有发生..

【问题讨论】:

    标签: c# winforms treeview


    【解决方案1】:

    您已将3 图像添加到您的StateImageList,但(int)CheckBoxState.MixedNormal 的计算结果为9

    12 CheckBoxStates,因此您需要创建更多图像或向下折叠索引以保持在您的范围内:

    treeView1.SelectedImageIndex = ((int)CheckBoxState.MixedNormal) / 4;
    

    (除了normal,您还没有考虑disabledpressedhotstates。)

    您确实将treeView1.StateImageList 设置为StateImageList并且treeView1.ImageList 设置为其他Imagelist,当然?

    【讨论】:

    • 其他状态无关紧要,因为我只将这 3 个添加到 StateImageList 中。图像被添加到 treeView1.StateImageList。另外我很抱歉,但我应该编写代码:SelectedImageIndex = 2;
    • 那么现在在选择节点的时候是不是按预期工作了?
    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多