الاخوه الأفاضل
الكلاس التالي عبتره عن كلاس الماسج بوكس وهو يعتبر بديلا للماسج بوكس الموجود في الفيجوال استوديو أتمني ان يكون مفيدا للزملاء
Public NotInheritable Class MsgBoxGhost
#Region "Enums"
Public Enum MessageBoxButtons
OK = 0
OKCancel = 1
AbortRetryIgnore = 2
YesNoCancel = 3
YesNo = 4
RetryCancel = 5
End Enum
Public Enum MessageBoxIcon
None = &H0
[Error] = &H10
Hand = &H10
[Stop] = &H10
Question = &H20
Exclamation = &H30
Warning = &H30
Asterisk = &H40
Information = &H40
[Exit] = &H50
Globe = &H60
UACshield = &H70
Keys = &H80
Camera = &H90
Cut = &H100
NetWork = &H110
Mouse = &H120
Search = &H130
SecurityLock = &H140
End Enum
_
Public Enum MessageBoxOptions
DefaultDesktopOnly = &H20000
RightAlign = &H80000
RtlReading = &H100000
ServiceNotification = &H200000
End Enum
Public Enum MessageBoxDefaultButton
Button1 = 0
Button2 = &H100
Button3 = &H200
End Enum
#End Region
#Region "Property"
Public Shared MsgBoxTxtColor As Color = Color.FromArgb(255, Color.Black)
Public Shared Property TextColor() As Color
Get
Return MsgBoxTxtColor
End Get
Set(ByVal value As Color)
If (value <> (MsgBoxTxtColor)) Then
MsgBoxTxtColor = value
End If
End Set
End Property
#End Region
#Region "Constructor"
Private Sub New()
End Sub
#End Region
#Region "Private MessageBoxForm Class"
Private Class MessageBoxForm
Inherits Form
#Region "Variables"
Private Const space_border As Integer = 15
Private Const space_image_text As Integer = 10
Private msgbox_text As String
Private size_known As Boolean = False
Private m_image As Image
Private textleft_up As Point
Private msgbox_buttons As MessageBoxButtons
Private msgbox_default As MessageBoxDefaultButton
Private buttons_placed As Boolean = False
Private button_left As Integer
Private buttons As Button() = New Button(2) {}
#End Region
#Region "MessageBoxForm Constructors"
Public Sub New(ByVal own As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon)
Select Case icon
Case MessageBoxIcon.Asterisk
If True Then
m_image = My.Resources.info
Exit Select
End If
Case MessageBoxIcon.Error
If True Then
m_image = My.Resources._error
Exit Select
End If
Case MessageBoxIcon.Exclamation
If True Then
m_image = My.Resources.info
Exit Select
End If
Case MessageBoxIcon.Hand
If True Then
m_image = My.Resources.Warning
Exit Select
End If
Case MessageBoxIcon.Information
If True Then
m_image = My.Resources.info
Exit Select
End If
Case MessageBoxIcon.None
If True Then
m_image = Nothing
Exit Select
End If
Case MessageBoxIcon.Question
If True Then
m_image = My.Resources.help
Exit Select
End If
Case MessageBoxIcon.Stop
If True Then
m_image = My.Resources.Warning
Exit Select
End If
Case MessageBoxIcon.Warning
If True Then
m_image = My.Resources.Warning
Exit Select
End If
Case MessageBoxIcon.Exit
If True Then
m_image = My.Resources._error
Exit Select
End If
Case MessageBoxIcon.Globe
If True Then
m_image = My.Resources._1403_Globe
Exit Select
End If
Case MessageBoxIcon.UACshield
If True Then
m_image = My.Resources.UAC_shield
Exit Select
End If
Case MessageBoxIcon.Keys
If True Then
m_image = My.Resources.Keys
Exit Select
End If
Case MessageBoxIcon.Camera
If True Then
m_image = My.Resources.Camera
Exit Select
End If
Case MessageBoxIcon.Cut
If True Then
m_image = My.Resources.cut
Exit Select
End If
Case MessageBoxIcon.NetWork
If True Then
m_image = My.Resources.Network
Exit Select
End If
Case MessageBoxIcon.Mouse
If True Then
m_image = My.Resources.Mouse
Exit Select
End If
Case MessageBoxIcon.Search
If True Then
m_image = My.Resources.search
Exit Select
End If
Case MessageBoxIcon.SecurityLock
If True Then
m_image = My.Resources.SecurityLock
Exit Select
End If
End Select
msgbox_text = text
msgbox_buttons = buttons
msgbox_default = MessageBoxDefaultButton.Button1
If own IsNot Nothing Then
Owner = Control.FromHandle(own.Handle).FindForm()
End If
Me.Text = caption
End Sub
Public Sub New(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton, ByVal options As MessageBoxOptions)
Me.New(owner, text, caption, buttons, icon)
msgbox_default = defaultButton
End Sub
#End Region
#Region "Methods"
Private Sub PopulateForm()
Dim tb_width As Integer = 0
Dim g As Graphics = Me.CreateGraphics
' First we have to know the size of text + image
Dim tsize As Drawing.SizeF = g.MeasureString(msgbox_text, Me.Font)
If m_image IsNot Nothing Then
tsize.Width += m_image.Width + 10
If m_image.Height > tsize.Height Then
' Place text middle-right
textleft_up = New Point(m_image.Width + space_image_text + space_border, _
CInt(((m_image.Height / 2) - (tsize.Height / 2))) + space_border)
Else
textleft_up = New Point(m_image.Width + space_image_text + space_border, 2 + space_border)
End If
If tsize.Height < m_image.Height Then
tsize.Height = m_image.Height
End If
Else
tsize.Width += space_border * 2
textleft_up = New Point(space_border + 12, space_border + 12)
tsize.Height += space_border * 2
End If
Select Case msgbox_buttons
Case MessageBoxButtons.AbortRetryIgnore
If True Then
tb_width = 75 * 3
Exit Select
End If
Case MessageBoxButtons.OK
If True Then
tb_width = 75 * 1
Exit Select
End If
Case MessageBoxButtons.OKCancel
If True Then
tb_width = 75 * 2
Exit Select
End If
Case MessageBoxButtons.RetryCancel
If True Then
tb_width = 75 * 2
Exit Select
End If
Case MessageBoxButtons.YesNo
If True Then
tb_width = 75 * 2
Exit Select
End If
Case MessageBoxButtons.YesNoCancel
If True Then
tb_width = 75 * 3
Exit Select
End If
End Select
' Now we choose the good size for the form
If tsize.ToSize().Width > tb_width Then
Me.ClientSize = New Size(tsize.ToSize().Width + 10 + (space_border * 2), WindowHelper(Height, tsize.ToSize().Height + 40 + (space_border * 2)))
Else
Me.ClientSize = New Size(tb_width + 10 + (space_border * 2), WindowHelper(Height, tsize.ToSize().Height + 40 + (space_border * 2)))
End If
' set the left of the buttons
button_left = (Me.ClientSize.Width / 2) - (tb_width / 2) + 5
AddButtons()
size_known = True
' implement defaultButton and options
Select Case msgbox_default
Case MessageBoxDefaultButton.Button1
If True Then
AcceptButton = Me.buttons(0)
Exit Select
End If
Case MessageBoxDefaultButton.Button2
If True Then
If Me.buttons(1) IsNot Nothing Then
AcceptButton = Me.buttons(1)
End If
Exit Select
End If
Case MessageBoxDefaultButton.Button3
If True Then
If Me.buttons(2) IsNot Nothing Then
AcceptButton = Me.buttons(2)
End If
Exit Select
End If
End Select
End Sub
#End Region
#Region "Protected Overrides Methods"
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim m_brush As New SolidBrush(TextColor)
e.Graphics.DrawString(msgbox_text, Me.Font, m_brush, textleft_up)
If m_image IsNot Nothing Then
e.Graphics.DrawImage(m_image, New Point(space_border, space_border))
End If
End Sub
#End Region
#Region "Function"
Private Shared Function WindowHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Public Function IntializeDialog() As DialogResult
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
Me.MinimizeBox = False
Me.MaximizeBox = False
Me.StartPosition = FormStartPosition.CenterScreen
Dim ico As Icon = My.Resources.Monitor
Me.Icon = ico
If size_known = False Then
PopulateForm()
End If
Me.ShowDialog()
Return Me.DialogResult
End Function
#End Region
#Region "Button click handlers"
Private Sub OkClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
Private Sub CancelClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.Cancel
Me.Close()
End Sub
Private Sub AbortClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.Abort
Me.Close()
End Sub
Private Sub RetryClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.Retry
Me.Close()
End Sub
Private Sub IgnoreClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.Ignore
Me.Close()
End Sub
Private Sub YesClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.Yes
Me.Close()
End Sub
Private Sub NoClick(ByVal sender As Object, ByVal e As EventArgs)
Me.DialogResult = DialogResult.No
Me.Close()
End Sub
#End Region
#Region "Functions for Adding buttons"
Private Sub AddButtons()
If Not buttons_placed Then
Select Case msgbox_buttons
Case MessageBoxButtons.OK
If True Then
buttons(0) = AddOkButton(0 + button_left)
Exit Select
End If
Case MessageBoxButtons.OKCancel
If True Then
buttons(0) = AddOkButton(0 + button_left)
buttons(1) = AddCancelButton(75 + button_left)
Exit Select
End If
Case MessageBoxButtons.AbortRetryIgnore
If True Then
buttons(0) = AddAbortButton(0 + button_left)
buttons(1) = AddRetryButton(75 + button_left)
buttons(2) = AddIgnoreButton(150 + button_left)
Exit Select
End If
Case MessageBoxButtons.YesNoCancel
If True Then
buttons(0) = AddYesButton(0 + button_left)
buttons(1) = AddNoButton(75 + button_left)
buttons(2) = AddCancelButton(150 + button_left)
Exit Select
End If
Case MessageBoxButtons.YesNo
If True Then
buttons(0) = AddYesButton(0 + button_left)
buttons(1) = AddNoButton(75 + button_left)
Exit Select
End If
Case MessageBoxButtons.RetryCancel
If True Then
buttons(0) = AddRetryButton(0 + button_left)
buttons(1) = AddCancelButton(75 + button_left)
Exit Select
End If
End Select
buttons_placed = True
End If
End Sub
Private Function AddOkButton(ByVal left As Integer) As Button
Dim OkButton As New Button()
OkButton.Text = "OK"
OkButton.Width = 67
OkButton.Height = 25
OkButton.Top = Me.ClientSize.Height - 35 - space_border
OkButton.Left = left
AddHandler OkButton.Click, AddressOf OkClick
AcceptButton = OkButton
Me.Controls.Add(OkButton)
Return OkButton
End Function
Private Function AddCancelButton(ByVal left As Integer) As Button
Dim CancelButton As New Button()
CancelButton.Text = "Cancel"
CancelButton.Width = 67
CancelButton.Height = 25
CancelButton.Top = Me.ClientSize.Height - 35 - space_border
CancelButton.Left = left
AddHandler CancelButton.Click, AddressOf CancelClick
CancelButton = CancelButton
Me.Controls.Add(CancelButton)
Return CancelButton
End Function
Private Function AddAbortButton(ByVal left As Integer) As Button
Dim AbortButton As New Button()
AbortButton.Text = "Abort"
AbortButton.Width = 67
AbortButton.Height = 25
AbortButton.Top = Me.ClientSize.Height - 35 - space_border
AbortButton.Left = left
AddHandler AbortButton.Click, AddressOf AbortClick
CancelButton = AbortButton
Me.Controls.Add(AbortButton)
Return AbortButton
End Function
Private Function AddRetryButton(ByVal left As Integer) As Button
Dim RetryButton As New Button()
RetryButton.Text = "Retry"
RetryButton.Width = 67
RetryButton.Height = 25
RetryButton.Top = Me.ClientSize.Height - 35 - space_border
RetryButton.Left = left
AddHandler RetryButton.Click, AddressOf RetryClick
AcceptButton = RetryButton
Me.Controls.Add(RetryButton)
Return RetryButton
End Function
Private Function AddIgnoreButton(ByVal left As Integer) As Button
Dim IgnoreButton As New Button()
IgnoreButton.Text = "Ignore"
IgnoreButton.Width = 67
IgnoreButton.Height = 25
IgnoreButton.Top = Me.ClientSize.Height - 35 - space_border
IgnoreButton.Left = left
AddHandler IgnoreButton.Click, AddressOf IgnoreClick
Me.Controls.Add(IgnoreButton)
Return IgnoreButton
End Function
Private Function AddYesButton(ByVal left As Integer) As Button
Dim YesButton As New Button()
YesButton.Text = "Yes"
YesButton.Width = 67
YesButton.Height = 25
YesButton.Top = Me.ClientSize.Height - 35 - space_border
YesButton.Left = left
AddHandler YesButton.Click, AddressOf YesClick
AcceptButton = YesButton
Me.Controls.Add(YesButton)
Return YesButton
End Function
Private Function AddNoButton(ByVal left As Integer) As Button
Dim NoButton As New Button()
NoButton.Text = "No"
NoButton.Width = 67
NoButton.Height = 25
NoButton.Top = Me.ClientSize.Height - 35 - space_border
NoButton.Left = left
AddHandler NoButton.Click, AddressOf NoClick
CancelButton = NoButton
Me.Controls.Add(NoButton)
Return NoButton
End Function
#End Region
End Class
#End Region
#Region "Methods"
Public Shared Function Show(ByVal text As String) As DialogResult
Dim form As New MessageBoxForm(Nothing, text, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.None)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal owner As IWin32Window, ByVal text As String) As DialogResult
Dim form As New MessageBoxForm(owner, text, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.None)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal text As String, ByVal caption As String) As DialogResult
Dim form As New MessageBoxForm(Nothing, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons) As DialogResult
Dim form As New MessageBoxForm(Nothing, text, caption, buttons, MessageBoxIcon.None)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons) As DialogResult
Dim form As New MessageBoxForm(owner, text, caption, buttons, MessageBoxIcon.None)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon) As DialogResult
Dim form As New MessageBoxForm(owner, text, caption, buttons, icon)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String) As DialogResult
Dim form As New MessageBoxForm(owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon) As DialogResult
Dim form As New MessageBoxForm(Nothing, text, caption, buttons, icon)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton) As DialogResult
Dim form As New MessageBoxForm(Nothing, text, caption, buttons, icon, defaultButton, _
MessageBoxOptions.DefaultDesktopOnly)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton) As DialogResult
Dim form As New MessageBoxForm(owner, text, caption, buttons, icon, defaultButton, _
MessageBoxOptions.DefaultDesktopOnly)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton, ByVal options As MessageBoxOptions) As DialogResult
Dim form As New MessageBoxForm(Nothing, text, caption, buttons, icon, defaultButton, _
options)
Return form.IntializeDialog()
End Function
Public Shared Function Show(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton, _
ByVal options As MessageBoxOptions) As DialogResult
Dim form As New MessageBoxForm(owner, text, caption, buttons, icon, defaultButton, _
options)
Return form.IntializeDialog()
End Function
#End Region
End Class
_
كيفية استخدام الكلاس اعلاه
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' chnage the msg box text color
MsgBoxGhost.TextColor = Color.Red
' Show the msg box
MsgBoxGhost.Show("Slect Button", "Test", _
MsgBoxGhost.MessageBoxButtons.AbortRetryIgnore, MsgBoxGhost.MessageBoxIcon.SecurityLock)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim m_Result As DialogResult
MsgBoxGhost.TextColor = Color.Blue
m_Result = MsgBoxGhost.Show("Press the Key You Want", "Test", MsgBoxGhost.MessageBoxButtons.YesNoCancel, MsgBoxGhost.MessageBoxIcon.UACshield)
If m_Result = DialogResult.Yes Then
Me.Text = "You Pressed : " & DialogResult.Yes.ToString & " " & "Button"
ElseIf m_Result = DialogResult.No Then
Me.Text = "You Pressed : " & DialogResult.No.ToString & " " & "Button"
ElseIf m_Result = DialogResult.Cancel Then
Me.Text = "You Pressed : " & DialogResult.Cancel.ToString & " " & "Button"
End If
End Sub
End Class
Public Class Form1
كما تلاحظون يمكن التعديل في لون الخط المستخدم وهناك الكثير من الايقونات مستخدمة داخل الكلاس وهي ايقونات فيستا طبعا يمكن تطوير الفورم وتلوينه وتغيير اي شئ تريده
يمكن إنزال الكود بنسخة 2005 من اللينك التالي
http://www.geocities.com/a_pess/MessageBoxDemo2005.zip
يمكن إنزال الكود بنسخة 2008 من اللينك التالي
http://www.geocities.com/a_pess/MsgBoxDemo2008.zip
بالتوفيق
أخوكم عمر