skip to main | skip to sidebar

Donate

If you found my work useful. Please consider donating
Thank you so much in advance!

Monday, August 31, 2009

Sunday, August 30, 2009

Get default icon on file/folder

2 comments

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. image
if EXE, you’ll get the default company program icon. image

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

0 comments

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
image
Download count:

Cheers!

Friday, August 21, 2009

Upcoming Mobile Applications

0 comments

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
    image image

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

  • Well, combining touch and note, here’s an interesting replacement for your WM Note application.
  • Here are some screen shots
  • image image

Wednesday, August 19, 2009

JSPA Program Manager 3 for WM6 & 6.5 – Supports all Resolution.

5 comments

.NET CF Open File Dialog and Folder Dialog

0 comments

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?
image image
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
image

NETCFOpenFileDialog + NETCFFolderDialog Control

image image image
image image image
image image
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

image  

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

0 comments
Jayson Ragasa, 26, currently working as a full time Applications Developer in Anomalist Design LLC. Been on this company for 2 years and a half or so, and It’s always been fun and challenging working with my boss Matthew Raymer (@matthew_raymer)

100_0392Am happily married to my beautiful wife Irish Mangilit Ragasa and we have 1 handsome baby boy Chevi Chrysler Ragasa ^^

Hope you enjoy your stay here.