Custom Control--XP Style button (you can set text color)

Source: Internet
Author: User
Tags integer relative
Buttons | controls

Imports System.Drawing

Imports System.ComponentModel

Public Class Winxpbutton

Inherits System.Windows.Forms.Button

Private My_mousedown as Boolean = False ' mouse down

Private my_mousehover as Boolean = False ' mouse move to top

Private M_textcolor as color = System.Drawing.Color.Black ' Font color

<description ("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 by the Windows Forms Designer.

InitializeComponent ()

' Add any initialization after the InitializeComponent () call, true to apply the specified style to the control

' Set control style bit to fully change control behavior

Me.setstyle (Controlstyles.userpaint, True)

' Associating event delegates

AddHandler Me.mousedown, AddressOf My_onmousedown

AddHandler Me.mouseup, AddressOf my_onmouseup

AddHandler Me.mouseenter, AddressOf my_onmouseenter

AddHandler Me.mouseleave, AddressOf my_onmouseleave

Height = 23

Width = 75

End Sub

Protected Overrides Sub OnPaint (ByVal pevent as System.Windows.Forms.PaintEventArgs)

' Pevent. ClipRectangle refers to the rectangle in which the rectangle is drawn, that is, using the background color of the parent control to draw this rectangular button

Pevent. Graphics.FillRectangle (New SolidBrush (Me.Parent.BackColor), pevent. ClipRectangle)

If (Enabled = False) Then

' Draw unavailable State

Drawdisablebutton (pevent. Graphics)

ElseIf (My_mousedown) Then ' Draw mouse down state

Drawmousedownbutton (pevent. Graphics)

ElseIf (my_mousehover) Then ' draw the mouse to move to its state

Drawmousehoverbutton (pevent. Graphics)

ElseIf (focused) Then ' has focus, but the mouse has not moved to it

Drawcontainfocusbutton (pevent. Graphics)

Else ' in general

Drawnormalbutton (pevent. Graphics)

End If

' Write text

WRITETEXT (pevent. Graphics)

End Sub

' Mouse down state processing

Private Sub My_onmousedown (ByVal sender as Object, ByVal e as MouseEventArgs)

My_mousedown = True ' mouse down

End Sub

' Mouse loose state of processing

Private Sub My_onmouseup (ByVal sender as Object, ByVal e as MouseEventArgs)

My_mousedown = False ' Mouse release

' The Paint event occurs when the control is redrawn. PaintEventArgs Specifies the Graphics used to draw the control

' And the cliprectangle where the control is drawn.

Dim pe as PaintEventArgs = New PaintEventArgs (creategraphics (), ClientRectangle)

OnPaint (PE)

End Sub

' Mouse into

Private Sub My_onmouseenter (ByVal sender as Object, ByVal e as EventArgs)

My_mousehover = True ' mouse moves over it

'

Dim pe as PaintEventArgs = New PaintEventArgs (creategraphics (), ClientRectangle)

OnPaint (PE)

End Sub

' Mouse Move Open

Private Sub My_onmouseleave (ByVal sender as Object, ByVal e as EventArgs)

My_mousehover = False ' mouse move Open

'

Dim pe as PaintEventArgs = New PaintEventArgs (creategraphics (), ClientRectangle)

OnPaint (PE)

End Sub

Private Sub Drawborder (ByVal g as Graphics, ByVal State as Integer)

If (state = 1) Then ' draws 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 the brush, followed by the first point of the coordinates, the second point of the coordinates

G.drawline (P, 1, 1, 1, Height-2) ' Draw left vertical bar

G.drawline (P, 1, 1, Width-2, 1) ' Draw above horizontal

G.drawline (P, Width-1, 2, Width-1, Height-2) ' draws the right vertical bar, since the horizontal line has been drawn (ordinate 1), so starting from 2

G.drawline (P, 2, Height-1, Width-2, Height-1) ' Draw the following horizontal line

ElseIf (state = 2) Then ' draw a border that moves to it

' With the general border with high light is the display of 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 a pressed display border

' With the general border with high light is the display of 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 ' draw an unavailable status border

' With the general border with high light is the display of 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 the focus but not the mouse

' With the general border with high light is the display of blue color

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

'//finish the processing as above and then use and cannot be used for round edge processing (that is, the 4 corners of the button for the round processing)

If (state = 4) Then ' is not available

' Using brushes, Color.FromArgb (161, 161, 146) is to create a system color 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 bar (except for two left-sided lines)

G.drawline (P, 2, 0, Width-3, 0) ' Top horizontal line (except for the remaining lines of two corners)

G.drawline (P, Width-1, 2, Width-1, Height-3) ' Right vertical bar (except for two left-sided lines)

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

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

G.drawline (p, 0, Height-3, 2, Height-1) ' lower 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) ' lower right corner

Else ' draw Normal style border

' Draw a corner with the default black

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 (ByVal g as Graphics)

' Draw Border, width is 1

Drawborder (g, 1)

' Draw the background, with the high light point color

Paintback (g, Systemcolors.controllightlight)

End Sub

' The mouse moves to the state on it

Private Sub Drawmousehoverbutton (ByVal g as Graphics)

Drawborder (g, 2)

Paintback (g, Systemcolors.controllightlight)

End Sub

Private Sub Drawmousedownbutton (ByVal g as Graphics)

Drawborder (g, 3)

' Draw the background, with the bright colors of the 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)

' High light point

Paintback (g, Systemcolors.controllightlight)

End Sub

' Draw background color

Private Sub Paintback (ByVal g as Graphics, ByVal C as Color)

' Fill with: monochrome brush, relative to (0,0) coordinates (3,3) position, size is wide-6, high-6

G.fillrectangle (New SolidBrush (c), 3, 3, Width-6, Height-6)

End Sub

' Write text

Private Sub WriteText (ByVal g as Graphics)

' Calculate the position of the text

Dim x as Integer = 0

Dim y as Integer = 0

' Size is a rectangular area with a wide-high ordered pair

Dim s as Size = G.measurestring (Text, Font). Tosize ()

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

y = (height-s.height)/2 ' text relative to control y offset

' Write text

if (Enabled) Then ' If the control is available, the 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, the gray text

g.DrawString (Text, Font, Brushes.gray, X, y)

End If

End Sub

End Class



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.