ShareIdeas

本博客不再更新,欢迎访问我的github,https://github.com/sunke-github/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

     经过几天的查找资料,已经完成上位机对冠丽(GUAN`LI)四通摇控信号的采集,程序开发环境 VS2008+DirectX   ,具体关于 DirectInput 的使用方法这里不做多介绍 ,网上有很多资料。

这是程序 运行截图

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using Microsoft.DirectX.DirectInput;
10 using System.Diagnostics;
11 
12 namespace DirectInput_GL2
13 {
14     public partial class Form1 : Form
15     {
16         private JoystickInterface.Joystick jst;
17         public Form1()
18         {
19             InitializeComponent();
20         }
21       
22 
23         private void timer1_Tick_1(object sender, EventArgs e)
24         {
25             jst.UpdateStatus();
26             textBox1.Text =Convert.ToString( jst.AxisE);
27             textBox2.Text =Convert.ToString( jst.AxisD);
28             textBox3.Text =Convert.ToString( jst.AxisA);
29             textBox4.Text =Convert .ToString( jst.AxisC);
30            label1.Invalidate();              //调用label1的绘图函数
31             label2.Invalidate();
32         }
33 
34         private void Form1_Load_1(object sender, EventArgs e)
35         {
36             // grab the joystick
37             jst = new JoystickInterface.Joystick(this.Handle);
38             string[] sticks = jst.FindJoysticks();

                if (sticks == null)
                {
                    MessageBox.Show("未连接摇杆!");
                    Process.GetCurrentProcess().Kill();
                }

 39             jst.AcquireJoystick(sticks[0]);

40             timer1.Enabled = true;
41         }
42 
43         private void label1_Paint_1(object sender, PaintEventArgs e)
44         {
45             int x_temp = (65535 - jst.AxisE) / 295 - 10;   //做的略微校准
46             int y_temp = (65535 - jst.AxisD) / 451 - 10;
47             Graphics g = e.Graphics;
48             SolidBrush b1 = new SolidBrush(Color.Blue);
49             g.DrawString("+", new Font("宋体", 10), b1, new PointF(x_temp, y_temp));
50         }
51 
52         private void label2_Paint_1(object sender, PaintEventArgs e)
53         {
54             int z_temp = (65535 - jst.AxisA) / 295 - 10;
55             int w_temp = (65535 - jst.AxisC) / 451 - 5;
56             Graphics g = e.Graphics;
57             SolidBrush b1 = new SolidBrush(Color.Blue);
58             g.DrawString("+", new Font("宋体", 10), b1, new PointF(z_temp, w_temp));
59         }
60     }
61 }

 

      这个是窗体Form1.cs 中的程序,程序调用了我自己编写 的库文件JoystickInterface.dll。这里先说说程序的思路,先新建一个joystick对象、FindJoysticks()这个方法用于发现设备、AcquireJoystick(string x)这个方法用于请求设备;接着使能定时器 ,这个private void timer1_Tick_1(object sender, EventArgs e)是定时器的方法,每20ms做一个请求(时间可以设定),程序最终在这里做不断的循环。程序的效果就是  “+”随着摇杆的移动而移动,这个程序适合所以USB手柄和四通遥控器,但效果可能会不太一样。

这是.dll文件 https://files.cnblogs.com/dreamfactory/JoystickInterface.rar   。

posted on 2012-08-03 19:05  ShareIdeas  阅读(1405)  评论(0编辑  收藏  举报