游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6126|回复: 5

如何用directinput捕捉鼠标事件

[复制链接]

3

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2005-8-14 16:57:00 | 显示全部楼层 |阅读模式
在dirextx下面,发现用c#下的鼠标事件不能用
考虑用directinput,查了一下午资料,还是不行.
哪位大哥可以帖一下完整代码,谢谢
在directinput下捕捉到的鼠标的坐标是不是和directx是一致的?

5

主题

19

帖子

29

积分

注册会员

Rank: 2

积分
29
发表于 2005-8-18 18:55:00 | 显示全部楼层

Re:如何用directinput捕捉鼠标事件

Device mouse = null;
mouse = new Device(SystemGuid.Mouse);
mouse.SetDataFormat(DeviceDataFormat.Mouse);
mouse.SetCooperativeLevel(Owner,
                                         CooperativeLevelFlags.Foreground |
                                         CooperativeLevelFlags.NonExclusive);
            this.mouse.Properties.AxisModeAbsolute = true;
mouse.Acquire();
//取得属性
this.mouse.CurrentMouseState;


但是坐标位置狠奇怪,55555!

5

主题

19

帖子

29

积分

注册会员

Rank: 2

积分
29
发表于 2005-8-19 12:56:00 | 显示全部楼层

Re:如何用directinput捕捉鼠标事件

关于鼠标操作(MDX9 C#)

首先需要引用
using Microsoft.DirectX.DirectInput;命名空间

准备变量
private Device m_deviceMouse = null;

1、初始化设备
//取得设备句柄
m_deviceMouse = new Device(SystemGuid.Mouse);
//设置数据类型
m_deviceMouse.SetDataFormat(DeviceDataFormat.Mouse);
//协作级别
m_deviceMouse.SetCooperativeLevel(parentWindowControl,
                             CooperativeLevelFlags.Foreground |
                             CooperativeLevelFlags.NonExclusive);
/*
CooperativeLevelFlags
Background
The device can be used in the background, and can be acquired at any time, even if the associated window isn't the active window.
Foreground
The device can only be used if the window is in the foreground. When this window loses focus, any acquired device will be unacquired.
注意此种模式下,如果主窗口失去焦点则需要重新获得设备
Exclusive
The device requires exclusive access. While acquired exclusively, no other application can acquire the device exclusively. Nonexclusive acquires are still allowed. For security reasons, exclusive and background flags are not allowed to be used together on certain devices, like keyboards and mice.
NonExclusive
The device can be shared among many applications and does not require exclusive access.
NoWindowsKey
Disables the windows key.
*/
//设置坐标显示模式                        
this.m_deviceMouse.Properties.AxisModeAbsolute = true;
//取得设备
m_deviceMouse.Acquire();

2、关于坐标的说明

坐标分成X,Y,Z轴,X,Y对应屏幕的横轴和纵轴,Z对应鼠标滚轮。
X轴和Y轴的精度如果不设置AxisModeAbsolute的时候是很粗(具体多少我没测算),但是设置了AxisModeAbsolute之后是基本和屏幕象素一致。
Z轴的精度是120,也就是说鼠标滚轮向前滚一下则数值增加120,否则减少120。
从鼠标状态 m_deviceMouse.CurrentMouseState;得到的X,Y值不能明确指出当前鼠标在屏幕上的位置,如果需要得到屏幕鼠标位置,则需要使用Cursor.Position属性即可。

3、鼠标按钮
鼠标按钮在DXI中没有明确定义,需要自己定义。MouseState的GetMouseButtons方法取得鼠标的数组,按钮使用数组实现。
4、事件机制
普通模式取得鼠标状态在程序的主循环中使用轮询机制取得,但是MS提供了线程方式取得
实例代码如下
  private void frmUI_Load(object sender, System.EventArgs e)
        {
            threadData = new Thread(new ThreadStart(this.MouseEvent));
            threadData.Start();

            eventFire = new AutoResetEvent(false);
            
   // Create the device.
            try
            {
                applicationDevice = new Device(SystemGuid.Mouse);
            }
            catch(InputException)
            {
                MessageBox.Show("Unable to create device. Sample will now exit.");
                Close();
            }
   // Set the cooperative level for the device.
   applicationDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
            // Set a notification event.   
            applicationDevice.SetEventNotification(eventFire);
            // Acquire the device.
            try{ applicationDevice.Acquire(); }
            catch{}               
        }
        
        public void MouseEvent()
        {
            // This is the mouse event thread.
            // This thread will wait until a
            // mouse event occurrs, and invoke
            // the delegate on the main thread to
            // grab the current mouse state and update
            // the UI.
            while(Created)               
            {
                eventFire.WaitOne(-1, false);
               
                try
                {
                    applicationDevice.Poll();
                }
                catch(InputException)
                {
                    continue;
                }
                this.BeginInvoke(new UIDelegate(UpdateUI));
            }
        }
        public void UpdateUI()
  {
      MouseState mouseStateData = new MouseState();
            
            // Get the current state of the mouse device.
            mouseStateData = applicationDevice.CurrentMouseState;
   byte[] buttons = mouseStateData.GetMouseButtons();
  }

3

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
 楼主| 发表于 2005-8-19 21:00:00 | 显示全部楼层

Re: 如何用directinput捕捉鼠标事件

呵呵,我觉的用directinput得到鼠标位置很麻烦,我就索性用cursor属性了,自己在坐标转换一下,就ok了.至于鼠标事件,我用的是onmousedown函数,挺简单的.

13

主题

24

帖子

24

积分

注册会员

Rank: 2

积分
24
发表于 2005-9-4 21:22:00 | 显示全部楼层

Re: 如何用directinput捕捉鼠标事件(to:jeasonzhao)

to:jeasonzhao
在绝对坐标下对鼠标的坐标值我想了两天了都没搞懂啊,放弃了,请问你用cursor的属性来获得鼠标相对窗口的坐标是如何用的

8

主题

71

帖子

76

积分

注册会员

Rank: 2

积分
76
发表于 2005-10-21 16:53:00 | 显示全部楼层

Re:如何用directinput捕捉鼠标事件

你取的是什么坐标啊
是百分比  还是 像素点?
还是窗口位置?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-22 08:56

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表