C#Windows窗体半不透明 [英] C# Windows Forms semi-opacity

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

问题描述

我已经阅读了许多有关C#中Windows窗体上的不透明度/透明度的主题,但这并不是我想要的效果.我希望 Form 是100%透明的,但是 Panel 的透明度是可调的,并且透明效果已转移到Form背后的元素(Windows桌面,Web浏览器等)上.).随附的照片显示了我想要获得的效果(我是用图形程序制作的).感谢您的帮助.

解决方案

 公共局部类MyOwnerForm:表格{公共MyOwnerForm(){InitializeComponent();this.BackColor = Color.Magenta;this.TransparencyKey = Color.Magenta;this.StartPosition = FormStartPosition.Manual;this.DesktopLocation = new Point(100,100);this.ClientSize = new Size(330,330);}受保护的重写void OnShown(EventArgs e){base.OnShown(e);CreateForm(1,新Point(10,10),新Size(150,150)).Show();CreateForm(0.75,新Point(170,10),新Size(150,150)).Show();CreateForm(0.50,新Point(10,170),新Size(150,150)).Show();CreateForm(0.25,新Point(170,170),新Size(150,150)).Show();}受保护的重写void OnMove(EventArgs e){base.OnMove(e);if(OwnedForms.Length> 0){var p = PointToScreen(new Point(10,10));var dx = p.X-OwnedForms [0] .Location.X;var dy = p.Y-OwnedForms [0] .Location.Y;foreach(OwnedForms中的var f)f.Location =新Point(f.Location.X + dx,f.Location.Y + dy);}}表格CreateForm(双重不透明度,点位置,大小大小){var f = new Form();f.FormBorderStyle = FormBorderStyle.None;f.BackColor = Color.Lime;f.不透明度=不透明度;f.StartPosition = FormStartPosition.Manual;f.DesktopLocation = PointToScreen(location);f.ClientSize =大小;f.Owner =这个;f.ShowInTaskbar = false;返回f;}} 

Windows窗体-分层Windows

作为一种选择,您可以使用

 公共子类MyLayeredForm:PerPixelAlphaForm{公共MyLayeredForm(){InitializeComponent();var bm = new Bitmap(230,230);使用(var g = Graphics.FromImage(bm)){使用(var b = new SolidBrush(Color.FromArgb(255,Color.Lime)))g.FillRectangle(b,10,10,100,100);使用(var b = new SolidBrush(Color.FromArgb(255 * 75/100,Color.Lime)))g.FillRectangle(b,120,10,100,100);使用(var b = new SolidBrush(Color.FromArgb(255 * 50/100,Color.Lime)))g.FillRectangle(b,10,120,100,100);使用(var b = new SolidBrush(Color.FromArgb(255 * 25/100,Color.Lime)))g.FillRectangle(b,120,120,100,100);}this.SelectBitmap(bm);}} 

WPF-具有不透明度的透明表单和控件

满足此类UI要求的更好框架是WPF.

为此,您可以将窗口的 Background 设置为 Transparent ,将 WindowStyle 设置为 None ,并设置 AllowTransparency 转换为 True .同样,对于每个控件,您只需设置 Opacity 值:

 < Window x:Class ="WpfApp1.MainWindow"xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local ="clr-namespace:WpfApp1"mc:Ignorable ="d"标题="MainWindow"高度="261.154"宽度="232.923"Background ="Transparent" AllowsTransparency ="True"WindowStyle ="None" WindowStartupLocation ="CenterScreen">< Grid Margin ="0,0,0,0">< Grid Horizo​​ntalAlignment =左" Height ="100"Margin ="10,10,0,0" VerticalAlignment =顶部"Width ="100" Background ="Lime" Opacity ="1"/>< Grid Horizo​​ntalAlignment =左" Height ="100"Margin ="120,10,0,0" VerticalAlignment ="Top"宽度="100"背景=石灰"不透明度="0.75"Grid.ColumnSpan ="2"/>< Grid Horizo​​ntalAlignment =左" Height ="100"Margin ="10,120,0,0" VerticalAlignment =顶部"Width ="100" Background ="Lime" Opacity ="0.50"/>< Grid Horizo​​ntalAlignment =左" Height ="100"Margin ="120,120,0,0" VerticalAlignment ="Top"宽度="100"背景=石灰"不透明度="0.25"Grid.ColumnSpan ="2"/></Grid></Window> 

I have already read many topics about opacity/transparency on Windows Forms in C#, but it's not the effect I would like to get. I would like Form to be 100% transparent, but transparency of the Panel was adjustable, and the transparency effect was transferred to the elements behind Form (Windows desktop, web browser e.t.c.). The attached photo shows the effect that I would like to get (I made them in a graphic program). I will be grateful for your help.

解决方案

OP: If there is any other language / environment in which I can deal with this problem, of course I am ready to try it.

So in addition to Windows Forms solutions, I'll share a WPF solution as well (which is a better framework to satisfy this requirement):

  • Windows Forms - Owned Forms
  • Windows Forms - Layered Windows
  • WPF - Transparent Form and Control Opacity

Windows Forms - Owned Forms

As an option you can use Owned Forms.

Each of the panels can be a top-level border-less Form owned by the main form. The main has a transparency key equal to its back color and those owned forms has opacity. This way you should handle moving of the main form and move the owned forms as well:

public partial class MyOwnerForm : Form
{
    public MyOwnerForm()
    {
        InitializeComponent();
        this.BackColor = Color.Magenta;
        this.TransparencyKey = Color.Magenta;
        this.StartPosition = FormStartPosition.Manual;
        this.DesktopLocation = new Point(100, 100);
        this.ClientSize = new Size(330, 330);
    }
    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        CreateForm(1, new Point(10, 10), new Size(150, 150)).Show();
        CreateForm(0.75, new Point(170, 10), new Size(150, 150)).Show();
        CreateForm(0.50, new Point(10, 170), new Size(150, 150)).Show();
        CreateForm(0.25, new Point(170, 170), new Size(150, 150)).Show();
    }
    protected override void OnMove(EventArgs e)
    {
        base.OnMove(e);
        if(OwnedForms.Length>0)
        {
            var p = PointToScreen(new Point(10, 10));
            var dx = p.X - OwnedForms[0].Location.X;
            var dy = p.Y - OwnedForms[0].Location.Y;
            foreach (var f in OwnedForms)
                f.Location= new Point(f.Location.X+dx, f.Location.Y+dy);
        }
    }
    Form CreateForm(double opacity, Point location, Size size)
    {
        var f = new Form();
        f.FormBorderStyle = FormBorderStyle.None;
        f.BackColor = Color.Lime;
        f.Opacity = opacity;
        f.StartPosition = FormStartPosition.Manual;
        f.DesktopLocation = PointToScreen(location);
        f.ClientSize = size;
        f.Owner = this;
        f.ShowInTaskbar = false;
        return f;
    }
}

Windows Forms - Layered Windows

As an option you can use Layered Windows.

This way you can create the semi transparent image at run-time and set it as background image of your form. But your form will not receive any paint event and so hosting control on such form is pointless (however they are working and you somehow can force those control repaint).

public partial class MyLayeredForm : PerPixelAlphaForm
{
    public MyLayeredForm()
    {
        InitializeComponent();
        var bm = new Bitmap(230, 230);
        using (var g = Graphics.FromImage(bm))
        {
            using (var b = new SolidBrush(Color.FromArgb(255, Color.Lime)))
                g.FillRectangle(b, 10, 10, 100, 100);
            using (var b = new SolidBrush(Color.FromArgb(255 * 75 / 100, Color.Lime)))
                g.FillRectangle(b, 120, 10, 100, 100);
            using (var b = new SolidBrush(Color.FromArgb(255 * 50 / 100, Color.Lime)))
                g.FillRectangle(b, 10, 120, 100, 100);
            using (var b = new SolidBrush(Color.FromArgb(255 * 25 / 100, Color.Lime)))
                g.FillRectangle(b, 120, 120, 100, 100);
        }
        this.SelectBitmap(bm);
    }
}

WPF - Transparent Form and Controls having Opacity

A better framework to satisfy such UI requirement is WPF.

To do so, you can set Background of window to Transparent and WindowStyle to None and set AllowTransparency to True. Also for each control you can simply set Opacity value:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="261.154" Width="232.923" 
        Background="Transparent" AllowsTransparency="True" 
        WindowStyle="None" WindowStartupLocation="CenterScreen">
    <Grid Margin="0,0,0,0">
        <Grid HorizontalAlignment="Left" Height="100" 
              Margin="10,10,0,0" VerticalAlignment="Top" 
              Width="100" Background="Lime" Opacity="1"/>
        <Grid HorizontalAlignment="Left" Height="100"
              Margin="120,10,0,0" VerticalAlignment="Top"
              Width="100" Background="Lime" Opacity="0.75" 
              Grid.ColumnSpan="2"/>
        <Grid HorizontalAlignment="Left" Height="100"
              Margin="10,120,0,0" VerticalAlignment="Top"
              Width="100" Background="Lime" Opacity="0.50"/>
        <Grid HorizontalAlignment="Left" Height="100"
              Margin="120,120,0,0" VerticalAlignment="Top"
              Width="100" Background="Lime" Opacity="0.25"
              Grid.ColumnSpan="2"/>
    </Grid>
</Window>

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

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