vb.net 按钮图像、透明度、鼠标悬停颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31568506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:19:42  来源:igfitidea点击:

Button images, transparency, MouseOver colors

vb.netimagetransparency

提问by Bill

I have a button that I have made Flat, with no border so when there is an image in it, it is seamless looking.

我有一个扁平的按钮,没有边框,所以当里面有图像时,它看起来是无缝的。

The image I want to add to this button is basically just an unfilled rectangle (png from a file).

我想添加到这个按钮的图像基本上只是一个未填充的矩形(来自文件的 png)。

When I mouse over this button, I have the backcolor change to red. When it does, the rectangle that is the image obviously does not change to red..it stays whatever color it is from the image.

当我将鼠标悬停在此按钮上时,背景色变为红色。当它发生时,作为图像的矩形显然不会变成红色......它保持图像中的任何颜色。

I wanted to know if there is a way to add an image, who's actual backcolor is transparent (I suppose?).

我想知道是否有办法添加图像,谁的实际背景色是透明的(我想?)。

The image is a blue rectangle with a very very light brown background. When I mouse over I would like the ENTIRE button's backcolor to be red, but only maintain the blue rectangle. Right now when you mouseover you can clearly see the size of the image itself.

图像是一个蓝色矩形,背景是非常浅的棕色。当我将鼠标悬停在我希望整个按钮的背景色为红色时,但只保留蓝色矩形。现在,当您将鼠标悬停在鼠标上时,您可以清楚地看到图像本身的大小。

I'd rather not draw graphics to the button. (In actuality this image is an outline of a computer monitor...but it's essentially a rectangle for this case)

我宁愿不为按钮绘制图形。(实际上,此图像是计算机显示器的轮廓……但对于这种情况,它本质上是一个矩形)

Is there a way to do this with the Image or BackgroundImage properties?

有没有办法用 Image 或 BackgroundImage 属性来做到这一点?

回答by nelek

Put Your image in Resources(Project/Properties/Resources/Images/Add Resource and from dropdown menu choose Add Existing File...).

将您的图像放入Resources(项目/属性/资源/图像/添加资源,然后从下拉菜单中选择添加现有文件...)。

Then use this code :

然后使用此代码:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      With Button1
        .Text = ""
        .UseVisualStyleBackColor = False
        .BackColor = Color.Transparent
        .FlatStyle = FlatStyle.Flat
        .FlatAppearance.BorderSize = 0
        .FlatAppearance.MouseOverBackColor = Color.Red
        .BackgroundImageLayout = ImageLayout.Center
        .BackgroundImage = My.Resources.XXX 'image name from Resources
      End With
    End Sub

    Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseHover
            With Button1
                .BackgroundImage = Nothing
                .FlatAppearance.BorderColor = Color.Blue
                .FlatAppearance.BorderSize = 2  'or what is size of Your border in px (take from Your image)
            End With
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
            With Button1
                .BackColor = Color.Transparent
                .BackgroundImage = My.Resources.XXX 'image name from Resources
                .FlatAppearance.BorderSize = 0
            End With
    End Sub

Of course, You can create Your own button (class) and import into Your project, where You can avoid this MouseHoverand MouseLeavefor every button.

当然,您可以创建自己的按钮(类)并导入到您的项目中,您可以在其中避免这种情况MouseHoverMouseLeave每个按钮。

btw. All this You can do without image if Your picture have only borderand backgroundof image isn't gradient. Then You can avoid MouseHoverand MouseLeave.

顺便提一句。如果您的图片只有border并且background图像不是渐变的,那么您可以在没有图像的情况下完成所有这些。那么你可以避免MouseHoverMouseLeave

Edit :There is code if You don't need image (border of button is always blueof 2px, background color is some kind of light brown, and on hover background color is red but border stay blue with 2px).

编辑:如果您不需要图像,则有代码(按钮的边框始终blue2px,背景颜色为某种浅棕色,悬停时背景颜色为红色但边框保持蓝色,2px)。

With Button2
  .UseVisualStyleBackColor = False
  .BackColor = Color.FromArgb(217, 195, 154)
  .FlatStyle = FlatStyle.Flat
  .FlatAppearance.BorderSize = 2
  .FlatAppearance.BorderColor = Color.Blue
  .FlatAppearance.MouseOverBackColor = Color.Red
 End With