Making sure my XNA game has focus before handling mouse and keyboard events

穿精又带淫゛_ 提交于 2019-12-08 15:20:45

问题


I'm developing a game that is mixed with winforms (for the map editor). The problem I am facing is that even if the main game window isn't in focus, it still receives mouse change events (such as clicking and moving the mouse).

Is there a way to make sure my application is in focus before continuing on to handle these events are is there some kind of built in method?

Thanks!


回答1:


Use the Game.IsActive property to check if you should deal with mouse and keyboard input.




回答2:


If you add a reference to System.Windows.Forms you can check if the XNA window is active like this:

if (System.Windows.Forms.Form.ActiveForm ==
    (System.Windows.Forms.Control.FromHandle(Window.Handle) as System.Windows.Forms.Form))
{
    // Active form is the XNA window.
}


来源:https://stackoverflow.com/questions/9550640/making-sure-my-xna-game-has-focus-before-handling-mouse-and-keyboard-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!