1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
struct SearchData
{
public string WindowText;
public IntPtr ParentHandle;
public IntPtr ResultHandle;
}
delegate bool EnumWindowsCallback(IntPtr currentWindowHandle, ref SearchData searchData);
[DllImport("user32.dll")]
internal static extern IntPtr GetActiveWindow();
[DllImport("user32")]
private static extern IntPtr SetWindowLongPtr(IntPtr hwnd, int index,
IntPtr wndProc);
[DllImport("user32")]
private static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int index);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hwnd, int msg,
IntPtr wparam, IntPtr lparam);
[DllImport("user32.dll")]
static extern IntPtr BeginPaint(IntPtr hwnd, out PAINTSTRUCT lpPaint);
[DllImport("user32.dll")]
static extern bool EndPaint(IntPtr hWnd, [In] ref PAINTSTRUCT lpPaint);
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC", SetLastError = true)]
static extern IntPtr CreateCompatibleDC([In] IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern bool DeleteDC([In] IntPtr hdc);
[DllImport("user32.dll")]
static extern bool ValidateRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll")] static extern bool EnumChildWindows(IntPtr handle,EnumWindowsCallback callback, ref SearchData searchData);
[DllImport("user32.dll")] static extern IntPtr GetParent(IntPtr childHandle);
[DllImport("user32.dll")] static extern void GetWindowText(IntPtr handle, StringBuilder resultWindowText, int maxTextCapacity);
[DllImport("user32.dll")] static extern void GetClassName(IntPtr handle, StringBuilder resultWindowText, int maxTextCapacity);
static bool Callback(IntPtr currentWindowHandle, ref SearchData searchData)
{
bool continueEnumeration = true;
//IntPtr currentWindowParentHandle = GetParent(currentWindowHandle);
//if (currentWindowParentHandle == searchData.ParentHandle)
{
var windowText = new StringBuilder(1024);
GetClassName(currentWindowHandle, windowText, windowText.Capacity);
//if (windowText.ToString() == searchData.WindowText)
if (windowText.ToString().ToUpper().Contains(searchData.WindowText.ToUpper()))
{
searchData.ResultHandle = currentWindowHandle;
continueEnumeration = false;
}
}
return continueEnumeration;
}
IntPtr GetChildWindowHandle(string windowText, IntPtr parentHandle)
{
var searchData = new SearchData { ParentHandle = parentHandle, WindowText = windowText };
EnumChildWindows(parentHandle,Callback, ref searchData);
return searchData.ResultHandle;
}
private delegate IntPtr WndProcPointer(IntPtr hwnd, int msg,
IntPtr wparam, IntPtr lparam);
static IntPtr oldwndproc = IntPtr.Zero;
static WndProcPointer wa;
protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
{
IntPtr ret;
if (msg==0xf) //WM_PAINT
{
ret = tdxtt(hwnd, msg, wparam, lparam);
}
else
if ((msg == 0x200)) //WM_MOUSEMOVE
{
bool ta = true;
int iwparam = wparam.ToInt32();
if ((iwparam & 0x10) == 0x10) //mbutton
ta = false;
ret = tdxtt(hwnd, msg, wparam, lparam,ta);
}
else
if ( (msg == 0xa0) || (msg == 0x20) || (msg == 0x46) || (msg == 0x22)|| (msg == 0x07) || (msg == 0x08))
{
ret = tdxtt(hwnd, msg, wparam, lparam);
}
else
ret = CallWindowProc(oldwndproc, hwnd, msg, wparam, lparam);
return ret;
}
private IntPtr tdxtt(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam,bool traceit=true)
{
IntPtr ret;
if (traceit == false)
{
ret = CallWindowProc(oldwndproc, hwnd, msg, wparam, lparam);
}
else
{
Graphics g = Graphics.FromHwndInternal(hwnd);
GetClientRect(hwnd, out var nrc);
var rect = new System.Drawing.Rectangle(nrc.Left, nrc.Top, nrc.Width, nrc.Height);
if (bmp != null)
bmp.Dispose();
{
bmp = new Bitmap(rect.Width, rect.Height);
var grbmp = Graphics.FromImage(bmp);
trptest(grbmp);
grbmp.Dispose();
}
ret = CallWindowProc(oldwndproc, hwnd, msg, wparam, lparam);
g.DrawImage(bmp, 0, 0);
}
return ret;
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var uidoc = commandData.Application.ActiveUIDocument;
// commandData.Application.Idling += gcbar;
if (oldwndproc == IntPtr.Zero)
{
var ig = GetActiveWindow();
IntPtr hp = GetChildWindowHandle("AfxFrameOrView", ig);
wa = new WndProcPointer(WndProc);
var intptrwa = Marshal.GetFunctionPointerForDelegate(wa);
oldwndproc = SetWindowLongPtr(hp, GWL_WNDPROC, intptrwa);
var sel = uidoc.Selection;
try
{
var oo = sel.PickPoint();
}
finally
{
SetWindowLongPtr(hp, GWL_WNDPROC, oldwndproc);
oldwndproc = IntPtr.Zero;
}
}
return Result.Succeeded;
} |
Partager