Dim posScreen = Cursor.Position Dim posSelf = eventSourceControl.PointToClient(posScreen) Dim posContainer = eventSourceControl.Parent.PointToClient(posScreen) Dim posForm = eventSourceControl.FindForm().PointToClient(posScreen)
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load SharpDX.RawInput.Device.RegisterDevice( SharpDX.Multimedia.UsagePage.Generic, SharpDX.Multimedia.UsageId.GenericMouse, SharpDX.RawInput.DeviceFlags.None) AddHandler SharpDX.RawInput.Device.MouseInput, AddressOf RawInput_MouseInput End Sub Private Sub RawInput_MouseInput(sender As Object, e As SharpDX.RawInput.MouseInputEventArgs) Dim isMouseDown = e.ButtonFlags.HasFlag(SharpDX.RawInput.MouseButtonFlags.LeftButtonDown) Dim isMouseUp = e.ButtonFlags.HasFlag(SharpDX.RawInput.MouseButtonFlags.LeftButtonUp) '左マウスボタンが操作されていないので、何もしない If Not (isMouseDown OrElse isMouseUp) Then Return '現在のマウス位置のコントロールを調べる Dim child = HitTest(Me, MousePosition) 'それが Button でない場合は何もしない If TypeOf child IsNot Button Then Return 'MouseDown 箇所のボタンを赤く染める If isMouseDown Then child.BackColor = Color.Red 'MouseUp 箇所のボタンを青く染める If isMouseUp Then child.BackColor = Color.Blue End Sub Private Shared Function HitTest(root As Control, screenPos As Point) As Control Dim current = root Dim last As Control = Nothing Do Until current Is Nothing last = current current = current.GetChildAtPoint(current.PointToClient(screenPos)) Loop Return last End Function End Class