Added support for setting the application (use SHGetFileInfo to retrieve the correct icon and set it when constructing a new window).

This commit is contained in:
the_fiddler 2009-10-14 21:50:40 +00:00
parent 6ffe87813a
commit a8231a5eb4
2 changed files with 106 additions and 1 deletions

View file

@ -61,6 +61,7 @@ namespace OpenTK.Platform.Windows
using HRESULT = System.IntPtr;
using DWORD_PTR = System.IntPtr;
using UINT_PTR = System.UIntPtr;
using TIMERPROC = Functions.TimerProc;
@ -1402,6 +1403,13 @@ namespace OpenTK.Platform.Windows
public delegate void TimerProc(HWND hwnd, WindowMessage uMsg, UINT_PTR idEvent, DWORD dwTime);
#endregion
#region Shell Functions
[DllImport("shell32.dll")]
public static extern DWORD_PTR SHGetFileInfo(LPCTSTR pszPath, DWORD dwFileAttributes, ref SHFILEINFO psfi, UINT cbFileInfo, ShGetFileIconFlags uFlags);
#endregion
}
#region --- Constants ---
@ -2690,6 +2698,22 @@ namespace OpenTK.Platform.Windows
#endregion
#region ShFileInfo
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
#endregion
#endregion
#region --- Enums ---
@ -3933,6 +3957,51 @@ namespace OpenTK.Platform.Windows
#endregion
#region ShGetFileIcon
[Flags]
enum ShGetFileIconFlags : int
{
/// <summary>get icon</summary>
Icon = 0x000000100,
/// <summary>get display name</summary>
DisplayName = 0x000000200,
/// <summary>get type name</summary>
TypeName = 0x000000400,
/// <summary>get attributes</summary>
Attributes = 0x000000800,
/// <summary>get icon location</summary>
IconLocation = 0x000001000,
/// <summary>return exe type</summary>
ExeType = 0x000002000,
/// <summary>get system icon index</summary>
SysIconIndex = 0x000004000,
/// <summary>put a link overlay on icon</summary>
LinkOverlay = 0x000008000,
/// <summary>show icon in selected state</summary>
Selected = 0x000010000,
/// <summary>get only specified attributes</summary>
Attr_Specified = 0x000020000,
/// <summary>get large icon</summary>
LargeIcon = 0x000000000,
/// <summary>get small icon</summary>
SmallIcon = 0x000000001,
/// <summary>get open icon</summary>
OpenIcon = 0x000000002,
/// <summary>get shell size icon</summary>
ShellIconSize = 0x000000004,
/// <summary>pszPath is a pidl</summary>
PIDL = 0x000000008,
/// <summary>use passed dwFileAttribute</summary>
UseFileAttributes = 0x000000010,
/// <summary>apply the appropriate overlays</summary>
AddOverlays = 0x000000020,
/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
OverlayIndex = 0x000000040,
}
#endregion
#endregion
#region --- Callbacks ---

View file

@ -33,6 +33,7 @@ using System.Text;
using OpenTK.Graphics;
using OpenTK.Input;
using System.Collections.Generic;
using System.IO;
namespace OpenTK.Platform.Windows
{
@ -126,6 +127,8 @@ namespace OpenTK.Platform.Windows
keyboards.Add(keyboard);
mice.Add(mouse);
Icon = GetApplicationIcon();
}
#endregion
@ -539,6 +542,7 @@ namespace OpenTK.Platform.Windows
wc.Instance = Instance;
wc.WndProc = WindowProcedureDelegate;
wc.ClassName = ClassName;
wc.Icon = Icon != null ? Icon.Handle : IntPtr.Zero;
//wc.Background = Functions.GetStockObject(5);
ushort atom = Functions.RegisterClassEx(ref wc);
@ -579,6 +583,35 @@ namespace OpenTK.Platform.Windows
#endregion
#region GetApplicationIcon
// Gets the shell application icon for the executing process or the default icon, if not available.
Icon GetApplicationIcon()
{
IntPtr retval = IntPtr.Zero;
try
{
SHFILEINFO info = new SHFILEINFO();
info.szDisplayName = "";
info.szTypeName = "";
int cbFileInfo = Marshal.SizeOf(info);
ShGetFileIconFlags flags = ShGetFileIconFlags.Icon | ShGetFileIconFlags.SmallIcon | ShGetFileIconFlags.UseFileAttributes;
string path = System.Reflection.Assembly.GetEntryAssembly().CodeBase;
retval = Functions.SHGetFileInfo(path, 256, ref info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
catch
{
// Shallow exceptions and fall-back to default icon.
Debug.Print("SHGetFileInfo failed, return value: {0}", retval);
return null;
}
}
#endregion
#endregion
#region INativeWindow Members
@ -714,8 +747,9 @@ namespace OpenTK.Platform.Windows
}
set
{
Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : value.Handle);
icon = value;
if (window.WindowHandle != IntPtr.Zero)
Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : value.Handle);
}
}
@ -1077,6 +1111,8 @@ namespace OpenTK.Platform.Windows
{
// Safe to clean managed resources
DestroyWindow();
if (Icon != null)
Icon.Dispose();
}
else
{