Code (csharp) Select
//' WIN32 API (Window handling)
// Must have System.Runtime.InteropServices
[DllImport("user32.dll")]
extern static bool GetCursorPos(ref Point lpPoint);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public Rectangle ToRectangle()
{
return new Rectangle(Left, Top, Right - Left, Bottom - Top);
}
}
[DllImport("user32.dll")]
extern static IntPtr WindowFromPoint(Point point);
[DllImport("user32.dll")]
extern static bool ScreenToClient(IntPtr handle, ref Point point);
[DllImport("user32.dll")]
extern static IntPtr ChildWindowFromPointEx(IntPtr hWndParent, Point pt, uint uFlags);
[DllImport("user32.dll")]
extern static bool ClientToScreen(IntPtr hwnd, ref Point lpPoint);
[DllImport("user32.dll")]
extern static bool IsChild(IntPtr hWndParent, IntPtr hWnd);
[DllImport("user32.dll")]
extern static IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
extern static IntPtr GetAncestor(IntPtr hWnd, int flags);
public enum GetAncestor_Flags
{
GetParent = 1,
GetRoot = 2,
GetRootOwner = 3
}
public enum GetWindow_Cmd : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
[DllImport("user32.dll")]
extern static IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll")]
extern static bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll")]
extern static IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
extern static int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
extern public static int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
extern static Int32 ReleaseDC(IntPtr hWnd, IntPtr hdc);
[DllImport("user32.dll", SetLastError = true)]
private extern static int GetWindowLong(IntPtr hWnd, [MarshalAs(UnmanagedType.I4)]
WindowLongFlags nIndex);
public enum WindowLongFlags : int
{
GWL_EXSTYLE = -20,
GWLP_HINSTANCE = -6,
GWLP_HWNDPARENT = -8,
GWL_ID = -12,
GWL_STYLE = -16,
GWL_USERDATA = -21,
GWL_WNDPROC = -4,
DWLP_USER = 0x8,
DWLP_MSGRESULT = 0x0,
DWLP_DLGPROC = 0x4
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
extern static bool GetWindowRect(HandleRef hWnd, ref RECT lpRect);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
// SetCursorPos
extern static int SetCursorPos(int X, int Y);
[DllImport("user32", EntryPoint = "mouse_event", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
// mouse_event
extern static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
// mouse_event constants
public const int MOUSEEVENTF_LEFTDOWN = 0x2;
public const int MOUSEEVENTF_LEFTUP = 0x4;
// Helper function returns a Rectangle object directly
public static Rectangle GetWindowRect(IntPtr hWnd)
{
Debug.Assert(hWnd != IntPtr.Zero);
RECT rect = new RECT();
if (GetWindowRect(hWnd, ref rect) == false)
{
throw new Exception("GetWindowRect failed");
}
return rect.ToRectangle();
}
/// <summary>
/// Gets window text (title) from passed window
/// </summary>
/// <param name="hWnd">Target window</param>
/// <returns>String with text/title</returns>
public static string GetWindowText(IntPtr hWnd)
{
Debug.Assert(hWnd != IntPtr.Zero);
StringBuilder WindowText = new StringBuilder(GetWindowTextLength(hWnd) + 1);
GetWindowText(hWnd, WindowText, WindowText.Capacity);
return WindowText.ToString();
}
//' GDI (Graphics)
public enum RopMode : int
{
R2_NOT = 6
}
[DllImport("gdi32.dll")]
extern static int SetROP2(IntPtr hdc, int fnDrawMode);
public enum PenStyles : int
{
PS_INSIDEFRAME = 6
}
[DllImport("gdi32.dll")]
extern static IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor);
[DllImport("gdi32.dll")]
extern static IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
public enum StockObjects : int
{
NULL_BRUSH = 5
}
[DllImport("gdi32.dll")]
extern static IntPtr GetStockObject(int fnObject);
[DllImport("gdi32.dll")]
extern static uint Rectangle(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("gdi32.dll")]
extern static bool DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
extern static bool IsWindowVisible(IntPtr hwnd);
[DllImport("user32", EntryPoint = "GetWindowTextA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowText(int hWnd, string lpString, int nMaxCount);
[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
private static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool DeleteDC(IntPtr hDC);
private const int SRCCOPY = 0xcc0020;
public static Image CaptureWindow(IntPtr handle)
{
// Check if it is a window for a start
if (IsWindow(handle) == false)
{
// Clear picturebox
//fCapture.BackgroundImage = null;
// Halt
return null;
}
// get te hDC of the target window
IntPtr hdcSrc = GetWindowDC(handle);
// get the size
RECT windowRect = new RECT();
GetWindowRect(handle, ref windowRect);
int width = windowRect.Right - windowRect.Left;
int height = windowRect.Bottom - windowRect.Top;
// create a device context we can copy to
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = SelectObject(hdcDest, hBitmap);
// bitblt over
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
// restore selection
SelectObject(hdcDest, hOld);
// clean up
DeleteDC(hdcDest);
ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
DeleteObject(hBitmap);
return img;
}
//CaptureWindow
/// <summary>
/// Paint a rect into the given window
/// </summary>
/// <param name="window">Pointer/Handle identifying window on which inverted rectangle is going to be painted</param>
///
public static void ShowInvertRectTracker(IntPtr window)
{
if (window != IntPtr.Zero)
{
// get the coordinates from the window on the screen
Rectangle WindowRect = GetWindowRect(window);
// get the window's device context
IntPtr dc = GetWindowDC(window);
// Create an inverse pen that is the size of the window border
SetROP2(dc, Convert.ToInt32(RopMode.R2_NOT));
Color cColor = Color.FromArgb(0, 255, 0);
IntPtr Pen = CreatePen(6, GetSystemMetrics(SystemMetric.SM_CXBORDER) * 3, Convert.ToUInt32(ColorTranslator.ToWin32(cColor)));
// Draw the rectangle around the window
IntPtr OldPen = SelectObject(dc, Pen);
IntPtr OldBrush = SelectObject(dc, GetStockObject(Convert.ToInt32(StockObjects.NULL_BRUSH)));
Rectangle(dc, 0, 0, WindowRect.Width, WindowRect.Height);
SelectObject(dc, OldBrush);
SelectObject(dc, OldPen);
//release the device context, and destroy the pen
ReleaseDC(window, dc);
DeleteObject(Pen);
}
}
}
}