zoukankan      html  css  js  c++  java
  • 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   。

  • 相关阅读:
    vue-动画
    vue笔记-路由,组件
    自定义键盘信息
    自定义指令
    vue-笔记2
    轻松搭建基于 Serverless 的文档图片在线转换服务
    轻松搭建基于 SpringBoot + Vue 的 Web 商城应用
    一小时快速搭建基于阿里云容器服务-Kubernetes的Web应用
    阿里云正式推出内容平台“云栖号”:全面助力企业和个人上云决策
    云原生安全-更安全的密文管理 Vault on ACK
  • 原文地址:https://www.cnblogs.com/dreamfactory/p/2622157.html
Copyright © 2011-2022 走看看