Imports System.Windows.Forms
''' -----------------------------------------------------------------------------
''' Project	 : NHenry.Exception
''' Class	 : Exception.FrmShowError
''' -----------------------------------------------------------------------------
''' <summary>
''' Formulaire d'affichage du message d'erreur
''' </summary>
''' -----------------------------------------------------------------------------
Public Class FrmShowError
    Inherits System.Windows.Forms.Form

    Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As Integer
    Private Declare Function DrawIcon Lib "user32.dll" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal hIcon As Integer) As Integer
    Private Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Integer) As Integer


    Private IsOpen As Boolean = False
    Private mIcon As Integer = 0
    Private mReturnValue As Windows.Forms.DialogResult = DialogResult.None
    Private mExpand As Boolean = False
#Region " Code généré par le Concepteur Windows Form "

    Private Sub New()
        MyBase.New()

        'Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        'Ajoutez une initialisation quelconque après l'appel InitializeComponent()

    End Sub

    'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.
    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

    'Requis par le Concepteur Windows Form
    Private components As System.ComponentModel.IContainer

    'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form
    'Elle peut être modifiée en utilisant le Concepteur Windows Form.  
    'Ne la modifiez pas en utilisant l'éditeur de code.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents PictIcon As System.Windows.Forms.PictureBox
    Friend WithEvents PanBtn As System.Windows.Forms.Panel
    Friend WithEvents LblMessage As System.Windows.Forms.Label
    Friend WithEvents TxtDetail As System.Windows.Forms.TextBox
    Friend WithEvents ImgDetail As System.Windows.Forms.ImageList
    Friend WithEvents CmdCopy As System.Windows.Forms.Button
    Friend WithEvents CmdOK As System.Windows.Forms.Button
    Friend WithEvents CmdCancel As System.Windows.Forms.Button
    Friend WithEvents CmdYes As System.Windows.Forms.Button
    Friend WithEvents CmdNo As System.Windows.Forms.Button
    Friend WithEvents CmdRetry As System.Windows.Forms.Button
    Friend WithEvents CmdAbort As System.Windows.Forms.Button
    Friend WithEvents CmdIgnore As System.Windows.Forms.Button
    Friend WithEvents CmdShowDetail As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmShowError))
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.LblMessage = New System.Windows.Forms.Label
        Me.PictIcon = New System.Windows.Forms.PictureBox
        Me.PanBtn = New System.Windows.Forms.Panel
        Me.CmdIgnore = New System.Windows.Forms.Button
        Me.CmdAbort = New System.Windows.Forms.Button
        Me.CmdRetry = New System.Windows.Forms.Button
        Me.CmdNo = New System.Windows.Forms.Button
        Me.CmdYes = New System.Windows.Forms.Button
        Me.CmdCancel = New System.Windows.Forms.Button
        Me.CmdOK = New System.Windows.Forms.Button
        Me.CmdCopy = New System.Windows.Forms.Button
        Me.CmdShowDetail = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.TxtDetail = New System.Windows.Forms.TextBox
        Me.ImgDetail = New System.Windows.Forms.ImageList(Me.components)
        Me.Panel1.SuspendLayout()
        Me.PanBtn.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.LblMessage)
        Me.Panel1.Controls.Add(Me.PictIcon)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Top
        Me.Panel1.Location = New System.Drawing.Point(0, 0)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(464, 88)
        Me.Panel1.TabIndex = 0
        '
        'LblMessage
        '
        Me.LblMessage.Dock = System.Windows.Forms.DockStyle.Fill
        Me.LblMessage.Location = New System.Drawing.Point(64, 0)
        Me.LblMessage.Name = "LblMessage"
        Me.LblMessage.Size = New System.Drawing.Size(400, 88)
        Me.LblMessage.TabIndex = 1
        Me.LblMessage.Text = "Label1"
        '
        'PictIcon
        '
        Me.PictIcon.Dock = System.Windows.Forms.DockStyle.Left
        Me.PictIcon.Location = New System.Drawing.Point(0, 0)
        Me.PictIcon.Name = "PictIcon"
        Me.PictIcon.Size = New System.Drawing.Size(64, 88)
        Me.PictIcon.TabIndex = 0
        Me.PictIcon.TabStop = False
        '
        'PanBtn
        '
        Me.PanBtn.Controls.Add(Me.CmdIgnore)
        Me.PanBtn.Controls.Add(Me.CmdAbort)
        Me.PanBtn.Controls.Add(Me.CmdRetry)
        Me.PanBtn.Controls.Add(Me.CmdNo)
        Me.PanBtn.Controls.Add(Me.CmdYes)
        Me.PanBtn.Controls.Add(Me.CmdCancel)
        Me.PanBtn.Controls.Add(Me.CmdOK)
        Me.PanBtn.Controls.Add(Me.CmdCopy)
        Me.PanBtn.Controls.Add(Me.CmdShowDetail)
        Me.PanBtn.Dock = System.Windows.Forms.DockStyle.Top
        Me.PanBtn.Location = New System.Drawing.Point(0, 88)
        Me.PanBtn.Name = "PanBtn"
        Me.PanBtn.Size = New System.Drawing.Size(464, 40)
        Me.PanBtn.TabIndex = 1
        '
        'CmdIgnore
        '
        Me.CmdIgnore.Location = New System.Drawing.Point(296, 16)
        Me.CmdIgnore.Name = "CmdIgnore"
        Me.CmdIgnore.Size = New System.Drawing.Size(72, 23)
        Me.CmdIgnore.TabIndex = 8
        Me.CmdIgnore.Text = "&Ignorer"
        '
        'CmdAbort
        '
        Me.CmdAbort.Location = New System.Drawing.Point(368, 0)
        Me.CmdAbort.Name = "CmdAbort"
        Me.CmdAbort.Size = New System.Drawing.Size(80, 23)
        Me.CmdAbort.TabIndex = 7
        Me.CmdAbort.Text = "&Abandonner"
        '
        'CmdRetry
        '
        Me.CmdRetry.Location = New System.Drawing.Point(296, 0)
        Me.CmdRetry.Name = "CmdRetry"
        Me.CmdRetry.Size = New System.Drawing.Size(72, 23)
        Me.CmdRetry.TabIndex = 6
        Me.CmdRetry.Text = "&Rééssayer"
        '
        'CmdNo
        '
        Me.CmdNo.Location = New System.Drawing.Point(224, 16)
        Me.CmdNo.Name = "CmdNo"
        Me.CmdNo.Size = New System.Drawing.Size(72, 23)
        Me.CmdNo.TabIndex = 5
        Me.CmdNo.Text = "&Non"
        '
        'CmdYes
        '
        Me.CmdYes.Location = New System.Drawing.Point(224, 0)
        Me.CmdYes.Name = "CmdYes"
        Me.CmdYes.Size = New System.Drawing.Size(72, 23)
        Me.CmdYes.TabIndex = 4
        Me.CmdYes.Text = "&Oui"
        '
        'CmdCancel
        '
        Me.CmdCancel.Location = New System.Drawing.Point(152, 16)
        Me.CmdCancel.Name = "CmdCancel"
        Me.CmdCancel.Size = New System.Drawing.Size(72, 23)
        Me.CmdCancel.TabIndex = 3
        Me.CmdCancel.Text = "&Annuler"
        '
        'CmdOK
        '
        Me.CmdOK.Location = New System.Drawing.Point(152, 0)
        Me.CmdOK.Name = "CmdOK"
        Me.CmdOK.Size = New System.Drawing.Size(72, 23)
        Me.CmdOK.TabIndex = 2
        Me.CmdOK.Text = "&OK"
        '
        'CmdCopy
        '
        Me.CmdCopy.Image = CType(resources.GetObject("CmdCopy.Image"), System.Drawing.Image)
        Me.CmdCopy.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.CmdCopy.Location = New System.Drawing.Point(80, 8)
        Me.CmdCopy.Name = "CmdCopy"
        Me.CmdCopy.Size = New System.Drawing.Size(64, 23)
        Me.CmdCopy.TabIndex = 1
        Me.CmdCopy.Text = "&Copier"
        Me.CmdCopy.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.CmdCopy.Visible = False
        '
        'CmdShowDetail
        '
        Me.CmdShowDetail.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.CmdShowDetail.Location = New System.Drawing.Point(8, 8)
        Me.CmdShowDetail.Name = "CmdShowDetail"
        Me.CmdShowDetail.Size = New System.Drawing.Size(64, 23)
        Me.CmdShowDetail.TabIndex = 0
        Me.CmdShowDetail.Text = "&Détail"
        Me.CmdShowDetail.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.TxtDetail)
        Me.GroupBox1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.GroupBox1.Location = New System.Drawing.Point(0, 128)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(464, 129)
        Me.GroupBox1.TabIndex = 2
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Détail"
        '
        'TxtDetail
        '
        Me.TxtDetail.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TxtDetail.Location = New System.Drawing.Point(3, 16)
        Me.TxtDetail.Multiline = True
        Me.TxtDetail.Name = "TxtDetail"
        Me.TxtDetail.ReadOnly = True
        Me.TxtDetail.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TxtDetail.Size = New System.Drawing.Size(458, 110)
        Me.TxtDetail.TabIndex = 0
        Me.TxtDetail.Text = "TextBox1"
        '
        'ImgDetail
        '
        Me.ImgDetail.ImageSize = New System.Drawing.Size(16, 16)
        Me.ImgDetail.ImageStream = CType(resources.GetObject("ImgDetail.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImgDetail.TransparentColor = System.Drawing.Color.Transparent
        '
        'FrmShowError
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 257)
        Me.ControlBox = False
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.PanBtn)
        Me.Controls.Add(Me.Panel1)
        Me.MaximumSize = New System.Drawing.Size(472, 284)
        Me.MinimumSize = New System.Drawing.Size(472, 125)
        Me.Name = "FrmShowError"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "FrmError1"
        Me.TopMost = True
        Me.Panel1.ResumeLayout(False)
        Me.PanBtn.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Affiche un message avec les valeurs spécifiées
    ''' </summary>
    ''' <param name="pMessage">Message de description</param>
    ''' <param name="pDetail">Texte à afficher pour les détails</param>
    ''' <param name="pTitle">Titre de la fenetre</param>
    ''' <param name="pIcon">Icone à afficher</param>
    ''' <param name="pButtons">Style des boutons</param>
    ''' <param name="pDefault">Bouton activé par défaut</param>
    ''' <returns>Retourne le bouton qui a été appuyé</returns>
    ''' -----------------------------------------------------------------------------
    Public Shared Function InitDialog(ByVal pMessage As String, ByVal pDetail As String, ByVal pTitle As String, ByVal pIcon As MessageBoxIcon, ByVal pButtons As MessageBoxButtons, Optional ByVal pDefault As MessageBoxDefaultButton = MessageBoxDefaultButton.Button1) As DialogResult
        Dim lFrm As New FrmShowError
        Try
            With lFrm
                .LblMessage.Text = pMessage
                .TxtDetail.Text = pDetail
                .Text = pTitle
                'Buttons
                .CmdAbort.Visible = False
                .CmdCancel.Visible = False
                .CmdIgnore.Visible = False
                .CmdNo.Visible = False
                .CmdOK.Visible = False
                .CmdRetry.Visible = False
                .CmdYes.Visible = False
                .CmdAbort.Top = 8
                .CmdCancel.Top = .CmdAbort.Top
                .CmdIgnore.Top = .CmdAbort.Top
                .CmdNo.Top = .CmdAbort.Top
                .CmdOK.Top = .CmdAbort.Top
                .CmdRetry.Top = .CmdAbort.Top
                .CmdYes.Top = .CmdAbort.Top

                Select Case pButtons
                    Case MessageBoxButtons.AbortRetryIgnore
                        .CmdAbort.Visible = True
                        .CmdIgnore.Visible = True
                        .CmdRetry.Visible = True
                        .CmdIgnore.Left = .PanBtn.Width - .CmdIgnore.Width - 8
                        .CmdRetry.Left = .CmdIgnore.Left - .CmdRetry.Width - 8
                        .CmdAbort.Left = .CmdRetry.Left - .CmdAbort.Width - 8
                        Select Case pDefault
                            Case MessageBoxDefaultButton.Button1
                                .AcceptButton = .CmdAbort
                            Case MessageBoxDefaultButton.Button2
                                .AcceptButton = .CmdRetry
                            Case MessageBoxDefaultButton.Button3
                                .AcceptButton = .CmdIgnore
                        End Select
                        .CancelButton = .CmdShowDetail
                    Case MessageBoxButtons.OK
                        .CmdOK.Visible = True
                        .CmdOK.Left = .PanBtn.Width - .CmdOK.Width - 8
                        .AcceptButton = .CmdOK
                        .CancelButton = .CmdShowDetail
                    Case MessageBoxButtons.OKCancel
                        .CmdCancel.Visible = True
                        .CmdOK.Visible = True
                        .CmdCancel.Left = .PanBtn.Width - .CmdCancel.Width - 8
                        .CmdOK.Left = .CmdCancel.Left - .CmdOK.Width - 8
                        Select Case pDefault
                            Case MessageBoxDefaultButton.Button2
                                .AcceptButton = .CmdCancel
                                .CancelButton = .CmdOK
                            Case Else
                                .AcceptButton = .CmdOK
                                .CancelButton = .CmdCancel
                        End Select
                    Case MessageBoxButtons.RetryCancel
                        .CmdCancel.Visible = True
                        .CmdRetry.Visible = True
                        .CmdCancel.Left = .PanBtn.Width - .CmdCancel.Width - 8
                        .CmdRetry.Left = .CmdCancel.Left - .CmdRetry.Width - 8
                        Select Case pDefault
                            Case MessageBoxDefaultButton.Button2
                                .AcceptButton = .CmdCancel
                                .CancelButton = .CmdRetry
                            Case Else
                                .AcceptButton = .CmdRetry
                                .CancelButton = .CmdCancel
                        End Select
                    Case MessageBoxButtons.YesNo
                        .CmdNo.Visible = True
                        .CmdYes.Visible = True
                        .CmdNo.Left = .PanBtn.Width - .CmdNo.Width - 8
                        .CmdYes.Left = .CmdNo.Left - .CmdYes.Width - 8
                        Select Case pDefault
                            Case MessageBoxDefaultButton.Button2
                                .AcceptButton = .CmdNo
                                .CancelButton = .CmdYes
                            Case Else
                                .AcceptButton = .CmdYes
                                .CancelButton = .CmdNo
                        End Select
                    Case MessageBoxButtons.YesNoCancel
                        .CmdCancel.Visible = True
                        .CmdNo.Visible = True
                        .CmdYes.Visible = True
                        .CmdCancel.Left = .PanBtn.Width - .CmdCancel.Width - 8
                        .CmdNo.Left = .CmdCancel.Width - .CmdNo.Width - 8
                        .CmdYes.Left = .CmdNo.Left - .CmdYes.Width - 8
                        Select Case pDefault
                            Case MessageBoxDefaultButton.Button1
                                .AcceptButton = .CmdYes
                            Case MessageBoxDefaultButton.Button2
                                .AcceptButton = .CmdNo
                            Case MessageBoxDefaultButton.Button3
                                .AcceptButton = .CmdCancel
                        End Select
                        .CancelButton = .CmdShowDetail
                    Case Else
                        .CmdOK.Visible = True
                        .CmdOK.Left = .PanBtn.Width - .CmdOK.Width - 8
                        .AcceptButton = .CmdOK
                        .CancelButton = .CmdShowDetail
                End Select

                'Icon
                'USER32.dll
                ' ! 1
                ' ? 2
                ' X 3
                ' I 4
                Select Case pIcon
                    Case MessageBoxIcon.Error
                        .mIcon = ExtractIcon(.Handle.ToInt32, Environment.SystemDirectory + "\user32.dll", 3)
                    Case MessageBoxIcon.Exclamation
                        .mIcon = ExtractIcon(.Handle.ToInt32, Environment.SystemDirectory + "\user32.dll", 1)
                    Case MessageBoxIcon.Information
                        .mIcon = ExtractIcon(.Handle.ToInt32, Environment.SystemDirectory + "\user32.dll", 4)
                    Case MessageBoxIcon.Question
                        .mIcon = ExtractIcon(.Handle.ToInt32, Environment.SystemDirectory + "\user32.dll", 2)
                    Case Else
                        .mIcon = 0
                End Select

                Dim lVal As DialogResult
                .Show()
                Application.DoEvents()
                Do While .IsOpen
                    Application.DoEvents()
                    Threading.Thread.Sleep(10)
                Loop
                lVal = .mReturnValue
                .Close()
                Return lVal
            End With
        Catch ex As System.Exception
            MessageBox.Show(ex.ToString)
            Return Windows.Forms.DialogResult.None
        End Try
    End Function

    Private Sub FrmError_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CmdShowDetail.Image = ImgDetail.Images(0)
        Me.ClientSize = Me.MinimumSize
        IsOpen = True
    End Sub
    Private Sub FrmError_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If mIcon <> 0 Then
            DestroyIcon(mIcon)
            mIcon = 0
        End If
        IsOpen = False
    End Sub
    Private Sub CmdShowDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdShowDetail.Click
        If mExpand Then
            Me.ClientSize = Me.MinimumSize
            CmdShowDetail.Image = Me.ImgDetail.Images(0)
            CmdCopy.Visible = False
        Else
            Me.ClientSize = Me.MaximumSize
            CmdShowDetail.Image = Me.ImgDetail.Images(1)
            CmdCopy.Visible = True
        End If
        mExpand = Not (mExpand)
    End Sub
    Private Sub CmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdCopy.Click
        Clipboard.SetDataObject(TxtDetail.Text, True)
    End Sub

    Private Sub PictIcon_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictIcon.Paint
        If mIcon <> 0 Then
            e.Graphics.DrawIcon(Icon.FromHandle(New IntPtr(mIcon)), Convert.ToInt32((PictIcon.Width - 32) / 2), Convert.ToInt32((PictIcon.Height - 32) / 2))
        End If
    End Sub

    Private Sub Cmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdOK.Click, CmdYes.Click, CmdRetry.Click, CmdAbort.Click, CmdCancel.Click, CmdNo.Click, CmdIgnore.Click
        Dim lStr As String = CType(sender, Button).Name.ToLower
        lStr = lStr.Substring(3, lStr.Length - 3)
        Select Case lStr
            Case "ok"
                mReturnValue = DialogResult.OK
            Case "yes"
                mReturnValue = DialogResult.Yes
            Case "retry"
                mReturnValue = DialogResult.Retry
            Case "abort"
                mReturnValue = DialogResult.Abort
            Case "cancel"
                mReturnValue = DialogResult.Cancel
            Case "no"
                mReturnValue = DialogResult.No
            Case "ignore"
                mReturnValue = DialogResult.Ignore
            Case Else
                mReturnValue = DialogResult.None
        End Select
        Me.Close()
    End Sub
End Class
