HtmlElement.ClientRectangle プロパティ

定義

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 ピクセル未満の場合にこの要素を取得し、そのサイズを拡張し、ユーザーがテキストを入力できるように DIVcontentEditable 状態に設定する方法を示します。

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 ページ上の任意の座標に配置できます。

適用対象

こちらもご覧ください