/// <summary> /// 注册 /// </summary> /// <param name="hWnd">需要捕获手柄消息的窗口</param> /// <param name="joystickId">要捕获的手柄Id</param> public bool Register(IntPtr hWnd, int joystickId) { bool flag = false; int result = 0; WinAPI.JOYCAPS caps = new WinAPI.JOYCAPS(); if (WinAPI.Win32API.joyGetNumDevs() != 0) { //拥有手柄.则判断手柄状态 result = WinAPI.Win32API.joyGetDevCaps(joystickId, ref caps, Marshal.SizeOf(typeof(WinAPI.JOYCAPS))); if (result == Joy.JOYERR_NOERROR) { //手柄处于正常状态 flag = true; } } if (flag) { //注册消息 if (!this.IsRegister) { Application.AddMessageFilter(this); } this.IsRegister = true; result = WinAPI.Win32API.joySetCapture(hWnd, joystickId, caps.wPeriodMin * 2, false); if (result != Joy.JOYERR_NOERROR) { flag = false; } } return(flag); }
public static extern int joyGetDevCaps(int uJoyID, ref JOYCAPS pjc, int cbjc);