I have highlighted the UnsafeNativeMethods in RED.
#region Namespaces
using System;
using System.Text;
using System.Security;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Drawing;
#endregion Namespaces
namespace Utilites
{
/// <summary>
/// UnsafeNativeMethods. Totally unsafe.
/// Interacts with kernel32, user32, gdi32 bit versions
/// </summary>
[SuppressUnmanagedCodeSecurity]
public sealed class UnsafeNativeMethods
{
#region public constants
public const uint MB_ICONASTERISK = 0x40;
public const uint MB_ICONERROR = 0x10;
public const uint MB_ICONEXCLAMATION = 0x30;
public const uint MB_ICONHAND = 0x10;
public const uint MB_ICONINFORMATION = 0x40;
public const uint MB_ICONQUESTION = 0x20;
public const uint MB_ICONWARNING = 0x30;
public const uint MB_OK = 0;
public const uint MB_OKCANCEL = 1;
public const uint MB_SETFOREGROUND = 0x10000;
public const uint MB_YESNO = 4;
public const uint MB_YESNOCANCEL = 3;
public const uint SRCCOPY = 0xcc0020;
public const uint WS_EX_TOOLWINDOW = 0x80;
public const uint WS_EX_TOPMOST = 8;
public const uint WS_EX_TRANSPARENT = 0x20;
public const uint WS_EX_WINDOWEDGE = 0x100;
public const uint WS_POPUP = 0x80000000;
public const int SM_CXSCREEN = 0;
public const int SM_CYSCREEN = 1;
public const int SW_SHOW = 5;
public const int WM_CLOSE = 0x10;
public const int GWL_EXSTYLE = -20;
public const int WS_VISIBLE = 0x10000000;
public static uint LWA_ALPHA = 0x1;
public static uint WS_EX_LAYERED = 0x00080000;
public static string DesktopWindow =
"#32769";
#endregion public constants
/// <summary>
/// Gets the short name of the path.
/// </summary>
/// <param name="lpszLongPath">The LPSZ long path.</param>
/// <param name="lpszShortPath">The LPSZ short path.</param>
/// <param name="cchBuffer">The CCH buffer.</param>
/// <returns></returns>
[DllImport(
"kernel32.dll", CharSet = CharSet.Auto)]
public static extern uint GetShortPathName(
string lpszLongPath, StringBuilder lpszShortPath,
uint cchBuffer);
/// <summary>
/// Begins the paint.
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="lpPaint">The lp paint.</param>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern IntPtr BeginPaint(IntPtr hwnd,
out PAINTSTRUCT lpPaint);
/// <summary>
/// Bits the BLT.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <param name="nXDest">The n X dest.</param>
/// <param name="nYDest">The n Y dest.</param>
/// <param name="nWidth">Width of the n.</param>
/// <param name="nHeight">Height of the n.</param>
/// <param name="hdcSrc">The HDC SRC.</param>
/// <param name="nXSrc">The n X SRC.</param>
/// <param name="nYSrc">The n Y SRC.</param>
/// <param name="dwRop">The dw rop.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"gdi32.dll")]
public static extern bool BitBlt(IntPtr hdc,
int nXDest,
int nYDest,
int nWidth,
int nHeight, IntPtr hdcSrc,
int nXSrc,
int nYSrc,
uint dwRop);
/// <summary>
/// Creates the compatible DC.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <returns></returns>
[DllImport(
"gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
/// <summary>
/// Creates the compatible DC.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <returns></returns>
[DllImport(
"gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
int hdc);
/// <summary>
/// Creates the compatible bitmap.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <param name="nWidth">Width of the n.</param>
/// <param name="nHeight">Height of the n.</param>
/// <returns></returns>
[DllImport(
"gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
int nWidth,
int nHeight);
/// <summary>
/// Creates the window ex.
/// </summary>
/// <param name="dwExStyle">The dw ex style.</param>
/// <param name="lpClassName">Name of the lp class.</param>
/// <param name="lpWindowName">Name of the lp window.</param>
/// <param name="dwStyle">The dw style.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="nWidth">Width of the n.</param>
/// <param name="nHeight">Height of the n.</param>
/// <param name="hWndParent">The h WND parent.</param>
/// <param name="hMenu">The h menu.</param>
/// <param name="hInstance">The h instance.</param>
/// <param name="lpParam">The lp param.</param>
/// <returns></returns>
[DllImport(
"user32.dll", EntryPoint =
"CreateWindowExW", CharSet = CharSet.Unicode)]
public static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
/// <summary>
/// Defs the window proc.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="uMsg">The u MSG.</param>
/// <param name="wParam">The w param.</param>
/// <param name="lParam">The l param.</param>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hWnd,
uint uMsg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Deletes the DC.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"gdi32.dll")]
public static extern bool DeleteDC(IntPtr hdc);
/// <summary>
/// Deletes the object.
/// </summary>
/// <param name="hObject">The h object.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
/// <summary>
/// Dispatches the message.
/// </summary>
/// <param name="lpmsg">The LPMSG.</param>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern IntPtr DispatchMessage([In]
ref Message lpmsg);
/// <summary>
/// Ends the paint.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="lpPaint">The lp paint.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"user32.dll")]
public static extern bool EndPaint(IntPtr hWnd,
ref PAINTSTRUCT lpPaint);
/// <summary>
/// Gets the DC.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern IntPtr GetDC(HandleRef hWnd);
/// <summary>
/// Gets the window DC.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <returns></returns>
[DllImport(
"User32.dll")]
public static extern IntPtr GetWindowDC(
int hWnd);
/// <summary>
/// Gets the desktop window.
/// </summary>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern int GetDesktopWindow();
/// <summary>
/// Gets the message.
/// </summary>
/// <param name="msg">The MSG.</param>
/// <param name="hWnd">The h WND.</param>
/// <param name="uMsgFilterMin">The u MSG filter min.</param>
/// <param name="uMsgFilterMax">The u MSG filter max.</param>
/// <returns></returns>
[DllImport(
"user32.dll", EntryPoint =
"GetMessageW", CharSet = CharSet.Unicode, ExactSpelling =
true)]
public static extern int GetMessage([In, Out]
ref MSG msg, IntPtr hWnd,
int uMsgFilterMin,
int uMsgFilterMax);
/// <summary>
/// Gets the module handle.
/// </summary>
/// <param name="lpModuleName">Name of the lp module.</param>
/// <returns></returns>
[DllImport(
"kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(
string lpModuleName);
/// <summary>
/// Gets the object.
/// </summary>
/// <param name="hgdiobj">The hgdiobj.</param>
/// <param name="cbBuffer">The cb buffer.</param>
/// <param name="lpvObject">The LPV object.</param>
/// <returns></returns>
[DllImport(
"gdi32.dll")]
public static extern int GetObject(IntPtr hgdiobj,
int cbBuffer, IntPtr lpvObject);
/// <summary>
/// Gets the system metrics.
/// </summary>
/// <param name="nIndex">Index of the n.</param>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern int GetSystemMetrics(
int nIndex);
/// <summary>
/// Gets the window long.
/// </summary>
/// <param name="handle">The handle.</param>
/// <param name="index">The index.</param>
/// <returns></returns>
[DllImport(
"user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(IntPtr handle,
int index);
/// <summary>
/// Loads the bitmap.
/// </summary>
/// <param name="hInstance">The h instance.</param>
/// <param name="lpBitmapName">Name of the lp bitmap.</param>
/// <returns></returns>
[DllImport(
"user32.dll", EntryPoint =
"LoadBitmapW", CharSet = CharSet.Unicode)]
public static extern IntPtr LoadBitmap(IntPtr hInstance, IntPtr lpBitmapName);
/// <summary>
/// Messages the box.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="lpText">The lp text.</param>
/// <param name="lpCaption">The lp caption.</param>
/// <param name="uType">Type of the u.</param>
/// <returns></returns>
[DllImport(
"user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd,
string lpText,
string lpCaption,
uint uType);
/// <summary>
/// Posts the message.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="Msg">The MSG.</param>
/// <param name="wParam">The w param.</param>
/// <param name="lParam">The l param.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"user32.dll")]
public static extern bool PostMessage(HandleRef hWnd,
uint Msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Registers the class ex W.
/// </summary>
/// <param name="lpwcx">The LPWCX.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.U2)]
[DllImport(
"user32.dll")]
public static extern short RegisterClassExW([In]
ref WNDCLASSEX lpwcx);
/// <summary>
/// Selects the object.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <param name="hgdiobj">The hgdiobj.</param>
/// <returns></returns>
[DllImport(
"gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
/// <summary>
/// Sets the foreground window.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// Sets the layered window attributes.
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="crKey">The cr key.</param>
/// <param name="bAlpha">The b alpha.</param>
/// <param name="dwFlags">The dw flags.</param>
/// <returns></returns>
[DllImport(
"user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd,
uint crKey,
byte bAlpha,
uint dwFlags);
/// <summary>
/// Sets the window long.
/// </summary>
/// <param name="handle">The handle.</param>
/// <param name="index">The index.</param>
/// <param name="dwNewLong">The dw new long.</param>
/// <returns></returns>
[DllImport(
"user32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowLong(IntPtr handle,
int index,
long dwNewLong);
/// <summary>
/// Shows the window.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="nCmdShow">The n CMD show.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd,
int nCmdShow);
/// <summary>
/// Selects the object.
/// </summary>
/// <param name="hdc">The HDC.</param>
/// <param name="hgdiobj">The hgdiobj.</param>
/// <returns></returns>
[DllImport(
"gdi32.dll")]
public static extern int SelectObject(
int hdc,
int hgdiobj);
/// <summary>
/// Translates the message.
/// </summary>
/// <param name="lpMsg">The lp MSG.</param>
/// <returns></returns>
[
return: MarshalAs(UnmanagedType.Bool)]
[DllImport(
"user32.dll")]
public static extern bool TranslateMessage([In]
ref MSG lpMsg);
[StructLayout(LayoutKind.Sequential)]
public struct BITMAP
{
public int bmType;
public int bmWidth;
public int bmHeight;
public int bmWidthBytes;
public ushort bmPlanes;
public ushort bmBitsPixel;
public IntPtr bmBits;
}
[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
public bool fErase;
public RECT rcPaint;
public bool fRestore;
public bool fIncUpdate;
[MarshalAs(UnmanagedType.ByValArray,
SizeConst = 0x20)]
public byte[] rgbReserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential,
CharSet = CharSet.Auto)]
public struct WNDCLASSEX
{
public uint cbSize;
public uint style;
public WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
/// <summary>Windows Message</summary>
[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public WindowMessage msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
/// <summary>
/// WndProc delegate.
/// </summary>
public delegate IntPtr WndProc(IntPtr hWnd,
uint msg, IntPtr wParam, IntPtr lParam);
}
/// <summary>
/// This class shows the examples of usage Of UnSafeNativeMethods.
/// Note: The exceptions are centrally handled in my application. If you need exception
handling,
/// Please add try/catch blocks for specific win32 exceptions.
/// </summary>
public class usageOfUnSafeNativeMethods
{
/// <summary>
/// Gives the short name of the path.
/// </summary>
/// <param name="longPath">The long path.</param>
/// <returns></returns>
public static string ToShortPathName(
string longPath)
{
uint size = 256;
StringBuilder
sbshortName =
new StringBuilder((
int)size);
//sbshortName holds the result.
uint result = Utilites.UnsafeNativeMethods
.GetShortPathName(longPath, sbshortName, size);
return sbshortName.ToString();
}
/// <summary>
/// Does the A screen shot.
/// </summary>
/// <param name="imageFormat">The image format.</param>
/// <param name="saveLocation">The save location.</param>
public void SaveDesktopScreen(ImageFormat imageFormat,
string saveLocation,
int left,
int top,
int width,
int height)
{
long imgHeight;
long imgWidth;
IntPtr
hdcSrc = UnsafeNativeMethods.
GetWindowDC(UnsafeNativeMethods.GetDesktopWindow());
IntPtr
hdcDest = UnsafeNativeMethods.
CreateCompatibleDC(hdcSrc);
IntPtr
hBitmap = UnsafeNativeMethods.
CreateCompatibleBitmap(hdcSrc, width, height);
UnsafeNativeMethods.
SelectObject(hdcDest, hBitmap);
//Left denotes the xsource and Top denotes the ysource.
UnsafeNativeMethods.
BitBlt(hdcDest,0, 0, width, height, hdcSrc, left, top, 0x00CC0020);
//Get the height and width of the image after save.
SaveScreenshot(hBitmap,
saveLocation, imageFormat,
out imgHeight,
out imgWidth);
//free up memory
UnsafeNativeMethods.
DeleteDC(hdcDest);
UnsafeNativeMethods.
DeleteDC(hdcSrc);
}
/// <summary>
/// Saves the image as.
/// </summary>
/// <param name="hBitmap">The h bitmap.</param>
/// <param name="saveLocation">The save location.</param>
/// <param name="imageFormat">The image format.</param>
/// <param name="ImgHeight">Height of the img.</param>
/// <param name="ImgWidth">Width of the img.</param>
private void SaveScreenshot(IntPtr hBitmap,
string saveLocation, ImageFormat imageFormat,
out long imgHeight,
out long imgWidth)
{
Bitmap
image =
new Bitmap(Image.FromHbitmap(hBitmap),
Image.FromHbitmap(hBitmap).Width,
Image.FromHbitmap(hBitmap).Height);
image.Save(saveLocation,
imageFormat);
imgHeight
= image.Height;
imgWidth
= image.Width;
}
/// <summary>
/// Creates the A pop up screen.
/// </summary>
/// <param name="handle">The handle.</param>
private void CreateAPopUpScreen(IntPtr handle)
{
//To call this method
//CreateAPopUpScreen(Control.handle);
IntPtr
h = UnsafeNativeMethods.
CreateWindowEx
(UnsafeNativeMethods.
WS_EX_LAYERED, UnsafeNativeMethods.
DesktopWindow,
"NewPopupWindow", UnsafeNativeMethods.
WS_VISIBLE, 400, 500, 100, 100, handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
UnsafeNativeMethods.
SetLayeredWindowAttributes(h, 0, 128, UnsafeNativeMethods.
LWA_ALPHA);
UnsafeNativeMethods.
ShowWindow(h, 0);
}
}
}