有人可以帮助我,如何在VB.NET中创建像那样的按钮/控件..我的意思是,当我指向光标或点击控件时,如何创建/使用样式,样式就像透明一样。那种风格与按钮风格不同..请帮助我..我到处都搜索但没有。
之前的谢谢这是图片:
答案 0 :(得分:0)
如果你想完成你想要的东西,看看这个例子,它可以帮助你解决你遇到的任何问题:
Sub Main()
Dim myButton As Button = New Button()
myButton.Text = "Hello World"
myButton.BackColor = Color.Blue
myButton.FlatAppearance.BorderColor = Color.LightBlue
myButton.FlatAppearance.BorderSize = 2
AddHandler myButton.Click, AddressOf Me.OnMyButtonClick
AddHandler myButton.MouseHover, AddressOf Me.OnMyButtonEnter
AddHandler myButton.MouseLeave, AddressOf Me.OnMyButtonLeave
Panel1.Controls.Add(myButton)
End Sub
正如您所看到的,我创建了一个新按钮,并且可以修改您正在查找的按钮的所有属性:背景颜色,边框颜色和边框宽度,甚至是文本或图像等内容。现在,您需要创建将执行您要完成的操作的事件:
Private Sub OnMyButtonClick(sender As Object, e As EventArgs)
Dim currentButton As Button = CType(sender, Button)
currentButton.Text = "I have been clicked!"
currentButton.BackColor = Color.LightBlue
currentButton.FlatAppearance.BorderColor = Color.Blue
currentButton.FlatAppearance.BorderSize = 1
End Sub
Private Sub OnMyButtonEnter(sender As Object, e As EventArgs)
Dim currentButton As Button = CType(sender, Button)
currentButton.BackColor = Color.LightGreen
End Sub
Private Sub OnMyButtonLeave(sender As Object, e As EventArgs)
Dim currentButton As Button = CType(sender, Button)
currentButton.BackColor = Color.Blue
End Sub
正如您所看到的,我们已经定义了您想要捕获鼠标单击,鼠标悬停和鼠标离开的3个事件,并且在每个事件中我们可以根据需要修改按钮。从这里你可以创建一个监视按钮颜色的全局参数,或者你可以监视状态,从那里你可以知道你想要做什么。以鼠标离开事件为例。
你表明你想要创建一个边框,但是你可以看到一个白色背景,你只需要做的就是:
myButton.BackColor = Color.White
myButton.FlatAppearance.BorderColor = Color.LightBlue
这很容易,但有一个非常重要的部分你需要知道。如果您希望按钮实际执行您创建的事件,则必须将它们附加到按钮。让我们看看这个:
AddHandler myButton.Click, AddressOf Me.OnMyButtonClick
AddHandler myButton.MouseHover, AddressOf Me.OnMyButtonEnter
AddHandler myButton.MouseLeave, AddressOf Me.OnMyButtonLeave
从上面的代码中可以看出,您可以指示程序在发生特定事件时如何执行操作,例如鼠标单击。与事件处理程序相关的一个重要部分是在创建后附加它们,以便它们立即执行操作。这就是为什么我在将按钮添加到Panel1之前将事件处理程序的附件放在Main()中。
答案 1 :(得分:0)
让我们试试这个:
将您的图片导入资源。
您将需要3个不同的图像:正常,悬停和单击。将其更改为您的图像名称:CustomButtonN,CustomButtonH和CustomButtonC。
添加任何名称的类。然后添加以下代码:
Inherits Windows.Forms.Button 'To make this class as button command
Public Sub New()
Me.FlatStyle = Windows.Forms.FlatStyle.Flat 'Make it flat
Me.FlatAppearance.BorderSize = 0 'With borderless
Me.FlatAppearance.MouseDownBackColor = Color.Transparent 'You know this
Me.FlatAppearance.MouseOverBackColor = Color.Transparent 'And this
Me.BackColor = Color.Transparent 'Transparent key
Me.ForeColor = Color.Black 'Text color
Me.Width = 32 'Button width
Me.Height = 32 'Button height
Me.BackgroundImage = My.Resources.CustomButtonN 'Normal
Me.BackgroundImageLayout = ImageLayout.Stretch 'To stretch the image
End Sub
Private Sub CustomButton_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles Me.MouseDown
Me.BackgroundImage = My.Resources.CustomButtonC 'Click event
End Sub
Private Sub CustomButton_MouseHover(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.MouseHover
Me.BackgroundImage = My.Resources.MiniButtonH 'Hover event
End Sub
Private Sub CustomButton_MouseLeave(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.MouseLeave
Me.BackgroundImage = My.Resources.CustomButtonN 'Leave even
End Sub
Private Sub CustomButton_MouseUp(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles Me.MouseUp
Me.BackgroundImage = My.Resources.CustomButtonH 'Click release
End Sub
注意:您必须在添加课程或编辑后编译项目:
按 F5 启动它然后等待3秒钟,然后停止它。
最后看到您的工具箱面板,点击您的按钮名称,然后将其放入您的表单。