19 using System.Collections.Generic;
20 using System.ComponentModel;
22 using System.Runtime.InteropServices;
32 #region Public methods 34 public static void OpenClipboard(IntPtr hWndNewOwner)
37 bool opened = Win32_OpenClipboard(hWndNewOwner);
38 while (!opened && attempts < CLIPBOARD_MAX_ATTEMPTS)
41 System.Threading.Thread.Sleep(CLIPBOARD_WAIT_MS * attempts);
42 opened = Win32_OpenClipboard(hWndNewOwner);
44 if (!opened && !Win32_OpenClipboard(hWndNewOwner))
47 string s = String.Format(
48 "Unable to get clipboard access; it is still locked by another application even after {0} attempts over {1:0.0} seconds",
49 attempts, CLIPBOARD_WAIT_MS * attempts * (attempts + 1) / 2 / 1000);
51 throw new Win32Exception(Marshal.GetLastWin32Error(), s);
55 public static void CloseClipboard()
57 Win32_CloseClipboard();
60 public static IntPtr GetClipboardData(uint uFormat)
62 IntPtr result = Win32_GetClipboardData(uFormat);
63 if (result == IntPtr.Zero)
65 throw new Win32Exception(Marshal.GetLastWin32Error());
70 public static IntPtr CopyEnhMetaFile(IntPtr hemfSrc,
string lpszFile)
72 IntPtr result = Win32_CopyEnhMetaFile(hemfSrc, lpszFile);
73 if (result == IntPtr.Zero)
75 throw new Win32Exception(Marshal.GetLastWin32Error());
80 public static void DeleteEnhMetaFile(IntPtr hemf)
82 Win32_DeleteEnhMetaFile(hemf);
85 public static string GetColorDirectory()
88 StringBuilder sb =
new StringBuilder((
int)bufSize);
89 if (Win32_GetColorDirectory(IntPtr.Zero, sb, ref bufSize))
99 public static IntPtr FindWindow(
string className)
101 return FindWindow(className, null);
106 #region Win32 API constants 108 public const uint CF_ENHMETAFILE = 14;
112 #region Win32 DLL imports 114 [DllImport(
"user32.dll", EntryPoint =
"OpenClipboard", SetLastError =
true)]
115 static extern bool Win32_OpenClipboard(IntPtr hWndNewOwner);
117 [DllImport(
"user32.dll", EntryPoint =
"CloseClipboard", SetLastError =
true)]
118 static extern bool Win32_CloseClipboard();
120 [DllImport(
"user32.dll", EntryPoint =
"GetClipboardOwner", SetLastError =
true)]
121 static extern IntPtr Win32_GetClipboardOwner();
123 [DllImport(
"user32.dll", EntryPoint =
"GetClipboardData", SetLastError =
true)]
124 static extern IntPtr Win32_GetClipboardData(uint uFormat);
126 [DllImport(
"gdi32.dll", EntryPoint =
"CopyEnhMetaFile", SetLastError =
true)]
127 static extern IntPtr Win32_CopyEnhMetaFile(IntPtr hemfSrc,
string lpszFile);
129 [DllImport(
"gdi32.dll", EntryPoint =
"DeleteEnhMetaFile", SetLastError =
true)]
130 static extern bool Win32_DeleteEnhMetaFile(IntPtr hemf);
132 [DllImport(
"mscms.dll", EntryPoint =
"GetColorDirectory", SetLastError =
true,
133 CharSet = CharSet.Auto, BestFitMapping =
false)]
134 static extern bool Win32_GetColorDirectory(IntPtr pMachineName, StringBuilder pBuffer,
179 [DllImport(
"user32.dll", SetLastError =
true)]
180 static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName);
183 [DllImport(
"user32.dll", EntryPoint =
"FindWindow", SetLastError =
true)]
184 static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly,
string lpWindowName);
189 #region Private constants 191 private const int CLIPBOARD_MAX_ATTEMPTS = 5;
192 private const int CLIPBOARD_WAIT_MS = 200;
198 private static NLog.Logger Logger {
get {
return _logger.Value; } }
200 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
Wrappers for Win32 API calls.