简体   繁体   English

如何在C#中使表单的一部分部分透明?

[英]How can I make a part of the form partially transparent in C#?

I want to make a part of a form semi-transparent, with additional text on that part which is not transparent. 我想使表单的一部分半透明,在该部分上的附加文本不透明。

How can I accomplish this in C#? 我怎样才能在C#中实现这一目标?

I don't think you can apply transparency (more correctly termed, Opacity ) to only a part of a form rather than the complete, whole form. 我不认为你可以将透明度(更正确地称为“ 不透明度” )仅应用于表单的一部分而不是完整的整个表单。

You can, however, create a custom shaped form (ie non-rectangular) quite easily, with various parts of that form being transparent. 但是,您可以非常轻松地创建自定义形状(即非矩形),该形式的各个部分都是透明的。 Depending upon the exact "look" that you're trying to achieve, this may be closest you'll get. 根据您尝试实现的确切“外观”,这可能是您最接近的。

Take a look at these links for creating your own custom-shaped form: 看看这些链接,以创建自己的自定义形状:

Creating Custom Shaped Windows Forms in .NET 在.NET中创建自定义形状的Windows窗体
Custom shaped form with a drop down in C# 自定义形状的表格,下拉C#
Shaped Windows Forms and Controls in Visual Studio .NET Visual Studio .NET中的异形Windows窗体和控件

The only other alternative may be to display two forms, one of which is set to be partially transparent. 唯一的另一种选择可能是显示两种形式,其中一种形式设置为部分透明。 You would have to programmatically ensure that the second form is positioned immediately next to the "main" form, and is moved/resized proportionately when the user moves/resizes the "main" form. 您必须以编程方式确保第二个表单紧邻“主”表单放置,并在用户移动/调整“主”表单时按比例移动/调整大小。 Although this is two forms, it could be made to look, to the user, that it's actually only one form, although this could be quite tricky to pull off, and would not be a perfect solution (but may be "good enough" depending upon your exact needs). 虽然这是两种形式,但是可以向用户说明它实际上只有一种形式,虽然这可能非常难以实现,并且不是一个完美的解决方案(但可能“足够好”取决于根据您的确切需求)。

You can do this by creating a fully-transparent window in your form, and then floating a semi-transparent form over the fully-transparent window. 您可以通过在表单中​​创建一个完全透明的窗口,然后在完全透明的窗口上浮动半透明窗体来完成此操作。

First, set the TransparencyKey of your main form to Color.Red, then place a Panel named panel1 on the form, and set its BackColor to Red. 首先,将主窗体的TransparencyKey设置为Color.Red,然后在窗体上放置一个名为panel1的面板,并将其BackColor设置为Red。 This will create the fully-transparent "window". 这将创建完全透明的“窗口”。 Create a form-level Form reference like this: 创建一个表单级表单引用,如下所示:

private Form _floater;

Next, put this code in your main form's Load event: 接下来,将此代码放在主窗体的Load事件中:

_floater = new Form();
_floater.ShowInTaskbar = false;
_floater.FormBorderStyle = FormBorderStyle.None;
_floater.Opacity = .5;
_floater.Size = panel1.Size;
_floater.StartPosition = FormStartPosition.Manual;
_floater.Location = panel1.PointToScreen(new Point(0, 0));
_floater.Show(this);

Finally, put this code in your main form's Move event: 最后,将此代码放在主窗体的Move事件中:

_floater.Location = panel1.PointToScreen(new Point(0, 0));

The only issue here is that if the user clicks in the semi-transparent "window", the second form will get the focus, so your main form's TitleBar is grayed out. 这里唯一的问题是,如果用户点击半透明的“窗口”,第二个表单将获得焦点,因此您的主表单的TitleBar将显示为灰色。

Click here to run a sample application with this semi-transparent window on a form. 单击此处以在窗体上使用此半透明窗口运行示例应用程序。

简单:

myForm.Opacity = 80; // Change 80 with your value

I'am not sure if this is even possible with WinForms without exzessessive use of p/invoke calls and workarounds. 我不确定WinForms是否可以在没有充分利用p / invoke调用和变通方法的情况下实现这一点。

You may want to use Windows Presentation Foundation (WPF) instead, where such effects are almost trivial to implement. 您可能希望使用Windows Presentation Foundation(WPF),这样的效果几乎无法实现。

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

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