【发布时间】:2012-03-27 17:56:41
【问题描述】:
使用 ListView,我需要为一组可执行文件显示(大)图标。
是否有执行此操作的标准方法/“模式”(无论是设计还是其他)?
一个问题:这些 .exe 应该只能从这个 ListView 运行。如果一个人通过资源管理器导航到 .exe,他们应该无法从那里运行它们。 IOW,用户必须先登录系统才能看到程序图标数组(他们看到的内容取决于他们的角色)*,这是运行这些应用程序的唯一网关。
- 因此,必须以编程方式添加这些应用程序图标。
想法?
更新:
尝试使用下面的代码创建一个“又快又脏”的应用程序。
这是我的代码:
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;
using System.IO;
namespace ListViewWithAppIcons {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
DirectoryInfo dir = new DirectoryInfo(@"C:\SpotRun");
foreach (FileInfo file in dir.GetFiles()) {
try {
this.imageList1.Images.Add(Image.FromFile(file.FullName));
} catch {
Console.WriteLine("This is not a Duck-billed Platypus");
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++) {
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}
}
}
..这是“由工具生成的代码”(不是我,另一个工具):
namespace ListViewWithAppIcons {
partial class Form1 {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
this.listView1 = new System.Windows.Forms.ListView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// listView1
//
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(555, 408);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(555, 408);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ImageList imageList1;
}
}
【问题讨论】:
-
把它分解成几个问题。 1. 列表视图向我们显示您拥有的不起作用的代码。 2. 使用包含图标的行填充列表视图没有“模式”,它只是将列表视图绑定到数据源。 3. exe 只能从这个 ListView 运行是一个单独的问题。 ps 我没有对你投反对票,但不会让你回到零
-
我的朋友,要让我回到零需要不止一个支持。
-
我没有不起作用的代码,我只是想知道问题是否已经被聪明的人解决了。我现在有点像,但不相信我的方式“非常运动”(随机/多余的公主新娘参考)。
标签: c# .net winforms listview listviewitem