简体   繁体   English

C#半透明的“ LightBox”控件调用父级的绘制方法

[英]C# semi-transparent 'LightBox' Control invoking Parent's paint method

I am trying to create a lightbox effect within my application. 我正在尝试在我的应用程序中创建灯箱效果 To achieve this, I have a UserControl with a panel representing the semi-transparent overlay and a seperate panel hosting all the necessary content. 为此,我有一个UserControl,其中的一个面板代表半透明的覆盖层,另一个面板包含所有必要的内容。

When I show this UserControl It often attempts to render itself 2-3 times causing the background to appear to get darker and darker. 当我显示此UserControl时,它经常尝试渲染2-3次,导致背景看起来越来越暗。 I thought about doing the following in the UserControl 我考虑过在UserControl中执行以下操作

protected override void OnPaint ( PaintEventArgs e )
{
    if ( Parent != null )
    {
        Parent.Refresh();
    }

    base.OnPaint( e );
} 

unfortunatly, this appears to cause a horrible loop-effect whereby the Parent red-raws itelf and then teh UserControl redraws itelf... very messy. 不幸的是,这似乎造成了可怕的循环效应,即父级拖拉Itelf,然后UserControl重绘Itelf ...非常混乱。 Is there a way of somehow stopping this? 有办法以某种方式阻止这种情况吗? possibly by taking a printscreen image from the application, displaying that in the user control with the overlay ontop? 可能是通过从应用程序中获取打印屏幕图像,并在用户控件上显示该图像,并在顶部显示覆盖图?

EDIT 编辑

I have noticed this question although Im hoping that I don't need to create a new form for every lightbox I create! 我注意到了这个问题,尽管我希望我不需要为我创建的每个灯箱创建一个新表单!

As a rule of the thumb, .Invalidate() is often much better than .Refresh(), since .Refresh() causes an immediate redraw, so if you call it twice, two redraws are being made. 根据经验,.Invalidate()通常比.Refresh()好得多,因为.Refresh()会导致立即重绘,因此,如果您调用两次,则会进行两次重绘。 I don't think it will help you much here, though. 不过,我认为这不会对您有太大帮助。

This behaviour appears to be because I was updating the UserControl's Region within the onPaint function eg 此行为似乎是因为我正在onPaint函数中更新UserControl的Region,例如

protected override void OnPaint ( PaintEventArgs e )
{
    // Update Region here

    base.OnPaint( e );
}

This apparently caused the control to Invalidate itself and re-draw itself to overcome this I have used: 显然,这导致控件自身失效并重新绘制自身,以克服我已经使用的方法:

Graphics g = e.Graphics;
g.SetClip( Region, CombineMode.Intersect );

This can then be used to pain on the opaque lightbox effect whilst allowing the content to paint itself. 然后,可以使用它来减轻不透明灯箱效果的痛苦,同时允许内容自行绘制。

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

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