简体   繁体   中英

C# Winforms: Transparent Drag and Drop Overlay

I am using a Webview (actually Geckofx) in a Windows Forms Application and would like to have a transparent Drag and Drop Layout above the Webview - also, if possible, it should pass mouse events to the Webview. Is this possible?

My solution is a transparent panel that also passes all events on to the underlying parent form

public class TransparentPanel : Panel
{
    Timer Wriggler = new Timer();

    public TransparentPanel()
    {
        Wriggler.Tick += new EventHandler(TickHandler);
        this.Wriggler.Interval = 500;
        this.Wriggler.Enabled = true;
    }

    protected void TickHandler(object sender, EventArgs e)
    {
        this.InvalidateEx();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;

            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 

            return cp;
        }
    }

    protected void InvalidateEx()
    {
        if (Parent == null)
        {
            return;
        }

        Rectangle rc = new Rectangle(this.Location, this.Size);

        Parent.Invalidate(rc, true);
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_NCHITTEST = 0x0084;
        const int HTTRANSPARENT = (-1);

        if (m.Msg == WM_NCHITTEST)
        {
            m.Result = (IntPtr)HTTRANSPARENT;
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // Do not allow the background to be painted  
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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