问题描述
我有一个按钮,我做了Flat,没有边框,所以当有图像时,它是无缝的。
I have a button that I have made Flat, with no border so when there is an image in it, it is seamless looking.
我想要的图像添加到此按钮基本上只是一个未填充的矩形(文件中的png)。
The image I want to add to this button is basically just an unfilled rectangle (png from a file).
当我将鼠标悬停在此按钮上时,我将背景颜色更改为红色。如果是的话,作为图像的矩形显然不会变成红色..它会保留图像中的任何颜色。
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)
有没有办法用Image或BackgroundImage属性?
Is there a way to do this with the Image or BackgroundImage properties?
推荐答案
将您的图像放入资源
(项目/属性) / Resources / Images / Add Resource并从下拉菜单中选择Add Existing File ...)。
Put Your image in Resources
(Project/Properties/Resources/Images/Add Resource and from dropdown menu choose Add Existing File...).
然后使用此代码:
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
当然,你可以创建自己的按钮(类)并导入到你的项目中,你可以避免这个 MouseHover $ c每个按钮都有$ c>和
MouseLeave
。
Of course, You can create Your own button (class) and import into Your project, where You can avoid this MouseHover
and MouseLeave
for every button.
btw。如果您的图片只有 border
并且背景
的图片不是渐变,那么你可以不用图像。然后你可以避免 MouseHover
和 MouseLeave
。
btw. All this You can do without image if Your picture have only border
and background
of image isn't gradient. Then You can avoid MouseHover
and MouseLeave
.
编辑:
如果您不需要图片,则会有代码(按钮的边框始终为蓝色
2px
,背景颜色是某种浅棕色,悬停时背景颜色为红色,但边框保持蓝色,2px)。
Edit :There is code if You don't need image (border of button is always blue
of 2px
, background color is some kind of light brown, and on hover background color is red but border stay blue with 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
这篇关于按钮图像,透明度,MouseOver颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!