Joystick using C# (GUAN`LI) 之完成篇
經過幾天的查找資料,已經完成上位機對冠麗(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 。
總結
以上是生活随笔為你收集整理的Joystick using C# (GUAN`LI) 之完成篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TinyWebServer:一个Linu
- 下一篇: Bluetooth LMP介绍