简体   繁体   English

C#将WndProc消息从一个表单传递到另一个表单

[英]C# Passing WndProc Messages from a form to another form

I'm working on a C# application with two Windows forms. 我正在使用具有两个Windows窗体的C#应用​​程序。 Both forms are full screen and one form sits on top of the other form as a transparent overlay. 两种形式都是全屏形式,一种形式位于另一种形式的顶部,作为透明的覆盖层。 The bottom form contains a web browser (also full screen). 底部的表单包含一个Web浏览器(也是全屏)。 I'm tracking touch input on the transparent overlay form in order to capture gestures and draw buttons and other controls on the screen. 我正在跟踪透明覆盖窗体上的触摸输入,以便捕获手势并在屏幕上绘制按钮和其他控件。 What I need to do is to send all the windows messages that the overlay form gets in its WndProc function to the form below (or the web browser control in the form since that's basically all the lower form is used to contain). 我需要做的是将覆盖窗体在其WndProc函数中获取的所有Windows消息发送到下面的窗体(或窗体中的Web浏览器控件,因为基本上所有下面的窗体都包含该窗体)。

Basically I just need to deal with the gestures on the overlay, all the mouse messages need to still transfer to the web browser to provide certain functionality. 基本上,我只需要处理覆盖图上的手势,所有鼠标消息仍需要传输到Web浏览器以提供某些功能。

I've tried the naive method of just calling the lower form's WndProc method given the message from the overlay. 我已经尝试过天真的方法,只要给定叠加层中的消息,就调用较低形式的WndProc方法。 (And similarly I've tried passing it further to the WndProc of the browser control window). (并且类似地,我尝试将其进一步传递到浏览器控制窗口的WndProc)。 Neither of these methods works. 这些方法都不起作用。 I've also tried simply swapping the HWnd parameter of the Message object to be the handle of the lower form/browser while attempting this and that has also not worked. 我也尝试过尝试简单地将Message对象的HWnd参数交换为低级表单/浏览器的句柄,但这也没有用。

So, would anyone be able to think of more methods to try, or possibly the correct method of passing windows messages between forms? 因此,任何人都可以想到更多尝试方法,或者可能是在窗体之间传递Windows消息的正确方法吗?

Thanks, Alex 谢谢,亚历克斯

How about create one master form, and let these two forms be the child forms. 如何创建一个主表单,然后将这两个表单作为子表单。 Delegate events from the child forms to master, and let master process whatever events you want to process. 将事件从子窗体委派给母版,让母版处理您要处理的所有事件。

var topForm = new Form1(); // top form
var browserForm = new Form2(); // bottom form with webbrowser control

add MouseClick event to Form1 将MouseClick事件添加到Form1

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
     browserForm.DoClick(e.X, e.Y);
}

create custom WebBrowserEx control to process mouse click events and add this control to Form2 创建自定义WebBrowserEx控件以处理鼠标单击事件并将此控件添加到Form2

namespace WindowsFormsApplication1
{
    public class WebBrowserEx: System.Windows.Forms.WebBrowser
    {
        public void DoClick(int x, int y)
        {
            base.OnMouseClick(new MouseEventArgs(MouseButtons.Left, 1, x, y, 0));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM