Mouse problems


I cant make mouse to work, there is offset (not constant)... How did you solve that problem? Adn I cant figure out how to change resolution....


msmith52 wrote Jul 19, 2012 at 8:02 PM

I found that by adding the following statement into the PreparingDeviceSettings will solve the offset issue regarding the mouse.

Microsoft.Xna.Framework.Input.Mouse.WindowHandle = this.m_windowHandle;

However the width and height are not scaled to the mouse's position in regards to the control window. In other words moving the mouse to the bottom left corner of the control leaves the mouse point at the center of the screen. Anyone have a solution for this?

As to the other issue, I just refactored the last few lines of ChangeGraphics section into a public method called UpdateResolution as follows so that way I could update the resolution. Here is that function.
     public void UpdateResolution()
        var changeDevice = m_graphics.GetType().GetMethod("ChangeDevice", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        changeDevice.Invoke(m_graphics, new object[] { !m_bLoaded });

kbillemeyer wrote Nov 6, 2012 at 2:42 AM

The issue is that the resolution of the XNA graphics device is being hard coded to some value that differs from the actual display size in the WPF application. Thus, when the mouse moves X units in WPF, it is actually moving X * some scale factor (the ratio between the XNA resolution and the WPF control's size) relative to XNA. To fix this you need to modify the call to ChangeGraphics by passing in the control's size instead of the arbitrary value.

I did this by changing the constructor of XNAGameControl to take in an int width, int height in addition to the parameters already specified.

Refering to the sample solution, my modified code looks like this:

public XNAControlGame(IntPtr windowHandle, string contentRoot, int width, int height)
        : base()
   ChangeGraphics(width, height);

Then in the Game1 class, you need to update the constructor to match the superclass.

public Game1(IntPtr handle, int width, int height)
        : base(handle, "Content", width, height)
        Animation = true;
Lastly, you need to update the Game1 instantiation in MainWindow.cs. Not that you can't get the size of the control until it has been fully loaded, otherwise you will get NaN or 0 (depending on which property you fetch the height/width with).

public MainWindow()
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
        m_game = new XNAControlGame.Game1(
I'm not sure if this is the best way to get the width and height, but it works for my purposes. You can play with it on your own.

wrote Feb 14, 2013 at 6:48 PM