📄 messagebox.vb
字号:
Option Explicit On
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
''' <summary>
''' Name :Gehan Fernando.
''' Date : 06th Nov 2007
''' Version : 2.0.1
''' About WinMessage
''' ###################################################################
''' # Win Message Is Customized Glass Message Box For Real World
''' # Applications.
''' ###################################################################
'''
''' About Author
''' ###################################################################
''' # Software Engineer In John Keells Computer Services (Pvt) Ltd
''' # After 3 Visual Basic Experice, Myself Converted To VB.Net
''' # Most Of Time Myself Doing RND, Database Programming And
''' # Network Programming.
''' # Contact Details 0094777582778 , 0094112524596 , 0094112300770
''' # Most Of Codes U Can Download From
''' # www.vbcode.com / www.a1vbcode.com
''' #
''' # I Hope U Enjoy With This Code .......
''' #
''' ###################################################################
''' </summary>
''' <remarks></remarks>
'''
Public Class GlassBox
#Region "Message Variables ..."
Private Shared ObjWinMessage As GlassBox
Private Shared WithEvents CmdOk As Button
Private Shared WithEvents CmdYes As Button
Private Shared WithEvents CmdNo As Button
Private Shared WithEvents CmdCancel As Button
Private Shared WithEvents CmdAbort As Button
Private Shared WithEvents CmdRetry As Button
Private Shared WithEvents CmdIgnore As Button
Private Shared WithEvents WinMsg As Form
Private Shared WithEvents LblHeader As Label
Private Shared PicIcon As PictureBox
Private Shared WithEvents WinMessage As Label
Private Shared MaxWidth As Integer = SystemInformation.WorkingArea.Width * 0.8
Private Shared MaxHeight As Integer = SystemInformation.WorkingArea.Height
Private Shared FormWidth As Integer = 0
Private Shared FormHeight As Integer = 0
Private Shared HeaderWidth As Integer = 0
Private Shared MsgWidth As Integer = 0
Private Shared MsgHeight As Integer = 0
Private Shared WinReturn As Integer = 0
Private Shared WinFnt As New Font("Consolas", 9, FontStyle.Regular)
Private Shared WinLocation As Point
Private Shared FormSize As Size
Private Shared MessageSize As Size
Private Shared WinButtons As MessageBoxButtons
Private Shared WinDefault As MessageBoxDefaultButton
Private Shared WinResult As DialogResult
Private Shared WinMake As DialogResult
Private Shared MessageText As String = ""
Private Shared HeaderText As String = ""
#End Region
#Region "Class Constructor"
Private Sub New()
WinMsg = New Form()
End Sub
#End Region
''' <summary>
''' Win Message Box :- Display Body Message, Header Message, Message Icon, Message Buttons With Default Focus
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ShowMessage(ByVal WinText As String, Optional ByVal WinHeader As String = "", _
Optional ByVal WinIcon As MessageBoxIcon = MessageBoxIcon.None, _
Optional ByVal WinButtons As MessageBoxButtons = MessageBoxButtons.OK, _
Optional ByVal WinDefault As MessageBoxDefaultButton = MessageBoxDefaultButton.Button1) As DialogResult
WinResult = MakeMessage(WinText, WinHeader, WinIcon, WinButtons, WinDefault)
Return WinResult
End Function
Private Shared Function MakeMessage(ByVal WinText As String, Optional ByVal WinHeader As String = "", _
Optional ByVal WinIcon As MessageBoxIcon = MessageBoxIcon.None, Optional ByVal WinButtons As MessageBoxButtons = MessageBoxButtons.OK, _
Optional ByVal WinDefault As MessageBoxDefaultButton = MessageBoxDefaultButton.Button1) As DialogResult
ObjWinMessage = Nothing
ObjWinMessage = New GlassBox()
MessageText = "" : MessageText = WinText
HeaderText = "" : HeaderText = WinHeader
FormWidth = 0 : FormHeight = 0
HeaderWidth = 0
MsgWidth = 0 : MsgHeight = 0
WinReturn = 0
REM Check Message And Header Text Length When Equal To Zero
If MessageText.Trim().Length = 0 And HeaderText.Trim().Length = 0 Then
FormSize = New Size(305, 135)
FormWidth = 305 : FormHeight = 135
WinMsg.Size = New Size(FormSize.Width, FormSize.Height)
GoTo Mess
End If
HeaderWidth = StringSize(HeaderText.Trim(), MaxWidth, WinFnt).Width
MessageSize = StringSize(MessageText.Trim(), MaxWidth, WinFnt)
MsgWidth = MessageSize.Width : MsgHeight = MessageSize.Height
If HeaderText.Trim().Length > 0 And MessageText.Trim().Length = 0 Then
HeaderWidth = HeaderWidth + 80
WinReturn = Math.Max(HeaderWidth, 305)
FormSize = New Size(WinReturn, 135)
FormWidth = FormSize.Width : FormHeight = FormSize.Height
GoTo Mess
End If
HeaderWidth = HeaderWidth + 80
FormWidth = MsgWidth + 60
FormHeight = MsgHeight + 120
If HeaderText.Trim().Length = 0 And MessageText.Trim().Length > 0 Then
FormWidth = Math.Max(FormWidth, 305)
FormHeight = Math.Max(FormHeight, 135)
FormSize = New Size(FormWidth, FormHeight)
FormWidth = FormSize.Width : FormHeight = FormSize.Height
GoTo Mess
End If
If HeaderText.Trim().Length > 0 And MessageText.Trim().Length > 0 Then
WinReturn = Math.Max(HeaderWidth, FormWidth)
FormWidth = Math.Max(WinReturn, 305)
FormHeight = Math.Max(FormHeight, 135)
FormSize = New Size(FormWidth, FormHeight)
FormWidth = FormSize.Width : FormHeight = FormSize.Height
GoTo Mess
End If
Mess:
Call CreateBaseScreen()
Call CreateHeader()
Call CreateMessageIcon()
Call CreateMeaasge()
Call AddToBaseScreen()
WinMessage.Text = WinText
LblHeader.Text = WinHeader
Select Case WinIcon
Case MessageBoxIcon.Asterisk
PicIcon.Image = Drawing.SystemIcons.Asterisk.ToBitmap()
Case MessageBoxIcon.Error
PicIcon.Image = Drawing.SystemIcons.Error.ToBitmap()
Case MessageBoxIcon.Exclamation
PicIcon.Image = Drawing.SystemIcons.Exclamation.ToBitmap()
Case MessageBoxIcon.Hand
PicIcon.Image = Drawing.SystemIcons.Hand.ToBitmap()
Case MessageBoxIcon.Information
PicIcon.Image = Drawing.SystemIcons.Information.ToBitmap()
Case MessageBoxIcon.None
PicIcon.Image = Nothing
Case MessageBoxIcon.Question
PicIcon.Image = Drawing.SystemIcons.Question.ToBitmap()
Case MessageBoxIcon.Stop
PicIcon.Image = Drawing.SystemIcons.Error.ToBitmap()
Case MessageBoxIcon.Warning
PicIcon.Image = Drawing.SystemIcons.Warning.ToBitmap()
End Select
Call CreateMessageButtons(WinButtons)
Select Case WinButtons
Case MessageBoxButtons.AbortRetryIgnore
Select Case WinDefault
Case MessageBoxDefaultButton.Button1
CmdAbort.Select()
CmdAbort.Focus()
Case MessageBoxDefaultButton.Button2
CmdRetry.Select()
CmdRetry.Focus()
Case MessageBoxDefaultButton.Button3
CmdIgnore.Select()
CmdIgnore.Focus()
End Select
Case MessageBoxButtons.OK
Select Case WinDefault
Case MessageBoxDefaultButton.Button1
CmdOk.Select()
CmdOk.Focus()
Case MessageBoxDefaultButton.Button2
CmdOk.Select()
CmdOk.Focus()
Case MessageBoxDefaultButton.Button3
CmdOk.Select()
CmdOk.Focus()
End Select
Case MessageBoxButtons.OKCancel
Select Case WinDefault
Case MessageBoxDefaultButton.Button1
CmdOk.Select()
CmdOk.Focus()
Case MessageBoxDefaultButton.Button2
CmdCancel.Select()
CmdCancel.Focus()
Case MessageBoxDefaultButton.Button3
CmdCancel.Select()
CmdCancel.Focus()
End Select
Case MessageBoxButtons.RetryCancel
Select Case WinDefault
Case MessageBoxDefaultButton.Button1
CmdRetry.Select()
CmdRetry.Focus()
Case MessageBoxDefaultButton.Button2
CmdCancel.Select()
CmdCancel.Focus()
Case MessageBoxDefaultButton.Button3
CmdCancel.Select()
CmdCancel.Focus()
End Select
Case MessageBoxButtons.YesNo
Select Case WinDefault
Case MessageBoxDefaultButton.Button1
CmdYes.Select()
CmdYes.Focus()
Case MessageBoxDefaultButton.Button2
CmdNo.Select()
CmdNo.Focus()
Case MessageBoxDefaultButton.Button3
CmdNo.Select()
CmdNo.Focus()
End Select
Case MessageBoxButtons.YesNoCancel
Select Case WinDefault
Case MessageBoxDefaultButton.Button1
CmdYes.Select()
CmdYes.Focus()
Case MessageBoxDefaultButton.Button2
CmdNo.Select()
CmdNo.Focus()
Case MessageBoxDefaultButton.Button3
CmdCancel.Select()
CmdCancel.Focus()
End Select
End Select
WinMsg.ShowDialog()
Return WinMake
End Function
Private Shared Sub CreateBaseScreen()
With WinMsg
.Text = ""
.Size = New Size(FormWidth, FormHeight)
.StartPosition = FormStartPosition.CenterParent
.FormBorderStyle = FormBorderStyle.None
.ShowInTaskbar = False : .ShowIcon = False
.Opacity = 0.85
.Font = WinFnt
End With
End Sub
Private Shared Sub CreateHeader()
LblHeader = New Label()
With LblHeader
.Text = ""
.AutoSize = False
.Dock = DockStyle.Top
.BackColor = Color.Transparent
.TextAlign = ContentAlignment.MiddleLeft
.Height = 24
.Font = WinFnt
.SendToBack()
.Visible = True
End With
End Sub
Private Shared Sub CreateMessageIcon()
PicIcon = New PictureBox()
With PicIcon
.Size = New Size(35, 35)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -