HtmlElement.ClientRectangle 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 HTML 文件中元素的用戶端區域邊界。
public:
property System::Drawing::Rectangle ClientRectangle { System::Drawing::Rectangle get(); };
public System.Drawing.Rectangle ClientRectangle { get; }
member this.ClientRectangle : System.Drawing.Rectangle
Public ReadOnly Property ClientRectangle As Rectangle
屬性值
元素佔據的客戶區域,扣除邊框和捲軸所佔的區域。 要取得元素的位置與尺寸,包括其裝飾,請使用 OffsetRectangle 。
範例
假設你已經將以下 HTML 頁面載入到控制項的託管實例 WebBrowser 中。
<HTML>
<BODY>
<DIV id="div1" style="position:absolute;top:100px;left:100px;border- style:solid;border-width:1px;">
Edit this text.
</DIV>
</BODY>
</HTML>
以下程式碼範例示範了當用戶端區域寬度小於 400 像素、高 50 像素時,如何擷取並擴展其尺寸,並且將 設定 DIV 為 contentEditable 狀態,讓使用者能輸入文字。
private void EnableEditing()
{
if (webBrowser1.Document != null)
{
HtmlElement elem = webBrowser1.Document.GetElementById("div1");
if (elem != null)
{
if (elem.ClientRectangle.Width < 200)
{
elem.SetAttribute("width", "200px");
}
if (elem.ClientRectangle.Height < 50)
{
elem.SetAttribute("height", "50px");
}
elem.SetAttribute("contentEditable", "true");
//elem.SetFocus();
}
}
}
Private Sub EnableEditing()
Dim Elem As HtmlElement = WebBrowser1.Document.GetElementById("div1")
If (Not Elem Is Nothing) Then
If (Elem.ClientRectangle.Width < 200) Then
Elem.SetAttribute("width", "200px")
End If
If (Elem.ClientRectangle.Height < 50) Then
Elem.SetAttribute("height", "50px")
End If
Elem.SetAttribute("contentEditable", "true")
Elem.Focus()
End If
End Sub
備註
ClientRectangle 僅回傳已明確指定高度與寬度,或使用絕對位置的元素位置資料。 文件若其位置風格設為 absolute,則該文件為絕對定位,之後可在 HTML 頁面上任意座標放置。