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.