【发布时间】: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
什么都没有发生..
【问题讨论】: