using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Media; using System.Windows; using System.Windows.Input; using System.Reflection; namespace BuzzGUI.Common { [CLSCompliant(false)] public static class Win32 { [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll")] public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, DeviceCap nIndex); public enum DeviceCap { /// /// Device driver version /// DRIVERVERSION = 0, /// /// Device classification /// TECHNOLOGY = 2, /// /// Horizontal size in millimeters /// HORZSIZE = 4, /// /// Vertical size in millimeters /// VERTSIZE = 6, /// /// Horizontal width in pixels /// HORZRES = 8, /// /// Vertical height in pixels /// VERTRES = 10, /// /// Number of bits per pixel /// BITSPIXEL = 12, /// /// Number of planes /// PLANES = 14, /// /// Number of brushes the device has /// NUMBRUSHES = 16, /// /// Number of pens the device has /// NUMPENS = 18, /// /// Number of markers the device has /// NUMMARKERS = 20, /// /// Number of fonts the device has /// NUMFONTS = 22, /// /// Number of colors the device supports /// NUMCOLORS = 24, /// /// Size required for device descriptor /// PDEVICESIZE = 26, /// /// Curve capabilities /// CURVECAPS = 28, /// /// Line capabilities /// LINECAPS = 30, /// /// Polygonal capabilities /// POLYGONALCAPS = 32, /// /// Text capabilities /// TEXTCAPS = 34, /// /// Clipping capabilities /// CLIPCAPS = 36, /// /// Bitblt capabilities /// RASTERCAPS = 38, /// /// Length of the X leg /// ASPECTX = 40, /// /// Length of the Y leg /// ASPECTY = 42, /// /// Length of the hypotenuse /// ASPECTXY = 44, /// /// Shading and Blending caps /// SHADEBLENDCAPS = 45, /// /// Logical pixels inch in X /// LOGPIXELSX = 88, /// /// Logical pixels inch in Y /// LOGPIXELSY = 90, /// /// Number of entries in physical palette /// SIZEPALETTE = 104, /// /// Number of reserved entries in palette /// NUMRESERVED = 106, /// /// Actual color resolution /// COLORRES = 108, // Printing related DeviceCaps. These replace the appropriate Escapes /// /// Physical Width in device units /// PHYSICALWIDTH = 110, /// /// Physical Height in device units /// PHYSICALHEIGHT = 111, /// /// Physical Printable Area x margin /// PHYSICALOFFSETX = 112, /// /// Physical Printable Area y margin /// PHYSICALOFFSETY = 113, /// /// Scaling factor x /// SCALINGFACTORX = 114, /// /// Scaling factor y /// SCALINGFACTORY = 115, /// /// Current vertical refresh rate of the display device (for displays only) in Hz /// VREFRESH = 116, /// /// Horizontal width of entire desktop in pixels /// DESKTOPVERTRES = 117, /// /// Vertical height of entire desktop in pixels /// DESKTOPHORZRES = 118, /// /// Preferred blt alignment /// BLTALIGNMENT = 119 } [StructLayout(LayoutKind.Sequential)] struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; }; public const uint SHGFI_ICON = 0x100; public const uint SHGFI_LARGEICON = 0x0; // Large icon public const uint SHGFI_SMALLICON = 0x1; // Small icon [DllImport("shell32.dll")] static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); [DllImport("User32.dll", SetLastError = true)] public static extern bool DestroyIcon(IntPtr handle); public static IntPtr LoadIcon(string name) { SHFILEINFO shinfo = new SHFILEINFO(); lock (typeof(Win32)) { SHGetFileInfo(name, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON); } return shinfo.hIcon; } public static ImageSource LoadIconAsImageSource(string name) { SHFILEINFO shinfo = new SHFILEINFO(); lock (typeof(Win32)) { SHGetFileInfo(name, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON); } ImageSource imgs; try { imgs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(shinfo.hIcon, Int32Rect.Empty, null); } finally { Win32.DestroyIcon(shinfo.hIcon); } return imgs; } [DllImport("user32.dll")] static extern int MapVirtualKey(int uCode, int uMapType); public static int GetScanCode(KeyEventArgs e) { int vkey = KeyInterop.VirtualKeyFromKey(e.Key); int scancode = MapVirtualKey(vkey, 0); bool isExtended = (bool)typeof(KeyEventArgs).InvokeMember("IsExtendedKey", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance, null, e, null); if (isExtended) scancode = 0; return scancode; } [DllImport("shlwapi.dll", CharSet = CharSet.Auto)] static extern bool PathCompactPathEx([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags); public static string CompactPath(string longPathName, int wantedLength) { // NOTE: You need to create the builder with the required capacity before calling function. // See http://msdn.microsoft.com/en-us/library/aa446536.aspx StringBuilder sb = new StringBuilder(wantedLength + 1); PathCompactPathEx(sb, longPathName, wantedLength + 1, 0); return sb.ToString(); } } }