winforms 将SystemIcons转换为ImageList

oaxa6hgo  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(101)

我尝试将System.Drawing.SystemIcons转换为System.Windows.Forms.ImageList,以便在ListView中使用它。
我尝试以下代码:

ImageList iconlist = new ImageList();
PropertyInfo[] properties = typeof(SystemIcons).GetProperties();
foreach (PropertyInfo property in properties)
{
   Debug.WriteLine(property.Name);
   iconlist.Images.Add(property.Name, property.GetValue(SystemIcons).ToBitmap());
}

但这是回报:“SystemIcons”是一种类型,在给定上下文中无效

3z6pesqy

3z6pesqy1#

当你想获取一个静态属性的值时,你必须传递null作为示例。此外,GetValue()返回一个object,所以你必须强制转换它:

iconlist.Images.Add(property.Name, (Icon)property.GetValue(null));

幸运的是,ImageCollection.Add()已经有了一个接受图标的重载,所以不需要ToBitmap()

相关问题