Fri Nov 07, 2008 1:19 pm
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FileTypeAndIcon
{
public partial class IconForm : Form
{
#region PROPERTIES
/// <summary>
/// Used for containing file types and thier icons information.
/// </summary>
private Hashtable icons;
/// <summary>
/// USed for getting registered file types and their icons information.
/// </summary>
private RegisteredFileType fileType;
#endregion
#region CONSTRUCTORS
public IconForm()
{
InitializeComponent();
this.fileType = new RegisteredFileType();
}
#endregion
#region GUI EVENTS
private void IconForm_Load(object sender, EventArgs e)
{
try
{
//Gets file type and icon info.
this.icons = this.fileType.GetFileTypeAndIcon();
//Loads file types to ListBox.
foreach (object objString in this.icons.Keys)
{
this.lbxFileType.Items.Add(objString);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
return;
string searchName = "";
if (!this.txtSearch.Text.Contains("."))
searchName = "." + this.txtSearch.Text; //Add a dot if the search text does not have it.
else
searchName = this.txtSearch.Text;
//Searches in the collections of file types and icons.
object objName = this.icons[searchName];
if (objName != null)
{
this.lbxFileType.SelectedItem = searchName;
}
}
private void lbxFileType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (this.lbxFileType.Items.Count <= 0 || this.lbxFileType.SelectedItem == null)
return;
string fileType = this.lbxFileType.SelectedItem.ToString();
this.showIcon(fileType);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
{
this.btnSearch.Enabled = false;
}
else
{
this.btnSearch.Enabled = true;
}
}
/// <summary>
/// Shows the icon associates with a specific file type.
/// </summary>
/// <param name="fileType">The type of file (or file extension).</param>
private void showIcon(string fileType)
{
try
{
string fileAndParam = (this.icons[fileType]).ToString();
if (String.IsNullOrEmpty(fileAndParam))
return;
//Use to store the file contains icon.
string fileName = "";
//The index of the icon in the file.
int iconIndex = 0;
string iconIndexString = "";
int index = fileAndParam.IndexOf(",");
//if fileAndParam is some thing likes that: "C:\\Program Files\\NetMeeting\\conf.exe,1".
if (index > 0)
{
fileName = fileAndParam.Substring(0, index);
iconIndexString = fileAndParam.Substring(index + 1);
}
else
fileName = fileAndParam;
if (!string.IsNullOrEmpty(iconIndexString))
{
//Get the index of icon.
iconIndex = int.Parse(iconIndexString);
if (iconIndex < 0)
iconIndex = 0; //To avoid the invalid index.
}
//Gets the handle of the icon.
IntPtr lIcon = RegisteredFileType.ExtractIcon(this.Handle.ToInt32(), fileName, iconIndex);
//The handle cannot be zero.
if (lIcon != IntPtr.Zero)
{
//Gets the real icon.
Icon icon = Icon.FromHandle(lIcon);
//Draw the icon to the picture box.
this.pbIconView.Image = icon.ToBitmap();
}
else //if the icon is invalid, show an error image.
this.pbIconView.Image = this.pbIconView.ErrorImage;
}
catch (Exception exc)
{
throw exc;
}
}
#endregion
}
}
Sun Jan 20, 2013 10:59 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.