【问题标题】:How can I check the touch gestures is enabled for the control or not?如何检查控件是否启用了触摸手势?
【发布时间】:2017-03-22 05:06:45
【问题描述】:

在 Windows 窗体控件中,我已使用 SetGestureConfig 方法正确配置了触摸手势。它正确配置了控件的触摸手势。在某些情况下,我需要检查控件是否正确启用了特定的触摸手势。我尝试使用 GetGestureConfig 方法来检查特定手势是否启用。但是这个方法总是 返回错误值。我还尝试使用 GetLastError() 方法 来获取错误消息,但它总是返回 value 0。请在下面找到代码,

        int gestureConfigSize = Marshal.SizeOf(new GESTURECONFIG());
        GESTURECONFIG gc = new GESTURECONFIG();
        gc.dwID = 0;
        gc.dwWant = WindowMessages.GC_ALLGESTURES;
        gc.dwBlock = 0;
        if (SetGestureConfig(control.Handle, 0, 1, ref gc, gestureConfigSize))
            MessageBox.Show("Zoom gesture configured properly");

        GESTURECONFIG gc1 = new GESTURECONFIG();
        gc1.dwID = 0;
        gc1.dwWant = WindowMessages.GC_ALLGESTURES;
        gc1.dwBlock = 0;
        GESTURECONFIG[] gestures = new GESTURECONFIG[] { gc1 };

        bool value = GetGestureConfig(control.Handle, 0, 0, 1, gestures, gestureConfigSize);
        if (!value)
        {
            int errorValue = GetLastError();
        }

请在下面找到 Dll 导入代码,

    [DllImport("Kernel32.dll")]
    static extern Int32 GetLastError();

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetGestureConfig(IntPtr hWnd, int dwReserved, int cIDs, [In] [Out] GESTURECONFIG[] pGestureConfig, int cbSize);

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetGestureConfig(IntPtr hWnd, int dwReserved, int cIDs, ref GESTURECONFIG pGestureConfig, int cbSize);

    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetGestureConfig(IntPtr hWnd, int dwReserved, int flags, int cIDs, [In] [Out] GESTURECONFIG[] pGestureConfig, int cbSize);

请建议如何使用 GetGestureConfig 方法检查控件是否启用了触摸手势。

谢谢。

【问题讨论】:

    标签: c# winforms touch gestures gesture-recognition


    【解决方案1】:
    1. 您不应该在托管代码中自己[DllImport]GetLastError,请致电Marshal.GetLastError(请参阅https://blogs.msdn.microsoft.com/adam_nathan/2003/04/25/getlasterror-and-managed-code/)了解更多详情。
    2. 为此,您需要在[DllImport] 中设置SetLastError=true 标志以确保捕获错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      相关资源
      最近更新 更多