1

这是这种情况:

在此处输入图像描述

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

我应该使用什么代码?(也许把它放在 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)。

4

2 回答 2

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
于 2013-07-08T13:47:14.907 回答
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
于 2013-07-08T13:44:12.957 回答