Visual Studio C# Windows Forms Application touch screen exception error

Gennady Gurin 41 Reputation points
2024-06-06T19:12:04.4133333+00:00

I have Windows Forms Visual Studio Project; I have added the code to be able to use a finger instead of the mouse on touch screen monitor.

When I use a mouse, everything is working, but when I use a finger to top and double top on the screen also working only if I do it on any item on the form, but wh

en I use a finger on a form (on the empty space) I get an exception error, I attached a picture as well as code for the finger as a pointer.

Anyone knows what should be done, thanks in advance.

Code Here:

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 Form1).Form1_MouseDown((this as object), me);

            break;

        case Win32.WM_POINTERUP:

            Console.WriteLine("LEVANTOU");

            (Parent as Form1).Form1_MouseUp((this as object), me);

            break;

        case Win32.WM_POINTERUPDATE:

            Console.WriteLine("UPDATE");

            (Parent as Form1).Form1_MouseMove((this as object), me);

            break;

    }

}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,875 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,892 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,656 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-06-07T08:37:08.4766667+00:00

    Hi @Gennady Gurin , Welcome to Microsoft Q&A,

    When touching in the blank area, there is no target control to receive the message, which may cause an exception. If the Parent object is not of Form1 type, the forced conversion will fail. To ensure that Parent is of Form1 type, you can use the as keyword and perform type checking.

    protected override void WndProc(ref Message m)
    {
        Form1 parentForm = Parent as Form1;
    
        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();
            return;
        }
    
        Point pt = PointToClient(pi.PtPixelLocation.ToPoint());
        MouseEventArgs me = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, pt.X, pt.Y, 0);
    
        if (parentForm != null)
        {
            switch (m.Msg)
            {
                case Win32.WM_POINTERDOWN:
                    Console.WriteLine("TOCOU" + pt);
                    parentForm.Form1_MouseDown(this, me);
                    break;
    
                case Win32.WM_POINTERUP:
                    Console.WriteLine("LEVANTOU");
                    parentForm.Form1_MouseUp(this, me);
                    break;
    
                case Win32.WM_POINTERUPDATE:
                    Console.WriteLine("UPDATE");
                    parentForm.Form1_MouseMove(this, me);
                    break;
            }
        }
        else
        {
            Console.WriteLine("Parent form is not of type Form1.");
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful