• 大小: 11KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C#
  • 标签: c#  Joystick  

资源简介

用C#写的游戏手柄的访问程序,并封装成windows 消息,访问非常方便。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace JoyKeys
{
    public partial class Form1 : Form
    {
        private Core.Joystick joystick;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void onload(EventArgs e)
        {
            base.onload(e);
            joystick = new JoyKeys.Core.Joystick();
            joystick.Click += new EventHandler(joystick_Click);
            joystick.Register(base.Handle Core.API.JOYSTICKID1);
            joystick.Register(base.Handle Core.API.JOYSTICKID2);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            joystick.UnRegister(Core.API.JOYSTICKID1);
            joystick.UnRegister(Core.API.JOYSTICKID2);
            base.OnClosing(e);
        }
        void joystick_Click(object sender JoyKeys.Core.JoystickEventArgs e)
        {
           // Core.API.JOYINFO joy2 = new JoyKeys.Core.API.JOYINFO();
            if (e.JoystickId == Core.API.JOYSTICKID1)
            {
                this.Text = “1号手柄“;
            }
            else if (e.JoystickId == Core.API.JOYSTICKID2)
            {
                this.Text = “2号手柄“;
            }

            int x = 1;
            int y = 1;
            string str = ““;


            this.Text = ““ + e.Move.X + “  “ + e.Move.Y;

            if ((e.Buttons & JoyKeys.Core.JoystickButtons.UP) == JoyKeys.Core.JoystickButtons.UP) y--;
            if ((e.Buttons & JoyKeys.Core.JoystickButtons.Down) == JoyKeys.Core.JoystickButtons.Down) y++;
            if ((e.Buttons & JoyKeys.Core.JoystickButtons.Left) == JoyKeys.Core.JoystickButtons.Left) x--;
            if ((e.Buttons & JoyKeys.Core.JoystickButtons.Right) == JoyKeys.Core.JoystickButtons.Right) x++;

            if (x == 0 && y == 0) this.label1.TextAlign = ContentAlignment.TopLeft;
            if (x == 1 && y == 0) this.label1.TextAlign = ContentAlignment.TopCenter;
            if (x == 2 && y == 0) this.label1.TextAlign = ContentAlignment.TopRight;

            if (x == 0 && y == 1) this.label1.TextAlign = ContentAlignment.MiddleLeft;
            if (x == 1 && y == 1) this.label1.TextAlign = ContentAlignment.MiddleCenter;
            if (x == 2 && y == 1) this.label1.TextAlign = ContentAlignment.MiddleRight;

            if (x == 0 && y == 2) this.label1.TextAlign = ContentAlignment.BottomLeft;
            if (x == 1 && y == 2) this.label1.TextAlign = ContentAlignment.BottomCenter;
            if (x == 2 && y == 2) this.label1.TextAlign = ContentAlignment.BottomRight;
            this.label1.Text = “+“;

            this.label2.BackColor = ((e.Buttons & JoyKeys.Core.JoystickButtons.B1) == JoyKeys.Core.JoystickButtons.B1) ? Color.Red : SystemColors.Control;
  

评论

共有 条评论