andrew4582

Window Screen Capturing with WPF Helper

Apr 5th, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 36.81 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace ScreenCapture
  10. {
  11.     /// <summary>
  12.     /// WPF helper
  13.     /// </summary>
  14.     public class ScreenCaptureWPFHelper
  15.     {
  16.         public static System.Windows.Media.Imaging.BitmapImage CaptureWindow(System.Windows.Window window)
  17.         {
  18.             var winhelper = new System.Windows.Interop.WindowInteropHelper(window);
  19.             using (var bitmap = ScreenCapturing.GetWindowCaptureAsBitmap(winhelper.EnsureHandle().ToInt32()))
  20.             {
  21.                 return BitmapToImageSource(bitmap, System.Drawing.Imaging.ImageFormat.Png);
  22.             }
  23.         }
  24.  
  25.         static System.Windows.Media.Imaging.BitmapImage BitmapToImageSource(System.Drawing.Bitmap bitmap, System.Drawing.Imaging.ImageFormat imgFormat)
  26.         {
  27.             using (MemoryStream memory = new MemoryStream())
  28.             {
  29.                 bitmap.Save(memory, imgFormat);
  30.                 memory.Position = 0;
  31.                 var bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
  32.                 bitmapImage.BeginInit();
  33.                 bitmapImage.StreamSource = memory;
  34.                 bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
  35.                 bitmapImage.EndInit();
  36.  
  37.                 return bitmapImage;
  38.             }
  39.         }
  40.     }
  41.  
  42.     /// <summary>
  43.     /// Provides methods for capturing windows, and window icons as bitmap images
  44.     /// </summary>
  45.     public class ScreenCapturing
  46.     {
  47.         /// <summary>
  48.         /// Gets a large icon from the window as a Bitmap
  49.         /// </summary>
  50.         public static Bitmap GetWindowLargeIconAsBitmap(int handle)
  51.         {
  52.             try
  53.             {
  54.                 int result;
  55.                 IntPtr hWnd = new IntPtr(handle);
  56.                 Win32.SendMessageTimeout(hWnd, Win32.WM_GETICON, Win32.ICON_BIG, 0, Win32.SMTO_ABORTIFHUNG, 1000, out result);
  57.  
  58.                 IntPtr hIcon = new IntPtr(result);
  59.  
  60.                 if (hIcon == IntPtr.Zero)
  61.                 {
  62.                     result = Win32.GetClassLong(hWnd, Win32.GCL_HICON);
  63.                     hIcon = new IntPtr(result);
  64.                 }
  65.  
  66.                 if (hIcon == IntPtr.Zero)
  67.                 {
  68.                     Win32.SendMessageTimeout(hWnd, Win32.WM_QUERYDRAGICON, 0, 0, Win32.SMTO_ABORTIFHUNG, 1000, out result);
  69.                     hIcon = new IntPtr(result);
  70.                 }
  71.  
  72.                 if (hIcon == IntPtr.Zero)
  73.                     return null;
  74.                 else
  75.                     return Bitmap.FromHicon(hIcon);
  76.             }
  77.             catch (System.Exception)
  78.             {
  79.                 //              System.Diagnostics.Trace.WriteLine(systemException);
  80.             }
  81.             return null;
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Gets a small icon from the window as a Bitmap
  86.         /// </summary>
  87.         public static Bitmap GetWindowSmallIconAsBitmap(int handle)
  88.         {
  89.             try
  90.             {
  91.                 int result;
  92.                 IntPtr hWnd = new IntPtr(handle);
  93.                 Win32.SendMessageTimeout(hWnd, Win32.WM_GETICON, Win32.ICON_SMALL, 0, Win32.SMTO_ABORTIFHUNG, 1000, out result);
  94.  
  95.                 IntPtr hIcon = new IntPtr(result);
  96.  
  97.                 if (hIcon == IntPtr.Zero)
  98.                 {
  99.                     result = Win32.GetClassLong(hWnd, Win32.GCL_HICONSM);
  100.                     hIcon = new IntPtr(result);
  101.                 }
  102.  
  103.                 if (hIcon == IntPtr.Zero)
  104.                 {
  105.                     Win32.SendMessageTimeout(hWnd, Win32.WM_QUERYDRAGICON, 0, 0, Win32.SMTO_ABORTIFHUNG, 1000, out result);
  106.                     hIcon = new IntPtr(result);
  107.                 }
  108.  
  109.                 if (hIcon == IntPtr.Zero)
  110.                     return null;
  111.                 else
  112.                     return Bitmap.FromHicon(hIcon);
  113.             }
  114.             catch (System.Exception)
  115.             {
  116.                 //              System.Diagnostics.Trace.WriteLine(systemException);
  117.             }
  118.             return null;
  119.         }
  120.  
  121.         /// <summary>
  122.         /// Gets a Bitmap of the window (aka. screen capture)
  123.         /// </summary>
  124.         public static Bitmap GetPrimaryDesktopWindowCaptureAsBitmap()
  125.         {
  126.             // create a graphics object from the window handle
  127.             using (Graphics gfxWindow = Graphics.FromHwnd(IntPtr.Zero))
  128.             {
  129.  
  130.                 // create a bitmap from the visible clipping bounds of the graphics object from the window
  131.                 Bitmap bitmap = new Bitmap((int)gfxWindow.VisibleClipBounds.Width, (int)gfxWindow.VisibleClipBounds.Height, gfxWindow);
  132.  
  133.                 // create a graphics object from the bitmap
  134.                 using (Graphics gfxBitmap = Graphics.FromImage(bitmap))
  135.                 {
  136.  
  137.                     // get a device context for the window
  138.                     IntPtr hdcWindow = gfxWindow.GetHdc();
  139.  
  140.                     // get a device context for the bitmap
  141.                     IntPtr hdcBitmap = gfxBitmap.GetHdc();
  142.                     try
  143.                     {
  144.  
  145.                         // bitblt the window to the bitmap
  146.                         Win32.BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcWindow, 0, 0, (int)Win32.TernaryRasterOperations.SRCCOPY);
  147.  
  148.                     }
  149.                     finally
  150.                     {
  151.                         // release the bitmap's device context
  152.                         if (hdcBitmap != IntPtr.Zero)
  153.                             gfxBitmap.ReleaseHdc(hdcBitmap);
  154.  
  155.                         // release the window's device context
  156.                         if (hdcWindow != IntPtr.Zero)
  157.                             gfxWindow.ReleaseHdc(hdcWindow);
  158.                     }
  159.                 }
  160.                 // return the bitmap of the window
  161.                 return bitmap;
  162.             }
  163.         }
  164.         public static Bitmap GetDesktopWindowCaptureAsBitmap(bool justPrimary = false)
  165.         {
  166.             Rectangle rcScreen = Rectangle.Empty;
  167.             Screen[] screens = Screen.AllScreens;
  168.  
  169.             if (!justPrimary)
  170.             {
  171.                 // Create a rectangle encompassing all screens...
  172.                 foreach (Screen screen in screens)
  173.                     rcScreen = Rectangle.Union(rcScreen, screen.Bounds);
  174.                 //          System.Diagnostics.Trace.WriteLine(rcScreen);
  175.             }
  176.             else
  177.             {
  178.                 rcScreen = Screen.PrimaryScreen.Bounds;
  179.                 //rcScreen = Rectangle.Union(rcScreen,Screen.PrimaryScreen.Bounds);
  180.             }
  181.             // Create a composite bitmap of the size of all screens...
  182.             Bitmap finalBitmap = new Bitmap(rcScreen.Width, rcScreen.Height);
  183.  
  184.             // Get a graphics object for the composite bitmap and initialize it...
  185.             using (Graphics g = Graphics.FromImage(finalBitmap))
  186.             {
  187.  
  188.                 IntPtr hdcDestination = IntPtr.Zero;
  189.                 try
  190.                 {
  191.                     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
  192.                     g.FillRectangle(SystemBrushes.Desktop, 0, 0, rcScreen.Width - rcScreen.X, rcScreen.Height - rcScreen.Y);
  193.  
  194.                     // Get an HDC for the composite area...
  195.                     hdcDestination = g.GetHdc();
  196.                     // Now, loop through screens, BitBlting each to the composite HDC created above...
  197.                     foreach (Screen screen in screens)
  198.                     {
  199.  
  200.                         // Create DC for each source monitor...
  201.                         IntPtr hdcSource = Win32.CreateDC(IntPtr.Zero, screen.DeviceName, IntPtr.Zero, IntPtr.Zero);
  202.  
  203.                         try
  204.                         {
  205.  
  206.                             // Blt the source directly to the composite destination...
  207.                             int xDest = screen.Bounds.X - rcScreen.X;
  208.                             int yDest = screen.Bounds.Y - rcScreen.Y;
  209.  
  210.                             //bool success = BitBlt(hdcDestination, xDest, yDest, screen.Bounds.Width, screen.Bounds.Height, hdcSource, 0, 0, (int)TernaryRasterOperations.SRCCOPY);
  211.                             bool success = Win32.StretchBlt(hdcDestination, xDest, yDest, screen.Bounds.Width, screen.Bounds.Height, hdcSource, 0, 0, screen.Bounds.Width, screen.Bounds.Height, (int)Win32.TernaryRasterOperations.SRCCOPY);
  212.  
  213.                             //System.Diagnostics.Trace.WriteLine(screen.Bounds);
  214.                             if (!success)
  215.                             {
  216.                                 System.ComponentModel.Win32Exception win32Exception = new System.ComponentModel.Win32Exception();
  217.                                 System.Diagnostics.Trace.WriteLine(win32Exception);
  218.                             }
  219.                         }
  220.                         finally
  221.                         {
  222.                             // Cleanup source HDC...
  223.                             Win32.DeleteDC(hdcSource);
  224.                         }
  225.                     }
  226.                 }
  227.                 finally
  228.                 {
  229.                     // Cleanup destination HDC and Graphics...
  230.                     if (hdcDestination != IntPtr.Zero)
  231.                         g.ReleaseHdc(hdcDestination);
  232.                 }
  233.             }
  234.             return finalBitmap;
  235.         }
  236.  
  237.         /// <summary>
  238.         /// Gets a Bitmap of the window (aka. screen capture)
  239.         /// </summary>
  240.         public static Bitmap GetWindowCaptureAsBitmap(int handle)
  241.         {
  242.             IntPtr hWnd = new IntPtr(handle);
  243.             Win32.Rect rc = new Win32.Rect();
  244.             if (!Win32.GetWindowRect(hWnd, ref rc))
  245.                 return null;
  246.  
  247.             //          Win32.WindowInfo wi = new Win32.WindowInfo();
  248.             //          wi.size = Marshal.SizeOf(wi);
  249.             //          if (!Win32.GetWindowInfo(hWnd, ref wi))
  250.             //              return null;
  251.  
  252.             // create a bitmap from the visible clipping bounds of the graphics object from the window
  253.             Bitmap bitmap = new Bitmap(rc.Width, rc.Height);
  254.  
  255.             // create a graphics object from the bitmap
  256.             using (Graphics gfxBitmap = Graphics.FromImage(bitmap))
  257.             {
  258.  
  259.                 // get a device context for the bitmap
  260.                 IntPtr hdcBitmap = gfxBitmap.GetHdc();
  261.  
  262.                 // get a device context for the window
  263.                 IntPtr hdcWindow = Win32.GetWindowDC(hWnd);
  264.  
  265.                 // bitblt the window to the bitmap
  266.                 Win32.BitBlt(hdcBitmap, 0, 0, rc.Width, rc.Height, hdcWindow, 0, 0, (int)Win32.TernaryRasterOperations.SRCCOPY);
  267.  
  268.                 // release the bitmap's device context
  269.                 gfxBitmap.ReleaseHdc(hdcBitmap);
  270.  
  271.                 Win32.ReleaseDC(hWnd, hdcWindow);
  272.             }
  273.             // return the bitmap of the window
  274.             return bitmap;
  275.         }
  276.  
  277.         /// <summary>
  278.         /// Convert a bitmap to a byte array
  279.         /// </summary>
  280.         /// <param name="bmp"></param>
  281.         /// <returns></returns>
  282.         public static byte[] GetBytes(Bitmap bmp)
  283.         {
  284.             if (bmp == null)
  285.                 return new byte[0];
  286.  
  287.             using (MemoryStream stream = new System.IO.MemoryStream())
  288.             {
  289.                 bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  290.                 return stream.GetBuffer();
  291.             }
  292.         }
  293.  
  294.         public static byte[] GetBytes(Icon icon)
  295.         {
  296.             if (icon == null)
  297.                 return new byte[0];
  298.  
  299.             using (MemoryStream stream = new System.IO.MemoryStream())
  300.             {
  301.                 icon.Save(stream);
  302.                 return stream.GetBuffer();
  303.             }
  304.         }
  305.  
  306.         public static Icon GetIcon(byte[] bytes)
  307.         {
  308.             if (bytes == null)
  309.                 return null;
  310.  
  311.             if (bytes.Length == 0)
  312.                 return null;
  313.  
  314.             using (MemoryStream stream = new System.IO.MemoryStream(bytes))
  315.             {
  316.                 using (Icon icon = new Icon(stream))
  317.                 {
  318.                     try
  319.                     {
  320.                         return (Icon)icon.Clone();
  321.                     }
  322.                     catch (Exception ex)
  323.                     {
  324.                         Trace.TraceError(ex.ToString());
  325.                     }
  326.                     return null;
  327.                 }
  328.             }
  329.         }
  330.  
  331.         /// <summary>
  332.         /// Converts a byte array to a bitmap
  333.         /// </summary>
  334.         /// <param name="bytes"></param>
  335.         /// <returns></returns>
  336.         public static Bitmap GetBitmap(byte[] bytes)
  337.         {
  338.             if (bytes == null)
  339.                 return null;
  340.  
  341.             if (bytes.Length == 0)
  342.                 return null;
  343.  
  344.             using (MemoryStream stream = new System.IO.MemoryStream(bytes))
  345.             {
  346.                 using (Bitmap b = new Bitmap(stream))
  347.                 {
  348.                     try
  349.                     {
  350.                         return (Bitmap)b.Clone();
  351.                     }
  352.                     catch (Exception ex)
  353.                     {
  354.                         Trace.TraceError(ex.ToString());
  355.                     }
  356.                     return null;
  357.                 }
  358.             }
  359.         }
  360.  
  361.         /// <summary>
  362.         /// Gets a byte array of a bitmap for the large icon for the window specified by the handle
  363.         /// </summary>
  364.         /// <param name="hWnd"></param>
  365.         /// <returns></returns>
  366.         public static byte[] GetWindowLargeIconAsByteArray(int handle)
  367.         {
  368.             return ScreenCapturing.GetBytes(ScreenCapturing.GetWindowLargeIconAsBitmap(handle));
  369.         }
  370.  
  371.         /// <summary>
  372.         /// Gets a byte array of a bitmap for the small icon for the window specified by the handle
  373.         /// </summary>
  374.         /// <param name="hWnd"></param>
  375.         /// <returns></returns>
  376.         public static byte[] GetWindowSmallIconAsByteArray(int handle)
  377.         {
  378.             return ScreenCapturing.GetBytes(ScreenCapturing.GetWindowSmallIconAsBitmap(handle));
  379.         }
  380.  
  381.         /// <summary>
  382.         /// Gets a byte array of a bitmap for the desktop window
  383.         /// </summary>
  384.         /// <returns></returns>
  385.         public static byte[] GetDesktopWindowCaptureAsByteArray()
  386.         {
  387.             return ScreenCapturing.GetBytes(ScreenCapturing.GetDesktopWindowCaptureAsBitmap());
  388.         }
  389.         public static void WriteDesktopWindowCaptureToFile(string fileName)
  390.         {
  391.             System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Png;
  392.             WriteDesktopWindowCaptureToFile(fileName, format);
  393.         }
  394.         public static void WriteDesktopWindowCaptureToFile(string fileName, System.Drawing.Imaging.ImageFormat format)
  395.         {
  396.             using (Bitmap bmp = ScreenCapturing.GetDesktopWindowCaptureAsBitmap())
  397.             {
  398.                 if (bmp == null)
  399.                     return;
  400.                 bmp.Save(fileName, format);
  401.  
  402.             }
  403.         }
  404.         public static void WritePrimaryDesktopWindowCaptureToFile(string fileName, System.Drawing.Imaging.ImageFormat format)
  405.         {
  406.             using (Bitmap bmp = ScreenCapturing.GetDesktopWindowCaptureAsBitmap(true))
  407.             {
  408.                 if (bmp == null)
  409.                     return;
  410.                 bmp.Save(fileName, format);
  411.             }
  412.         }
  413.  
  414.         /// <summary>
  415.         /// Gets a byte array of a bitmap for the window specified by the handle
  416.         /// </summary>
  417.         /// <param name="hWnd"></param>
  418.         /// <returns></returns>
  419.         public static byte[] GetWindowCaptureAsByteArray(int handle)
  420.         {
  421.             return ScreenCapturing.GetBytes(ScreenCapturing.GetWindowCaptureAsBitmap(handle));
  422.         }
  423.  
  424.         /// <summary>
  425.         /// Calculates the total screen size, for all attached monitors, by unioning each monitor's bounds together
  426.         /// </summary>
  427.         /// <returns></returns>
  428.         public static Rectangle CalculateTotalScreenSize()
  429.         {
  430.             Rectangle rcScreen = Rectangle.Empty;
  431.             Screen[] screens = Screen.AllScreens;
  432.  
  433.             // Create a rectangle encompassing all screens...
  434.             foreach (Screen screen in screens)
  435.                 rcScreen = Rectangle.Union(rcScreen, screen.Bounds);
  436.  
  437.             return rcScreen;
  438.         }
  439.  
  440.         /// <summary>
  441.         /// Exposes constants, structures, enumerations, and functions from the Win32 API found in the Platform SDK.
  442.         /// </summary>
  443.         class Win32
  444.         {
  445.             public const uint MAX_PATH = 260;
  446.  
  447.             #region Hooks - user32.dll
  448.  
  449.             /// <summary>
  450.             /// Defines the layout of the MouseHookStruct
  451.             /// </summary>
  452.             [StructLayout(LayoutKind.Sequential)]
  453.             public struct MSLLHOOKSTRUCT
  454.             {
  455.                 public Point Point;
  456.                 public int MouseData;
  457.                 public int Flags;
  458.                 public int Time;
  459.                 public int ExtraInfo;
  460.             }
  461.  
  462.             /// <summary>
  463.             ///  EventArgs class for use as parameters by HookEventHandler delegates
  464.             /// </summary>
  465.             public class HookEventArgs : EventArgs
  466.             {
  467.                 private int _code;
  468.                 private IntPtr _wParam;
  469.                 private IntPtr _lParam;
  470.  
  471.                 /// <summary>
  472.                 /// Initializes a new instance of the HookEventArgs class
  473.                 /// </summary>
  474.                 /// <param name="code">the hook code</param>
  475.                 /// <param name="wParam">hook specific information</param>
  476.                 /// <param name="lParam">hook specific information</param>
  477.                 public HookEventArgs(int code, IntPtr wParam, IntPtr lParam)
  478.                 {
  479.                     _code = code;
  480.                     _wParam = wParam;
  481.                     _lParam = lParam;
  482.                 }
  483.  
  484.                 /// <summary>
  485.                 /// The hook code
  486.                 /// </summary>
  487.                 public int Code
  488.                 {
  489.                     get
  490.                     {
  491.                         return _code;
  492.                     }
  493.                 }
  494.  
  495.                 /// <summary>
  496.                 /// A pointer to data
  497.                 /// </summary>
  498.                 public IntPtr wParam
  499.                 {
  500.                     get
  501.                     {
  502.                         return _wParam;
  503.                     }
  504.                 }
  505.  
  506.                 /// <summary>
  507.                 /// A pointer to data
  508.                 /// </summary>
  509.                 public IntPtr lParam
  510.                 {
  511.                     get
  512.                     {
  513.                         return _lParam;
  514.                     }
  515.                 }
  516.             }
  517.  
  518.             /// <summary>
  519.             /// Event delegate for use with the HookEventArgs class
  520.             /// </summary>
  521.             public delegate void HookEventHandler(object sender, HookEventArgs e);
  522.  
  523.             /// <summary>
  524.             /// Defines the various types of hooks that are available in Windows
  525.             /// </summary>
  526.             public enum HookTypes : int
  527.             {
  528.                 WH_JOURNALRECORD = 0,
  529.                 WH_JOURNALPLAYBACK = 1,
  530.                 WH_KEYBOARD = 2,
  531.                 WH_GETMESSAGE = 3,
  532.                 WH_CALLWNDPROC = 4,
  533.                 WH_CBT = 5,
  534.                 WH_SYSMSGFILTER = 6,
  535.                 WH_MOUSE = 7,
  536.                 WH_HARDWARE = 8,
  537.                 WH_DEBUG = 9,
  538.                 WH_SHELL = 10,
  539.                 WH_FOREGROUNDIDLE = 11,
  540.                 WH_CALLWNDPROCRET = 12,
  541.                 WH_KEYBOARD_LL = 13,
  542.                 WH_MOUSE_LL = 14
  543.             }
  544.  
  545.             public enum ShellHookMessages
  546.             {
  547.                 HSHELL_WINDOWCREATED = 1,
  548.                 HSHELL_WINDOWDESTROYED = 2,
  549.                 HSHELL_ACTIVATESHELLWINDOW = 3,
  550.                 HSHELL_WINDOWACTIVATED = 4,
  551.                 HSHELL_GETMINRECT = 5,
  552.                 HSHELL_REDRAW = 6,
  553.                 HSHELL_TASKMAN = 7,
  554.                 HSHELL_LANGUAGE = 8,
  555.                 HSHELL_ACCESSIBILITYSTATE = 11
  556.             }
  557.  
  558.             public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  559.  
  560.             [DllImport("user32.dll")]
  561.             public static extern IntPtr SetWindowsHookEx(HookTypes hookType, HookProc hookProc, IntPtr hInstance, int nThreadId);
  562.  
  563.             [DllImport("user32.dll")]
  564.             public static extern int UnhookWindowsHookEx(IntPtr hookHandle);
  565.  
  566.             [DllImport("user32.dll")]
  567.             public static extern int CallNextHookEx(IntPtr hookHandle, int nCode, IntPtr wParam, IntPtr lParam);
  568.  
  569.             [DllImport("user32.dll")]
  570.             public static extern int RegisterShellHookWindow(IntPtr hWnd);
  571.  
  572.             [DllImport("user32.dll")]
  573.             public static extern int DeregisterShellHookWindow(IntPtr hWnd);
  574.  
  575.             #endregion Hooks
  576.  
  577.             #region System - Kernel32.dll
  578.  
  579.             [DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  580.             public static extern Int32 GetLastError();
  581.  
  582.             #endregion
  583.  
  584.             #region Windows - user32.dll
  585.  
  586.             public const int GWL_HWNDPARENT = (-8);
  587.             public const int GWL_EXSTYLE = (-20);
  588.             public const int GWL_STYLE = (-16);
  589.             public const int GCL_HICON = (-14);
  590.             public const int GCL_HICONSM = (-34);
  591.             public const int WM_QUERYDRAGICON = 0x37;
  592.             public const int WM_GETICON = 0x7F;
  593.             public const int WM_SETICON = 0x80;
  594.             public const int ICON_SMALL = 0;
  595.             public const int ICON_BIG = 1;
  596.             public const int SMTO_ABORTIFHUNG = 0x2;
  597.             public const int TRUE = 1;
  598.             public const int FALSE = 0;
  599.  
  600.             public const int WHITE_BRUSH = 0;
  601.             public const int LTGRAY_BRUSH = 1;
  602.             public const int GRAY_BRUSH = 2;
  603.             public const int DKGRAY_BRUSH = 3;
  604.             public const int BLACK_BRUSH = 4;
  605.             public const int NULL_BRUSH = 5;
  606.             public const int HOLLOW_BRUSH = NULL_BRUSH;
  607.             public const int WHITE_PEN = 6;
  608.             public const int BLACK_PEN = 7;
  609.             public const int NULL_PEN = 8;
  610.             public const int OEM_FIXED_FONT = 10;
  611.             public const int ANSI_FIXED_FONT = 11;
  612.             public const int ANSI_VAR_FONT = 12;
  613.             public const int SYSTEM_FONT = 13;
  614.             public const int DEVICE_DEFAULT_FONT = 14;
  615.             public const int DEFAULT_PALETTE = 15;
  616.             public const int SYSTEM_FIXED_FONT = 16;
  617.  
  618.  
  619.             public const int RDW_INVALIDATE = 0x0001;
  620.             public const int RDW_INTERNALPAINT = 0x0002;
  621.             public const int RDW_ERASE = 0x0004;
  622.  
  623.             public const int RDW_VALIDATE = 0x0008;
  624.             public const int RDW_NOINTERNALPAINT = 0x0010;
  625.             public const int RDW_NOERASE = 0x0020;
  626.  
  627.             public const int RDW_NOCHILDREN = 0x0040;
  628.             public const int RDW_ALLCHILDREN = 0x0080;
  629.  
  630.             public const int RDW_UPDATENOW = 0x0100;
  631.             public const int RDW_ERASENOW = 0x0200;
  632.  
  633.             public const int RDW_FRAME = 0x0400;
  634.             public const int RDW_NOFRAME = 0x0800;
  635.  
  636.  
  637.  
  638.             public enum ShowWindowCmds
  639.             {
  640.                 SW_HIDE = 0,
  641.                 SW_SHOWNORMAL = 1,
  642.                 SW_NORMAL = 1,
  643.                 SW_SHOWMINIMIZED = 2,
  644.                 SW_SHOWMAXIMIZED = 3,
  645.                 SW_MAXIMIZE = 3,
  646.                 SW_SHOWNOACTIVATE = 4,
  647.                 SW_SHOW = 5,
  648.                 SW_MINIMIZE = 6,
  649.                 SW_SHOWMINNOACTIVE = 7,
  650.                 SW_SHOWNA = 8,
  651.                 SW_RESTORE = 9,
  652.                 SW_SHOWDEFAULT = 10,
  653.                 SW_FORCEMINIMIZE = 11,
  654.                 SW_MAX = 11
  655.             }
  656.  
  657.             public const int HIDE_WINDOW = 0;
  658.             public const int SHOW_OPENWINDOW = 1;
  659.             public const int SHOW_ICONWINDOW = 2;
  660.             public const int SHOW_FULLSCREEN = 3;
  661.             public const int SHOW_OPENNOACTIVATE = 4;
  662.             public const int SW_PARENTCLOSING = 1;
  663.             public const int SW_OTHERZOOM = 2;
  664.             public const int SW_PARENTOPENING = 3;
  665.             public const int SW_OTHERUNZOOM = 4;
  666.  
  667.             public const int SWP_NOSIZE = 0x0001;
  668.             public const int SWP_NOMOVE = 0x0002;
  669.             public const int SWP_NOZORDER = 0x0004;
  670.             public const int SWP_NOREDRAW = 0x0008;
  671.             public const int SWP_NOACTIVATE = 0x0010;
  672.             public const int SWP_FRAMECHANGED = 0x0020; /* The frame changed: send WM_NCCALCSIZE */
  673.             public const int SWP_SHOWWINDOW = 0x0040;
  674.             public const int SWP_HIDEWINDOW = 0x0080;
  675.             public const int SWP_NOCOPYBITS = 0x0100;
  676.             public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
  677.             public const int SWP_NOSENDCHANGING = 0x0400;  /* Don't send WM_WINDOWPOSCHANGING */
  678.             public const int SWP_DRAWFRAME = SWP_FRAMECHANGED;
  679.             public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER;
  680.             public const int SWP_DEFERERASE = 0x2000;
  681.             public const int SWP_ASYNCWINDOWPOS = 0x4000;
  682.  
  683.             public const int HWND_TOP = 0;
  684.             public const int HWND_BOTTOM = 1;
  685.             public const int HWND_TOPMOST = -1;
  686.             public const int HWND_NOTOPMOST = -2;
  687.  
  688.             public enum PeekMessageFlags
  689.             {
  690.                 PM_NOREMOVE = 0,
  691.                 PM_REMOVE = 1,
  692.                 PM_NOYIELD = 2
  693.             }
  694.  
  695.             public enum WindowMessages
  696.             {
  697.                 WM_NULL = 0x0000,
  698.                 WM_CREATE = 0x0001,
  699.                 WM_DESTROY = 0x0002,
  700.                 WM_MOVE = 0x0003,
  701.                 WM_SIZE = 0x0005,
  702.                 WM_ACTIVATE = 0x0006,
  703.                 WM_SETFOCUS = 0x0007,
  704.                 WM_KILLFOCUS = 0x0008,
  705.                 WM_ENABLE = 0x000A,
  706.                 WM_SETREDRAW = 0x000B,
  707.                 WM_SETTEXT = 0x000C,
  708.                 WM_GETTEXT = 0x000D,
  709.                 WM_GETTEXTLENGTH = 0x000E,
  710.                 WM_PAINT = 0x000F,
  711.                 WM_CLOSE = 0x0010,
  712.                 WM_QUERYENDSESSION = 0x0011,
  713.                 WM_QUIT = 0x0012,
  714.                 WM_QUERYOPEN = 0x0013,
  715.                 WM_ERASEBKGND = 0x0014,
  716.                 WM_SYSCOLORCHANGE = 0x0015,
  717.                 WM_ENDSESSION = 0x0016,
  718.                 WM_SHOWWINDOW = 0x0018,
  719.                 WM_CTLCOLOR = 0x0019,
  720.                 WM_WININICHANGE = 0x001A,
  721.                 WM_SETTINGCHANGE = 0x001A,
  722.                 WM_DEVMODECHANGE = 0x001B,
  723.                 WM_ACTIVATEAPP = 0x001C,
  724.                 WM_FONTCHANGE = 0x001D,
  725.                 WM_TIMECHANGE = 0x001E,
  726.                 WM_CANCELMODE = 0x001F,
  727.                 WM_SETCURSOR = 0x0020,
  728.                 WM_MOUSEACTIVATE = 0x0021,
  729.                 WM_CHILDACTIVATE = 0x0022,
  730.                 WM_QUEUESYNC = 0x0023,
  731.                 WM_GETMINMAXINFO = 0x0024,
  732.                 WM_PAINTICON = 0x0026,
  733.                 WM_ICONERASEBKGND = 0x0027,
  734.                 WM_NEXTDLGCTL = 0x0028,
  735.                 WM_SPOOLERSTATUS = 0x002A,
  736.                 WM_DRAWITEM = 0x002B,
  737.                 WM_MEASUREITEM = 0x002C,
  738.                 WM_DELETEITEM = 0x002D,
  739.                 WM_VKEYTOITEM = 0x002E,
  740.                 WM_CHARTOITEM = 0x002F,
  741.                 WM_SETFONT = 0x0030,
  742.                 WM_GETFONT = 0x0031,
  743.                 WM_SETHOTKEY = 0x0032,
  744.                 WM_GETHOTKEY = 0x0033,
  745.                 WM_QUERYDRAGICON = 0x0037,
  746.                 WM_COMPAREITEM = 0x0039,
  747.                 WM_GETOBJECT = 0x003D,
  748.                 WM_COMPACTING = 0x0041,
  749.                 WM_COMMNOTIFY = 0x0044,
  750.                 WM_WINDOWPOSCHANGING = 0x0046,
  751.                 WM_WINDOWPOSCHANGED = 0x0047,
  752.                 WM_POWER = 0x0048,
  753.                 WM_COPYDATA = 0x004A,
  754.                 WM_CANCELJOURNAL = 0x004B,
  755.                 WM_NOTIFY = 0x004E,
  756.                 WM_INPUTLANGCHANGEREQUEST = 0x0050,
  757.                 WM_INPUTLANGCHANGE = 0x0051,
  758.                 WM_TCARD = 0x0052,
  759.                 WM_HELP = 0x0053,
  760.                 WM_USERCHANGED = 0x0054,
  761.                 WM_NOTIFYFORMAT = 0x0055,
  762.                 WM_CONTEXTMENU = 0x007B,
  763.                 WM_STYLECHANGING = 0x007C,
  764.                 WM_STYLECHANGED = 0x007D,
  765.                 WM_DISPLAYCHANGE = 0x007E,
  766.                 WM_GETICON = 0x007F,
  767.                 WM_SETICON = 0x0080,
  768.                 WM_NCCREATE = 0x0081,
  769.                 WM_NCDESTROY = 0x0082,
  770.                 WM_NCCALCSIZE = 0x0083,
  771.                 WM_NCHITTEST = 0x0084,
  772.                 WM_NCPAINT = 0x0085,
  773.                 WM_NCACTIVATE = 0x0086,
  774.                 WM_GETDLGCODE = 0x0087,
  775.                 WM_SYNCPAINT = 0x0088,
  776.                 WM_NCMOUSEMOVE = 0x00A0,
  777.                 WM_NCLBUTTONDOWN = 0x00A1,
  778.                 WM_NCLBUTTONUP = 0x00A2,
  779.                 WM_NCLBUTTONDBLCLK = 0x00A3,
  780.                 WM_NCRBUTTONDOWN = 0x00A4,
  781.                 WM_NCRBUTTONUP = 0x00A5,
  782.                 WM_NCRBUTTONDBLCLK = 0x00A6,
  783.                 WM_NCMBUTTONDOWN = 0x00A7,
  784.                 WM_NCMBUTTONUP = 0x00A8,
  785.                 WM_NCMBUTTONDBLCLK = 0x00A9,
  786.                 WM_KEYDOWN = 0x0100,
  787.                 WM_KEYUP = 0x0101,
  788.                 WM_CHAR = 0x0102,
  789.                 WM_DEADCHAR = 0x0103,
  790.                 WM_SYSKEYDOWN = 0x0104,
  791.                 WM_SYSKEYUP = 0x0105,
  792.                 WM_SYSCHAR = 0x0106,
  793.                 WM_SYSDEADCHAR = 0x0107,
  794.                 WM_KEYLAST = 0x0108,
  795.                 WM_IME_STARTCOMPOSITION = 0x010D,
  796.                 WM_IME_ENDCOMPOSITION = 0x010E,
  797.                 WM_IME_COMPOSITION = 0x010F,
  798.                 WM_IME_KEYLAST = 0x010F,
  799.                 WM_INITDIALOG = 0x0110,
  800.                 WM_COMMAND = 0x0111,
  801.                 WM_SYSCOMMAND = 0x0112,
  802.                 WM_TIMER = 0x0113,
  803.                 WM_HSCROLL = 0x0114,
  804.                 WM_VSCROLL = 0x0115,
  805.                 WM_INITMENU = 0x0116,
  806.                 WM_INITMENUPOPUP = 0x0117,
  807.                 WM_MENUSELECT = 0x011F,
  808.                 WM_MENUCHAR = 0x0120,
  809.                 WM_ENTERIDLE = 0x0121,
  810.                 WM_MENURBUTTONUP = 0x0122,
  811.                 WM_MENUDRAG = 0x0123,
  812.                 WM_MENUGETOBJECT = 0x0124,
  813.                 WM_UNINITMENUPOPUP = 0x0125,
  814.                 WM_MENUCOMMAND = 0x0126,
  815.                 WM_CTLCOLORMSGBOX = 0x0132,
  816.                 WM_CTLCOLOREDIT = 0x0133,
  817.                 WM_CTLCOLORLISTBOX = 0x0134,
  818.                 WM_CTLCOLORBTN = 0x0135,
  819.                 WM_CTLCOLORDLG = 0x0136,
  820.                 WM_CTLCOLORSCROLLBAR = 0x0137,
  821.                 WM_CTLCOLORSTATIC = 0x0138,
  822.                 WM_MOUSEMOVE = 0x0200,
  823.                 WM_LBUTTONDOWN = 0x0201,
  824.                 WM_LBUTTONUP = 0x0202,
  825.                 WM_LBUTTONDBLCLK = 0x0203,
  826.                 WM_RBUTTONDOWN = 0x0204,
  827.                 WM_RBUTTONUP = 0x0205,
  828.                 WM_RBUTTONDBLCLK = 0x0206,
  829.                 WM_MBUTTONDOWN = 0x0207,
  830.                 WM_MBUTTONUP = 0x0208,
  831.                 WM_MBUTTONDBLCLK = 0x0209,
  832.                 WM_MOUSEWHEEL = 0x020A,
  833.                 WM_PARENTNOTIFY = 0x0210,
  834.                 WM_ENTERMENULOOP = 0x0211,
  835.                 WM_EXITMENULOOP = 0x0212,
  836.                 WM_NEXTMENU = 0x0213,
  837.                 WM_SIZING = 0x0214,
  838.                 WM_CAPTURECHANGED = 0x0215,
  839.                 WM_MOVING = 0x0216,
  840.                 WM_DEVICECHANGE = 0x0219,
  841.                 WM_MDICREATE = 0x0220,
  842.                 WM_MDIDESTROY = 0x0221,
  843.                 WM_MDIACTIVATE = 0x0222,
  844.                 WM_MDIRESTORE = 0x0223,
  845.                 WM_MDINEXT = 0x0224,
  846.                 WM_MDIMAXIMIZE = 0x0225,
  847.                 WM_MDITILE = 0x0226,
  848.                 WM_MDICASCADE = 0x0227,
  849.                 WM_MDIICONARRANGE = 0x0228,
  850.                 WM_MDIGETACTIVE = 0x0229,
  851.                 WM_MDISETMENU = 0x0230,
  852.                 WM_ENTERSIZEMOVE = 0x0231,
  853.                 WM_EXITSIZEMOVE = 0x0232,
  854.                 WM_DROPFILES = 0x0233,
  855.                 WM_MDIREFRESHMENU = 0x0234,
  856.                 WM_IME_SETCONTEXT = 0x0281,
  857.                 WM_IME_NOTIFY = 0x0282,
  858.                 WM_IME_CONTROL = 0x0283,
  859.                 WM_IME_COMPOSITIONFULL = 0x0284,
  860.                 WM_IME_SELECT = 0x0285,
  861.                 WM_IME_CHAR = 0x0286,
  862.                 WM_IME_REQUEST = 0x0288,
  863.                 WM_IME_KEYDOWN = 0x0290,
  864.                 WM_IME_KEYUP = 0x0291,
  865.                 WM_MOUSEHOVER = 0x02A1,
  866.                 WM_MOUSELEAVE = 0x02A3,
  867.                 WM_CUT = 0x0300,
  868.                 WM_COPY = 0x0301,
  869.                 WM_PASTE = 0x0302,
  870.                 WM_CLEAR = 0x0303,
  871.                 WM_UNDO = 0x0304,
  872.                 WM_RENDERFORMAT = 0x0305,
  873.                 WM_RENDERALLFORMATS = 0x0306,
  874.                 WM_DESTROYCLIPBOARD = 0x0307,
  875.                 WM_DRAWCLIPBOARD = 0x0308,
  876.                 WM_PAINTCLIPBOARD = 0x0309,
  877.                 WM_VSCROLLCLIPBOARD = 0x030A,
  878.                 WM_SIZECLIPBOARD = 0x030B,
  879.                 WM_ASKCBFORMATNAME = 0x030C,
  880.                 WM_CHANGECBCHAIN = 0x030D,
  881.                 WM_HSCROLLCLIPBOARD = 0x030E,
  882.                 WM_QUERYNEWPALETTE = 0x030F,
  883.                 WM_PALETTEISCHANGING = 0x0310,
  884.                 WM_PALETTECHANGED = 0x0311,
  885.                 WM_HOTKEY = 0x0312,
  886.                 WM_PRINT = 0x0317,
  887.                 WM_PRINTCLIENT = 0x0318,
  888.                 WM_HANDHELDFIRST = 0x0358,
  889.                 WM_HANDHELDLAST = 0x035F,
  890.                 WM_AFXFIRST = 0x0360,
  891.                 WM_AFXLAST = 0x037F,
  892.                 WM_PENWINFIRST = 0x0380,
  893.                 WM_PENWINLAST = 0x038F,
  894.                 WM_APP = 0x8000,
  895.                 WM_USER = 0x0400,
  896.                 WM_REFLECT = WM_USER + 0x1c00
  897.             }
  898.  
  899.             /// <summary>
  900.             /// Defines the style bits that a window can have
  901.             /// </summary>
  902.             public enum WindowStyles : uint
  903.             {
  904.                 WS_BORDER = 0x800000,
  905.                 WS_CAPTION = 0xC00000,//               '  WS_BORDER Or WS_DLGFRAME
  906.                 WS_CHILD = 0x40000000,
  907.                 WS_CHILDWINDOW = (WS_CHILD),
  908.                 WS_CLIPCHILDREN = 0x2000000,
  909.                 WS_CLIPSIBLINGS = 0x4000000,
  910.                 WS_DISABLED = 0x8000000,
  911.                 WS_DLGFRAME = 0x400000,
  912.                 WS_EX_ACCEPTFILES = 0x10,
  913.                 WS_EX_DLGMODALFRAME = 0x1,
  914.                 WS_EX_NOPARENTNOTIFY = 0x4,
  915.                 WS_EX_TOPMOST = 0x8,
  916.                 WS_EX_TRANSPARENT = 0x20,
  917.                 WS_EX_TOOLWINDOW = 0x80,
  918.                 WS_EX_APPWINDOW = 0x40000,
  919.                 WS_GROUP = 0x20000,
  920.                 WS_HSCROLL = 0x100000,
  921.                 WS_MAXIMIZE = 0x1000000,
  922.                 WS_MAXIMIZEBOX = 0x10000,
  923.                 WS_MINIMIZE = 0x20000000,
  924.                 WS_MINIMIZEBOX = 0x20000,
  925.                 WS_OVERLAPPED = 0x0,
  926.                 WS_POPUP = 0x80000000,
  927.                 WS_SYSMENU = 0x80000,
  928.                 WS_TABSTOP = 0x10000,
  929.                 WS_THICKFRAME = 0x40000,
  930.                 WS_VISIBLE = 0x10000000,
  931.                 WS_VSCROLL = 0x200000,
  932.                 WS_ICONIC = WS_MINIMIZE,
  933.                 WS_POPUPWINDOW = (WS_POPUP | WS_BORDER | WS_SYSMENU),
  934.                 WS_SIZEBOX = WS_THICKFRAME,
  935.                 WS_TILED = WS_OVERLAPPED,
  936.                 WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX)
  937.             }
  938.  
  939.             public struct WindowInfo
  940.             {
  941.                 public int size;
  942.                 public Rectangle window;
  943.                 public Rectangle client;
  944.                 public int style;
  945.                 public int exStyle;
  946.                 public int windowStatus;
  947.                 public uint xWindowBorders;
  948.                 public uint yWindowBorders;
  949.                 public short atomWindowtype;
  950.                 public short creatorVersion;
  951.             }
  952.  
  953.             public struct WindowPlacement
  954.             {
  955.                 public int length;
  956.                 public int flags;
  957.                 public int showCmd;
  958.                 public Point minPosition;
  959.                 public Point maxPosition;
  960.                 public Rectangle normalPosition;
  961.             }
  962.  
  963.             public struct Rect
  964.             {
  965.                 public int left;
  966.                 public int top;
  967.                 public int right;
  968.                 public int bottom;
  969.  
  970.                 public int Width
  971.                 {
  972.                     get
  973.                     {
  974.                         return right - left;
  975.                     }
  976.                 }
  977.  
  978.                 public int Height
  979.                 {
  980.                     get
  981.                     {
  982.                         return bottom - top;
  983.                     }
  984.                 }
  985.             }
  986.  
  987.             public delegate bool EnumWindowEventHandler(IntPtr hWnd, Int32 lParam);
  988.  
  989.             [DllImport("user32.dll")]
  990.             public static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  991.  
  992.             [DllImport("user32.dll")]
  993.             public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  994.  
  995.             [DllImport("user32.dll")]
  996.             public static extern bool GetWindowPlacement(IntPtr window, ref WindowPlacement position);
  997.  
  998.             [DllImport("User32.dll")]
  999.             public static extern int RegisterWindowMessage(string message);
  1000.  
  1001.             [DllImport("User32.dll")]
  1002.             public static extern void EnumWindows(EnumWindowEventHandler callback, Int32 lParam);
  1003.  
  1004.             //      [DllImport("User32.dll")]
  1005.             //      public static extern Int32 GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);
  1006.  
  1007.             [DllImport("User32.dll")]
  1008.             public static extern bool IsWindowVisible(IntPtr hWnd);
  1009.  
  1010.             [DllImport("User32.dll")]
  1011.             public static extern Int32 GetWindow(IntPtr hWnd, Int32 wCmd);
  1012.  
  1013.             //      [DllImport("User32.dll")]
  1014.             //      public static extern WindowStyles GetWindowLong(IntPtr hWnd, Int32 index);
  1015.  
  1016.             [DllImport("User32.dll")]
  1017.             public static extern IntPtr GetParent(IntPtr hWnd);
  1018.  
  1019.             [DllImport("User32.dll")]
  1020.             public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  1021.  
  1022.             [DllImport("User32.dll")]
  1023.             public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  1024.  
  1025.             [DllImport("User32.dll")]
  1026.             public static extern WindowStyles GetWindowLong(IntPtr hWnd, int index);
  1027.  
  1028.             [DllImport("User32.dll")]
  1029.             public static extern int SendMessageTimeout(IntPtr hWnd, int uMsg, int wParam, int lParam, int fuFlags, int uTimeout, out int lpdwResult);
  1030.  
  1031.             [DllImport("User32.dll")]
  1032.             public static extern int GetClassLong(IntPtr hWnd, int index);
  1033.  
  1034.             [DllImport("User32.dll")]
  1035.             public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
  1036.  
  1037.             [DllImport("User32.dll")]
  1038.             public static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
  1039.  
  1040.             [DllImport("User32.dll")]
  1041.             public static extern int PostMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
  1042.  
  1043.             [DllImport("User32.dll")]
  1044.             public static extern void SwitchToThisWindow(IntPtr hWnd, int altTabActivated);
  1045.  
  1046.             [DllImport("User32.dll")]
  1047.             public static extern int ShowWindowAsync(IntPtr hWnd, int command);
  1048.  
  1049.             [DllImport("user32.dll")]
  1050.             public static extern IntPtr GetDesktopWindow();
  1051.  
  1052.             [DllImport("user32.dll")]
  1053.             public static extern IntPtr GetForegroundWindow();
  1054.  
  1055.             [DllImport("user32.dll")]
  1056.             public static extern bool BringWindowToTop(IntPtr window);
  1057.  
  1058.             [DllImport("user32.dll")]
  1059.             public static extern bool GetWindowInfo(IntPtr hwnd, ref WindowInfo info);
  1060.  
  1061.             [DllImport("user32.dll")]
  1062.             public static extern IntPtr GetWindowDC(IntPtr hwnd);
  1063.  
  1064.             [DllImport("user32.dll")]
  1065.             public static extern IntPtr GetDC(IntPtr hwnd);
  1066.  
  1067.             [DllImport("user32.dll")]
  1068.             public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
  1069.  
  1070.             [DllImport("user32.dll")]
  1071.             public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
  1072.  
  1073.             [DllImport("user32.dll")]
  1074.             public static extern bool GetClientRect(IntPtr hwnd, ref Rect rectangle);
  1075.  
  1076.             [DllImport("user32.dll")]
  1077.             public static extern IntPtr WindowFromPoint(Point pt);
  1078.  
  1079.             [DllImport("user32.dll")]
  1080.             public static extern IntPtr SetCapture(IntPtr hWnd);
  1081.  
  1082.             [DllImport("user32.dll")]
  1083.             public static extern int ReleaseCapture();
  1084.  
  1085.             [DllImport("user32.dll")]
  1086.             public static extern IntPtr SelectObject(IntPtr hDc, IntPtr hObject);
  1087.  
  1088.             [DllImport("user32.dll")]
  1089.             public static extern IntPtr GetStockObject(int nObject);
  1090.  
  1091.             [DllImport("user32.dll")]
  1092.             public static extern int InvalidateRect(IntPtr hWnd, IntPtr lpRect, int bErase);
  1093.  
  1094.             [DllImport("user32.dll")]
  1095.             public static extern int UpdateWindow(IntPtr hWnd);
  1096.  
  1097.             [DllImport("user32.dll")]
  1098.             public static extern int RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);
  1099.  
  1100.             #endregion Windows
  1101.  
  1102.             #region GDI - gdi32.dll
  1103.  
  1104.             public enum BinaryRasterOperations
  1105.             {
  1106.                 R2_BLACK = 1,   /*  0       */
  1107.                 R2_NOTMERGEPEN = 2,   /* DPon     */
  1108.                 R2_MASKNOTPEN = 3,   /* DPna     */
  1109.                 R2_NOTCOPYPEN = 4,   /* PN       */
  1110.                 R2_MASKPENNOT = 5,   /* PDna     */
  1111.                 R2_NOT = 6,   /* Dn       */
  1112.                 R2_XORPEN = 7,   /* DPx      */
  1113.                 R2_NOTMASKPEN = 8,   /* DPan     */
  1114.                 R2_MASKPEN = 9,   /* DPa      */
  1115.                 R2_NOTXORPEN = 10,  /* DPxn     */
  1116.                 R2_NOP = 11,  /* D        */
  1117.                 R2_MERGENOTPEN = 12,  /* DPno     */
  1118.                 R2_COPYPEN = 13,  /* P        */
  1119.                 R2_MERGEPENNOT = 14,  /* PDno     */
  1120.                 R2_MERGEPEN = 15,  /* DPo      */
  1121.                 R2_WHITE = 16,  /*  1       */
  1122.                 R2_LAST = 16
  1123.             }
  1124.  
  1125.             public enum TernaryRasterOperations
  1126.             {
  1127.                 SRCCOPY = 0x00CC0020, /* dest = source                   */
  1128.                 SRCPAINT = 0x00EE0086, /* dest = source OR dest           */
  1129.                 SRCAND = 0x008800C6, /* dest = source AND dest          */
  1130.                 SRCINVERT = 0x00660046, /* dest = source XOR dest          */
  1131.                 SRCERASE = 0x00440328, /* dest = source AND (NOT dest )   */
  1132.                 NOTSRCCOPY = 0x00330008, /* dest = (NOT source)             */
  1133.                 NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
  1134.                 MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)     */
  1135.                 MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest     */
  1136.                 PATCOPY = 0x00F00021, /* dest = pattern                  */
  1137.                 PATPAINT = 0x00FB0A09, /* dest = DPSnoo                   */
  1138.                 PATINVERT = 0x005A0049, /* dest = pattern XOR dest         */
  1139.                 DSTINVERT = 0x00550009, /* dest = (NOT dest)               */
  1140.                 BLACKNESS = 0x00000042, /* dest = BLACK                    */
  1141.                 WHITENESS = 0x00FF0062 /* dest = WHITE                    */
  1142.  
  1143.             }
  1144.  
  1145.             [DllImport("gdi32.dll")]
  1146.             public static extern bool BitBlt(IntPtr hdcDst, int xDst, int yDst, int cx, int cy, IntPtr hdcSrc, int xSrc, int ySrc, uint ulRop);
  1147.  
  1148.             [DllImport("gdi32.dll")]
  1149.             public static extern bool StretchBlt(IntPtr hdcDst, int xDst, int yDst, int cx, int cy, IntPtr hdcSrc, int xSrc, int ySrc, int cxSrc, int cySrc, uint ulRop);
  1150.  
  1151.             [DllImport("gdi32.dll")]
  1152.             public static extern IntPtr CreateDC(IntPtr lpszDriver, string lpszDevice, IntPtr lpszOutput, IntPtr lpInitData);
  1153.  
  1154.             [DllImport("gdi32.dll")]
  1155.             public static extern IntPtr DeleteDC(IntPtr hdc);
  1156.  
  1157.             #endregion
  1158.  
  1159.             #region Shlwapi.dll
  1160.  
  1161.             [DllImport("Shlwapi.dll")]
  1162.             public static extern string PathGetArgs(string path);
  1163.  
  1164.             public static string SafePathGetArgs(string path)
  1165.             {
  1166.                 try
  1167.                 {
  1168.                     return Win32.PathGetArgs(path);
  1169.                 }
  1170.                 catch (System.Exception) { }
  1171.                 return string.Empty;
  1172.             }
  1173.  
  1174.             [DllImport("Shlwapi.dll")]
  1175.             public static extern int PathCompactPathEx(
  1176.                 System.Text.StringBuilder pszOut, /* Address of the string that has been altered */
  1177.                 System.Text.StringBuilder pszSrc, /* Pointer to a null-terminated string of max length (MAX_PATH) that contains the path to be altered */
  1178.                 uint cchMax,                      /* Maximum number of chars to be contained in the new string, including the null character. Example: cchMax = 8, then 7 chars will be returned, the last for the null character. */
  1179.                 uint dwFlags);                    /* Reserved */
  1180.  
  1181.             public static string PathCompactPathEx(string source, uint maxChars)
  1182.             {
  1183.                 StringBuilder pszOut = new StringBuilder((int)Win32.MAX_PATH);
  1184.                 StringBuilder pszSrc = new StringBuilder(source);
  1185.  
  1186.                 int result = Win32.PathCompactPathEx(pszOut, pszSrc, maxChars, (uint)0);
  1187.                 if (result == 1)
  1188.                     return pszOut.ToString();
  1189.                 else
  1190.                 {
  1191.                     System.Diagnostics.Debug.WriteLine("Win32.PathCompactPathEx failed to compact the path '" + source + "' down to '" + maxChars + "' characters.");
  1192.                     return string.Empty;
  1193.                 }
  1194.             }
  1195.  
  1196.             #endregion
  1197.  
  1198.             #region Hotkeys
  1199.  
  1200.             [Flags()]
  1201.             public enum HotkeyModifiers
  1202.             {
  1203.                 MOD_ALT = 0x0001,
  1204.                 MOD_CONTROL = 0x0002,
  1205.                 MOD_SHIFT = 0x0004,
  1206.                 MOD_WIN = 0x0008
  1207.             }
  1208.  
  1209.             [DllImport("User32")]
  1210.             public static extern int RegisterHotKey(IntPtr hWnd, int id, uint modifiers, uint virtualkeyCode);
  1211.  
  1212.             [DllImport("User32")]
  1213.             public static extern int UnregisterHotKey(IntPtr hWnd, int id);
  1214.  
  1215.             [DllImport("Kernel32")]
  1216.             public static extern short GlobalAddAtom(string atomName);
  1217.  
  1218.             [DllImport("Kernel32")]
  1219.             public static extern short GlobalDeleteAtom(short atom);
  1220.  
  1221.             #endregion
  1222.  
  1223.             [DllImport("User32")]
  1224.             public static extern int LockWindowUpdate(IntPtr windowHandle);
  1225.  
  1226.             public static short MAKEWORD(byte a, byte b)
  1227.             {
  1228.                 return ((short)(((byte)(a & 0xff)) | ((short)((byte)(b & 0xff))) << 8));
  1229.             }
  1230.  
  1231.             public static byte LOBYTE(short a)
  1232.             {
  1233.                 return ((byte)(a & 0xff));
  1234.             }
  1235.  
  1236.             public static byte HIBYTE(short a)
  1237.             {
  1238.                 return ((byte)(a >> 8));
  1239.             }
  1240.  
  1241.             public static int MAKELONG(short a, short b)
  1242.             {
  1243.                 return (((int)(a & 0xffff)) | (((int)(b & 0xffff)) << 16));
  1244.             }
  1245.  
  1246.             public static short HIWORD(int a)
  1247.             {
  1248.                 return ((short)(a >> 16));
  1249.             }
  1250.  
  1251.             public static short LOWORD(int a)
  1252.             {
  1253.                 return ((short)(a & 0xffff));
  1254.             }
  1255.  
  1256.             [DllImport("Kernel32")]
  1257.             public static extern int CopyFile(string source, string destination, int failIfExists);
  1258.         }
  1259.     }
  1260.  
  1261. }
Advertisement
Add Comment
Please, Sign In to add comment