Form background transparency but still have click

2019-09-09 19:15发布

问题:

I have been trying to work out if this is possible.

Essentially what I want is to have a form with a border as normal but with a transparent background. In this area I want a live display of what's behind (a video feed is highly likely) into which I can capture the click events to display further information onto my overly form.

Currently I have tried to use Form.TransparencyKey and BackColor but I then lose the click events in the transparent sections of the from. I have also attempted to use a 99% transparent png as the BackgroundImage, but this just gives a dark gray background.

I have noted if I draw to the gap (ie a simple line) I can capture the click events over these, just not the transparent sections. Is there way I could draw a semi transparent rectangle in the on paint first maybe?

I would like this to be mono compatible if at all possible.

回答1:

As this has turned out to be impossible I ended up floating a 2nd dynamically generated form over the top with Opacity set to 0.01. I then used the Resize and LocationChanged events on the main form to resize and move the overlay form as required. I then just needed to handle the overlay forms click event to do as I needed.

public MainFrom()
    {
        InitializeComponent();

        f = new Form();
        f.Size = panel1.Size;
        f.FormBorderStyle = FormBorderStyle.None;
        f.BackColor = Color.Black;
        f.Opacity = 0.01;
        f.ShowInTaskbar = false;
        f.Show(this);

        f.Click += new System.EventHandler(this.UserClicked);
    }

This code then resizes the overlay form to account for the main forms border:

private void SetLocation()
        {
            // These next 2 lines calculate the width of the border 
            // and the title bar and then use these to off set the floating
            // forms location correctly.
            int xOffset = (this.Width - this.ClientSize.Width) / 2;
            int yOffset= this.Height - this.ClientSize.Height - xOffset;
            int x = this.Location.X + panel1.Location.X + xOffset;
            int y = this.Location.Y + panel1.Location.Y + yOffset;
            f.Location = new Point(x, y);
        }

According to the MoMa tool this is 100% mono compatible and the code compiles in MonoDevelop as expected. However if I run this I am seeing a null reference exception I do not see in MS .NET on load, this might be fixable but I am yet to look into this.



标签: c# winforms mono