如何在XNA中使用DirectX.DirectInput

本文关键字:DirectX DirectInput XNA | 更新日期: 2023-09-27 17:49:27

joystick.cs

using System;
using Microsoft.DirectX.DirectInput;
namespace gameproject
{
    /// <summary>
    /// Description of device.
    /// </summary>
    class joysticks
    {
        public static Device joystick;
        public static JoystickState state;
        public static void InitDevices() //Function of initialize device
        {
            //create joystick device.
            foreach (DeviceInstance di in Manager.GetDevices(
                DeviceClass.GameControl,
                EnumDevicesFlags.AttachedOnly))
            {
                joystick = new Device(di.InstanceGuid);
                break;
            }
            if (joystick == null)
            {
                //Throw exception if joystick not found.
            }
            //Set joystick axis ranges.
            else {
                foreach (DeviceObjectInstance doi in joystick.Objects)
                {
                    if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
                    {
                        joystick.Properties.SetRange(
                            ParameterHow.ById,
                            doi.ObjectId,
                            new InputRange(-5000, 5000));
                    }
                }
                joystick.Properties.AxisModeAbsolute = true;
                joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
                //Acquire devices for capturing.
                joystick.Acquire();
                state = joystick.CurrentJoystickState;
            }
        }
        public static void UpdateJoystick()   // Capturing from device joystick
        {
            //Get Joystick State.
            if(joystick!=null)
                state = joystick.CurrentJoystickState;
        }
    }
}

在这一行中,发生了一个错误,

    joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive 
| CooperativeLevelFlags.Background);

错误,

Error 1 The type 'System.Windows.Forms.Control' is defined in an 
assembly that is not referenced.
     You must add a reference to assembly 'System.Windows.Forms...

我正在处理XNA 3.0和.NET 3.5,那么这个错误意味着什么呢?

如何在XNA中使用DirectX.DirectInput

SetCooperativeLevelSystem.Windows.Forms.Control对象作为第一个参数(其中为null(,因此您仍然应该引用应用程序中定义此类的程序集。从应用程序/游戏中添加引用do System.Windows.Forms.dll,然后重试。如果您正在使用的代码使用的是一些您没有在后台引用的其他类,这是可以的,但当它们是公共的(就像它们是参数或从您正在调用的方法返回(时,您必须引用其中定义了这些类型的程序集。

类似stackoverflow帖子:调试错误";类型';xx';在未被引用的程序集中定义";