我怎样才能增加透明度,以一个C#的形式,同时保持控制可见? [英] How can I add transparency to a c# form while keeping controls visible?

查看:117
本文介绍了我怎样才能增加透明度,以一个C#的形式,同时保持控制可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我休假从与透明的东西几天搞乱。我开始再次今晚搞乱。我使用了汉斯帕桑特的解决方案,新的结果:
  
   http://img3.imageshack.us/img3/4265/icontransp.jpg

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: http://img3.imageshack.us/img3/4265/icontransp.jpg

帕桑特的解决方案确实解决了透明背景渐变的问题。不过,我仍然运行到与透明的颜色在我的图标,窗体的背景色混合的问题。你可以看到周围的图标的各个部分的紫红色上述形象。

Passant's solution does solve the issue of the transparent background gradient. However, I'm still running into the problem with the transparent colors in my icon blending with the form's BackColor. You can see the fuchsia around various parts of the icon in the above image.

原创内容:

我已经在这个持续了几个小时,现在,我还没有多少运气。我搞砸与Control.Region,Form.TransparencyKey,Form.Opacity,和其他几个随机的东西有一些时髦的效果。

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.

最近,我一直在试图定义我的桌面,并决定惹应用码头。看到苹果码头和一些第三方Windows的实现必须提供什么之后,我决定建立自己的。

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.

最后,我想移动到使用Win32 API。现在我只是想要得到的东西用尽可能多的C#和.NET Framework的功能尽可能的工作。

Eventually I want to move on to using the Win32 API. For now I just want to get something working using as much C# and .Net framework capabilities as possible.

还有我希望能够在这个应用做了几件事情:


  • 显示带有渐变背景的形式/菜单。

  • 允许的形式/菜单有透明度,同时保持图标不透明。

  • 包含透明背景显示的图标。

  • 菜单和图标应该能够接收鼠标相关的事件(悬停,离开,单击的dragover,的DragDrop,和其他一些)。

这是我拍摄的效果:

http://img207.imageshack.us/img207/5716/desired.jpg

此图片显示在视觉效果我想实现的。这是一个皮肤我一个名为Rainmeter的程序进行。图像显示记事本+ +背后有一些皮肤的文件的皮肤编辑器中打开。菜单是透明的,但是它们的图标仍然是不透明的。

This image shows the visual effects I'm trying to achieve. This was a skin I made for a program called Rainmeter. The image shows Notepad++ behind the skin with a few of the skin's files open in the editor. The menu is transparent, but the icons remain opaque.

我的方法:

使用窗体来充当菜单似乎是一个逻辑的第一选择我。我有事件的一个基本的了解。我不太知道如何创建自己的单击事件,所以形式会使处理事件一点点更容易。我认为是图标的几个选项。我决定我会使用PictureBoxes的图标,因为他们可以容纳图像和接收事件。

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.

在我完成了code为我的菜单的所有结构逻辑,我开始玩弄它,试图得到视觉效果我想要的。 Form.Opacity影响表单上的所有内容的透明度。因为我想要的图标是完全不透明的,我独自离开这个属性。我尝试了背景色设置为Color.Transparent,而是给出了一个错误。我打周围的几个组合...

http://img204.imageshack.us/img204/757/effectsi.jpg

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. 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. I played around with a few combinations... http://img204.imageshack.us/img204/757/effectsi.jpg

我画的梯度一个Drawing2D.LinearGradientBrush成位图。那么此位图放在为Form.BackgroundImage或作为PictureBox.Image。如果使用的话,该图片的大小能够覆盖整个表格并发送至后面

I drew the gradient with a Drawing2D.LinearGradientBrush into a Bitmap. This Bitmap was then placed as the Form.BackgroundImage or as a PictureBox.Image. If used, the PictureBox was sized to cover the entire Form and sent to the back.

我注意到,一些Form.BackgroundColor会用我的图标轮廓混合在一起。的图标具有沿着一个平滑的外观的边缘透明度。由于图标拿起窗体的BackgroundColor中,这让我觉得PictureBoxes正在创造新的图像时的图标被加载到形式。图像的半透明部分,然后用窗体的BackgroundColor中合并时,应与任何颜色的形式后面合并

I noticed that some of the Form.BackgroundColor would be mixed in with the outlines of my icons. 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. 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.


http://img838.imageshack.us/img838/8299/whitedesktop.jpg

在这个图像,你可以看到紫红色的图标存在即使窗体的紫红色现在是完全透明的。忘指出同一绿色到黄梯度为150的阿尔法值是在任何情况下使用。在坡度不看绿色的图像,这是因为透明的颜色与背景紫红色混合。

In this image you can see the Fuchsia existing in the icons even though the Form's Fuchsia color is now completely transparent. I forgot to point out that the same green to yellow gradient with an Alpha value of 150 was used in every case. In the images where the gradient doesn't look green, it's because the transparent colors are blending with the Fuchsia background.

我真的不知道该怎么在这里做。我觉得我能得到我想要的东西,如果我能以某种方式使窗体独自完全透明的。我也在想我可能有更好的运气只是绘制的图标,而不是使用PictureBoxes。那么问题是建立图标接收鼠标事件。 (我从来没有把我自己的事件,我认为这会涉及一些Win32 API调用)。

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. 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.)

有没有别的东西我可以用PictureBoxes做得到我想要的效果吗?无论情况,我接受任何意见或建议,我想达到的整体效果。

Is there something else I can do with the PictureBoxes to get the effect I want? Whichever the case, I'm open to any ideas or suggestions for the overall effect I'm trying to achieve.

推荐答案

这是pretty容易的WinForms做。你需要的是的两个的形式三明治。底部应该提供透明渐变背景,上面应该吸取的图标和处理鼠标点击。一些示例code:

This is pretty easy to do in 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:

这篇关于我怎样才能增加透明度,以一个C#的形式,同时保持控制可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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