Reputation: 3269
Ok, consider this image.
I develop an IE extension in c# and I would :
- the distance in red, between top of screen and top of `visible webpage`
- the distance in red between left of screen and left of `visible webpage`
- the width/heigth of the visible webpage
Of course considering that i have the whole screen size. If i have red and black I can calculate green.
What the point ?
I have thousand screen coordinates (X,Y), i have to calcul the coordinate relative to the webpage.
Example :
Considering
Screen size : 1200 * 800
Webpage size : 400*300
Red distance between left screen border and left webpage border : 200
Red distance between top screen border and top webpage border : 300
So my coordinates screen => relative webpage becomes :
( 100, 100 ) => OUTSIDE WEBPAGE( ignored )
( 1100, 650 ) => OUTSIDE WEBPAGE ( ignored )
( 200, 300 ) => ( 0,0 )
( 250, 400 ) => ( 50, 100 )
Actually i have this code, this
is inherited from AddinExpress.IE.ADXIEModule
, thetoolbarObj is the toolbar that I added to InternetExplorer. So i can use pointToScreen on it and i'm not far of what I need, but the left corner of the toolbar is not what I need, I need the leftcorner of the webpage.
public void getUtilsDimension()
{
Rectangle resolution = Screen.PrimaryScreen.Bounds;
Int32 screenWidth = resolution.Width;
Int32 screenHeight = resolution.Height;
AddinExpress.IE.ADXIEToolBarItem toolbarItem = this.ToolBars[0];
AddinExpress.IE.ADXIEToolbar toolbarObj = toolbarItem.ToolBarObj;
Point leftCornerWebPage = toolbarObj.PointToScreen(new Point(0, 0));
Int32 toolbarHeight = toolbarObj.Height;
Int32 toolbarWidth = toolbarObj.Width;
Debug.WriteLine("Largeur écran : " + screenWidth);
Debug.WriteLine("Hauteur écran : " + screenHeight);
Debug.WriteLine("LeftCornerX : " + leftCornerWebPage.X);
Debug.WriteLine("LeftCornerY : " + leftCornerWebPage.Y);
Debug.WriteLine("toolbarHeight : " + toolbarHeight);
Debug.WriteLine("toolbarWidth : " + toolbarWidth);
}
This is what I get actually, the screen is 1600*900, pointToScreen return the coordinates of the red cross ( 484,158 ). But I need the coordinates of the blue cross, as the width and heigh of visible webpage. I know I can get that with $(window) in Jquery, but i don't know how with c#.
I can access at the HTLMDocument (typeof mshtml.HTMLDocument
) with this.HTMLDocument
, unfortunately pointToScreen is not available on HTMLDocument object.
Edit : It s chrome on the first screenshot but of course that should be IE
Update 08/12
OK I have the width and height of the visible webpage ( black line on my screen shot ) The only missing thing is the coordinates of blue cross on my screenshot 2
var heightVisibleWebPage = HTMLDocument.documentElement.offsetHeight;
var widthVisibleWebPage = HTMLDocument.documentElement.offsetWidth;
For the bounty, I need the exact coordinates of the blue cross. No matter how. It should work no matter the Internet explorer version, favorites/tool/command/state bar displayed or not.
Update 08/12 HTMLDocument
HTMLDocument
is from AddinExpress, it's not a System.Windows.Forms.HtmlDocument
public mshtml.HTMLDocument HTMLDocument
{
get
{
return (this.HTMLDocumentObj as mshtml.HTMLDocument);
}
}
His parent HTMLDocument.parentWindows is a IHTMLWindow2 object
HTMLDocumentObj is a member of
public class ADXIEModule : Component, IRemoteModule2, IRemoteModule, IObjectWithSite, IWin32Window
{
...
//
// Résumé :
// Gets the automation object (a COM object) of the active document, if any.
//
// Notes :
// When the active document is an HTML page, this property provides access to
// the contents of the HTML Document Object Model (DOM). Specifically, it returns
// an HTMLDocument object reference. The HTMLDocument object is functionally
// equivalent to the HTML document object used in HTML page script. It supports
// all the properties and methods necessary to access the entire contents of
// the active HTML document.
// The HTMLDocument object can be used through the IHTMLDocument interface,
// the IHTMLDocument2 interface, and the IHTMLDocument3 interface.
// When other document types are active, such as a Microsoft Word document,
// this property returns the document automation object of that document. For
// Word documents, this is the Document object.
[Browsable(false)]
public object HTMLDocumentObj { get; }
...
}
Explain when -1 for the community please ;)
Upvotes: 4
Views: 607
Reputation: 7214
These are the steps:
Find Internet Explorer
window handle with EnumWindows()
api. Class name is IEFrame
Iterate through all child windows with EnumChildWindows()
api. Class name is Internet Explorer_Server
Find x, y
coordinate with GetWindowRect()
api
Code:
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsCallback lpEnumFunc, int lParam);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsCallback lpEnumFunc, int lParam);
public delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
public static extern void GetClassName(IntPtr hwnd, StringBuilder s, int nMaxCount);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
private IntPtr ieHandle, ieChildHandle;
private void GetWindows()
{
EnumWindows(Callback, 0);
}
private bool Callback(IntPtr hwnd, int lParam)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, className.Capacity);
if (className.ToString().Equals("IEFrame"))
{
ieHandle = hwnd;
return false;
}
return true; //continue enumeration
}
private void GetChildWindows()
{
if (ieHandle != IntPtr.Zero)
{
EnumChildWindows(ieHandle, CallbackChild, 0);
}
}
private bool CallbackChild(IntPtr hwnd, int lParam)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, className.Capacity);
if (className.ToString().Equals("Internet Explorer_Server"))
{
ieChildHandle = hwnd;
return false;
}
return true; //continue enumeration
}
To get the coordinates:
GetWindows();
GetChildWindows();
if (ieChildHandle != IntPtr.Zero)
{
RECT rect;
if (GetWindowRect(ieChildHandle, out rect))
{
//rect.Left, rect.Top
}
}
ieChildHandle = IntPtr.Zero;
ieHandle = IntPtr.Zero;
Tested with IE 6, 9 and 11
Upvotes: 2
Reputation: 81
Get a handle on the current tab:
foreach (InternetExplorer ie in new ShellWindows())
{
// Find
// Current
// Tab
//currentTab.left // left edge in pixels
}
You may need to drill down into parent objects using ".parent" to add all the the offsets needed until you get the total offset of the browser tab.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa752084(v=vs.85).aspx#properties
Upvotes: 0