Calling Pick Icon dialog from C#

  • Thread starter Thread starter kurotsuke
  • Start date Start date
K

kurotsuke

Can anybody tell me how I can call the Pick Icon Dialog from C#? I
couldn't find any example.
Thanks.
 
kurotsuke said:
Can anybody tell me how I can call the Pick Icon Dialog from C#? I
couldn't find any example.
Thanks.

[DllImport("shell32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Winapi)]
private static extern int PickIconDlg(IntPtr hwndOwner,
System.Text.StringBuilder lpstrFile, int nMaxFile, ref int lpdwIconIndex);

private void button1_Click(object sender, System.EventArgs e)
{
string iconfile;
int iconindex = 0;
int retval;
System.Text.StringBuilder sb;

iconfile = Environment.GetFolderPath(Environment.SpecialFolder.System);
iconfile = iconfile + @"\shell32.dll";
sb = new System.Text.StringBuilder(iconfile, 500);
retval = PickIconDlg(this.Handle, sb, sb.Length, ref iconindex);
iconfile = sb.ToString();
}

Cheers

Arne Janning
 
kurotsuke said:
Can anybody tell me how I can call the Pick Icon Dialog from C#? I
couldn't find any example.
Thanks.

[DllImport("shell32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Winapi)]
private static extern int PickIconDlg(IntPtr hwndOwner,
System.Text.StringBuilder lpstrFile, int nMaxFile, ref int lpdwIconIndex);

private void button1_Click(object sender, System.EventArgs e)
{
string iconfile;
int iconindex = 0;
int retval;
System.Text.StringBuilder sb;

iconfile = Environment.GetFolderPath(Environment.SpecialFolder.System);
iconfile = iconfile + @"\shell32.dll";
sb = new System.Text.StringBuilder(iconfile, 500);
retval = PickIconDlg(this.Handle, sb, sb.Length, ref iconindex);
iconfile = sb.ToString();
}

Cheers

Arne Janning

Thanks. Can you give me an hint on using the ExtractIcon to extract
the retrieve icon? How can I draw it on a PictureBox. Thanks again.
Andrea
 
kurotsuke said:
Thanks. Can you give me an hint on using the ExtractIcon to extract
the retrieve icon? How can I draw it on a PictureBox. Thanks again.
Andrea

[DllImport("shell32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Winapi)]
private static extern int PickIconDlg(IntPtr hwndOwner,
System.Text.StringBuilder lpstrFile, int nMaxFile, ref int lpdwIconIndex);

[DllImport("shell32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Winapi)]
public static extern IntPtr ExtractIcon(IntPtr hInstance, string
strFileName, uint uiIconIndex);

private void button1_Click(object sender, System.EventArgs e)
{
string iconfile;
int iconindex = 2;
int retval;
System.Text.StringBuilder sb;

iconfile = Environment.GetFolderPath(Environment.SpecialFolder.System);
iconfile = iconfile + @"\shell32.dll";
sb = new System.Text.StringBuilder(iconfile, 500);
retval = PickIconDlg(this.Handle, sb, sb.Capacity, ref iconindex);
iconfile = sb.ToString();
Graphics g = pictureBox1.CreateGraphics();
g.DrawIconUnstretched(extractIcon(iconfile, iconindex), new
Rectangle(0, 0, 50, 50));
}

public static Icon extractIcon(string strPath, int nIndex)
{
Icon icon = null;
IntPtr hIcon = ExtractIcon(IntPtr.Zero, strPath, (uint)nIndex);
if (IntPtr.Zero != hIcon)
{
icon = Icon.FromHandle(hIcon);
}
return icon;
}

Cheers

Arne Janning
 
[DllImport("shell32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Winapi)]
private static extern int PickIconDlg(IntPtr hwndOwner,
System.Text.StringBuilder lpstrFile, int nMaxFile, ref int lpdwIconIndex);

[DllImport("shell32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Winapi)]
public static extern IntPtr ExtractIcon(IntPtr hInstance, string
strFileName, uint uiIconIndex);

private void button1_Click(object sender, System.EventArgs e)
{
string iconfile;
int iconindex = 2;
int retval;
System.Text.StringBuilder sb;

iconfile = Environment.GetFolderPath(Environment.SpecialFolder.System);
iconfile = iconfile + @"\shell32.dll";
sb = new System.Text.StringBuilder(iconfile, 500);
retval = PickIconDlg(this.Handle, sb, sb.Capacity, ref iconindex);
iconfile = sb.ToString();
Graphics g = pictureBox1.CreateGraphics();
g.DrawIconUnstretched(extractIcon(iconfile, iconindex), new
Rectangle(0, 0, 50, 50));
}

public static Icon extractIcon(string strPath, int nIndex)
{
Icon icon = null;
IntPtr hIcon = ExtractIcon(IntPtr.Zero, strPath, (uint)nIndex);
if (IntPtr.Zero != hIcon)
{
icon = Icon.FromHandle(hIcon);
}
return icon;
}

Cheers

Arne Janning

Thanks again for your help. I'm exepriencing a problem though. I
noticed that the returned iconfile is
@"C:\windows\system32\shell32.dl" (with only one 'L' instead of two).
That causes an error?
Why do I have to use the StringBuilder structure to call the API?
Couldn't I just use the string?
Since sb is modified by the API shouldn't I pass it by reference?
One final question? After extracting the icon, shouldn't I call
DestoryIcon?

Thanks again for your help and patience.
Andrea
 
Back
Top