Small cleanups [skip ci]

This commit is contained in:
Robin 2016-04-17 23:32:22 +02:00
commit 98e6be5eb6
171 changed files with 1607 additions and 1769 deletions

View file

@ -19,13 +19,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Diagnostics.CodeAnalysis;
namespace GreenshotPlugin.UnmanagedHelpers {
/// <summary>
/// Window Style Flags
/// </summary>
[Flags]
public enum WindowStyleFlags : long {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowStyleFlags : long {
//WS_OVERLAPPED = 0x00000000,
WS_POPUP = 0x80000000,
WS_CHILD = 0x40000000,
@ -76,7 +78,8 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
[Flags]
public enum ExtendedWindowStyleFlags : uint {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ExtendedWindowStyleFlags : uint {
WS_EX_DLGMODALFRAME = 0x00000001,
WS_EX_NOPARENTNOTIFY = 0x00000004,
WS_EX_TOPMOST = 0x00000008,
@ -112,6 +115,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowPlacementFlags : uint {
// The coordinates of the minimized window may be specified.
// This flag must be specified if the coordinates are set in the ptMinPosition member.
@ -123,6 +127,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
WPF_RESTORETOMAXIMIZED = 0x0002
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ShowWindowCommand : uint {
/// <summary>
/// Hides the window and activates another window.
@ -149,7 +154,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
ShowMaximized = 3,
/// <summary>
/// Displays a window in its most recent size and position. This value
/// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
/// is similar to <see cref="ShowWindowCommand.Normal"/>, except
/// the window is not actived.
/// </summary>
ShowNoActivate = 4,
@ -164,13 +169,13 @@ namespace GreenshotPlugin.UnmanagedHelpers {
Minimize = 6,
/// <summary>
/// Displays the window as a minimized window. This value is similar to
/// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
/// <see cref="ShowWindowCommand.ShowMinimized"/>, except the
/// window is not activated.
/// </summary>
ShowMinNoActive = 7,
/// <summary>
/// Displays the window in its current size and position. This value is
/// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
/// similar to <see cref="ShowWindowCommand.Show"/>, except the
/// window is not activated.
/// </summary>
ShowNA = 8,
@ -194,7 +199,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
ForceMinimize = 11
}
public enum SYSCOLOR : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SYSCOLOR
{
SCROLLBAR = 0,
BACKGROUND = 1,
DESKTOP = 1,
@ -234,7 +241,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// ai_productions@verizon.net or osirisgothra@hotmail.com
/// Obtained on pinvoke.net, please contribute your code to support the wiki!
/// </summary>
public enum SystemMetric : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SystemMetric
{
/// <summary>
/// Width of the screen of the primary display monitor, in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, HORZRES).
/// </summary>
@ -612,7 +621,8 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// </summary>
SM_REMOTECONTROL=0x2001
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum RegionResult {
REGION_ERROR = 0,
REGION_NULLREGION = 1,
@ -621,6 +631,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
// See http://msdn.microsoft.com/en-us/library/aa969530(v=vs.85).aspx
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DWMWINDOWATTRIBUTE {
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY,
@ -639,7 +650,8 @@ namespace GreenshotPlugin.UnmanagedHelpers {
DWMWA_FREEZE_REPRESENTATION, // Since Windows 8
DWMWA_LAST
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum GetWindowCommand : uint {
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
@ -651,13 +663,16 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DWM_BB {
Enable = 1,
BlurRegion = 2,
TransitionMaximized = 4
}
public enum ClassLongIndex : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ClassLongIndex
{
GCL_CBCLSEXTRA = -20, // the size, in bytes, of the extra memory associated with the class. Setting this value does not change the number of extra bytes already allocated.
GCL_CBWNDEXTRA = -18, // the size, in bytes, of the extra window memory associated with each window in the class. Setting this value does not change the number of extra bytes already allocated. For information on how to access this memory, see SetWindowLong.
GCL_HBRBACKGROUND = -10, // a handle to the background brush associated with the class.
@ -670,7 +685,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
GCL_WNDPROC = -24, // the address of the window procedure, or a handle representing the address of the window procedure. You must use the CallWindowProc function to call the window procedure.
}
public enum WindowsMessages : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowsMessages
{
WM_NULL = 0x0000,
WM_CREATE = 0x0001,
WM_DESTROY = 0x0002,
@ -877,7 +894,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
// Get/Set WindowLong Enum See: http://msdn.microsoft.com/en-us/library/ms633591.aspx
public enum WindowLongIndex : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowLongIndex
{
GWL_EXSTYLE = -20, // Sets a new extended window style.
GWL_HINSTANCE = -6, // Sets a new application instance handle.
GWL_ID = -12, // Sets a new identifier of the child window. The window cannot be a top-level window.
@ -888,7 +907,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
// See: http://msdn.microsoft.com/en-us/library/ms633545.aspx
[Flags]
public enum WindowPos : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WindowPos
{
SWP_ASYNCWINDOWPOS = 0x4000, // If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE = 0x2000, // Prevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAME = 0x0020, // Draws a frame (defined in the window's class description) around the window.
@ -906,13 +927,18 @@ namespace GreenshotPlugin.UnmanagedHelpers {
SWP_SHOWWINDOW = 0x0040 //Displays the window.
}
public enum ScrollBarDirection : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ScrollBarDirection
{
SB_HORZ = 0,
SB_VERT = 1,
SB_CTL = 2,
SB_BOTH = 3
}
public enum ScrollbarCommand : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ScrollbarCommand
{
SB_LINEUP = 0, // Scrolls one line up.
SB_LINEDOWN = 1, // Scrolls one line down.
SB_PAGEUP = 2, // Scrolls one page up.
@ -924,6 +950,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
SB_ENDSCROLL = 8 // Ends scroll.
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ScrollInfoMask {
SIF_RANGE = 0x1,
SIF_PAGE = 0x2,
@ -937,6 +964,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// See: http://www.pinvoke.net/default.aspx/Enums/SendMessageTimeoutFlags.html
/// </summary>
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SendMessageTimeoutFlags : uint {
SMTO_NORMAL = 0x0,
SMTO_BLOCK = 0x1,
@ -945,6 +973,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum ProcessAccessFlags : uint {
All = 0x001F0FFF,
Terminate = 0x00000001,
@ -962,7 +991,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// See: http://msdn.microsoft.com/en-us/library/aa909766.aspx
/// </summary>
[Flags]
public enum SoundFlags : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum SoundFlags
{
SND_SYNC = 0x0000, // play synchronously (default)
SND_ASYNC = 0x0001, // play asynchronously
SND_NODEFAULT = 0x0002, // silence (!default) if sound not found
@ -979,6 +1010,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// Used by GDI32.GetDeviceCaps
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877%28v=vs.85%29.aspx
/// </summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DeviceCaps {
/// <summary>
/// Device driver version
@ -1147,7 +1179,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// Used for User32.SetWinEventHook
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd373640%28v=vs.85%29.aspx
/// </summary>
public enum WinEventHookFlags : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WinEventHookFlags
{
WINEVENT_SKIPOWNTHREAD = 1,
WINEVENT_SKIPOWNPROCESS = 2,
WINEVENT_OUTOFCONTEXT = 0,
@ -1158,6 +1192,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// Used for User32.SetWinEventHook
/// See MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd318066%28v=vs.85%29.aspx
/// </summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum WinEvent : uint {
EVENT_OBJECT_ACCELERATORCHANGE = 32786,
EVENT_OBJECT_CREATE = 32768,
@ -1207,7 +1242,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// Used for User32.SetWinEventHook
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd373606%28v=vs.85%29.aspx#OBJID_WINDOW
/// </summary>
public enum EventObjects : int {
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum EventObjects
{
OBJID_ALERT = -10,
OBJID_CARET = -8,
OBJID_CLIENT = -4,
@ -1223,6 +1260,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
}
[Flags]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum DesktopAccessRight : uint {
DESKTOP_READOBJECTS = 0x00000001,
DESKTOP_CREATEWINDOW = 0x00000002,

View file

@ -53,7 +53,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// <param name="graphics"></param>
/// <returns>SafeDeviceContextHandle</returns>
public static SafeDeviceContextHandle GetSafeDeviceContext(this Graphics graphics) {
return SafeDeviceContextHandle.fromGraphics(graphics);
return SafeDeviceContextHandle.FromGraphics(graphics);
}
}
@ -77,10 +77,6 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// A hbitmap SafeHandle implementation
/// </summary>
public class SafeHBitmapHandle : SafeObjectHandle {
[SecurityCritical]
private SafeHBitmapHandle() : base(true) {
}
[SecurityCritical]
public SafeHBitmapHandle(IntPtr preexistingHandle) : base(true) {
SetHandle(preexistingHandle);
@ -105,10 +101,6 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// A dibsection SafeHandle implementation
/// </summary>
public class SafeDibSectionHandle : SafeObjectHandle {
[SecurityCritical]
private SafeDibSectionHandle() : base(true) {
}
[SecurityCritical]
public SafeDibSectionHandle(IntPtr preexistingHandle) : base(true) {
SetHandle(preexistingHandle);
@ -123,20 +115,16 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[DllImport("gdi32", SetLastError = true)]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
private SafeHandle hdc;
[SecurityCritical]
private SafeSelectObjectHandle() : base(true) {
}
private readonly SafeHandle _hdc;
[SecurityCritical]
public SafeSelectObjectHandle(SafeDCHandle hdc, SafeHandle newHandle) : base(true) {
this.hdc = hdc;
_hdc = hdc;
SetHandle(SelectObject(hdc.DangerousGetHandle(), newHandle.DangerousGetHandle()));
}
protected override bool ReleaseHandle() {
SelectObject(hdc.DangerousGetHandle(), handle);
SelectObject(_hdc.DangerousGetHandle(), handle);
return true;
}
}
@ -153,10 +141,6 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteDC(IntPtr hDC);
[SecurityCritical]
private SafeCompatibleDCHandle() : base(true) {
}
[SecurityCritical]
public SafeCompatibleDCHandle(IntPtr preexistingHandle) : base(true) {
SetHandle(preexistingHandle);
@ -175,20 +159,16 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// A DeviceContext SafeHandle implementation
/// </summary>
public class SafeDeviceContextHandle : SafeDCHandle {
private Graphics graphics = null;
[SecurityCritical]
private SafeDeviceContextHandle() : base(true) {
}
private readonly Graphics _graphics;
[SecurityCritical]
public SafeDeviceContextHandle(Graphics graphics, IntPtr preexistingHandle) : base(true) {
this.graphics = graphics;
_graphics = graphics;
SetHandle(preexistingHandle);
}
protected override bool ReleaseHandle() {
graphics.ReleaseHdc(handle);
_graphics.ReleaseHdc(handle);
return true;
}
@ -196,7 +176,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
return new SafeSelectObjectHandle(this, newHandle);
}
public static SafeDeviceContextHandle fromGraphics(Graphics graphics) {
public static SafeDeviceContextHandle FromGraphics(Graphics graphics) {
return new SafeDeviceContextHandle(graphics, graphics.GetHdc());
}
}
@ -221,41 +201,6 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public static extern uint GetPixel(SafeHandle hdc, int nXPos, int nYPos);
[DllImport("gdi32", SetLastError=true)]
public static extern int GetDeviceCaps(SafeHandle hdc, DeviceCaps nIndex);
/// <summary>
/// StretchBlt extension for the graphics object
/// Doesn't work?
/// </summary>
/// <param name="target"></param>
/// <param name="source"></param>
public static void StretchBlt(this Graphics target, Bitmap sourceBitmap, Rectangle source, Rectangle destination) {
using (SafeDeviceContextHandle targetDC = target.GetSafeDeviceContext()) {
using (SafeCompatibleDCHandle safeCompatibleDCHandle = CreateCompatibleDC(targetDC)) {
using (SafeHBitmapHandle hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap())) {
using (safeCompatibleDCHandle.SelectObject(hBitmapHandle)) {
StretchBlt(targetDC, destination.X, destination.Y, destination.Width, destination.Height, safeCompatibleDCHandle, source.Left, source.Top, source.Width, source.Height, CopyPixelOperation.SourceCopy);
}
}
}
}
}
/// <summary>
/// Bitblt extension for the graphics object
/// </summary>
/// <param name="target"></param>
/// <param name="source"></param>
public static void BitBlt(this Graphics target, Bitmap sourceBitmap, Rectangle source, Point destination, CopyPixelOperation rop) {
using (SafeDeviceContextHandle targetDC = target.GetSafeDeviceContext()) {
using (SafeCompatibleDCHandle safeCompatibleDCHandle = CreateCompatibleDC(targetDC)) {
using (SafeHBitmapHandle hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap())) {
using (safeCompatibleDCHandle.SelectObject(hBitmapHandle)) {
BitBlt(targetDC, destination.X, destination.Y, source.Width, source.Height, safeCompatibleDCHandle, source.Left, source.Top, rop);
}
}
}
}
}
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]
@ -380,10 +325,12 @@ namespace GreenshotPlugin.UnmanagedHelpers {
bV5BlueMask = (uint)255;
bV5AlphaMask = (uint)255 << 24;
bV5CSType = 1934772034; // sRGB
bV5Endpoints = new CIEXYZTRIPLE();
bV5Endpoints.ciexyzBlue = new CIEXYZ(0);
bV5Endpoints.ciexyzGreen = new CIEXYZ(0);
bV5Endpoints.ciexyzRed = new CIEXYZ(0);
bV5Endpoints = new CIEXYZTRIPLE
{
ciexyzBlue = new CIEXYZ(0),
ciexyzGreen = new CIEXYZ(0),
ciexyzRed = new CIEXYZ(0)
};
bV5GammaRed = 0;
bV5GammaGreen = 0;
bV5GammaBlue = 0;

View file

@ -21,9 +21,7 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
using log4net;
using Microsoft.Win32.SafeHandles;
using System.Reflection;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
@ -94,7 +92,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// GDIplus Helpers
/// </summary>
public static class GDIplus {
private static ILog LOG = LogManager.GetLogger(typeof(GDIplus));
private static readonly ILog LOG = LogManager.GetLogger(typeof(GDIplus));
[DllImport("gdiplus.dll", SetLastError = true, ExactSpelling = true)]
private static extern int GdipBitmapApplyEffect(IntPtr bitmap, IntPtr effect, ref RECT rectOfInterest, bool useAuxData, IntPtr auxData, int auxDataSize);
@ -107,7 +105,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
private static extern int GdipCreateEffect(Guid guid, out IntPtr effect);
[DllImport("gdiplus.dll", SetLastError = true, ExactSpelling = true)]
private static extern int GdipDeleteEffect(IntPtr effect);
private static Guid BlurEffectGuid = new Guid("{633C80A4-1843-482B-9EF2-BE2834C5FDD4}");
private static readonly Guid BlurEffectGuid = new Guid("{633C80A4-1843-482B-9EF2-BE2834C5FDD4}");
// Constant "FieldInfo" for getting the nativeImage from the Bitmap
private static readonly FieldInfo FIELD_INFO_NATIVE_IMAGE = typeof(Bitmap).GetField("nativeImage", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
@ -118,7 +116,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
// Constant "FieldInfo" for getting the nativeImageAttributes from the ImageAttributes
private static readonly FieldInfo FIELD_INFO_NATIVE_IMAGEATTRIBUTES = typeof(ImageAttributes).GetField("nativeImageAttributes", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
private static bool isBlurEnabled = Environment.OSVersion.Version.Major >= 6;
private static bool _isBlurEnabled = Environment.OSVersion.Version.Major >= 6;
/// <summary>
/// Get the nativeImage field from the bitmap
/// </summary>
@ -173,10 +171,11 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// </summary>
/// <param name="radius"></param>
/// <returns></returns>
public static bool isBlurPossible(int radius) {
if (!isBlurEnabled) {
public static bool IsBlurPossible(int radius) {
if (!_isBlurEnabled) {
return false;
} else if (Environment.OSVersion.Version.Minor >= 2 && radius < 20) {
}
if (Environment.OSVersion.Version.Minor >= 2 && radius < 20) {
return false;
}
return true;
@ -191,7 +190,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// <param name="expandEdges">bool true if the edges are expanded with the radius</param>
/// <returns>false if there is no GDI+ available or an exception occured</returns>
public static bool ApplyBlur(Bitmap destinationBitmap, Rectangle area, int radius, bool expandEdges) {
if (!isBlurPossible(radius)) {
if (!IsBlurPossible(radius)) {
return false;
}
IntPtr hBlurParams = IntPtr.Zero;
@ -226,7 +225,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
// Everything worked, return true
return true;
} catch (Exception ex) {
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem using GdipBitmapApplyEffect: ", ex);
return false;
} finally {
@ -240,7 +239,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
Marshal.FreeHGlobal(hBlurParams);
}
} catch (Exception ex) {
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem cleaning up ApplyBlur: ", ex);
}
}
@ -251,7 +250,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// </summary>
/// <returns>false if there is no GDI+ available or an exception occured</returns>
public static bool DrawWithBlur(Graphics graphics, Bitmap image, Rectangle source, Matrix transform, ImageAttributes imageAttributes, int radius, bool expandEdges) {
if (!isBlurPossible(radius)) {
if (!IsBlurPossible(radius)) {
return false;
}
@ -291,7 +290,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
// Everything worked, return true
return true;
} catch (Exception ex) {
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem using GdipDrawImageFX: ", ex);
return false;
} finally {
@ -305,7 +304,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
Marshal.FreeHGlobal(hBlurParams);
}
} catch (Exception ex) {
isBlurEnabled = false;
_isBlurEnabled = false;
LOG.Error("Problem cleaning up DrawWithBlur: ", ex);
}
}

View file

@ -37,39 +37,16 @@ namespace GreenshotPlugin.UnmanagedHelpers {
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
#region Structs
[StructLayout(LayoutKind.Sequential)]
private struct SHITEMID {
public ushort cb;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] abID;
}
[StructLayout(LayoutKind.Sequential)]
private struct ITEMIDLIST {
public SHITEMID mkid;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
private struct BROWSEINFO {
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszTitle;
public uint ulFlags;
public IntPtr lpfn;
public int lParam;
public IntPtr iImage;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
private struct SHFILEINFO {
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
public readonly IntPtr hIcon;
public readonly int iIcon;
public readonly uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
public readonly string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
public readonly string szTypeName;
};
#endregion
@ -146,27 +123,27 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// Returns an icon for a given file extension - indicated by the name parameter.
/// See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx
/// </summary>
/// <param name="name">Filename</param>
/// <param name="filename">Filename</param>
/// <param name="size">Large or small</param>
/// <param name="linkOverlay">Whether to include the link icon</param>
/// <returns>System.Drawing.Icon</returns>
public static Icon GetFileIcon(string filename, IconSize size, bool linkOverlay) {
SHFILEINFO shfi = new SHFILEINFO();
// SHGFI_USEFILEATTRIBUTES makes it simulate, just gets the icon for the extension
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
if (true == linkOverlay) {
flags += Shell32.SHGFI_LINKOVERLAY;
if (linkOverlay) {
flags += SHGFI_LINKOVERLAY;
}
// Check the size specified for return.
if (IconSize.Small == size) {
flags += Shell32.SHGFI_SMALLICON;
flags += SHGFI_SMALLICON;
} else {
flags += Shell32.SHGFI_LARGEICON;
flags += SHGFI_LARGEICON;
}
SHGetFileInfo(Path.GetFileName(filename), Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags);
SHGetFileInfo(Path.GetFileName(filename), FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags);
// Only return an icon if we really got one
if (shfi.hIcon != IntPtr.Zero) {

View file

@ -63,7 +63,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
#region DllImports
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsWindowVisible(IntPtr hWnd);
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
[DllImport("user32", SetLastError = true)]
@ -75,9 +75,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[DllImport("user32", SetLastError = true)]
public static extern int ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int cch);
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int cch);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetWindowTextLength(IntPtr hWnd);
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
public static extern uint GetSysColor(int nIndex);
[DllImport("user32", SetLastError = true)]
@ -98,13 +98,13 @@ namespace GreenshotPlugin.UnmanagedHelpers {
public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WindowPlacement lpwndpl);
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsIconic(IntPtr hWnd);
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsZoomed(IntPtr hwnd);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
public static extern bool IsZoomed(IntPtr hwnd);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32", SetLastError = true)]
public static extern uint GetClassLong(IntPtr hWnd, int nIndex);
[DllImport("user32", SetLastError = true, EntryPoint = "GetClassLongPtr")]
public static extern IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex);
@ -112,13 +112,13 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32", CharSet=CharSet.Unicode, SetLastError=true)]
public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32", SetLastError = true)]
public extern static IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
[DllImport("user32", SetLastError = true, EntryPoint = "GetWindowLong")]
public extern static int GetWindowLong(IntPtr hwnd, int index);
public static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32", SetLastError = true, EntryPoint = "GetWindowLongPtr")]
public extern static IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex);
public static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int index, int styleFlags);
[DllImport("user32", SetLastError = true, EntryPoint = "SetWindowLongPtr")]
@ -131,9 +131,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowInfo(IntPtr hwnd, ref WindowInfo pwi);
[DllImport("user32", SetLastError = true)]
public extern static int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public static extern int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32", SetLastError = true)]
public extern static int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);
@ -187,7 +187,7 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);
[DllImport("user32", SetLastError = true)]
private static extern bool GetPhysicalCursorPos(out POINT cursorLocation);
[DllImport("user32", SetLastError=true)]
@ -359,9 +359,6 @@ namespace GreenshotPlugin.UnmanagedHelpers {
/// A SafeHandle class implementation for the hIcon
/// </summary>
public class SafeIconHandle : SafeHandleZeroOrMinusOneIsInvalid {
private SafeIconHandle() : base(true) {
}
public SafeIconHandle(IntPtr hIcon) : base(true) {
SetHandle(hIcon);
}
@ -381,24 +378,21 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[DllImport("user32", SetLastError = true)]
private static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
private IntPtr hWnd;
[SecurityCritical]
private SafeWindowDCHandle() : base(true) {
}
private readonly IntPtr _hWnd;
[SecurityCritical]
public SafeWindowDCHandle(IntPtr hWnd, IntPtr preexistingHandle) : base(true) {
this.hWnd = hWnd;
_hWnd = hWnd;
SetHandle(preexistingHandle);
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
protected override bool ReleaseHandle() {
bool returnValue = ReleaseDC(hWnd, handle);
bool returnValue = ReleaseDC(_hWnd, handle);
return returnValue;
}
public static SafeWindowDCHandle fromDesktop() {
public static SafeWindowDCHandle FromDesktop() {
IntPtr hWndDesktop = User32.GetDesktopWindow();
IntPtr hDCDesktop = GetWindowDC(hWndDesktop);
return new SafeWindowDCHandle(hWndDesktop, hDCDesktop);