Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows フォーム デザイナで生成されたコード " Public Sub New() MyBase.New() ' この呼び出しは Windows フォーム デザイナで必要です。 InitializeComponent() ' InitializeComponent() 呼び出しの後に初期化を追加します。 End Sub ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ' Windows フォーム デザイナで必要です。 Private components As System.ComponentModel.IContainer ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 'Windows フォーム デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 Friend WithEvents button1 As System.Windows.Forms.Button Friend WithEvents label7 As System.Windows.Forms.Label Friend WithEvents label6 As System.Windows.Forms.Label Friend WithEvents label5 As System.Windows.Forms.Label Friend WithEvents label3 As System.Windows.Forms.Label Friend WithEvents label2 As System.Windows.Forms.Label Friend WithEvents label1 As System.Windows.Forms.Label Friend WithEvents label8 As System.Windows.Forms.Label Friend WithEvents label4 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.button1 = New System.Windows.Forms.Button Me.label7 = New System.Windows.Forms.Label Me.label6 = New System.Windows.Forms.Label Me.label5 = New System.Windows.Forms.Label Me.label3 = New System.Windows.Forms.Label Me.label2 = New System.Windows.Forms.Label Me.label1 = New System.Windows.Forms.Label Me.label8 = New System.Windows.Forms.Label Me.label4 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'button1 ' Me.button1.Font = New System.Drawing.Font("MS UI Gothic", 48.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.button1.Location = New System.Drawing.Point(17, 312) Me.button1.Name = "button1" Me.button1.Size = New System.Drawing.Size(440, 104) Me.button1.TabIndex = 15 Me.button1.Text = "ボタン" ' 'label7 ' Me.label7.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(255, Byte)) Me.label7.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label7.Location = New System.Drawing.Point(17, 160) Me.label7.Name = "label7" Me.label7.Size = New System.Drawing.Size(176, 64) Me.label7.TabIndex = 14 Me.label7.Text = "クライアント領域(ボタンのクライアント座標):" Me.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label6 ' Me.label6.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte)) Me.label6.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label6.Location = New System.Drawing.Point(17, 88) Me.label6.Name = "label6" Me.label6.Size = New System.Drawing.Size(176, 64) Me.label6.TabIndex = 12 Me.label6.Text = "ウィンドウ領域(フォームのクライアント座標):" Me.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label5 ' Me.label5.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte)) Me.label5.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label5.Location = New System.Drawing.Point(17, 16) Me.label5.Name = "label5" Me.label5.Size = New System.Drawing.Size(176, 64) Me.label5.TabIndex = 11 Me.label5.Text = "クライアント領域(フォームのクライアント座標):" Me.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label3 ' Me.label3.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(255, Byte)) Me.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.label3.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label3.Location = New System.Drawing.Point(193, 160) Me.label3.Name = "label3" Me.label3.Size = New System.Drawing.Size(264, 64) Me.label3.TabIndex = 9 Me.label3.Text = "label3" Me.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label2 ' Me.label2.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte)) Me.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.label2.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label2.Location = New System.Drawing.Point(193, 88) Me.label2.Name = "label2" Me.label2.Size = New System.Drawing.Size(264, 64) Me.label2.TabIndex = 8 Me.label2.Text = "label2" Me.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label1 ' Me.label1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte)) Me.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.label1.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label1.Location = New System.Drawing.Point(193, 16) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(264, 64) Me.label1.TabIndex = 7 Me.label1.Text = "label1" Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label8 ' Me.label8.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(255, Byte)) Me.label8.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label8.Location = New System.Drawing.Point(17, 232) Me.label8.Name = "label8" Me.label8.Size = New System.Drawing.Size(176, 64) Me.label8.TabIndex = 13 Me.label8.Text = "ウィンドウ領域(ボタンのクライアント座標):" Me.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'label4 ' Me.label4.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(255, Byte)) Me.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.label4.Font = New System.Drawing.Font("MS UI Gothic", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.label4.Location = New System.Drawing.Point(193, 232) Me.label4.Name = "label4" Me.label4.Size = New System.Drawing.Size(264, 64) Me.label4.TabIndex = 10 Me.label4.Text = "label4" Me.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) Me.ClientSize = New System.Drawing.Size(474, 432) Me.Controls.Add(Me.button1) Me.Controls.Add(Me.label7) Me.Controls.Add(Me.label6) Me.Controls.Add(Me.label5) Me.Controls.Add(Me.label3) Me.Controls.Add(Me.label2) Me.Controls.Add(Me.label1) Me.Controls.Add(Me.label8) Me.Controls.Add(Me.label4) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' フォームのクライアント領域情報をラベルに表示する label1.Text = GetClientRectangle(Me).ToString() ' フォームのウィンドウ領域情報をラベルに表示する label2.Text = GetWindowRectangle(Me).ToString() ' ボタンのクライアント領域の情報をラベルに表示する label3.Text = GetClientRectangle(button1).ToString() ' ボタンのウィンドウ領域情報をラベルに表示する label4.Text = GetWindowRectangle(button1).ToString() End Sub ' ウィンドウのクライアント領域を取得する Private Function GetClientRectangle(ByVal ctrl As Control) As Rectangle '//////////////////////////////////////////////// ' 検証用のコード ' ClientRectangle.Locationプロパティ=Point(0, 0) ' ClientRectangle.Sizeプロパティ=ClientSizeプロパティ Debug.Assert(ctrl.ClientRectangle.Location.Equals(New Point(0, 0))) Debug.Assert(ctrl.ClientRectangle.Size.Equals(ctrl.ClientSize)) '//////////////////////////////////////////////// Return ctrl.ClientRectangle End Function ' ウィンドウのウィンドウ領域を取得する Private Function GetWindowRectangle(ByVal ctrl As Control) As Rectangle '//////////////////////////////////////////////// ' 検証用のコード ' Bounds.Locationプロパティ=Locationプロパティ '  =Point(Leftプロパティ, Topプロパティ) ' Bounds.Sizeプロパティ=Sizeプロパティ '  =Size (Rightプロパティ - Leftプロパティ, ' Bottomプロパティ - Topプロパティ) Debug.Assert(ctrl.Bounds.Location.Equals(ctrl.Location)) Debug.Assert(ctrl.Location.Equals(New Point(ctrl.Left, ctrl.Top))) Debug.Assert(ctrl.Bounds.Size.Equals(ctrl.Size)) Debug.Assert(ctrl.Size.Equals(New Size(ctrl.Right - ctrl.Left, ctrl.Bottom - ctrl.Top))) '//////////////////////////////////////////////// If TypeOf ctrl Is Form Then ' フォームの左上隅の原点を(クライアント座標で)取得する Dim winRectLocation As Point = ctrl.PointToClient(ctrl.Bounds.Location) ' フォームのウィンドウ領域を(クライアント座標で)取得する Dim winRect As Rectangle = New Rectangle(winRectLocation, ctrl.Bounds.Size) Return winRect Else ' コントロールの左上隅の原点を(スクリーン座標で)取得する Dim screenLocation As Point = ctrl.Parent.PointToScreen(ctrl.Bounds.Location) ' スクリーン座標をコントロールのクライアント座標に変換する Dim winRectLocation As Point = ctrl.PointToClient(screenLocation) ' コントロールのウィンドウ領域を(クライアント座標で)取得する Dim winRect As Rectangle = New Rectangle(winRectLocation, ctrl.Bounds.Size) Return winRect End If End Function End Class