using Microsoft.Win32.SafeHandles; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace zee.Views { /// <summary> /// Interaction logic for Inspector.xaml /// </summary> public partial class Inspector : System.Windows.Controls.UserControl { #region Win32 API Declarations [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; #region Helper methods public POINT(int x, int y) { this.X = x; this.Y = y; } public static implicit operator System.Drawing.Point(POINT p) { return new System.Drawing.Point(p.X, p.Y); } public static implicit operator POINT(System.Drawing.Point p) { return new POINT(p.X, p.Y); } #endregion } const int DSTINVERT = 0x00550009; [DllImport("gdi32.dll")] static extern bool PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, uint dwRop); [DllImport("user32.dll")] static extern IntPtr WindowFromPoint(POINT Point); [DllImport("user32.dll")] static extern int GetWindowText(int hWnd, StringBuilder text, int count); [DllImport("user32.dll")] static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] static extern bool OffsetRect(ref RECT lprc, int dx, int dy); [DllImport("user32.dll")] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); [DllImport("user32.dll")] public static extern bool GetCursorPos(out System.Drawing.Point lpPoint); [DllImport("User32.dll")] static extern IntPtr GetDC(IntPtr hwnd); [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int nIndex); [DllImport("user32.dll")] static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC); #endregion bool _dragging; IntPtr _hWndCurrent; System.Windows.Input.Cursor _curCross; System.Drawing.Image _imgAppCross; System.Drawing.Image _imgApp; public Inspector() { InitializeComponent(); Assembly assembly = Assembly.GetExecutingAssembly(); _imgAppCross = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream("zee.Assets.Images.app_cross.png")); _imgApp = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream("zee.Assets.Images.app.png")); _curCross = new System.Windows.Input.Cursor(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Assets\\Images\\cross.cur")); dragPictureBox.Image = _imgAppCross; } private System.Drawing.Point ConvertPixelsToUnits(double x, double y) { // get the system DPI IntPtr dDC = GetDC(IntPtr.Zero); // Get desktop DC int dpi = GetDeviceCaps(dDC, 88); bool rv = ReleaseDC(IntPtr.Zero, dDC); // WPF's physical unit size is calculated by taking the // "Device-Independant Unit Size" (always 1/96) // and scaling it by the system DPI double physicalUnitSize = (1d / 96d) * (double)dpi; System.Drawing.Point wpfUnits = new System.Drawing.Point(Convert.ToInt32(physicalUnitSize * (x)),Convert.ToInt32(physicalUnitSize * (y))); return wpfUnits; } public POINT GetCursorPosition() { System.Drawing.Point lpPoint, wpfPoint= new System.Drawing.Point(); if(GetCursorPos(out lpPoint)) { wpfPoint = ConvertPixelsToUnits(lpPoint.X, lpPoint.Y); } //bool success = User32.GetCursorPos(out lpPoint); // if (!success) return wpfPoint; } private void DrawRevFrame(IntPtr hWnd) { if (hWnd == IntPtr.Zero) return; IntPtr hdc = GetWindowDC(hWnd); RECT rect; GetWindowRect(hWnd, out rect); OffsetRect(ref rect, -rect.Left, -rect.Top); const int frameWidth = 3; PatBlt(hdc, rect.Left, rect.Top, rect.Right - rect.Left, frameWidth, DSTINVERT); PatBlt(hdc, rect.Left, rect.Bottom - frameWidth, frameWidth,-(rect.Bottom - rect.Top - 2 * frameWidth), DSTINVERT); PatBlt(hdc, rect.Right - frameWidth, rect.Top + frameWidth, frameWidth,rect.Bottom - rect.Top - 2 * frameWidth, DSTINVERT); PatBlt(hdc, rect.Right, rect.Bottom - frameWidth, -(rect.Right - rect.Left),frameWidth, DSTINVERT); } private string GetWindowText(IntPtr hWnd) { StringBuilder text = new StringBuilder(256); if (GetWindowText(hWnd.ToInt32(), text, text.Capacity) > 0) { return text.ToString(); } return String.Empty; } private string GetClassName(IntPtr hWnd) { StringBuilder className = new StringBuilder(100); if (GetClassName(hWnd, className, className.Capacity) > 0) { return className.ToString(); } return String.Empty; } private string GetApplication(IntPtr hWnd) { int procId; GetWindowThreadProcessId(hWnd, out procId); Process proc = Process.GetProcessById(procId); return proc.MainModule.ModuleName; } private void dragPictureBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { _dragging = true; Assembly assembly = Assembly.GetExecutingAssembly(); System.Windows.Input.Mouse.OverrideCursor = _curCross; dragPictureBox.Image = _imgApp; } } private void dragPictureBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if (_dragging) { _dragging = false; System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow; if (_hWndCurrent != IntPtr.Zero) { DrawRevFrame(_hWndCurrent); _hWndCurrent = IntPtr.Zero; // The image in the dragPictureBox will be restored when the context menu is closed } else { dragPictureBox.Image = _imgAppCross; } dragPictureBox.Image = _imgAppCross; } } private void dragPictureBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (_dragging) { IntPtr hWnd = WindowFromPoint(GetCursorPosition()); if (hWnd == dragPictureBox.Handle) { // Drawing a border around the dragPictureBox (where we start // dragging) doesn't look nice, so we ignore this window hWnd = IntPtr.Zero; } if (hWnd != _hWndCurrent) { if (_hWndCurrent != null) { DrawRevFrame(_hWndCurrent); } DrawRevFrame(hWnd); _hWndCurrent = hWnd; } if (hWnd != IntPtr.Zero) { txtWindowHandle.Text = hWnd.ToString(); txtWindowText.Text = GetWindowText(hWnd); txtClassName.Text = GetClassName(hWnd); txtApplication.Text = GetApplication(hWnd); } else { txtWindowHandle.Text = String.Empty; txtWindowText.Text = String.Empty; txtClassName.Text = String.Empty; txtApplication.Text = String.Empty; } } } }
So this is my code for control inspector in windows<strike> </strike>. It works almost fine<strike> </strike>for winforms , my problem is when i am trying to inspect a wpf or windows 10 // or website in browser , it is unable to inspect