两种形式的交叉跟随鼠标

时间:2013-07-08 13:01:05

标签: vb.net visual-studio-2010

情况就是这样:

enter image description here

我希望交点部分的中心跟随鼠标。

我应该使用什么代码? (也许把它放在form1加载事件中?)

其他信息:

    Form1.Height = Val(Screen.PrimaryScreen.WorkingArea.Height) + 30 [*]
    Form1.Width = 21
    Form2.Width = Val(Screen.PrimaryScreen.WorkingArea.Width)
    Form2.Height = 21

[*]

+30是我窗口中任务栏的宽度。我不知道为什么,但Screen.PrimaryScreen.WorkingArea.Height不包括我的屏幕分辨率的总高度(768px)。

2 个答案:

答案 0 :(得分:3)

在表单中添加一个计时器,并将其间隔设置为10

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Location = New Point(0, MousePosition.Y - 10)  'Form1
        Form2.Location = New Point(MousePosition.X - 10, 0) 'Form2
    End Sub

从实际位置减去10个像素,使光标居中于交点本身。

然后在Form1加载事件

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         Me.Width = Screen.PrimaryScreen.Bounds.Width
         Me.Height = 21
         Form2.Show()
    End Sub

并在您的Form2加载事件

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         Me.Height = Screen.PrimaryScreen.Bounds.Height
         Me.Width = 21
    End Sub

答案 1 :(得分:0)

您可以使用计时器:

Public Class Form1

   Dim SecondForm As Form = New Form()
   Dim MouseTimer As System.Timers.Timer = New System.Timers.Timer(10)

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
      Opacity = 0.5
      BackColor = Color.DarkGray
      Height = Screen.PrimaryScreen.Bounds.Height
      Width = 21
      StartPosition = FormStartPosition.Manual
      Location = New Point(0, 0)
      SecondForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
      SecondForm.Opacity = 0.5
      SecondForm.BackColor = Color.DarkGray
      SecondForm.Height = 21
      SecondForm.Width = Screen.PrimaryScreen.Bounds.Width
      SecondForm.StartPosition = FormStartPosition.Manual
      SecondForm.Location = New Point(0, 0)
      SecondForm.Show()

      AddHandler MouseTimer.Elapsed, AddressOf MouseTimer_Elapsed
      MouseTimer.AutoReset = True
      MouseTimer.Start()
   End Sub


   Private Sub MouseTimer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs)
      If (InvokeRequired) Then
         Invoke(DirectCast(Sub() MouseTimer_Elapsed(sender, e), MethodInvoker))
      Else
         Location = New Point(System.Windows.Forms.Cursor.Position.X, 0)
         SecondForm.Location = New Point(0, System.Windows.Forms.Cursor.Position.Y)
      End If
   End Sub

End Class