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 timer1 As System.Windows.Forms.Timer 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.components = New System.ComponentModel.Container Me.button1 = New System.Windows.Forms.Button Me.timer1 = New System.Windows.Forms.Timer(Me.components) 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 = "ボタン" ' 'timer1 ' Me.timer1.Enabled = True Me.timer1.Interval = 10 ' '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.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.Controls.Add(Me.button1) 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 Function GetWindowRectangle(ByVal ctrl As Control) As Rectangle 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 ' ある点がコントロールやフォームのクライアント領域内に ' 含まれるかどうかに関する情報を取得する Private Function GetClientContainState(ByVal ctrl As Control) As String Dim rect As Rectangle = ctrl.ClientRectangle Return GetContainState(ctrl, rect) End Function ' ある点がコントロールやフォームのウィンドウ領域内に ' 含まれるかどうかに関する情報を取得する Private Function GetWindowContainState(ByVal ctrl As Control) As String Dim rect As Rectangle = GetWindowRectangle(ctrl) Return GetContainState(ctrl, rect) End Function Private Function GetContainState(ByVal ctrl As Control, ByVal rect As Rectangle) As String Dim output As String ' マウス座標(スクリーン座標系)の取得 Dim mouseScreenPos As Point = Control.MousePosition ' マウス座標をクライアント座標系へ変換 Dim mouseClientPos As Point = ctrl.PointToClient(mouseScreenPos) ' マウス座標(クライアント座標系)が領域内かどうか Dim inside As Boolean = rect.Contains(mouseClientPos) output = String.Format("{0}" & vbLf & "{1} − {2}", _ rect, _ IIf(inside, "内部", "外部"), _ mouseClientPos) Return output End Function Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick ' フォーム・クライアント領域での情報をラベルに表示 label1.Text = GetClientContainState(Me) ' フォーム・ウィンドウ領域での情報をラベルに表示 label2.Text = GetWindowContainState(Me) ' ボタン・クライアント領域での情報をラベルに表示 label3.Text = GetClientContainState(button1) ' ボタン・ウィンドウ領域での情報をラベルに表示 label4.Text = GetWindowContainState(button1) End Sub End Class