如何检查控件是否启用了触摸手势



在Windows窗体控件中,我已经使用SetGestureConfig方法正确配置了触摸手势。它正确配置了控件的触摸手势。在某些情况下,我需要检查控件是否正确启用了特定的触摸手势。我尝试使用GetGestureConfig方法,以检查特定手势是否已启用。但是此方法始终返回 false 值。 而且我尝试使用 GetLastError(( 方法获取错误消息,但它始终返回值 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 方法检查控件是否启用了触摸手势。

谢谢。

  1. 您不应在托管代码中自己[DllImport] GetLastError,请调用Marshal.GetLastError(请参阅 https://blogs.msdn.microsoft.com/adam_nathan/2003/04/25/getlasterror-and-managed-code/(以获取更多详细信息。
  2. 为此,您需要在[DllImport]中设置 SetLastError=true 标志,以确保捕获错误。

最新更新