Recognize touch as MouseDown event

前端 未结 4 2014
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 18:58

I am building a puzzle game in C#, winforms for a touch screen.

I am able to handle touch events on mouse handlers, in fact, i did nothing at all, but it already rec

相关标签:
4条回答
  • 2020-12-31 19:07

    One suggestion that sounds as a plausible solution is to override WndProc and search for a WM_TOUCH event. More details here: https://stackoverflow.com/a/15785333/3330348

    0 讨论(0)
  • 2020-12-31 19:22

    To perform touch operations correct you should not use the mouse handler event's just because touch and use the mouse handler it is going through a library built to handle touch as mouse and not what you should be using for a game your able to register your application to handle touch events using the methods from user32.dll, below is an example of how to implement touch handling in your application.

        [DllImport("user32.DLL")]
        public static extern bool RegisterTouchWindow(IntPtr hwnd, int ulFlags);
    
        [DllImport("user32.DLL")]
        public static extern bool UnregisterTouchWindow(IntPtr hwnd);
    
    
        public Form1()
        {
            InitializeComponent();
            RegisterTouchWindow(button1.Handle, 0);
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterTouchWindow(button1.Handle);
        }
    
    0 讨论(0)
  • 2020-12-31 19:22

    This can be a problem with your touch screen's driver. Many touch screen drivers have utilities that set how a touch is interpretted and sent to windows. Sometimes just updating the driver will work.

    0 讨论(0)
  • 2020-12-31 19:29

    Thanks @PiotrWolkowski

    You were correct about the way i should follow... Some other issues appear, but i solved the initial problem overriding the WndProc as showed in the following:

    protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case Win32.WM_POINTERDOWN:
                case Win32.WM_POINTERUP:
                case Win32.WM_POINTERUPDATE:
                case Win32.WM_POINTERCAPTURECHANGED:
                    break;
    
                default:
                    base.WndProc(ref m);
                    return;
            }
            int pointerID = Win32.GET_POINTER_ID(m.WParam);
            Win32.POINTER_INFO pi = new Win32.POINTER_INFO();
            if (!Win32.GetPointerInfo(pointerID, ref pi))
            {
                Win32.CheckLastError();
            }
            Point pt = PointToClient(pi.PtPixelLocation.ToPoint());
            MouseEventArgs me = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, pt.X, pt.Y, 0);
            switch (m.Msg)
            {
                case Win32.WM_POINTERDOWN:
                        Console.WriteLine("TOCOU" + pt);
                        (Parent as Jogo).Form1_MouseDown((this as object), me);
                    break;
    
                case Win32.WM_POINTERUP:
                        Console.WriteLine("LEVANTOU");
                        (Parent as Jogo).Form1_MouseUp((this as object), me);
                    break;
    
                case Win32.WM_POINTERUPDATE:
                        //Console.WriteLine("UPDATE");
                        (Parent as Jogo).Form1_MouseMove((this as object), me);
                    break;
            }
        }
    

    It was supported by an "Win32.cs" that can be downloaded here:

    https://gist.github.com/RSchwoerer/bc5c04899c0510aefca24f088a79cebf

    Hope this is helpful for you;)

    0 讨论(0)
提交回复
热议问题