home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectInput / Joystick / Main.cs < prev   
Encoding:
Text File  |  2004-09-27  |  29.3 KB  |  734 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Main.cs
  3. //
  4. // Desc: The Joystick sample obtains and displays joystick data.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. //-----------------------------------------------------------------------------
  8. using System;
  9. using System.Windows.Forms;
  10. using Microsoft.DirectX.DirectInput;
  11. using Microsoft.DirectX;
  12.  
  13. namespace Joystick
  14. {
  15.     public class MainClass : System.Windows.Forms.Form
  16.     {
  17.         // These three fields hold common data that
  18.         // different threads will have to access
  19.         public static JoystickState state = new JoystickState();
  20.         private Device applicationDevice = null;
  21.         public static int numPOVs = 0;
  22.         private int SliderCount = 0; // Number of returned slider controls
  23.  
  24.         #region Window control declarations
  25.         private System.Windows.Forms.GroupBox groupBox1;
  26.         private System.Windows.Forms.Label labelXAxis;
  27.         private System.Windows.Forms.Label labelYAxis;
  28.         private System.Windows.Forms.Label labelZAxis;
  29.         private System.Windows.Forms.Label labelZRotation;
  30.         private System.Windows.Forms.Label labelYRotation;
  31.         private System.Windows.Forms.Label labelXRotation;
  32.         private System.Windows.Forms.Label labelPOV1;
  33.         private System.Windows.Forms.Label labelPOV0;
  34.         private System.Windows.Forms.Label labelSlider1;
  35.         private System.Windows.Forms.Label labelSlider0;
  36.         private System.Windows.Forms.Label labelPOV2;
  37.         private System.Windows.Forms.Label labelPOV3;
  38.         private System.Windows.Forms.Button buttonExit;
  39.         private System.Windows.Forms.Label labelXAxisText;
  40.         private System.Windows.Forms.Label labelYAxisText;
  41.         private System.Windows.Forms.Label labelZAxisText;
  42.         private System.Windows.Forms.Label labelXRotationText;
  43.         private System.Windows.Forms.Label labelYRotationText;
  44.         private System.Windows.Forms.Label labelZRotationText;
  45.         private System.Windows.Forms.Label labelSlider0Text;
  46.         private System.Windows.Forms.Label labelSlider1Text;
  47.         private System.Windows.Forms.Label labelPOV2Text;
  48.         private System.Windows.Forms.Label labelPOV1Text;
  49.         private System.Windows.Forms.Label labelPOV0Text;
  50.         private System.Windows.Forms.Label labelPOV3Text;
  51.         private System.Windows.Forms.Label label1;
  52.         private System.Windows.Forms.Label labelButtons;
  53.         private System.Windows.Forms.Label label2;
  54.         #endregion
  55.         private System.Windows.Forms.Timer timer1;
  56.         private System.ComponentModel.IContainer components;
  57.         
  58.  
  59.         
  60.         
  61.         #region Windows Form Designer generated code
  62.         /// <summary>
  63.         /// Required method for Designer support - do not modify
  64.         /// the contents of this method with the code editor.
  65.         /// </summary>
  66.         private void InitializeComponent()
  67.         {
  68.             this.components = new System.ComponentModel.Container();
  69.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  70.             this.labelButtons = new System.Windows.Forms.Label();
  71.             this.label1 = new System.Windows.Forms.Label();
  72.             this.labelPOV3 = new System.Windows.Forms.Label();
  73.             this.labelSlider1 = new System.Windows.Forms.Label();
  74.             this.labelSlider0 = new System.Windows.Forms.Label();
  75.             this.labelPOV2 = new System.Windows.Forms.Label();
  76.             this.labelPOV1 = new System.Windows.Forms.Label();
  77.             this.labelPOV0 = new System.Windows.Forms.Label();
  78.             this.labelZRotation = new System.Windows.Forms.Label();
  79.             this.labelYRotation = new System.Windows.Forms.Label();
  80.             this.labelXRotation = new System.Windows.Forms.Label();
  81.             this.labelZAxis = new System.Windows.Forms.Label();
  82.             this.labelYAxis = new System.Windows.Forms.Label();
  83.             this.labelXAxis = new System.Windows.Forms.Label();
  84.             this.labelPOV3Text = new System.Windows.Forms.Label();
  85.             this.labelPOV2Text = new System.Windows.Forms.Label();
  86.             this.labelPOV1Text = new System.Windows.Forms.Label();
  87.             this.labelPOV0Text = new System.Windows.Forms.Label();
  88.             this.labelSlider1Text = new System.Windows.Forms.Label();
  89.             this.labelSlider0Text = new System.Windows.Forms.Label();
  90.             this.labelZRotationText = new System.Windows.Forms.Label();
  91.             this.labelYRotationText = new System.Windows.Forms.Label();
  92.             this.labelXRotationText = new System.Windows.Forms.Label();
  93.             this.labelZAxisText = new System.Windows.Forms.Label();
  94.             this.labelYAxisText = new System.Windows.Forms.Label();
  95.             this.labelXAxisText = new System.Windows.Forms.Label();
  96.             this.buttonExit = new System.Windows.Forms.Button();
  97.             this.label2 = new System.Windows.Forms.Label();
  98.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  99.             this.groupBox1.SuspendLayout();
  100.             this.SuspendLayout();
  101.             // 
  102.             // groupBox1
  103.             // 
  104.             this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  105.                                                                                     this.labelButtons,
  106.                                                                                     this.label1,
  107.                                                                                     this.labelPOV3,
  108.                                                                                     this.labelSlider1,
  109.                                                                                     this.labelSlider0,
  110.                                                                                     this.labelPOV2,
  111.                                                                                     this.labelPOV1,
  112.                                                                                     this.labelPOV0,
  113.                                                                                     this.labelZRotation,
  114.                                                                                     this.labelYRotation,
  115.                                                                                     this.labelXRotation,
  116.                                                                                     this.labelZAxis,
  117.                                                                                     this.labelYAxis,
  118.                                                                                     this.labelXAxis,
  119.                                                                                     this.labelPOV3Text,
  120.                                                                                     this.labelPOV2Text,
  121.                                                                                     this.labelPOV1Text,
  122.                                                                                     this.labelPOV0Text,
  123.                                                                                     this.labelSlider1Text,
  124.                                                                                     this.labelSlider0Text,
  125.                                                                                     this.labelZRotationText,
  126.                                                                                     this.labelYRotationText,
  127.                                                                                     this.labelXRotationText,
  128.                                                                                     this.labelZAxisText,
  129.                                                                                     this.labelYAxisText,
  130.                                                                                     this.labelXAxisText});
  131.             this.groupBox1.Location = new System.Drawing.Point(8, 64);
  132.             this.groupBox1.Name = "groupBox1";
  133.             this.groupBox1.Size = new System.Drawing.Size(264, 184);
  134.             this.groupBox1.TabIndex = 0;
  135.             this.groupBox1.TabStop = false;
  136.             this.groupBox1.Text = "Joystick State";
  137.             // 
  138.             // labelButtons
  139.             // 
  140.             this.labelButtons.Location = new System.Drawing.Point(72, 157);
  141.             this.labelButtons.Name = "labelButtons";
  142.             this.labelButtons.Size = new System.Drawing.Size(184, 13);
  143.             this.labelButtons.TabIndex = 25;
  144.             // 
  145.             // label1
  146.             // 
  147.             this.label1.AutoSize = true;
  148.             this.label1.Location = new System.Drawing.Point(16, 157);
  149.             this.label1.Name = "label1";
  150.             this.label1.Size = new System.Drawing.Size(46, 13);
  151.             this.label1.TabIndex = 24;
  152.             this.label1.Text = "Buttons:";
  153.             // 
  154.             // labelPOV3
  155.             // 
  156.             this.labelPOV3.AutoSize = true;
  157.             this.labelPOV3.Enabled = false;
  158.             this.labelPOV3.Location = new System.Drawing.Point(192, 128);
  159.             this.labelPOV3.Name = "labelPOV3";
  160.             this.labelPOV3.Size = new System.Drawing.Size(10, 13);
  161.             this.labelPOV3.TabIndex = 23;
  162.             this.labelPOV3.Text = "0";
  163.             // 
  164.             // labelSlider1
  165.             // 
  166.             this.labelSlider1.AutoSize = true;
  167.             this.labelSlider1.Enabled = false;
  168.             this.labelSlider1.Location = new System.Drawing.Point(192, 40);
  169.             this.labelSlider1.Name = "labelSlider1";
  170.             this.labelSlider1.Size = new System.Drawing.Size(10, 13);
  171.             this.labelSlider1.TabIndex = 22;
  172.             this.labelSlider1.Text = "0";
  173.             // 
  174.             // labelSlider0
  175.             // 
  176.             this.labelSlider0.AutoSize = true;
  177.             this.labelSlider0.Enabled = false;
  178.             this.labelSlider0.Location = new System.Drawing.Point(192, 24);
  179.             this.labelSlider0.Name = "labelSlider0";
  180.             this.labelSlider0.Size = new System.Drawing.Size(10, 13);
  181.             this.labelSlider0.TabIndex = 21;
  182.             this.labelSlider0.Text = "0";
  183.             // 
  184.             // labelPOV2
  185.             // 
  186.             this.labelPOV2.AutoSize = true;
  187.             this.labelPOV2.Enabled = false;
  188.             this.labelPOV2.Location = new System.Drawing.Point(192, 112);
  189.             this.labelPOV2.Name = "labelPOV2";
  190.             this.labelPOV2.Size = new System.Drawing.Size(10, 13);
  191.             this.labelPOV2.TabIndex = 20;
  192.             this.labelPOV2.Text = "0";
  193.             // 
  194.             // labelPOV1
  195.             // 
  196.             this.labelPOV1.AutoSize = true;
  197.             this.labelPOV1.Enabled = false;
  198.             this.labelPOV1.Location = new System.Drawing.Point(192, 96);
  199.             this.labelPOV1.Name = "labelPOV1";
  200.             this.labelPOV1.Size = new System.Drawing.Size(10, 13);
  201.             this.labelPOV1.TabIndex = 19;
  202.             this.labelPOV1.Text = "0";
  203.             // 
  204.             // labelPOV0
  205.             // 
  206.             this.labelPOV0.AutoSize = true;
  207.             this.labelPOV0.Enabled = false;
  208.             this.labelPOV0.Location = new System.Drawing.Point(192, 80);
  209.             this.labelPOV0.Name = "labelPOV0";
  210.             this.labelPOV0.Size = new System.Drawing.Size(10, 13);
  211.             this.labelPOV0.TabIndex = 18;
  212.             this.labelPOV0.Text = "0";
  213.             // 
  214.             // labelZRotation
  215.             // 
  216.             this.labelZRotation.AutoSize = true;
  217.             this.labelZRotation.Enabled = false;
  218.             this.labelZRotation.Location = new System.Drawing.Point(88, 128);
  219.             this.labelZRotation.Name = "labelZRotation";
  220.             this.labelZRotation.Size = new System.Drawing.Size(10, 13);
  221.             this.labelZRotation.TabIndex = 17;
  222.             this.labelZRotation.Text = "0";
  223.             // 
  224.             // labelYRotation
  225.             // 
  226.             this.labelYRotation.AutoSize = true;
  227.             this.labelYRotation.Enabled = false;
  228.             this.labelYRotation.Location = new System.Drawing.Point(88, 112);
  229.             this.labelYRotation.Name = "labelYRotation";
  230.             this.labelYRotation.Size = new System.Drawing.Size(10, 13);
  231.             this.labelYRotation.TabIndex = 16;
  232.             this.labelYRotation.Text = "0";
  233.             // 
  234.             // labelXRotation
  235.             // 
  236.             this.labelXRotation.AutoSize = true;
  237.             this.labelXRotation.Enabled = false;
  238.             this.labelXRotation.Location = new System.Drawing.Point(88, 96);
  239.             this.labelXRotation.Name = "labelXRotation";
  240.             this.labelXRotation.Size = new System.Drawing.Size(10, 13);
  241.             this.labelXRotation.TabIndex = 15;
  242.             this.labelXRotation.Text = "0";
  243.             // 
  244.             // labelZAxis
  245.             // 
  246.             this.labelZAxis.AutoSize = true;
  247.             this.labelZAxis.Enabled = false;
  248.             this.labelZAxis.Location = new System.Drawing.Point(88, 56);
  249.             this.labelZAxis.Name = "labelZAxis";
  250.             this.labelZAxis.Size = new System.Drawing.Size(10, 13);
  251.             this.labelZAxis.TabIndex = 14;
  252.             this.labelZAxis.Text = "0";
  253.             // 
  254.             // labelYAxis
  255.             // 
  256.             this.labelYAxis.AutoSize = true;
  257.             this.labelYAxis.Enabled = false;
  258.             this.labelYAxis.Location = new System.Drawing.Point(88, 40);
  259.             this.labelYAxis.Name = "labelYAxis";
  260.             this.labelYAxis.Size = new System.Drawing.Size(10, 13);
  261.             this.labelYAxis.TabIndex = 13;
  262.             this.labelYAxis.Text = "0";
  263.             // 
  264.             // labelXAxis
  265.             // 
  266.             this.labelXAxis.AutoSize = true;
  267.             this.labelXAxis.Enabled = false;
  268.             this.labelXAxis.Location = new System.Drawing.Point(88, 24);
  269.             this.labelXAxis.Name = "labelXAxis";
  270.             this.labelXAxis.Size = new System.Drawing.Size(10, 13);
  271.             this.labelXAxis.TabIndex = 12;
  272.             this.labelXAxis.Text = "0";
  273.             // 
  274.             // labelPOV3Text
  275.             // 
  276.             this.labelPOV3Text.AutoSize = true;
  277.             this.labelPOV3Text.Enabled = false;
  278.             this.labelPOV3Text.Location = new System.Drawing.Point(136, 128);
  279.             this.labelPOV3Text.Name = "labelPOV3Text";
  280.             this.labelPOV3Text.Size = new System.Drawing.Size(41, 13);
  281.             this.labelPOV3Text.TabIndex = 11;
  282.             this.labelPOV3Text.Text = "POV 3:";
  283.             // 
  284.             // labelPOV2Text
  285.             // 
  286.             this.labelPOV2Text.AutoSize = true;
  287.             this.labelPOV2Text.Enabled = false;
  288.             this.labelPOV2Text.Location = new System.Drawing.Point(136, 112);
  289.             this.labelPOV2Text.Name = "labelPOV2Text";
  290.             this.labelPOV2Text.Size = new System.Drawing.Size(41, 13);
  291.             this.labelPOV2Text.TabIndex = 10;
  292.             this.labelPOV2Text.Text = "POV 2:";
  293.             // 
  294.             // labelPOV1Text
  295.             // 
  296.             this.labelPOV1Text.AutoSize = true;
  297.             this.labelPOV1Text.Enabled = false;
  298.             this.labelPOV1Text.Location = new System.Drawing.Point(136, 96);
  299.             this.labelPOV1Text.Name = "labelPOV1Text";
  300.             this.labelPOV1Text.Size = new System.Drawing.Size(41, 13);
  301.             this.labelPOV1Text.TabIndex = 9;
  302.             this.labelPOV1Text.Text = "POV 1:";
  303.             // 
  304.             // labelPOV0Text
  305.             // 
  306.             this.labelPOV0Text.AutoSize = true;
  307.             this.labelPOV0Text.Enabled = false;
  308.             this.labelPOV0Text.Location = new System.Drawing.Point(136, 80);
  309.             this.labelPOV0Text.Name = "labelPOV0Text";
  310.             this.labelPOV0Text.Size = new System.Drawing.Size(41, 13);
  311.             this.labelPOV0Text.TabIndex = 8;
  312.             this.labelPOV0Text.Text = "POV 0:";
  313.             // 
  314.             // labelSlider1Text
  315.             // 
  316.             this.labelSlider1Text.AutoSize = true;
  317.             this.labelSlider1Text.Enabled = false;
  318.             this.labelSlider1Text.Location = new System.Drawing.Point(136, 40);
  319.             this.labelSlider1Text.Name = "labelSlider1Text";
  320.             this.labelSlider1Text.Size = new System.Drawing.Size(46, 13);
  321.             this.labelSlider1Text.TabIndex = 7;
  322.             this.labelSlider1Text.Text = "Slider 1:";
  323.             // 
  324.             // labelSlider0Text
  325.             // 
  326.             this.labelSlider0Text.AutoSize = true;
  327.             this.labelSlider0Text.Enabled = false;
  328.             this.labelSlider0Text.Location = new System.Drawing.Point(136, 24);
  329.             this.labelSlider0Text.Name = "labelSlider0Text";
  330.             this.labelSlider0Text.Size = new System.Drawing.Size(46, 13);
  331.             this.labelSlider0Text.TabIndex = 6;
  332.             this.labelSlider0Text.Text = "Slider 0:";
  333.             // 
  334.             // labelZRotationText
  335.             // 
  336.             this.labelZRotationText.AutoSize = true;
  337.             this.labelZRotationText.Enabled = false;
  338.             this.labelZRotationText.Location = new System.Drawing.Point(16, 128);
  339.             this.labelZRotationText.Name = "labelZRotationText";
  340.             this.labelZRotationText.Size = new System.Drawing.Size(60, 13);
  341.             this.labelZRotationText.TabIndex = 5;
  342.             this.labelZRotationText.Text = "Z Rotation:";
  343.             // 
  344.             // labelYRotationText
  345.             // 
  346.             this.labelYRotationText.AutoSize = true;
  347.             this.labelYRotationText.Enabled = false;
  348.             this.labelYRotationText.Location = new System.Drawing.Point(16, 112);
  349.             this.labelYRotationText.Name = "labelYRotationText";
  350.             this.labelYRotationText.Size = new System.Drawing.Size(60, 13);
  351.             this.labelYRotationText.TabIndex = 4;
  352.             this.labelYRotationText.Text = "Y Rotation:";
  353.             // 
  354.             // labelXRotationText
  355.             // 
  356.             this.labelXRotationText.AutoSize = true;
  357.             this.labelXRotationText.Enabled = false;
  358.             this.labelXRotationText.Location = new System.Drawing.Point(16, 96);
  359.             this.labelXRotationText.Name = "labelXRotationText";
  360.             this.labelXRotationText.Size = new System.Drawing.Size(60, 13);
  361.             this.labelXRotationText.TabIndex = 3;
  362.             this.labelXRotationText.Text = "X Rotation:";
  363.             // 
  364.             // labelZAxisText
  365.             // 
  366.             this.labelZAxisText.AutoSize = true;
  367.             this.labelZAxisText.Enabled = false;
  368.             this.labelZAxisText.Location = new System.Drawing.Point(16, 56);
  369.             this.labelZAxisText.Name = "labelZAxisText";
  370.             this.labelZAxisText.Size = new System.Drawing.Size(39, 13);
  371.             this.labelZAxisText.TabIndex = 2;
  372.             this.labelZAxisText.Text = "Z Axis:";
  373.             // 
  374.             // labelYAxisText
  375.             // 
  376.             this.labelYAxisText.AutoSize = true;
  377.             this.labelYAxisText.Enabled = false;
  378.             this.labelYAxisText.Location = new System.Drawing.Point(16, 40);
  379.             this.labelYAxisText.Name = "labelYAxisText";
  380.             this.labelYAxisText.Size = new System.Drawing.Size(39, 13);
  381.             this.labelYAxisText.TabIndex = 1;
  382.             this.labelYAxisText.Text = "Y Axis:";
  383.             // 
  384.             // labelXAxisText
  385.             // 
  386.             this.labelXAxisText.AutoSize = true;
  387.             this.labelXAxisText.Enabled = false;
  388.             this.labelXAxisText.Location = new System.Drawing.Point(16, 24);
  389.             this.labelXAxisText.Name = "labelXAxisText";
  390.             this.labelXAxisText.Size = new System.Drawing.Size(39, 13);
  391.             this.labelXAxisText.TabIndex = 0;
  392.             this.labelXAxisText.Text = "X Axis:";
  393.             // 
  394.             // buttonExit
  395.             // 
  396.             this.buttonExit.Location = new System.Drawing.Point(232, 264);
  397.             this.buttonExit.Name = "buttonExit";
  398.             this.buttonExit.Size = new System.Drawing.Size(40, 24);
  399.             this.buttonExit.TabIndex = 0;
  400.             this.buttonExit.Text = "Exit";
  401.             this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
  402.             // 
  403.             // label2
  404.             // 
  405.             this.label2.AutoSize = true;
  406.             this.label2.Location = new System.Drawing.Point(8, 24);
  407.             this.label2.Name = "label2";
  408.             this.label2.Size = new System.Drawing.Size(255, 13);
  409.             this.label2.TabIndex = 1;
  410.             this.label2.Text = "This sample continously polls the joystick for data.";
  411.             // 
  412.             // timer1
  413.             // 
  414.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  415.             // 
  416.             // MainClass
  417.             // 
  418.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  419.             this.ClientSize = new System.Drawing.Size(280, 293);
  420.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  421.                                                                           this.label2,
  422.                                                                           this.buttonExit,
  423.                                                                           this.groupBox1});
  424.             this.KeyPreview = true;
  425.             this.MaximizeBox = false;
  426.             this.Name = "MainClass";
  427.             this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  428.             this.Text = "Joystick";
  429.             this.Load += new System.EventHandler(this.MainClass_Load);
  430.             this.groupBox1.ResumeLayout(false);
  431.             this.ResumeLayout(false);
  432.  
  433.         }
  434.         #endregion
  435.  
  436.  
  437.  
  438.  
  439.         public MainClass()
  440.         {
  441.             try
  442.             {
  443.                 // Load the icon from our resources
  444.                 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(this.GetType());
  445.                 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  446.             }
  447.             catch
  448.             {
  449.                 // It's no big deal if we can't load our icons, but try to load the embedded one
  450.                 try { this.Icon = new System.Drawing.Icon(this.GetType(), "directx.ico"); } 
  451.                 catch {}
  452.             }
  453.             //
  454.             // Required for Windows Form Designer support.
  455.             //
  456.             InitializeComponent();
  457.         }
  458.  
  459.  
  460.  
  461.  
  462.         private void buttonExit_Click(object sender, System.EventArgs e)
  463.         {
  464.             Close();
  465.         }
  466.  
  467.  
  468.  
  469.  
  470.         public bool InitDirectInput()
  471.         {
  472.             // Enumerate joysticks in the system.
  473.             foreach (DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
  474.             {
  475.                 // Create the device.  Just pick the first one
  476.                 applicationDevice = new Device(instance.InstanceGuid);
  477.                 break;
  478.             }
  479.  
  480.             if (null == applicationDevice)
  481.             {
  482.                 MessageBox.Show("Unable to create a joystick device. Sample will exit.", "No joystick found");
  483.                 return false;
  484.             }
  485.  
  486.             // Set the data format to the c_dfDIJoystick pre-defined format.
  487.             applicationDevice.SetDataFormat(DeviceDataFormat.Joystick);
  488.             // Set the cooperative level for the device.
  489.             applicationDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
  490.             // Enumerate all the objects on the device.
  491.             foreach (DeviceObjectInstance d in applicationDevice.Objects)
  492.             {
  493.                 // For axes that are returned, set the DIPROP_RANGE property for the
  494.                 // enumerated axis in order to scale min/max values.
  495.  
  496.                 if ((0 != (d.ObjectId & (int)DeviceObjectTypeFlags.Axis)))
  497.                 {
  498.                     // Set the range for the axis.
  499.                     applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId, new InputRange(-1000, +1000));
  500.                 }
  501.                 // Update the controls to reflect what
  502.                 // objects the device supports.
  503.                 UpdateControls(d);
  504.             }
  505.             return true;
  506.         }
  507.  
  508.         
  509.         
  510.         
  511.         /// <summary>
  512.         /// The main entry point for the application.
  513.         /// </summary>
  514.         static void Main()
  515.         {
  516.             // Create a new instance of 
  517.             // the MainClass class.
  518.             Application.Run(new MainClass());
  519.         }
  520.  
  521.         
  522.         
  523.         
  524.         protected override void Dispose(bool disposing)
  525.         {
  526.             timer1.Stop();
  527.  
  528.             // Unacquire all DirectInput objects.
  529.             if (null != applicationDevice)
  530.                 applicationDevice.Unacquire();
  531.  
  532.             if (disposing)
  533.             {
  534.                 if (components != null) 
  535.                 {
  536.                     components.Dispose();
  537.                 }
  538.             }
  539.             base.Dispose(disposing);
  540.         }
  541.  
  542.         
  543.         
  544.         
  545.         public void GetData()
  546.         {
  547.             // Make sure there is a valid device.
  548.             if (null == applicationDevice)
  549.                 return;
  550.  
  551.             try
  552.             {
  553.                 // Poll the device for info.
  554.                 applicationDevice.Poll();
  555.             }
  556.             catch(InputException inputex)
  557.             {
  558.                 if ((inputex is NotAcquiredException) || (inputex is InputLostException))
  559.                 {
  560.                     // Check to see if either the app
  561.                     // needs to acquire the device, or
  562.                     // if the app lost the device to another
  563.                     // process.
  564.                     try
  565.                     {
  566.                         // Acquire the device.
  567.                         applicationDevice.Acquire();
  568.                     }
  569.                     catch(InputException)
  570.                     {
  571.                         // Failed to acquire the device.
  572.                         // This could be because the app
  573.                         // doesn't have focus.
  574.                         return;
  575.                     }
  576.                 }
  577.                 
  578.             } //catch(InputException inputex)
  579.  
  580.             // Get the state of the device.
  581.             try {state = applicationDevice.CurrentJoystickState;}
  582.                 // Catch any exceptions. None will be handled here, 
  583.                 // any device re-aquisition will be handled above.  
  584.             catch(InputException)
  585.             {
  586.                 return;
  587.             }
  588.             
  589.             UpdateUI();
  590.         }
  591.         
  592.         
  593.         
  594.         
  595.         private void UpdateUI()
  596.         {
  597.             // This function updated the UI with
  598.             // joystick state information.
  599.  
  600.             string strText = null;
  601.  
  602.             labelXAxis.Text = state.X.ToString();
  603.             labelYAxis.Text = state.Y.ToString();
  604.             labelZAxis.Text = state.Z.ToString();
  605.  
  606.             labelXRotation.Text = state.Rx.ToString();
  607.             labelYRotation.Text = state.Ry.ToString();
  608.             labelZRotation.Text = state.Rz.ToString();
  609.         
  610.             
  611.             int[] slider = state.GetSlider();
  612.             
  613.             labelSlider0.Text = slider[0].ToString();
  614.             labelSlider1.Text = slider[1].ToString();
  615.  
  616.             int[] pov = state.GetPointOfView();
  617.  
  618.             labelPOV0.Text = pov[0].ToString();
  619.             labelPOV1.Text = pov[1].ToString();
  620.             labelPOV2.Text = pov[2].ToString();
  621.             labelPOV3.Text = pov[3].ToString();
  622.  
  623.             // Fill up text with which buttons are pressed
  624.             byte[] buttons = state.GetButtons();
  625.  
  626.             int button = 0;
  627.             foreach (byte b in buttons)
  628.             {
  629.                 if (0!= (b & 0x80))
  630.                     strText += button.ToString("00 ");
  631.                 button++;
  632.             }
  633.             labelButtons.Text = strText;
  634.         }
  635.  
  636.  
  637.  
  638.  
  639.         public void UpdateControls(DeviceObjectInstance d)
  640.         {
  641.             // Set the UI to reflect what objects the joystick supports.
  642.             if (ObjectTypeGuid.XAxis == d.ObjectType)
  643.             {
  644.                 labelXAxis.Enabled = true;
  645.                 labelXAxisText.Enabled = true;
  646.             }
  647.             if (ObjectTypeGuid.YAxis == d.ObjectType)
  648.             {
  649.                 labelYAxis.Enabled = true;
  650.                 labelYAxisText.Enabled = true;
  651.             }
  652.             if (ObjectTypeGuid.ZAxis == d.ObjectType)
  653.             {
  654.                 labelZAxis.Enabled = true;
  655.                 labelZAxisText.Enabled = true;
  656.             }
  657.             if (ObjectTypeGuid.RxAxis == d.ObjectType)
  658.             {
  659.                 labelXRotation.Enabled = true;
  660.                 labelXRotationText.Enabled = true;
  661.             }
  662.             if (ObjectTypeGuid.RyAxis == d.ObjectType)
  663.             {
  664.                 labelYRotation.Enabled = true;
  665.                 labelYRotationText.Enabled = true;
  666.             }
  667.             if (ObjectTypeGuid.RzAxis == d.ObjectType)
  668.             {
  669.                 labelZRotation.Enabled = true;
  670.                 labelZRotationText.Enabled = true;
  671.             }
  672.             if (ObjectTypeGuid.Slider == d.ObjectType)
  673.             {
  674.                 switch (SliderCount++)
  675.                 {
  676.                     case 0 :
  677.                         labelSlider0.Enabled = true;
  678.                         labelSlider0Text.Enabled = true;
  679.                         break;
  680.  
  681.                     case 1 :
  682.                         labelSlider1.Enabled = true;
  683.                         labelSlider1Text.Enabled = true;
  684.                         break;
  685.                 }
  686.             }
  687.             if (ObjectTypeGuid.PointOfView == d.ObjectType)
  688.             {
  689.                 switch (numPOVs++)
  690.                 {
  691.                     case 0 :
  692.                         labelPOV0.Enabled = true;
  693.                         labelPOV0Text.Enabled = true;
  694.                         break;
  695.  
  696.                     case 1 :
  697.                         labelPOV1.Enabled = true;
  698.                         labelPOV1Text.Enabled = true;
  699.                         break;
  700.  
  701.                     case 2 :
  702.                         labelPOV2.Enabled = true;
  703.                         labelPOV2Text.Enabled = true;
  704.                         break;
  705.  
  706.                     case 3 :
  707.                         labelPOV3.Enabled = true;
  708.                         labelPOV3Text.Enabled = true;
  709.                         break;
  710.                 }
  711.             }
  712.         }
  713.  
  714.  
  715.  
  716.  
  717.         private void MainClass_Load(object sender, System.EventArgs e)
  718.         {
  719.             if (!InitDirectInput())
  720.                 Close();
  721.  
  722.             timer1.Start();
  723.         }
  724.  
  725.  
  726.  
  727.  
  728.         private void timer1_Tick(object sender, System.EventArgs e)
  729.         {
  730.             GetData();
  731.         }
  732.     }
  733. }
  734.