GetActiveWindow()并不总是返回活动窗口?

时间:2011-04-20 02:04:52

标签: .net vb.net active-window

我正在制作一款使用Control.MouseButtonsGetActiveWindow()的游戏:

        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

ParentForm。)

但是,最奇怪的事情发生了;在我游戏的第一个“场景”(菜单)中,这可以正常工作。在第二个(实际游戏)中它似乎不起作用。最奇怪的事情发生了:我可以测试这个方法是否适用于渲染方法:

    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

1 个答案:

答案 0 :(得分:1)

没有足够的代码来尝试重新解决此问题。只是不要这样做。为表单的Deactivate事件实现一个处理程序,你的游戏循环应该停止更新所有内容。包括鼠标轮询。使用Activate事件再次启用它。

相关问题