2014-12-01 103 views
4

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) 

其實我有這樣的代碼,thisAddinExpress.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返回紅十字會(484158)的座標。但我需要將藍色十字的座標作爲可見網頁的寬度和高度。我知道我可以通過$(window)在Jquery中獲得,但我不知道如何與C#。

我可以在HTLMDocument(typeof mshtml.HTMLDocument)和this.HTMLDocument上訪問,不幸的是pointToScreen在HTMLDocument對象上不可用。

enter image description here

編輯:IT方面的第一張截圖,但當然鉻,應該是IE

更新08/12

OK我有可見的網頁的寬度和高度(我的屏幕截圖上的黑色線條) 唯一缺失的是我截圖上的藍色十字的座標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爲界,請;)

+0

downvote可能是因爲您沒有顯示您嘗試過的內容。這裏的用戶在這裏幫助解決您的代碼問題,而不是爲您編寫解決方案。你有代碼顯示嗎? – 2014-12-01 17:47:27

+0

好的,它的編輯 – amdev 2014-12-02 11:54:04

+0

HTMLDocument應該有parentWindow字段 - 它是否返回整個IE窗口或html繪圖矩形的容器? – Gerino 2014-12-08 17:22:25

回答

2

這些步驟如下:

  1. 查找Internet Explorer窗口句柄與EnumWindows() API。類名是IEFrame

  2. EnumChildWindows() api遍歷所有子窗口。類名是Internet Explorer_Server

  3. 查找x, yGetWindowRect() API協調

代碼:

[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測試

+0

這是一個非常有趣的方法!但是我的自定義工具欄(IE擴展)創建了一些錯誤,並且您的代碼給出了我們隱藏的addinexpress工具欄(http://www.hostingpics.net/viewer.php?id=213155Result.jpg)的位置,我們可以在屏幕截圖上看到奇怪的質感。但沒有我的自定義工具欄,它的作品不幸的是,我不能取消激活我的工具欄,因爲它是我的IE擴展的一部分。所以如果沒有人發佈更好的答案,我會在本週接受你的。 – amdev 2014-12-09 16:44:57

+0

@amdev添加一個列表'私人列表 childHandles =新列表();'。刪除'ieChildHandle = hwnd;返回false;'從'if(className.ToString()。Equals(「Internet Explorer_Server」))''並添加'childHandles.Add(hwnd);'而不是。不要忘記刪除'return false;'!在GetWindows()之後; GetChildWindows();'檢查'childHandles.Count'。 – 2014-12-09 17:23:55

+0

好的兒童手柄[1]就是我需要的!謝謝,偉大的方法,很好的答案。 – amdev 2014-12-09 19:11:23

相關問題