简体   繁体   English

如何在保持控件可见的同时为ac#表单添加透明度?

[英]How can I add transparency to a c# form while keeping controls visible?

UPDATE: I took a break from messing with the transparency stuff for a few days. 更新:我花了几天时间搞乱了透明度的东西。 I started messing with it again tonight. 我今晚又开始搞砸了。 I got a new result using Hans Passant's solution: 我使用Hans Passant的解决方案得到了一个新结果: 新结果 http://img3.imageshack.us/img3/4265/icontransp.jpg http://img3.imageshack.us/img3/4265/icontransp.jpg

Passant's solution does solve the issue of the transparent background gradient. Passant的解决方案确实解决了透明背景渐变的问题。 However, I'm still running into the problem with the transparent colors in my icon blending with the form's BackColor. 但是,我仍然遇到问题,我的图标中的透明颜色与表单的BackColor混合。 You can see the fuchsia around various parts of the icon in the above image. 您可以在上图中看到图标各个部分周围的紫红色。

ORIGINAL CONTENT: 原始内容:

I've been going at this for several hours now, and I haven't had much luck. 我已经在这几个小时了,而且我运气不好。 I've messed with Control.Region, Form.TransparencyKey, Form.Opacity, and a couple other random things with some funky effects. 我已经搞砸了Control.Region,Form.TransparencyKey,Form.Opacity以及其他一些带有一些时髦效果的随机事物。

Lately I've been trying to customize my desktop and decided to mess with Application Docks. 最近我一直在尝试自定义我的桌面,并决定搞乱应用程序码头。 After seeing what the Mac dock and a few third-party Windows implementations had to offer, I decided I wanted to build my own. 在看到Mac Dock和一些第三方Windows实现提供的内容之后,我决定构建自己的。

Eventually I want to move on to using the Win32 API. 最终我想继续使用Win32 API。 For now I just want to get something working using as much C# and .Net framework capabilities as possible. 目前我只想使用尽可能多的C#和.Net框架功能。

There are a few things I want to be able to do in this application: 我想在这个应用程序中做一些事情:

  • Display a form/menu with a gradient background. 显示带有渐变背景的表单/菜单。
  • Allow the form/menu to have transparency while keeping icons opaque. 允许表单/菜单具有透明度,同时保持图标不透明。
  • Display icons that contain transparent backgrounds. 显示包含透明背景的图标。
  • The Menu and Icons should be able to receive mouse-related events (hover, leave, click, dragover, dragdrop, and a few others). 菜单和图标应该能够接收与鼠标相关的事件(悬停,离开,点击,dragover,dragdrop和其他一些事件)。

This is the effect I'm shooting for: 这是我拍摄的效果: 期望的效果 http://img207.imageshack.us/img207/5716/desired.jpg http://img207.imageshack.us/img207/5716/desired.jpg

This image shows the visual effects I'm trying to achieve. 此图像显示了我想要实现的视觉效果。 This was a skin I made for a program called Rainmeter. 这是我为一个名为Rainmeter的程序制作的皮肤。 The image shows Notepad++ behind the skin with a few of the skin's files open in the editor. 图像显示了皮肤后面的Notepad ++,其中一些皮肤文件在编辑器中打开。 The menu is transparent, but the icons remain opaque. 菜单是透明的,但图标保持不透明。

My Approach: 我的方法:

Using a Form to act as the menu seemed like a logical first choice to me. 使用表单作为菜单似乎是我合乎逻辑的首选。 I have a basic understanding of events. 我对事件有基本的了解。 I'm not quite sure how to create my own click events, so a form would make working with events a tad easier. 我不太确定如何创建自己的点击事件,因此表单可以使事件处理更容易。 I considered a few options for the icons. 我考虑了一些图标选项。 I decided I'd use PictureBoxes for the icons, since they can hold images and receive events. 我决定使用PictureBoxes作为图标,因为它们可以保存图像并接收事件。

Once I finished the code for all the structural logic of my menu, I started playing around with it to try to get the visual effect I wanted. 一旦我完成了菜单中所有结构逻辑的代码,我就开始玩它来尝试获得我想要的视觉效果。 Form.Opacity affected the transparency of everything on the form. Form.Opacity影响了表单上所有内容的透明度。 Since I want the icons to be fully opaque, I left this property alone. 由于我希望图标完全不透明,所以我独自留下了这个属性。 I tried setting the BackColor to Color.Transparent, but that gives an error. 我尝试将BackColor设置为Color.Transparent,但这会产生错误。 I played around with a few combinations... 我玩了几个组合...... 组合效果 http://img204.imageshack.us/img204/757/effectsi.jpg http://img204.imageshack.us/img204/757/effectsi.jpg

I drew the gradient with a Drawing2D.LinearGradientBrush into a Bitmap. 我使用Drawing2D.LinearGradientBrush将渐变绘制到位图中。 This Bitmap was then placed as the Form.BackgroundImage or as a PictureBox.Image. 然后将此位图放置为Form.BackgroundImage或PictureBox.Image。 If used, the PictureBox was sized to cover the entire Form and sent to the back. 如果使用,PictureBox的大小将覆盖整个表单并发送到后面。

I noticed that some of the Form.BackgroundColor would be mixed in with the outlines of my icons. 我注意到一些Form.BackgroundColor会与我的图标轮廓混在一起。 The icons have transparency along the edges for a smoother appearance. 图标沿边缘具有透明度,使外观更加平滑。 Since the icons are picking up the Form's BackgroundColor, this makes me think that the PictureBoxes are creating new images when the icons are loaded into the form. 由于图标正在拾取Form的BackgroundColor,这使我认为PictureBoxes在图标加载到表单时创建新图像。 The semi-transparent portions of the image are then merged with the Form's BackgroundColor when they should merge with whatever colors are behind the form. 然后,当图像的半透明部分与表单后面的任何颜色合并时,它们将与Form的BackgroundColor合并。

白色桌面效果 http://img838.imageshack.us/img838/8299/whitedesktop.jpg http://img838.imageshack.us/img838/8299/whitedesktop.jpg

In this image you can see the Fuchsia existing in the icons even though the Form's Fuchsia color is now completely transparent. 在此图像中,即使Form的紫红色现在完全透明,您也可以看到图标中存在的紫红色。 I forgot to point out that the same green to yellow gradient with an Alpha value of 150 was used in every case. 我忘了指出在每种情况下使用相同的绿色到黄色渐变,Alpha值为150。 In the images where the gradient doesn't look green, it's because the transparent colors are blending with the Fuchsia background. 在渐变看起来不是绿色的图像中,这是因为透明颜色与紫红色背景混合在一起。

I'm not really sure what to do from here. 我不确定从这里做什么。 I feel like I could get what I want if I could somehow make the Form alone completely transparent. 如果我能以某种方式使表单完全透明,我觉得我能得到我想要的东西。 I was also thinking I may have better luck just drawing the icons instead of using PictureBoxes. 我也在想我可能只是绘制图标而不是使用PictureBoxes。 The problem then would be setting up the icons to receive mouse events. 那么问题是设置图标以接收鼠标事件。 (I've never made my own events, and I think it would involved some Win32 API calls.) (我从未做过自己的事件,我认为它会涉及一些Win32 API调用。)

Is there something else I can do with the PictureBoxes to get the effect I want? 我可以用PictureBoxes做些什么来获得我想要的效果吗? Whichever the case, I'm open to any ideas or suggestions for the overall effect I'm trying to achieve. 无论哪种情况,我都会接受任何有关我正在努力实现的整体效果的想法或建议。

This is pretty easy to do in Winforms. 在Winforms中这很容易做到。 What you need is a sandwich of two forms. 你需要的是两种形式的三明治。 The bottom one should provide the transparent gradient background, the top one should draw the icons and handle mouse clicks. 底部应提供透明渐变背景,顶部应绘制图标并处理鼠标单击。 Some sample code: 一些示例代码:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.TopMost = true;
        this.FormBorderStyle = FormBorderStyle.None;
        this.TransparencyKey = this.BackColor = Color.Fuchsia;
        this.Opacity = 0.3;
        var overlay = new Form();
        overlay.FormBorderStyle = FormBorderStyle.None;
        overlay.TransparencyKey = overlay.BackColor = Color.Fuchsia;
        overlay.StartPosition = FormStartPosition.Manual;
        overlay.Location = this.Location;
        overlay.MouseDown += HandleIconClick;
        this.Resize += delegate { overlay.Size = this.Size; };
        this.LocationChanged += delegate { overlay.Location = this.Location; };
        overlay.Paint += PaintIcons;
        this.Paint += PaintBackground;
        this.Load += delegate { overlay.Show(this); };
    }
    private void PaintBackground(object sender, PaintEventArgs e) {
        var rc = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
        using (var br = new LinearGradientBrush(rc, Color.Gainsboro, Color.Yellow, 0f)) {
            e.Graphics.FillRectangle(br, rc);
        }
    }
    private void PaintIcons(object sender, PaintEventArgs e) {
        e.Graphics.DrawIcon(Properties.Resources.ExampleIcon1, 50, 30);
        // etc...
    }
    void HandleIconClick(object sender, MouseEventArgs e) {
        // TODO
    }
}

Which looks like this with the somewhat random colors and icon I selected: 我选择的颜色和图标看起来像这样:

在此输入图像描述

OK, I got a bit lost in all that, but from the description in the original paragraph, I would make sure the background rectangle is NOT the visual parent of the pictureboxes. 好吧,我在所有这些中都有点迷失,但是根据原始段落中的描述,我会确保背景矩形不是图片框的可视父级。 Make them overlapping siblings, with the pictureboxes in front using Panel.Zindex. 使用Panel.Zindex将它们与前面的图片框重叠。

Then you can just change the opacity of the rectangle, without affecting the icons. 然后,您只需更改矩形的不透明度,而不会影响图标。 Also make sure the icon source image files have a transparent background. 还要确保图标源图像文件具有透明背景。

Should work I think. 我认为应该工作。

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

相关问题 如何在保持控件完全可见的同时使表单透明? - How do I make a form transparent while keeping the controls fully visible? 如何在保留 SelectionColor 的同时为 RichTextBox 制作“查找表单”? C# - How can I make a “Find Form” for RichTextBox while keeping SelectionColor? C# 如何向 C# 中的控件添加移动效果? - How can I add moving effects to my controls in C#? 我可以在C#MessageBox中添加控件吗? - Can I add controls to the C# MessageBox? 如何在WPF C#代码中向表单添加控件/元素? - How to add controls / Elements to a form in the WPF C# code? 如何以编程方式向所有控件添加工具提示-WPF,C#? - How to add tooltip to all controls in a form programmatically - WPF, C#? Visual C#:如何将控件添加到使用代码创建的表单中? - Visual C#: How to add controls to a form created with code? 如何保持Form在C#中多次打开,同时保持与它之前的父Form的连接? - How do I prevent a Form from opening multiple times in C#, while keeping a connection to the parent Form that came before it? 将控件添加到继承的Form C# - Add Controls to an inherited Form c# C#:如何在Windows应用程序中的Win窗体上自由移动控件? - C#: How can I move my controls on a win form in windows application freely?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM