Receiving RawInput from touchscreen in net core


I have an external hid touchscreen and i want to receive the rawinput data in my application. My application is just a console application and i want to create a window, so that i can use the handle to register a rawinputdevice.

I created a window like this:

namespace MyApplication

   delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    class Win32Window
        const UInt32 WS_OVERLAPPEDWINDOW = 0xcf0000;
        const UInt32 WS_VISIBLE = 0x10000000;
        const UInt32 CS_USEDEFAULT = 0x80000000;
        const UInt32 CS_DBLCLKS = 8;
        const UInt32 CS_VREDRAW = 1;
        const UInt32 CS_HREDRAW = 2;
        const UInt32 COLOR_WINDOW = 5;
        const UInt32 COLOR_BACKGROUND = 1;
        const UInt32 IDC_CROSS = 32515;
        const UInt32 WM_DESTROY = 2;
        const UInt32 WM_PAINT = 0x0f;
        const UInt32 WM_LBUTTONUP = 0x0202;
        const UInt32 WM_LBUTTONDBLCLK = 0x0203;
        const UInt32 WM_INPUT = 0x00FF;

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct WNDCLASSEX
            public int cbSize;
            public int style;
            public IntPtr lpfnWndProc; 
            public int cbClsExtra;
            public int cbWndExtra;
            public IntPtr hInstance;
            public IntPtr hIcon;
            public IntPtr hCursor;
            public IntPtr hbrBackground;
            public string lpszMenuName;
            public string lpszClassName;
            public IntPtr hIconSm;

        private WndProc delegWndProc = myWndProc;

        static extern bool UpdateWindow(IntPtr hWnd);

        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern bool DestroyWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true, EntryPoint = "CreateWindowEx")]
        public static extern IntPtr CreateWindowEx(
           int dwExStyle,
           UInt16 regResult,
           //string lpClassName,
           string lpWindowName,
           UInt32 dwStyle,
           int x,
           int y,
           int nWidth,
           int nHeight,
           IntPtr hWndParent,
           IntPtr hMenu,
           IntPtr hInstance,
           IntPtr lpParam);

        [DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterClassEx")]
        static extern System.UInt16 RegisterClassEx([In] ref WNDCLASSEX lpWndClass);

        static extern uint GetLastError();

        static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        static extern void PostQuitMessage(int 

        static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

        static extern bool TranslateMessage([In] ref MSG lpMsg);

        static extern IntPtr DispatchMessage([In] ref MSG lpmsg);    

        internal bool Create()
            WNDCLASSEX wind_class = new WNDCLASSEX();
            wind_class.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
   = (int)(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ) ; //Doubleclicks are active
            wind_class.hbrBackground = (IntPtr) COLOR_BACKGROUND  +1 ; //Black background, +1 is necessary
            wind_class.cbClsExtra = 0;
            wind_class.cbWndExtra = 0;
            wind_class.hInstance = Marshal.GetHINSTANCE(this.GetType().Module); ;// alternative: Process.GetCurrentProcess().Handle;
            wind_class.hIcon = IntPtr.Zero;
            wind_class.hCursor = LoadCursor(IntPtr.Zero, (int)IDC_CROSS);// Crosshair cursor;
            wind_class.lpszMenuName = null;
            wind_class.lpszClassName = "myClass";
            wind_class.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(delegWndProc);
            wind_class.hIconSm = IntPtr.Zero;
            ushort regResult = RegisterClassEx(ref wind_class);

            if (regResult == 0)
                uint error = GetLastError();
                return false;
            string wndClass = wind_class.lpszClassName;

            IntPtr hWnd = CreateWindowEx(0, regResult, "Hello Win32", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 400, IntPtr.Zero, IntPtr.Zero, wind_class.hInstance, IntPtr.Zero);

            if (hWnd == ((IntPtr)0))
                uint error = GetLastError();
                return false;
            ShowWindow(hWnd, 1);
            return true;

        private static IntPtr myWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
            switch (msg)
                case WM_INPUT:
                    // Get RawInput Data and do some stuff
                case WM_PAINT:

                case WM_LBUTTONDBLCLK :

                case WM_DESTROY:

                    //If you want to shutdown the application, call the next function instead of DestroyWindow

            return DefWindowProc(hWnd, msg, wParam, lParam);

I used the handle of this window to register a rawinputdevice, which is successful. Some windows messages are processed to the window, but not the wm_input messages. If i get these messages, i want to hide the window, because i don't need it.

If somebody knows an other way to get the data from an external touchscreen in net core, feel free to post it.

asked on Stack Overflow May 20, 2020 by Fryro • edited May 20, 2020 by Fryro

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0