C # takes the coordinates of the members in the page based on mouse coordinates. IE
Sometimes you need to get the element coordinates of the location of the IE mouse in the background, but it is not possible to use screen coordinates.
So we need to convert the coordinates into the internal coordinates of the browser and then get the page members through elementFromPoint.
private void Tmrwatcher_tick (object sender, EventArgs e) { IntPtr hWnd = Windowfrompoint (mouseposition); Dynamic document = Gethtmldocumentbyhandle (hWnd); if (document! = null) { Rectangle r = Gethtmlelementpoint (hWnd, mouseposition, document);//Take page member coordinates according to mouse coordinates Marshal.finalreleasecomobject (document); Console.WriteLine (r.x + ":" + r.y + ":" + R.width + ":" + r.height); } }
Above is a clock tmrwatcher tick callback function, which used the Windowfrompoint function is mainly to get
The window handle where the MousePosition is located, and then through the Gethtmldocumentbyhandle function (gets the document clause handle)
public static object Getcomobjectbyhandle (int Msg, Guid riid, IntPtr hWnd) { object _comobject; int lpdwresult = 0; if (! SendMessageTimeout (hWnd, MSG, 0, 0, Smto_abortifhung, +, ref Lpdwresult)) return null; if (Objectfromlresult (Lpdwresult, ref riid, 0, out _comobject)) return null; return _comobject; } public Object Gethtmldocumentbyhandle (IntPtr hWnd) { String buffer = new string (' \ n '); GetClassName (hWnd, ref buffer, +); if (Buffer! = "Internet explorer_server") return null; Return Getcomobjectbyhandle (Wm_html_getobject, iid_ihtmldocument, hWnd); }
Actually with my last Post: http://blog.csdn.net/u012395622/article/details/46404193
And there's no big difference, and getting a member of a Web document is just a simple dispatch mshtml COM interface
Public Rectangle Gethtmlelementpoint (IntPtr hWnd, point Point, dynamic Document) {if (document = = NULL && hWnd! = IntPtr.Zero) return rectangle.empty; ScreenToClient (hWnd, ref point); Dynamic element = Document.elementfrompoint (point. X, point. Y); if (element = = null) return rectangle.empty; try {Rectangle o = new Rectangle () {Y = Element.offsettop, X = element.offsetleft, Width = element.offsetwidth, Height = Elem Ent.offsetheight}; while (element.offsetparent! = null) {element = Element.offsetparent; O.y + = Element.offsettop; o.x + = Element.offsetleft; } return o; } catch {return rectangle.empty; } }
The above code is the implementation of the acquisition element in the Web page an exact coordinate, the whole is not difficult to read.
Why while (element.offsetparent! = null) {...} is because the Web page always does not match the client
It's cumbersome and hierarchical, as we can not see it in the same way as we do in the Win32 operation control position.
It's hard to be clear, so you don't even know how wide it is. But fortunately, a general calculation
The member element is where the window is, as long as the parent container's position is added together. Anyway, a little
The explanation is not clear, everybody is not offended
[DllImport ("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool Screentoc Lient (IntPtr hWnd, ref point lppoint); [DllImport ("user32.dll", SetLastError = True)] [Return:marshalas (Unmanagedtype.bool)] public static extern Bool GetClassName ([In]intptr hWnd, [MarshalAs (UNMANAGEDTYPE.VBBYREFSTR)]ref string ipclassname, [In]int nMaxCount]; [DllImport ("Oleacc.dll", SetLastError = True)] [Return:marshalas (Unmanagedtype.bool)] public static extern Bool Objectfromlresult ([In]int LResult, [In]ref Guid riid, [In]int WParam, [out, MarshalAs (unmanagedtype.iunknown)]out object Ppvob ject); [DllImport ("user32.dll", SetLastError = True)] [Return:marshalas (UNMANAGEDTYPE.I4)] public static extern int RegisterWindowMessage ([in]string Lpstrin g); [DllImport ("User32. dll ", EntryPoint =" Sendmessagetimeouta ", SetLastError = True)] [Return:marshalas (Unmanagedtype.bool)] Pub LIC static extern bool SendMessageTimeout ([In]intptr MSG, [In]int hWnd, [In]int WParam, [In]int LParam, [In]int fuflags, [In]int utimeout, [in, Out]ref int lpdwresult ); [DllImport ("user32.dll", SetLastError = True)] [Return:marshalas (Unmanagedtype.sysint)] public static extern IntPtr Windowfrompoint ([In]point Point ); public const int Smto_abortifhung = 2; public readonly static int wm_html_getobject = RegisterWindowMessage ("Wm_html_getobject"); Public readonly static GUID iid_ihtmldocument = new GUID ("626fc520-a41e-11cf-a731-00a0c9082637");
C # takes the coordinates of the members in a page based on mouse coordinates. IE