形成背景透明性,但仍有点击事件 [英] Form background transparency but still have click events
问题描述
如果可能的话,我一直在努力解决问题.
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.
当前,我尝试使用Form.TransparencyKey和BackColor,但随后失去from的透明部分中的click事件.我还尝试过使用99%透明的png作为BackgroundImage,但这只会产生深灰色的背景.
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.
推荐答案
由于事实证明这是不可能的,我最终在Opacity设置为0.01的情况下,在顶部浮动了第二个动态生成的表单.然后,我在主窗体上使用了Resize和LocationChanged事件来调整大小并根据需要移动覆盖窗体.然后,我只需要处理覆盖表单的click事件即可根据需要进行操作.
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);
}
根据MoMa工具,这是100%与Mono兼容的,并且代码可以按预期在MonoDevelop中编译.但是,如果运行此命令,我会看到一个空引用异常,该异常在加载时在MS .NET中没有出现,这可能是可修复的,但我尚未对此进行研究.
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.
这篇关于形成背景透明性,但仍有点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!