User Control - (XP Style Button)

xiaoxiao2021-03-06  22

Imports system.drawing

Imports system.componentmodel

Public Class WinxpButton

Inherits System.Windows.Forms.Button

PRIVATE MY_MOUSEDOWN AS BOOLEAN = false 'mouse Press

Private my_mousehover as boolean = false 'mouse moves to top

Private m_textcolor as color = system.drawing.color.black 'font color

Public property TextColor () AS Color

Get

Return M_TextColor

END GET

SET (ByVal Value As Color)

m_textColor = Value

Me.invalidate ()

End set

End Property

Public Sub New ()

Mybase.new ()

'This call is required for the Windows Form Designer.

InitializeComponent ()

'Add any initialization after the initializeComponent () call, True means applied to the specified style to the control

'Setting the control style bits to fully change the control behavior

Me.setStyle (ControlStyles.Userpaint, true)

'Related events

AddHandler Me.Mousedown, Addressof my_onmousedown

AddHandler Me.Mouseup, Addressof my_onmouseup

AddHandler Me.MouseEnter, Addressof my_onmounter

AddHandler Me.MouseLeave, Addressof my_onmouselave

HEIGHT = 23

Width = 75

End Sub

Protected Overrides Sub onpaint (Byval PEVENT As System.Windows.Forms.PainteventArgs)

'pevent.clipRectangle refers to the rectangle drawn, even if the background color of the parent control is used to draw this rectangle button.

PEVENT.GRAPHICS.FILLRECTANGLE (New Solidbrush (Me.Parent.BackColor), Pevent.ClipRectangle

IF (enabled = false) THEN

'Painting is not available

DrawDisableButton (PEVENT.GRAPHICS)

Elseif (My_MOUSEDOWN) THEN 'Plus Mouse Press Status

DrawMousedownButton (Pevent.graphics)

Elseif (my_mousehover) THEN 'Plus mouse moves to it

DrawMousehoverButton (PEVENT.GRAPHICS)

Elseif (Focused) Then 'is focused, but the mouse is not moved.

DrawContainfocusButton (Pevent.graphics)

ELSE '

DrawnormalButton (Pevent.graphics)

END IF

'Write text

WriteText (pevent.graphics) '// Write Text

End Sub

'The state of the mouse is processed

PRIVATE SUB MY_OMOUSEDOWN (Byval E AS Object, Byval E ASEEVENTARGS) MY_MOUSEDOWN = True 'Mouse Press

End Sub

'Treatment of the mouse loose state

Private sub my_onmouseup (Byval E AS Object, Byval E ASEEventArgs)

MY_MOUSEDOWN = false 'mouse loose

'A PAINT event occurs when re-drawing the control. Painteventargs Specifies the graphics for drawing controls

'And the CLIPRECTANGLE where the control is placed.

Dim pe as painteventargs = new painteventargs (cretegraphics (), clientRectangle

OnPaint (PE)

End Sub

'Mouse entry

Private sub my_onmouseenter (Byval E AS Object, Byval E As Eventargs)

MY_MOUSEHOVER = TRUE 'mouse moves to it

'

Dim pe as painteventargs = new painteventargs (cretegraphics (), clientRectangle

OnPaint (PE)

End Sub

'Mouse moving

Private sub my_onmouselave (Byval E AS Object, ByVal E as Eventargs)

MY_MOUSEHOVER = FALSE 'mouse movement

'

Dim pe as painteventargs = new painteventargs (cretegraphics (), clientRectangle

OnPaint (PE)

End Sub

Private Sub Drawborder (Byval G AS Graphics, ByVal State As Integer)

IF (State = 1) Then 'Draw a general border

'Draw a brush, high light point, width 2

DIM P as pen = new pen (SystemColors.Controllightlight, 2)

'g.drawline line, P is a brush, followed by the first point coordinate, the second point coordinate

g.drawline (p, 1, 1, 1, height - 2) 'Draw the left vertical line

g.drawline (p, 1, 1, width - 2, 1) 'Draw the upper horizontal line

g.drawline (p, width - 1, 2, width - 1, height - 2) 'Draw the right vertical line, since the horizontal lines have been drawn above, starting from 2

g.drawline (p, 2, height - 1, width - 2, height - 1) 'Draw the underlying horizontal line

Elseif (State = 2) Then 'Draws the border moved to it

'Difference with a general border is displayed in yellow

DIM P as pen = new pen (color.yellow, 2)

g.drawline (P, 1, 1, 1, HEIGHT - 2)

g.drawline (p, 1, 1, width - 2, 1)

g.drawline (p, width - 1, 2, width - 1, Height - 2)

g.drawline (P, 2, Height - 1, Width - 2, Height - 1) Elseif (state = 3) Then 'Draw Press Display Border

'The highlight of the general border is displayed on display dark brown

DIM P as pen = new pen (SystemColors.ControlDark, 2)

g.drawline (P, 1, 1, 1, HEIGHT - 2)

g.drawline (p, 1, 1, width - 2, 1)

g.drawline (p, width - 1, 2, width - 1, Height - 2)

g.drawline (P, 2, Height - 1, Width - 2, Height - 1)

Elseif (State = 4) Then 'Draws Unavailable State Borders

'The highlight of the general border is display bright colors.

DIM P as pen = new pen (SystemColors.ControlLight, 2)

g.drawline (P, 1, 1, 1, HEIGHT - 2)

g.drawline (p, 1, 1, width - 2, 1)

g.drawline (p, width - 1, 2, width - 1, Height - 2)

g.drawline (P, 2, Height - 1, Width - 2, Height - 1)

Elseif (State = 5) Then 'Draws a state of focus but the mouse is not on it.

'Different from a general border with a high-light

DIM P as pen = new pen (color.skyblue, 2)

g.drawline (P, 1, 1, 1, HEIGHT - 2)

g.drawline (p, 1, 1, width - 2, 1)

g.drawline (p, width - 1, 2, width - 1, Height - 2)

g.drawline (P, 2, Height - 1, Width - 2, Height - 1)

END IF

'// After the processing, the above processing is done, and the rooted edge processing (that is, the 4 angles of the button is added)

IF (state = 4) THEN 'is not available

'Using the brush, Color.Fromargb (161, 161, 146) is created from a 32-bit Argb value, with a width of 1

DIM P as pen = new pen (color.fromargb (161, 161, 146), 1)

g.drawline (p, 0, 2, 0, height - 3) 'left vertical line (except for the remaining lines left)

g.drawline (p, 2, 0, width - 3, 0) 'upper horizontal line (except for the remaining lines left)

G. Drawline (p, width - 1, 2, width - 1, height - 3) 'right vertical line (except for the remaining lines left)

G. Drawline (P, 2, Height - 1, Width - 3, Height - 1) 'below the horizontal line

g.drawline (p, 0, 2, 2, 0) 'left upper corner

g.drawline (p, 0, height - 3, 2, heiGHT - 1) 'Left corner

g.drawline (p, width - 3, 0, width - 1, 2) 'upper right corner

g.drawline (p, width - 3, height - 1, width - 1, height - 3) 'upper right corner

Else 'DRAW NORMAL STYLE BORDER

'Use the default black to draw the corner G. Drawline (Pens.Black, 0, 2, 0, Height - 3)

g.drawline (Pens.Black, 2, 0, Width - 3, 0)

g.drawline (Pens.Black, Width - 1, 2, Width - 1, Height - 3)

g.drawline (Pens.Black, 2, Height - 1, Width - 3, Height - 1)

g.drawline (Pens.Black, 0, 2, 2, 0)

g.drawline (Pens.black, 0, Height - 3, 2, Height - 1)

g.drawline (Pens.Black, Width - 3, 0, Width - 1, 2)

G. Drawline (Pens.Black, Width - 3, Height - 1, Width - 1, Height - 3)

END IF

End Sub

'General state

Private Sub DrawnormalButton (Byvalg as graphics)

'Draw the border, the width is 1

Drawborder (G, 1)

'Draw background, color point color

Paintback (G, SystemColors.ControlLightlight)

End Sub

The state of the mouse moves to it

Private Sub DrawMousehoverButton (Byval g as graphics)

Drawborder (G, 2)

Paintback (G, SystemColors.ControlLightlight)

End Sub

Private Sub DrawMouseDownButton (Byvalg as graphics)

Drawborder (G, 3)

'Draw background, bright colors of three-dimensional elements

Paintback (G, SystemColors.ControlLight)

End Sub

Private Sub DrawDisableButton (Byval G AS Graphics)

Drawborder (G, 4)

'Bright colors

Paintback (G, SystemColors.ControlLight)

End Sub

Private Sub DrawContainfocusButton (Byval g as graphics)

Drawborder (G, 5)

'Highlight

Paintback (G, SystemColors.ControlLightlight)

End Sub

'Draw background color

Private Sub Paintback (Byvalg AS Graphics, Byval C As Color)

'Fill in: Monochrome painting, relative to (0) coordinates (3, 3) position, size is width -6, high -6

G.FillRectangle (New Solidbrush (C), 3, 3, Width - 6, Height - 6)

End Sub

'Write text

Private Sub WriteText (Byvalg AS Graphics)

'Location of the text

DIM X as integer = 0

DIM Y as integer = 0

'size is used as a wide orderly to the rectangular area

DIM S as size = g.MeasureString (text, font) .tosize ()

X = (width - s.width) / 2 'text relative control X offset

Y = (Height - S.HEight) / 2 'text relative control Y offset' Write text

IF (enabled), if the control is available, black text

'g.drawstring (Text, Font, Brushes.black, x, y)

DIM B AS New Solidbrush (M_TextColor)

g.drawstring (Text, Font, B, X, Y)

Else 'If the control is not available, gray text

g.drawstring (Text, Font, Brushes.gray, x, y)

END IF

End Sub

END CLASS

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

New Post(0)