Monday, August 31, 2009
Sunday, August 30, 2009
Get default icon on file/folder
I have found this very neat class which you can extract the default icon of File / Folder. Just to make clear, the class is not for extracting the Icon”s” inside the DLL/EXE file.
So for instance on a DLL file, you’ll get the DLL file type icon.
if EXE, you’ll get the default company program icon.
This class by the way is already modified so you can have an option to extract which icon size you like, Large or Small so we don’t have to worry about the platforms which means if the device is QVGA, it’ll extract the 32x32 and 16x16 icons, and if VGA, it’ll extract the 64x64 and 32x32 icons.
so, here’s the class
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Coredll.API
{
// Implements a cache for shell icons that represent specific
// directories or files within the file system.
internal class ShellIconCache
{
public enum IconSize
{
Details, LargeIcon, List, SmallIcon
}
public bool AllowThumbnails = false;
public ImageList ImageList;
private Dictionary<int, int> shellIndexToOurIndex = new Dictionary<int, int>();
private IconSize Size;
/// <summary>
/// Creates a shell icon cache and associates it with a given managed image list.
/// </summary>
/// <param name="imageList">The image list to cache the shell icons in</param>
public ShellIconCache(IconSize iconsize)
{
int sm_x = 0;
int sm_y = 0;
this.Size = iconsize;
if (iconsize == IconSize.LargeIcon)
{
sm_x = SM_CXICON;
sm_y = SM_CYICON;
}
else
{
sm_x = SM_CXSMICON;
sm_y = SM_CYSMICON;
}
// Determine the correct icon size based upon
// the current screen DPI and resolution settings
int cx = GetSystemMetrics(sm_x);
int cy = GetSystemMetrics(sm_y);
// Store the image list and set
// it's size to suit the shell icons
this.ImageList = new ImageList();
this.ImageList.ImageSize = new Size(cx, cy);
}
/// <summary>
/// Returns an icon that should be utilised to represent the specified path
/// within a directory listing.
/// </summary>
/// <param name="path">The path (directory or file name) to fetch an icon for</param>
/// <returns>The index into the image list for the icon associated with the path</returns>
public int this[string path]
{
get
{
// Determine the index of the icon in the system image list
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr hImageList = IntPtr.Zero;
int key = 0;
if (this.Size == IconSize.LargeIcon)
{
hImageList = SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
}
else
{
hImageList = SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
}
// If we haven't fetched this icon yet
if (!shellIndexToOurIndex.ContainsKey(shinfo.iIcon))
{
// Fetch the icon
IntPtr hIcon = ImageList_GetIcon(hImageList, shinfo.iIcon, ILD_NORMAL);
Icon myIcon = Icon.FromHandle(hIcon);
DestroyIcon(hIcon);
// And add it to our managed image list
shellIndexToOurIndex.Add(shinfo.iIcon, ImageList.Images.Count);
ImageList.Images.Add(myIcon);
}
key = shinfo.iIcon;
// Return the index of the icon to use for this path.
return shellIndexToOurIndex[key];
}
}
public void AddIcon(Icon ico)
{
ImageList.Images.Add(ico);
}
public void AddIcon(Image ico)
{
ImageList.Images.Add(ico);
}
#region Platform Invoke
private const uint SHGFI_SYSICONINDEX = 0x000004000; // get system icon index
private const uint SHGFI_SMALLICON = 0x1; // Small icon
private const uint SHGFI_LARGEICON = 0x0; // Small icon
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public Int32 iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("coredll.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
private const uint ILD_NORMAL = 0x00;
[DllImport("coredll.dll")]
private static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
[DllImport("coredll.dll")]
private static extern int DestroyIcon(IntPtr hIcon);
private const int SM_CXICON = 11;
private const int SM_CYICON = 12;
private const int SM_CXSMICON = 49;
private const int SM_CYSMICON = 50;
[DllImport("coredll.dll")]
private static extern int GetSystemMetrics(int nIndex);
#endregion
}
}
And here’s how to use this class.
Don’t bother adding ImageList on your Listview or Treeview control, you have to use the built-in Imagelist of this class.
// Initialize all the default values and setup of controls here
void InitializeControls()
{
ShellIconCache iconcache16 = new ShellIconCache(ShellIconCache.IconSize.SmallIcon);
ListView1.SmallImageList = iconcache16.ImageList;
// do other important control initialization here
}
// get files from path
void GetFilesFromPath()
{
foreach (string f in Directory.GetFiles(@"\Windows"))
{
thisItem = new ListViewItem();
thisItem.ImageIndex = iconcache16[f];
thisItem.Text = Path.GetFileNameWithoutExtension(f);
thisItem.SubItems.Add(Path.GetExtension(f));
thisItem.Tag = f;
ListView1.Items.Add(thisItem);
}
}
Saturday, August 22, 2009
.NET CF Screen Orientation Awareness
There’s always an easy way making your mobile application aware of screen orientation changes without coding some hard math’s to update your control locations and sizes. A good example is setting the Control Anchor Property.
What does Anchor Property do?
Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent.
Anchor does the job for you for updating the control location and size! But one little problem for Windows Mobile. The Form doesn’t resize correctly when the screen orientation has changed.
Now how are we going to fix it? With just 2 simple codes, the problem will be solved.
Form.Hide() and Form.Show() follows.
Here’s a little video demonstrating how the problem and how will the 2 simple code can fix it.
Now why Hide() and Show() method? These can be use to refresh the form layout. I was also trying to find some simplest way to do it, but looks like this methods is the simplest.
Download the sample code here
Download count:
Cheers!
Friday, August 21, 2009
Upcoming Mobile Applications
Hmm… already enjoying my mobile applications development. Anyway, I have some applications which are under constructions and for sure I will be releasing it ASAP.
Here’s a sneak preview. I you find the UI ugly, please bear with me, these are all under construction.
JSPA Start Menu Cleaner .NET CF
- Start Menu Cleaner .NET meets mobile!
- You probably used my JSPA Program Manager or any shortcut organizer for your start menu. You’ll probably notice something or a thought of, what if you uninstall an application? Will it remove the shortcut I just move into the other folder? Tell you the answer, NO! You’ll have to delete it manually.
- This Start Menu Cleaner will do the job for you!
- Here are some screen shot
Wallpaper Today
- Bored of your wallpaper? You might have hundreds of wallpapers and changing it every hour or every day makes you sick..
- Wallpaper Today will do the job for you!
- No available screen shots yet
Touch Note
Wednesday, August 19, 2009
JSPA Program Manager 3 for WM6 & 6.5 – Supports all Resolution.
JSPA Program Manager allows you to manage your programs(shortcut links) in start menu on Windows Mobile 6.0 and 6.5 Smartphone. It allows you to Navigate, Move, Copy, Rename, Delete, and Create New Folder, Create New Shortcut on an easy steps.
First, I want to show you the screen shots of this friendly user interface program then I’ll going to take you to an actual demonstration (in video) using my HTC Touch Elf connected in MyMobilzer and recorded using Camtasia Studio so you can see where my mouse clicks.
| From a cluttered shortcuts | To an organized shortcuts |
And here’s the 17 minutes and 45 seconds Video. Took that long because as I’ve said, an actual demonstration in my HTC Touch Elf QVGA so while recording, I was also thinking what folder naming to use, what shortcut files to put in, etc. Heheh :D but so far so good :)
| Here’s Part 1 | and Part 2 |
Latest Release – Version 3
And PLEASE! If you encountered some bugs, please report it here! I'll appreciate it so much and fix it immediately!
.NET CF Open File Dialog and Folder Dialog
ah, probably the first annoying Windows Mobile control. As a first time developer for Windows Mobile, I found this control, very very very inconvenient to use. I was actually developing something in Windows Mobile and I haven’t gone far on it and I have to use OpenFileDialog and that control crashes my plans.
would you like to say hello?
iiiiiyuuccck!
Who in Microsoft Windows Mobile team make some stupid control like that? I can’t browse for subfolders!! What a lazy developer! So I made up my mind and decided to make my own. I actually made 2 controls.
NETCFOpenFileDialog and NETCFFolderDialog
Here are the screen shots and Code Snippets.
I made a test form so I can see the result after calling the forms
NETCFOpenFileDialog + NETCFFolderDialog Control
the file extension automatically adds to File Type dropdown. | Doesn’t look like the control familiar to you? :) |
Code Snippet
NETCFOpenFileDialog.OpenFileDialog ofd = new NETCFOpenFileDialog.OpenFileDialog();
ofd.DefaultPath = @"\Windows\Start Menu\Programs";
ofd.Title = "Open File Test";
ofd.Filter = "JPG|*jpg;PNG|*.png;ALL FILES|*.*";
ofd.OpenDialog();
if (ofd.Filename != string.Empty)
{
txPath.Text = ofd.Filename;
}
NETCFFolderDialog
Code Snippet
NETCFFolderDialog.FolderDialog ofd = new NETCFFolderDialog.FolderDialog();
ofd.DefaultFolder = @"\Windows\Start Menu\Programs";
ofd.OpenDialog();
if (ofd.Folder != string.Empty)
{
txFolder.Text = ofd.Folder;
}
Download the binaries here
NETCFOpenFileDialog.zip
About Me
Hope you enjoy your stay here.


