Produced picture button in VB.NET

xiaoxiao2021-03-06  15

Image button:

Idea: Very simple, just place a Button control on a PictureBox control, then add this button to PictureBox (make sure to drag the PictureBox, then drag but drop button), set this button background color (this time is relative to Picturebox Is transparent.

Imports system.componentmodel

Public Class PictureButton

Inherits System.Windows.Forms.userControl

#Region "Windows Form Designer Generated Code"

'UserControl overrides Dispose to clean the component list.

Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)

IF Disposing then

IF not (Components Is Nothing) THEN

Components.dispose ()

END IF

END IF

Mybase.dispose (Disposing)

End Sub

'Windows Form Designer

Private Components as System.comPonentModel.icontainer

'Note: The following process is necessary for the Windows Form Designer.

'You can modify this process using the Windows Form Designer.

'Don't modify it using the code editor.

Friend Withevents PictureBox1 As System.Windows.Forms.PictureBox

Friend Withevents Button1 As System.Windows.Forms.Button

private subinitializecomponent ()

Me.PictureBox1 = new system.windows.Forms.PictureBox ()

Me.Button1 = new system.windows.Forms.Button ()

Me.suspendlayout ()

'

'PictureBox1

'

Me.Picturebox1.name = "PictureBox1"

Me.Picturebox1.size = new system.drawing.size (136, 40)

Me.PictureBox1.tabindex = 0

Me.PictureBox1.tabstop = false

'

'Button1

'

Me.Button1.name = "button1"

Me.Button1.tabindex = 1

Me.Button1.text = "button1"

'

'PictureButton

'

Me.Controls.addrange (new system.windows.forms.control () {me.button1, me.picturebox1})

Me.Name = "PictureButton"

Me.ResumeLayout (false)

End Sub

#End region

Public Sub New ()

Mybase.new ()

'This call is required for the Windows Form Designer.

InitializeComponent ()

'Adding any initialization me.button1.width = 100' setting button after INITIALIZECOMPONENT ()

Me.button1.height = 23

Me.Button1.backcolor = color.transparent 'background color transparent

Me.Button1.Forecolor = color.black

Me.Picturebox1.controls.add (me.button1)

End Sub

Private m_text as string 'set button title

Private a as integer

'Private m_image as image

PUBLIC Property image () as image

Get

Return Me.Picturebox1.Image

END GET

Set (ByVal Value as Image)

Me.PictureBox1.Image = Value

Invalidate ()

End set

End Property

Shadows Property Forecolor () AS Color

Get

Return Me.Button1.ForeColor

END GET

SET (ByVal Value As Color)

Me.Button1.ForeColor = Value

Invalidate ()

End set

End Property

Shadows Sub ResetForeColor ()

Me.Button1.ForeColor = SystemColors.ControlText

End Sub

'

'Click event for the button

Event btnclick (Byval E AS Object, Byval E as System.EventArgs)

Private sub button1_click (byval e as system.eventargs) Handles Button1.click

RaiseEvent Btnclick (ME, E)

End Sub

'

'Control changes in size, need to redraw controls to make subtools

Private Sub FileTextBox_resize (Byval e as system.eventargs) Handles mybase.resize

RedrawControls ()

End Sub

The 'subfetero automatically continues the Font property of the container, so it is also necessary to redraw the control when changing the font properties of the container.

Protected Overrides Sub onFontChanged (Byval E AS System.EventArgs)

'Let the base control update text box

Mybase.onfontchanged (e)

'Redraw control

RedrawControls ()

End Sub

'Redraw control

Private Sub RedrawControls ()

'Control width

Dim width as integer = me.clientRectangle.width 'Get work area wide

'Determine the height of the control with the height of the button

Dim btnside as integer = button1.height

Dim btnwidth as integer = button1.width

If Me.ClientRectangle.height <> btnside Then

'Set the size of the control workspace

'Me.setClientSizecore (btnwidth, btnside) me.setClientSizecore (width, btnside)' This uses the width of the workspace because the button and Picturebox can adjust the width

'The above statement stimulates nested Resize events, so you need to quit immediately. If you do not quit, you will repeat the death cycle repeatedly.

EXIT SUB

END IF

'Adjust the size of the sub-control

'Txt.setbounds (0, 0, width, btnside)

'Btn.setbounds (Width - 19, 2, 17, btnside - 4)

Me.PictureBox1.setBounds (0, 0, Width, btnside)

Me.PictureBox1.SizeMode = PictureBoxSizeMode.stretchiMage

Me.button1.setbounds (0, 0, width, btnside)

End Sub

END CLASS

转载请注明原文地址:https://www.9cbs.com/read-47174.html

New Post(0)