如何在 C# 中使 UserControls BackColor 透明?

How to make a UserControls BackColor transparent in C#?(如何在 C# 中使 UserControls BackColor 透明?)
本文介绍了如何在 C# 中使 UserControls BackColor 透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 窗体用户控件(由一个单选按钮、三个标签和一个进度条组成)中创建了一个简单的火柴人.

I created a simple stick man in a Windows Form User-Control (consisting of a radio button and three labels and one progress bar).

我将新用户控件的背景颜色设置为透明,这样当我将它拖到表单上时,它会与表单上的其他颜色和绘图混合.我没有得到我想要达到的目标.

I set the back-color of the new user-control to transparent so that when I drag it onto my form, it blends with other colors and drawings on the form. I am not getting what I'm trying to achieve.

图片如下:

推荐答案

UserControl 已经支持这个,它的 ControlStyles.SupportsTransparentBackColor 样式标志已经打开.您所要做的就是将 BackColor 属性设置为 Color.Transparent.

UserControl already supports this, its ControlStyles.SupportsTransparentBackColor style flag is already turned on. All you have to do is set the BackColor property to Color.Transparent.

接下来要记住的是,这种透明度是模拟的,它是通过要求控件的父级绘制自身以产生背景来完成的.因此,重要的是您正确设置了 Parent .如果父级不是容器控件,这有点棘手.就像一个图片框.设计器将使表单成为父级,因此您将看到表单的背景,而不是图片框.您需要在代码中修复它,编辑表单构造函数并使其看起来类似于:

Next thing you have to keep in mind in that this transparency is simulated, it is done by asking the Parent of the control to draw itself to produce the background. So what is important is that you get the Parent set correctly. That's a bit tricky to do if the parent is not a container control. Like a PictureBox. The designer will make the Form the parent so you will see the form's background, not the picture box. You'll need to fix that in code, edit the form constructor and make it look similar to this:

var pos = this.PointToScreen(userControl11.Location);
userControl11.Parent = pictureBox1;
userControl11.Location = pictureBox1.PointToClient(pos);

这篇关于如何在 C# 中使 UserControls BackColor 透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)