我正在制作一款使用Control.MouseButtons
和GetActiveWindow()
的游戏:
Private Declare Auto Function GetActiveWindow Lib "user32.dll" () As IntPtr
Public Sub GetMouseState(ByRef x As Integer, ByRef y As Integer, ByRef lButton As Boolean)
Dim p As Point = Parent.PointToClient(Windows.Forms.Cursor.Position)
x = p.X
y = p.Y
lButton = _
(Control.MouseButtons And MouseButtons.Left) = MouseButtons.Left AndAlso _
Parent.Handle = GetActiveWindow()
End Sub
(Parent
是Form
。)
Dim down As Boolean
Game.GetMouseState(0, 0, down)
If down Then g.Clear(Color.Red)
果然,当我点击鼠标时,屏幕变红。但是,我的对象不动。当我删除Parent.Handle = GetActiveWindow()
支票时,我的对象会移动!这是代码,虽然它可能没关系:
Dim mouseX, mouseY As Integer, mouseDown As Boolean
Game.GetMouseState(mouseX, mouseY, mouseDown)
If mouseDown Then
Dim mouseLocation As New PointF(mouseX - Game.Width \ 2, mouseY - Game.Height \ 2)
centerParticle.MoveTowards(mouseLocation, centerParticle.Location)
End If
GetActiveWindow()仅在我的游戏运行时才返回正确的值吗? :P
答案 0 :(得分:1)
没有足够的代码来尝试重新解决此问题。只是不要这样做。为表单的Deactivate事件实现一个处理程序,你的游戏循环应该停止更新所有内容。包括鼠标轮询。使用Activate事件再次启用它。