【问题标题】:Ways of displaying exe icons in a ListView在 ListView 中显示 exe 图标的方法
【发布时间】: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


【解决方案1】:

嗨 Clay,我需要参考一个多余的公主新娘!

一个来自 MSDN 的示例,让您了解如何编写代码:

private void Form_Load(object sender, EventArgs e)
{
    DirectoryInfo dir = new DirectoryInfo(@"c:\pic");
    foreach (FileInfo file in dir.GetFiles())
    {
        try
        {
            this.imageList1.Images.Add(Image.FromFile(file.FullName));
        }
        catch
        {
            Console.WriteLine("This is not an image file");
        }
    }
    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);
    }
}

要防止人们打开 exe(除了您的程序)非常困难,如果您编写所有这些应用程序并且可能需要传递一个秘密参数(如命令行 arg)来启动,这将很容易其他应用程序。但是使用 Process Monitor/Explorer 的用户可以找到密钥。

或者,您可以将 exe 隐藏在某个文件夹中,但这里的技巧是 exe 的名称将显示在任务管理器中,一旦用户看到它,他们就可以搜索该 exe。我的意思是你可以在这里使用我的技术解决这个问题,但是兔子洞有多深:How to hide C# application from taskmanager processtab? - 看看我的回答,它得到了最多的选票。

也许比所有这些对用户隐藏东西更好的解决方案是 - Kiosk 模式:http://support.microsoft.com/kb/555463

【讨论】:

  • 我认为这确实有一些有用的代码,但我采用了它并制作了最简单的应用程序(我所做的只是添加一个 ListView 和一个 ImageList,将目录更改为一个只包含两个文件,并添加了“使用 system.IO”来编译它。在通过 foreach 循环的第一次迭代中,它失败并出现 OutOfMemoryException。它不会显示您是否只是运行应用程序(ListView 只是保持为空),但是单步执行它,就达到了“捕获”,这就是显示的异常。
  • 您可以将解决方案和文件通过电子邮件发送给我,我的电子邮件在我的个人资料中,或者只是将我们可以用来重现错误的代码发布到另一个问题中。谢谢克莱
  • 我查看了 Kiosk 模式,这对于某些场景来说非常有用,但用户也需要他们的计算机来处理“其他事情”——不仅仅是这些应用程序。
  • 好的,我会发布我的“Quick and Dirty”项目中的所有内容,看看你的想法。
猜你喜欢
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 2010-09-11
  • 1970-01-01
相关资源
最近更新 更多