Input.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:7k
- ////////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- using Microsoft.DirectX.DirectInput;
- namespace 输入事件
- {
- public partial class Input : Form
- {
- Microsoft.DirectX.Direct3D.Device device = null;//定义绘图设备
- private Microsoft.DirectX.DirectInput.Device keyboard;//定义键盘设备
- private Microsoft.DirectX.DirectInput.Device mouseDevice;//定义鼠标设备
- public Input()
- {
- this.ClientSize = new Size(800, 600);//指定窗体尺寸
- this.Text = "输入事件";//指定窗体标题
- }
- public bool InitializeDirect3D()
- {
- try
- {
- PresentParameters presentParams = new PresentParameters();
- presentParams.Windowed = true; //指定以Windows窗体形式显示
- presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
- device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
- return true;
- }
- catch (DirectXException e)
- {
- MessageBox.Show(e.ToString(), "Error"); //处理异常
- return false;
- }
- }
- public void InitializeKeyboard()
- {
- keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);//实例化键盘对象
- keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
- keyboard.Acquire();//链接键盘设备
- }
- public void InitialMouse()
- {
- mouseDevice = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);//实例化鼠标对象
- mouseDevice.Properties.AxisModeAbsolute = true;
- mouseDevice.SetCooperativeLevel(this, CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
- mouseDevice.Acquire();
- }
- private void KeyboardListen()
- {
- KeyboardState keys = keyboard.GetCurrentKeyboardState();
- if (keys[Key.Escape])
- {
- DialogResult result=MessageBox.Show("是否退出程序?","提示",MessageBoxButtons.YesNo);
- if (result == DialogResult.Yes)
- {
- Application.Exit();
- }
- }
- }
- private void MouseListen()
- {
- MouseState mouseState = mouseDevice.CurrentMouseState;
- byte[] buttons = mouseState.GetMouseButtons();
- string info = "Mouse:";
- info += "X:" + mouseState.X + " ";
- info += "Y:" + mouseState.Y + " ";
- info += "Z:" + mouseState.Z + " ";
- for (int i = 0; i < buttons.Length; i++)
- {
- if (buttons[i] != 0)
- {
- switch (i)
- {
- case 0:
- MessageBox.Show(info + "点击左键!");
- break;
- case 1:
- MessageBox.Show(info + "点击右键!");
- break;
- case 2:
- MessageBox.Show(info + "点击中键!");
- break;
- }
- }
- }
- }
- public void Render()
- {
- if (device == null) //如果device为空则不渲染
- {
- return;
- }
- Vector3 eye = new Vector3(30, 0, -30);
- Vector3 at = new Vector3(0, 0, 0);
- Vector3 up = new Vector3(0, 1, 0);
- Matrix viewMatrix = Matrix.LookAtLH(eye, at, up);
- Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width/this.Height, 1.0f, 50.0f);
- device.Transform.Projection = projection;
- device.Transform.View = viewMatrix;
- device.RenderState.Lighting = false;
- device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0); //清除windows界面为深蓝色
- device.BeginScene();
- //在此添加渲染图形代码
- CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];//定义顶点
- vertices[0].Position = new Vector3(0f, 0f, 0f);
- vertices[0].Color = Color.Red.ToArgb();
- vertices[1].Position = new Vector3(5f, 10f, 0f);
- vertices[1].Color = Color.Green.ToArgb();
- vertices[2].Position = new Vector3(10f, 0f, 0f);
- vertices[2].Color = Color.Yellow.ToArgb();
- device.VertexFormat = CustomVertex.PositionColored.Format;
- device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
- device.EndScene();
- device.Present();
-
- //MouseListen();//添加鼠标设备监听函数
- KeyboardListen();//添加键盘设备监听函数
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- MessageBox.Show("鼠标按下的位置在:X="+e.X.ToString() + ",Y=" + e.Y.ToString());
- }
- static void Main()
- {
- Input Input = new Input(); //创建窗体对象
- if (Input.InitializeDirect3D() == false) //检查Direct3D是否启动
- {
- MessageBox.Show("无法启动Direct3D!", "错误!");
- return;
- }
- Input.InitializeKeyboard();//初始化键盘设备
- Input.InitialMouse();//初始化鼠标设备
- Input.Show(); //如果一切都初始化成功,则显示窗体
- while (Input.Created) //设置一个循环用于实时更新渲染状态
- {
- Input.Render(); //保持device渲染,直到程序结束
- Application.DoEvents(); //处理键盘鼠标等输入事件
- }
- }
- }
- }