鼠标不适用于Winforms中的Monogame [英] Mouse not working with Monogame in Winforms

查看:52
本文介绍了鼠标不适用于Winforms中的Monogame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下代码将Monogame嵌入Windows窗体中:

I've embedded Monogame into a Windows Form using this code:

    private IntPtr drawSurface;
    private Control gameForm;

    public MapEditor(MainWindow window)
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        this.drawSurface = window.pcbViewport.Handle;
        graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
        Mouse.WindowHandle = drawSurface;
        gameForm = Control.FromHandle(this.Window.Handle);
        gameForm.VisibleChanged += new EventHandler(gameForm_VisibleChanged);
    }  

    private void gameForm_VisibleChanged(object sender, EventArgs e)
    {
        if (gameForm.Visible)
        {
            gameForm.Visible = false;
        }
    }

    private void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
    {
        e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = drawSurface;
    }

现在,此代码曾经可以工作...我认为.已经有一段时间了,并且代码在具有旧版本的另一台计算机上,所以我似乎还记得以前在Monogame上可以使用此功能.无论如何,问题在于鼠标输入不起作用!键盘和游戏板输入工作正常,但鼠标输入根本没有注册.我进行了实验,发现如果删除VisibleChanged事件,它可以工作,但它还显示GameWindow以及窗体(窗体在PictureBox中绘制时不需要).

Now, this code used to work... I think. It's been a while and the code is on another computer with an older version so I seem to remember this working before now with Monogame. Anyway, the problem is that Mouse input doesn't work! Keyboard and Gamepad input work fine, but Mouse input doesn't register at all. I've experimented and found that if I take out the VisibleChanged event, it works but it also shows the GameWindow as well as the form (which it doesn't need to as it's being drawn in the PictureBox.

我知道我可以将GameWindow放入控件中,如果需要的话,我会这样做,但是我试图查看是否存在使现有代码再次正常工作的解决方案.

I'm aware that I can put the GameWindow in a Control, and if need be then I'll do that but I'm trying to see if there is a solution to making the existing code work again.

推荐答案

您的方法存在的问题是,在 MonoGame 中设置 Mouse.WindowHandle 不会执行任何操作(如果您查看源代码,则此属性为空代码). 1

The issue with your approach is that setting Mouse.WindowHandle in MonoGame does nothing (the setter for this property is empty, if you look at the source code).1

这意味着您的 Game 始终绑定到其默认窗口(您正在隐藏的窗口),并且该窗口之外的所有鼠标输入都将被忽略.如果您不隐藏它,将会看到单击进入原始窗口会正确创建鼠标事件.另一方面,绘制正确地完成了对图片框的绘制.键盘事件不会按屏幕区域过滤,因此它们可以正常工作.

This means that your Game is always bound to its default window (the one that you are hiding), and that all mouse input outside that window is ignored. If you don't hide it, you will see that clicking into the original window creates mouse events correctly. Draws, on the other hand, are done to the picture box correctly. Keyboard events are not filtered by screen area, so they work without issues.

我的意见是,这不是在Windows Forms中托管XNA/MonoGame的方式,而是研究如何实现应从 System.Windows继承的 GraphicsDeviceControl 类..Forms.Control ,并提供使用XNA Framework GraphicsDevice 进行绘制的功能.本文(XNA 4,但可以轻松移植到MonoGame.

My opinion is that this is not how you should be hosting XNA/MonoGame inside Windows Forms, but instead look into how to implement a GraphicsDeviceControl class, which should inherit from System.Windows.Forms.Control and provide the ability to draw itself by using an XNA Framework GraphicsDevice. The approach is explained in this article (XNA 4, but can be easily ported to MonoGame).

如果您确实想使用当前使用的方法,那么 唯一可以做的就是签出MonoGame源代码,并找到 GameWindow 的实现.Windows窗体(即 WinFormsGameWindow ,它是一个内部类).由于 Mouse.GetState()仅返回 GameWindow.MouseState ,因此实例化您自己的 GameWindow 的可能性将使您可以通过您的窗口.

If you do want to use the approach you are using right now, the only thing you can do is checkout MonoGame source code, and find the implementation of GameWindow for windows forms (that is, WinFormsGameWindow, which is an internal class). Since Mouse.GetState() merely returns GameWindow.MouseState, a possibility to instantiate your own GameWindow would allow you to get the events filtered by your window.

或者,如果您仍然要更改MonoGame代码,则最好添加 Game 构造函数重载,该重载将从一开始就接受 Control 句柄.

Or, if you are changing MonoGame code anyway, then you might as well add a Game constructor overload which would accept the Control handle from the start.


具有讽刺意味的是, 1 里面有一条评论说,此设置程序仅出于XNA兼容性"而已,例如,与人们失去调试该代码的时间相比,抛出编译时错误是一个很大的问题.:-D


1 Ironically, there is a comment inside that this setter was left "only for XNA compatibility", like throwing a compile-time error is a big deal compared to the time people will lose debugging this. :-D

这篇关于鼠标不适用于Winforms中的Monogame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆