public struct ICON {
public Point pt; // icon position in pixel
public Icon iconImg; // icon image
public String name; // icon name
public bool shortcut; // boolean flag representing whether the icon is a shortcut
}
public IconLocator();
public ICON[] LocateIcons();
Return Value: An array that contains ICON object.
public void Move(int idx, Point pt);
Parameters:
idx: index of the icon.
pt: new position of the icon.
public Icon GetIcon(String path, bool large);
Parameters:
path: filepath - it is either a absolute or relative path.
it can be a directory path, too.
large: boolean flag representing the size of icon.
if true, the icon size is 32x32.
if false, the icon size is 16x16.
Return Value: Icon of the file.
This program displays desktop icons on its own view with
name and position.
The desktop icon moves when the user clicks the matched icon on the view.
It also displays the executable's icons - large and small - at the bottom.
using System;
using System.Drawing;
using System.Windows.Forms;
using IconManager;
// Include IconManager
class IconView : Form {
IconLocator loc
= new IconLocator();
// create an icon locator object
IconLocator.ICON[]
icons;
// declare
IconCanvas iCanvas;
public static void Main() {
Application.Run(new IconView());
}
public IconView() {
Text = "Icon Locator Test Program";
icons = loc.LocateIcons();
// call LocateIcons method
Icon largeIcon = loc.GetIcon("IconView.exe", true);
Icon smallIcon = loc.GetIcon("IconView.exe", false);
iCanvas = new IconCanvas(icons, largeIcon, smallIcon);
iCanvas.Size = new Size(700, 700);
iCanvas.MouseDown += new MouseEventHandler(onMouseDown);
Controls.Add(iCanvas);
ClientSize = iCanvas.Size;
}
private void onMouseDown(object
sender, MouseEventArgs e) {
int col = (e.X - 10) / 100;
int row = (e.Y - 10) / 80;
int idx = row * 7 + col;
if (idx < icons.Length) {
if (e.Button == MouseButtons.Left) {
icons[idx].pt.Offset(10, 10);
} else {
icons[idx].pt.Offset(-10, -10);
}
loc.Move(idx, icons[idx].pt);
// move the selected icon to the new position
iCanvas.Invalidate();
}
}
class IconCanvas : Control {
IconLocator.ICON[] icons;
Icon largeIcon;
Icon smallIcon;
static Font font = new Font("Helvetica", 10);
static SolidBrush bBrush = new SolidBrush(Color.Black);
static SolidBrush rBrush = new SolidBrush(Color.Red);
public IconCanvas(IconLocator.ICON[] icons, Icon lIcon, Icon sIcon) {
this.icons = icons;
this.largeIcon = lIcon;
this.smallIcon = sIcon;
// enable double buffering
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
BackColor = Color.White;
if (icons == null) {
return;
}
int col = 0;
int row = 0;
// enumerate all icons
for (int i = 0; i < icons.Length; i++) {
if (col * 100 + 100> Width) {
col = 0;
row++;
}
g.DrawIcon(icons[i].iconImg, 10+col*100,
10+row*80); //
draw an icon
String pos = "(" + icons[i].pt.X.ToString() + ", " +
icons[i].pt.Y.ToString() + ")";
g.DrawString(icons[i].name, font, bBrush,
10.0F+col*100, 45.0F+row*80); // draw an
icon name
g.DrawString(pos, font, bBrush, 10.0F+col*100,
65.0F+row*80);
// draw an icon position
if (icons[i].shortcut) {
g.DrawString("S", font, rBrush, 50.0F+col*100,
10+row*80);
// specify whether it's a shortcut
}
col++;
}
// draw large and small icons
if (largeIcon != null) {
g.DrawIcon(largeIcon, 10, 130+row*80);
}
if (smallIcon != null) {
g.DrawIcon(smallIcon, 50, 130+row*80);
}
}
}
}
csc /t:library IconManager.cs /unsafe
csc /r:IconManager.dll IconView.cs
IconView