2014-01-10 62 views
0

我試圖得到一張圖片或更具體的動畫來跟隨整個計算機的鼠標,而不僅僅是一個窗體。我發現的代碼VB.NET系統寬圖片框跟隨鼠標?

公共類Form1中

Private Sub Form1_MouseMove(ByVal sender As Object, _ 
          ByVal e As System.Windows.Forms.MouseEventArgs) _ 
          Handles Me.MouseMove 
    PictureBox1.Top = MousePosition.Y - Me.Top 
    PictureBox1.Left = MousePosition.X - Me.Left 
End Sub 

末級

這個小片段和工程花花公子所以現在我想這樣做沒有形式。

任何幫助將不勝感激。

回答

0

你不可能真的放棄表格,但你可以繞過它。創建表單,將Borderstyle設置爲none,並將TopMost設置爲true。添加一個Picturebox並將picturebox的.Dock屬性設置爲All。
在我的代碼中的計時器被使用,因爲我們將使窗體點擊低谷,然後Mouseevents將無法正常工作。

使用下面的代碼使表單遵循mousecursor。

Public Class Form1 
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="GetWindowLong")> Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer 
End Function 
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SetWindowLong")> Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer 
End Function 

Const xOff As Integer = 0 'How far the form will be away from the curson in x-direction 
Const yOff As Integer = 0 'How far the form will be away from the curson in y-direction 

Private WithEvents Timer1 As Timer 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) 
    Me.SetDesktopLocation(MousePosition.X - xOff, MousePosition.Y - yOff) 'Set the form's position 
End Sub 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    'Setup a timer 
    Timer1 = New Timer 
    Timer1.Interval = 1 
    AddHandler Timer1.Tick, AddressOf Timer1_Tick 
    Timer1.Start() 

    'Set the form to clickthrough 
    'See http://stackoverflow.com/questions/18294658/vb-net-click-through-form 
    Dim InitialStyle As Integer 
    InitialStyle = GetWindowLong(Me.Handle, -20) 
    SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20) 'Makes the window "click-throughable" 
End Sub 
End Class 

它與其他TopMost窗口和通過任務欄切換程序有問題,但它是一個開始。