Input.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:7k
源码类别:

DirextX编程

开发平台:

C#

  1. ////////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. using Microsoft.DirectX;
  15. using Microsoft.DirectX.Direct3D;
  16. using Microsoft.DirectX.DirectInput;
  17. namespace 输入事件
  18. {
  19.     public partial class Input : Form
  20.     {
  21.         Microsoft.DirectX.Direct3D.Device device = null;//定义绘图设备
  22.         private Microsoft.DirectX.DirectInput.Device keyboard;//定义键盘设备
  23.         private Microsoft.DirectX.DirectInput.Device mouseDevice;//定义鼠标设备
  24.         public Input()
  25.         {
  26.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  27.             this.Text = "输入事件";//指定窗体标题
  28.         }
  29.         public bool InitializeDirect3D()
  30.         {
  31.             try
  32.             {
  33.                 PresentParameters presentParams = new PresentParameters();
  34.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  35.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  36.                 device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  37.                 return true;
  38.             }
  39.             catch (DirectXException e)
  40.             {
  41.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  42.                 return false;
  43.             }
  44.         }
  45.         public void InitializeKeyboard()
  46.         {
  47.             keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);//实例化键盘对象
  48.             keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
  49.             keyboard.Acquire();//链接键盘设备
  50.         }
  51.         public void InitialMouse()
  52.         {            
  53.             mouseDevice = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);//实例化鼠标对象
  54.             mouseDevice.Properties.AxisModeAbsolute = true;
  55.             mouseDevice.SetCooperativeLevel(this, CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
  56.             mouseDevice.Acquire();            
  57.         }
  58.         private void KeyboardListen()
  59.         {
  60.             KeyboardState keys = keyboard.GetCurrentKeyboardState();
  61.             if (keys[Key.Escape])
  62.             {
  63.                 DialogResult result=MessageBox.Show("是否退出程序?","提示",MessageBoxButtons.YesNo);
  64.                 if (result == DialogResult.Yes)
  65.                 {
  66.                     Application.Exit();
  67.                 }               
  68.             }            
  69.         }
  70.         private void MouseListen()
  71.         {
  72.             MouseState mouseState = mouseDevice.CurrentMouseState;
  73.             byte[] buttons = mouseState.GetMouseButtons();
  74.             string info = "Mouse:";
  75.             info += "X:" + mouseState.X + " ";
  76.             info += "Y:" + mouseState.Y + " ";
  77.             info += "Z:" + mouseState.Z + " ";
  78.             for (int i = 0; i < buttons.Length; i++)
  79.             {
  80.                 if (buttons[i] != 0)
  81.                 {
  82.                     switch (i)
  83.                     {
  84.                         case 0:
  85.                             MessageBox.Show(info + "点击左键!");
  86.                             break;
  87.                         case 1:
  88.                             MessageBox.Show(info + "点击右键!");                            
  89.                             break;
  90.                         case 2:
  91.                             MessageBox.Show(info + "点击中键!");
  92.                             break;
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         public void Render()
  98.         {
  99.             if (device == null)   //如果device为空则不渲染
  100.             {
  101.                 return;
  102.             }
  103.             Vector3 eye = new Vector3(30, 0, -30);
  104.             Vector3 at = new Vector3(0, 0, 0);
  105.             Vector3 up = new Vector3(0, 1, 0);
  106.             Matrix viewMatrix = Matrix.LookAtLH(eye, at, up);
  107.             Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width/this.Height, 1.0f, 50.0f);
  108.             device.Transform.Projection = projection;
  109.             device.Transform.View = viewMatrix;
  110.             device.RenderState.Lighting = false;
  111.             device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  112.             device.BeginScene();
  113.             //在此添加渲染图形代码
  114.             CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];//定义顶点
  115.             vertices[0].Position = new Vector3(0f, 0f, 0f);
  116.             vertices[0].Color = Color.Red.ToArgb();
  117.             vertices[1].Position = new Vector3(5f, 10f, 0f);
  118.             vertices[1].Color = Color.Green.ToArgb();
  119.             vertices[2].Position = new Vector3(10f, 0f, 0f);
  120.             vertices[2].Color = Color.Yellow.ToArgb();
  121.             device.VertexFormat = CustomVertex.PositionColored.Format;
  122.             device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
  123.             device.EndScene();
  124.             device.Present();
  125.             
  126.             //MouseListen();//添加鼠标设备监听函数
  127.             KeyboardListen();//添加键盘设备监听函数
  128.         }
  129.         protected override void OnMouseDown(MouseEventArgs e)
  130.         {
  131.             base.OnMouseDown(e);
  132.             MessageBox.Show("鼠标按下的位置在:X="+e.X.ToString() + ",Y=" + e.Y.ToString());
  133.         }
  134.         static void Main()
  135.         {
  136.             Input Input = new Input(); //创建窗体对象
  137.             if (Input.InitializeDirect3D() == false) //检查Direct3D是否启动
  138.             {
  139.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  140.                 return;
  141.             }
  142.             Input.InitializeKeyboard();//初始化键盘设备
  143.             Input.InitialMouse();//初始化鼠标设备
  144.             Input.Show(); //如果一切都初始化成功,则显示窗体
  145.             while (Input.Created) //设置一个循环用于实时更新渲染状态
  146.             {
  147.                 Input.Render(); //保持device渲染,直到程序结束            
  148.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  149.             }
  150.         }
  151.     }
  152. }