获取屏幕边缘和网页之间的距离

时间:2014-12-01 16:48:56

标签: c# winforms internet-explorer add-in

enter image description here

好的,请考虑这张图片。

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

当然考虑到我有整个屏幕尺寸。如果我有红色和黑色,我可以计算绿色。

重点是什么?

我有一千个屏幕坐标(X,Y),我必须计算相对于网页的坐标。

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 )

实际上我有这段代码,this继承自AddinExpress.IE.ADXIEModule,thetoolbarObj是我添加到InternetExplorer的工具栏。所以我可以在它上面使用pointToScreen并且我不是我需要的,但是工具栏的左角不是我需要的,我需要网页的左角。

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);

}

这就是我实际得到的,屏幕是1600 * 900,po​​intToScreen返回红叉的坐标(484,158)。但我需要蓝色十字的坐标,作为可见网页的宽度和高度。我知道我可以用Jquery中的$(窗口)来获取它,但我不知道如何使用c#。

我可以使用mshtml.HTMLDocument访问HTLMDocument(typeof this.HTMLDocument),遗憾的是,在HTMLDocument对象上,pointToScreen不可用。

enter image description here

编辑:它是第一个截图的Chrome,但当然应该是IE

更新08/12

好的,我有可见网页的宽度和高度(我的屏幕截图上的黑线) 唯一缺少的是我的截图2上蓝色十字的坐标

var heightVisibleWebPage = HTMLDocument.documentElement.offsetHeight;
var widthVisibleWebPage = HTMLDocument.documentElement.offsetWidth;

对于赏金,我需要蓝十字的精确坐标。不管怎样。无论是否显示Internet Explorer版本,收藏夹/工具/命令/状态栏,它都应该有效。

更新08/12 HTMLDocument

HTMLDocument来自AddinExpress,它不是System.Windows.Forms.HtmlDocument

public mshtml.HTMLDocument HTMLDocument
{
    get
    {
        return (this.HTMLDocumentObj as mshtml.HTMLDocument);
    }

}

他的父HTMLDocument.parentWindows是IHTMLWindow2对象

HTMLDocumentObj是

的成员
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; }

    ...

}

解释什么时候-1为社区请;)

2 个答案:

答案 0 :(得分:2)

以下是步骤:

  1. 使用Internet Explorer api查找EnumWindows()窗口句柄。班级名称为IEFrame

  2. 使用EnumChildWindows() api遍历所有子窗口。班级名称为Internet Explorer_Server

  3. 使用x, y api

  4. 查找GetWindowRect()坐标

    <强>代码:

    [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
    }
    

    获取坐标:

    GetWindows();
    GetChildWindows();
    
    if (ieChildHandle != IntPtr.Zero)
    {
        RECT rect;
    
        if (GetWindowRect(ieChildHandle, out rect))
        {
            //rect.Left, rect.Top
        }
    
    }
    
    ieChildHandle = IntPtr.Zero;
    ieHandle = IntPtr.Zero;
    

    使用IE 6,9和11进行测试

答案 1 :(得分:0)

获取当前标签的句柄:

foreach (InternetExplorer ie in new ShellWindows())
{
    // Find
    // Current
    // Tab
    //currentTab.left // left edge in pixels
}

您可能需要使用“.parent”深入查看父对象,以添加所需的所有偏移,直到获得浏览器选项卡的总偏移量。

http://msdn.microsoft.com/en-us/library/windows/desktop/aa752084(v=vs.85).aspx#properties