.NET Windows窗体透明的控制 [英] .NET Windows Forms Transparent Control

查看:170
本文介绍了.NET Windows窗体透明的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模拟Web 2.0的收藏夹风格的UI在 Windows窗体应用程序 。也就是说,提请注意一些前台控制由调光在一个窗口的客户区的所有其他内容。

I want to simulate a 'Web 2.0' Lightbox style UI technique in a Windows Forms application. That is, to draw attention to some foreground control by 'dimming' all other content in the client area of a window.

显而易见的解决方案是创建一个控制,这只是一种局部透明的矩形,可停靠窗口的客户区域,并提请的Z顺序的前面。它需要像一个玻璃脏疼痛通过其他控件仍然可以看到(因此继续涂料本身)。这可能吗?

The obvious solution is to create a control that is simply a partially transparent rectangle that can be docked to the client area of a window and brought to the front of the Z-Order. It needs to act like a dirty pain of glass through which the other controls can still be seen (and therefore continue to paint themselves). Is this possible?

我有一个很好的狩猎一轮,并尝试了一些自己的技术,但迄今还没有成功。 如果它是不可能的,这将是另一种方式来做到这一点?

I've had a good hunt round and tried a few techniques myself but thus far have been unsuccessful. If it is not possible, what would be another way to do it?

请参阅: http://www.useit.com/alertbox/application-design。 HTML (下一个截图灯箱部分来说明我的意思。)

See: http://www.useit.com/alertbox/application-design.html (under the Lightbox section for a screenshot to illustrate what I mean.)

推荐答案

你能做到这一点。NET / C#?

Can you do this in .NET/C#?

是的,你当然可以,但需要的一点点努力。我建议以下办法。创建无边框或标题栏区域中的顶级表,然后给确保它不会吸引客户区背景的TransparencyKey和背景色设置为相同的值。所以,你现在有没有画一个窗口...

Yes you certainly can but it takes a little bit of effort. I would recommend the following approach. Create a top level Form that has no border or titlebar area and then give make sure it draws no client area background by setting the TransparencyKey and BackColor to the same value. So you now have a window that draws nothing...

public class DarkenArea : Form
{
    public DarkenArea()
    {
        FormBorderStyle = FormBorderStyle.None;
        SizeGripStyle = SizeGripStyle.Hide;
        StartPosition = FormStartPosition.Manual;
        MaximizeBox = false;
        MinimizeBox = false;
        ShowInTaskbar = false;
        BackColor = Color.Magenta;
        TransparencyKey = Color.Magenta;
        Opacity = 0.5f;
    }
}

在窗体的客户区创建和地位这一DarkenArea窗口。然后,你需要能够显示窗口,而不考虑它的重点,所以你需要平台调用下列方式没有它变得活跃,显示...

Create and position this DarkenArea window over the client area of your form. Then you need to be able to show the window without it taking the focus and so you will need to platform invoke in the following way to show without it becoming active...

public void ShowWithoutActivate()
{
    // Show the window without activating it (i.e. do not take focus)
    PlatformInvoke.ShowWindow(this.Handle, (short)SW_SHOWNOACTIVATE);
}

您需要使其实际绘制的东西,但排除要继续强调控制的区域图。因此重写OnPaint处理和​​黑色/蓝色或任何你想要的,但不包括该地区吸引你要保持光亮......

You need to make it actually draw something but exclude drawing in the area of the control you want to remain highlighted. So override the OnPaint handler and draw in black/blue or whatever you want but excluding the area you want to remain bright...

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    // Do your painting here be exclude the area you want to be brighter
}

最后,你需要重写WndProc以prevent鼠标与窗口交互如果用户尝试一些疯狂喜欢点击黑暗区域。事情是这样的......

Last you need to override the WndProc to prevent the mouse interacting with the window if the user tries something crazy like clicking on the darkened area. Something like this...

protected override void WndProc(ref Message m)
{
    if (m.Msg == (int)WM_NCHITTEST)
        m.Result = (IntPtr)HTTRANSPARENT;
    else
        base.WndProc(ref m);
}

这应该足以让预期的效果。当您准备扭转你处置DarkenArea实例的影响而矣。

That should be enough to get the desired effect. When you are ready to reverse the effect you dispose of the DarkenArea instance and carry on.

这篇关于.NET Windows窗体透明的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆