Imports System.IO
Imports System.Text
Public Class 桌面捷徑設定
    Dim RI As Integer = -1
    Dim SK As String = "要查找的 KEY"
    Dim Str As String = Application.StartupPath
    Private ReadOnly 對話框(9) As String
    Private Sub Set_語言()
        語言_dgv.DataSource = Nothing
        語言_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        語言_dgv.ColumnHeadersHeight = 25
        語言_dgv.AllowUserToAddRows = False
        For i As Integer = 0 To 語言_dgv.Rows.Count - 1 : 語言_dgv.Rows.RemoveAt(0) : Next
        If File.Exists(Str + "\LANGUAGE_FILE.csv") Then
            Dim filereader = My.Computer.FileSystem.OpenTextFileReader(Str + "\LANGUAGE_FILE.csv", System.Text.Encoding.Default)
            Dim line As String = filereader.ReadLine()
            While Not (line Is Nothing)
                line = filereader.ReadLine()
                If line <> Nothing Then
                    line = line.Replace("""", "")
                    If Strings.Left(line, 4) = "G000" Or Strings.Left(line, 8) = "H001-145" Or Strings.Left(line, 4) = 介面 Then
                        If 系統語言 = "繁體中文" Then
                            If Strings.Mid(line, 10, 2) = "CH" Then : 語言_dgv.Rows.Insert(0)
                                語言_dgv.Rows(0).Cells(0).Value = Strings.Left(line, 11)
                                If Strings.Mid(line, 13, 300).EndsWith(",") Then
                                    語言_dgv.Rows(0).Cells(1).Value = Strings.Mid(line, 13, 300).Substring(0, Strings.Mid(line, 13, 300).Length - 1)
                                Else : 語言_dgv.Rows(0).Cells(1).Value = Strings.Mid(line, 13, 300) : End If
                            End If
                        ElseIf 系統語言 = "English" Then
                            If Strings.Mid(line, 10, 2) = "EN" Then : 語言_dgv.Rows.Insert(0)
                                語言_dgv.Rows(0).Cells(0).Value = Strings.Left(line, 11)
                                If Strings.Mid(line, 13, 300).EndsWith(",") Then
                                    語言_dgv.Rows(0).Cells(1).Value = Strings.Mid(line, 13, 300).Substring(0, Strings.Mid(line, 13, 300).Length - 1)
                                Else : 語言_dgv.Rows(0).Cells(1).Value = Strings.Mid(line, 13, 300) : End If
                            End If
                        ElseIf 系統語言 = "Indonesian" Then
                            If Strings.Mid(line, 10, 2) = "IN" Then : 語言_dgv.Rows.Insert(0)
                                語言_dgv.Rows(0).Cells(0).Value = Strings.Left(line, 11)
                                If Strings.Mid(line, 13, 300).EndsWith(",") Then
                                    語言_dgv.Rows(0).Cells(1).Value = Strings.Mid(line, 13, 300).Substring(0, Strings.Mid(line, 13, 300).Length - 1) : Else
                                    語言_dgv.Rows(0).Cells(1).Value = Strings.Mid(line, 13, 300) : End If
                            End If
                        End If
                    End If
                End If
            End While
            filereader.Close()
        End If
    End Sub
    Private Sub Set_語言查詢()
         RI = -1 : For Each row As DataGridViewRow In 語言_dgv.Rows
            If Strings.Left(row.Cells(0).Value.ToString(), 8) = SK Then
                RI = row.Index : Exit For
            End If
        Next
    End Sub
    Private Sub 語言轉換讀取()
        Set_語言()
        SK = "G000-108" : Set_語言查詢() : ToolTip1.ToolTipTitle = 語言_dgv(1, RI).Value.ToString
        SK = "H001-145" : Set_語言查詢() : 主題_lb.Text = 語言_dgv(1, RI).Value.ToString : SK = "H003-100" : Set_語言查詢() : 對話框(0) = 語言_dgv(1, RI).Value.ToString
        SK = "G000-115" : Set_語言查詢() : 對話框(1) = 語言_dgv(1, RI).Value.ToString : SK = "G000-116" : Set_語言查詢() : 對話框(2) = 語言_dgv(1, RI).Value.ToString
        SK = "H003-101" : Set_語言查詢() : 對話框(3) = 語言_dgv(1, RI).Value.ToString : SK = "H003-102" : Set_語言查詢() : 對話框(4) = 語言_dgv(1, RI).Value.ToString
        SK = "H003-103" : Set_語言查詢() : 對話框(5) = 語言_dgv(1, RI).Value.ToString : SK = "H003-104" : Set_語言查詢() : 對話框(6) = 語言_dgv(1, RI).Value.ToString
        SK = "H003-105" : Set_語言查詢() : 對話框(7) = 語言_dgv(1, RI).Value.ToString : SK = "H003-106" : Set_語言查詢() : 對話框(8) = 語言_dgv(1, RI).Value.ToString
        SK = "H003-107" : Set_語言查詢() : 對話框(9) = 語言_dgv(1, RI).Value.ToString
    End Sub
    Private Sub 全部選擇_rbt_MouseEnter(sender As Object, e As EventArgs) Handles 全部選擇_rbt.MouseEnter
        ToolTip1.SetToolTip(Me.全部選擇_rbt, 對話框(1))
    End Sub
    Private Sub 全部取消_rbt_MouseEnter(sender As Object, e As EventArgs) Handles 全部取消_rbt.MouseEnter
        ToolTip1.SetToolTip(Me.全部取消_rbt, 對話框(2))
    End Sub
    Private Sub 業務_bt_MouseEnter(sender As Object, e As EventArgs) Handles 業務_bt.MouseEnter
        ToolTip1.SetToolTip(Me.業務_bt, 對話框(3))
    End Sub
    Private Sub 採購_bt_MouseEnter(sender As Object, e As EventArgs) Handles 採購_bt.MouseEnter
        ToolTip1.SetToolTip(Me.採購_bt, 對話框(4))
    End Sub
    Private Sub 工程_bt_MouseEnter(sender As Object, e As EventArgs) Handles 工程_bt.MouseEnter
        ToolTip1.SetToolTip(Me.工程_bt, 對話框(5))
    End Sub
    Private Sub 倉儲_bt_MouseEnter(sender As Object, e As EventArgs) Handles 倉儲_bt.MouseEnter
        ToolTip1.SetToolTip(Me.倉儲_bt, 對話框(6))
    End Sub
    Private Sub 人事_bt_MouseEnter(sender As Object, e As EventArgs) Handles 人事_bt.MouseEnter
        ToolTip1.SetToolTip(Me.人事_bt, 對話框(7))
    End Sub
    Private Sub 財務_bt_MouseEnter(sender As Object, e As EventArgs) Handles 財務_bt.MouseEnter
        ToolTip1.SetToolTip(Me.財務_bt, 對話框(9))
    End Sub
    Private Sub PnlTitleBar_Paint(sender As Object, e As PaintEventArgs) Handles PnlTitleBar.Paint
    End Sub
    Private Sub 桌面捷徑設定_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        語言_dgv.SendToBack() : H(3) = True : 介面 = "H003" : 語言轉換讀取()
        Panel2.SendToBack()
        捷徑_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 7.6)
        捷徑_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        捷徑_dgv.ColumnHeadersHeight = 25 : 捷徑_dgv.AllowUserToAddRows = False
        捷徑_dgv.RowTemplate.Height = 20 : 捷徑_dgv.Columns(0).FillWeight = 30
        捷徑_dgv.Columns(0).ReadOnly = True : 捷徑_dgv.Columns(1).ReadOnly = True
    End Sub
    Private Sub 桌面捷徑設定_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        計算分流1.Enabled = True
    End Sub
    Private Sub 計算分流1_Tick(sender As Object, e As EventArgs) Handles 計算分流1.Tick
        計算分流1.Enabled = False
        For i As Integer = 0 To 捷徑_dgv.Rows.Count - 1 : 捷徑_dgv.Rows.RemoveAt(0) : Next
        For i As Integer = 0 To 捷徑數量 - 1 : 捷徑_dgv.Rows.Insert(0) : Next
        For i As Integer = 0 To 捷徑數量 - 1
            捷徑_dgv.Rows(i).Cells(0).Value = False : 捷徑_dgv.Rows(i).Cells(1).Value = 顯示名稱(i)
            捷徑_dgv.Rows(i).Cells(2).Value = 捷徑名稱(i) : 捷徑_dgv.Rows(i).Cells(3).Value = 捷徑類別(i)
        Next
        Dim Str As String = Application.StartupPath
        If File.Exists(Str + "\" & gUserName & ".txt") Then
            Dim filereader = My.Computer.FileSystem.OpenTextFileReader(Str + "\" & gUserName & ".txt", System.Text.Encoding.Default)
            Dim line As String = filereader.ReadLine()
            While Not (line Is Nothing)
                line = filereader.ReadLine()
                For I As Integer = 0 To 捷徑_dgv.Rows.Count - 1
                    If 捷徑_dgv.Rows(I).Cells(2).Value = line Then : 捷徑_dgv.Rows(I).Cells(0).Value = True : Exit For : End If
                Next
            End While
            filereader.Close()
        End If
        首次開啟 = False
    End Sub
    Private Sub 捷徑_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 捷徑_dgv.RowPostPaint
        If 首次開啟 = False Then
            Dim linePen As New Pen(Color.Blue, 2)
            If e.RowIndex = 捷徑_dgv.Rows.Count - 1 Then
                Exit Sub
            Else
                If 捷徑_dgv(3, e.RowIndex).Value.ToString.ToLower() <> 捷徑_dgv(3, e.RowIndex + 1).Value.ToString.ToLower() Then
                    Dim startX As Integer = IIf(捷徑_dgv.RowHeadersVisible, 捷徑_dgv.RowHeadersWidth, 0)
                    Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                    Dim endX As Integer = startX + 捷徑_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 捷徑_dgv.HorizontalScrollingOffset
                    e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                    Exit Sub
                End If
            End If
        End If
    End Sub
    Private Sub 捷徑_dgv_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles 捷徑_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            原文_tb.Text = 捷徑_dgv.Rows(e.RowIndex).Cells(3).Value.ToString
            If 捷徑_dgv.Rows(e.RowIndex).Cells(0).Value = False Then
                捷徑_dgv.Rows(e.RowIndex).Cells(0).Value = True : 捷徑_dgv.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightBlue
            Else
                捷徑_dgv.Rows(e.RowIndex).Cells(0).Value = False : 捷徑_dgv.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
            End If
        End If
    End Sub
    Private Sub 存檔_rbt_Click(sender As Object, e As EventArgs) Handles 存檔_rbt.Click
        Dim SELE As Boolean = False
        For I As Integer = 0 To 捷徑_dgv.Rows.Count - 1 : If 捷徑_dgv.Rows(I).Cells(0).Value = True Then : SELE = True : Exit For : End If : Next
        If SELE = True Then
            Dim strDirName As String : Dim Strs As String = Application.StartupPath
            If File.Exists(Strs & "\" & gUserName & ".txt") Then : File.Delete(Strs & "\" & gUserName & ".txt") : End If
            strDirName = Strs & "\" & gUserName & ".txt"
            Try
                Using fs As New FileStream(strDirName, FileMode.Append)
                    Using sw As New StreamWriter(fs, Encoding.Default)
                        Dim str As String
                        str = "******************" : sw.WriteLine(str)
                        For I As Integer = 0 To 捷徑_dgv.Rows.Count - 1
                            If 捷徑_dgv.Rows(I).Cells(0).Value = True Then
                                str = 捷徑_dgv.Rows(I).Cells(2).Value : sw.WriteLine(str)   '插入一整行
                            End If
                        Next : sw.Flush()
                    End Using
                End Using
            Catch ex As Exception
            End Try
            Me.Close()
        Else : MGB(對話框(0), 1) : End If
    End Sub
    Private Sub 全部選擇_rbt_Click(sender As Object, e As EventArgs) Handles 全部選擇_rbt.Click
        If 原文_tb.Text = "" Then : For i As Integer = 0 To 捷徑_dgv.Rows.Count - 1 : 捷徑_dgv.Rows(i).Cells(0).Value = True : Next i
        Else : For i As Integer = 0 To 捷徑_dgv.Rows.Count - 1 : If 原文_tb.Text = 捷徑_dgv.Rows(i).Cells(3).Value.ToString Then : 捷徑_dgv.Rows(i).Cells(0).Value = True : End If : Next : End If
    End Sub
    Private Sub 全部取消_rbt_Click(sender As Object, e As EventArgs) Handles 全部取消_rbt.Click
        If 原文_tb.Text = "" Then : For i As Integer = 0 To 捷徑_dgv.Rows.Count - 1 : 捷徑_dgv.Rows(i).Cells(0).Value = False : Next i
        Else : For i As Integer = 0 To 捷徑_dgv.Rows.Count - 1 : If 原文_tb.Text = 捷徑_dgv.Rows(i).Cells(3).Value.ToString Then : 捷徑_dgv.Rows(i).Cells(0).Value = False : End If : Next : End If
    End Sub
    Private Sub 原文_tb_TextChanged(sender As Object, e As EventArgs) Handles 原文_tb.TextChanged
        If 原文_tb.Text = "業務" Then : 翻譯_rtb.Text = 對話框(3) : ElseIf 原文_tb.Text = "採購" Then : 翻譯_rtb.Text = 對話框(4)
        ElseIf 原文_tb.Text = "工程" Then : 翻譯_rtb.Text = 對話框(5) : ElseIf 原文_tb.Text = "倉儲" Then : 翻譯_rtb.Text = 對話框(6)
        ElseIf 原文_tb.Text = "人事" Then : 翻譯_rtb.Text = 對話框(7) : ElseIf 原文_tb.Text = "其他" Then : 翻譯_rtb.Text = 對話框(8)
        ElseIf 原文_tb.Text = "財務" Then : 翻譯_rtb.Text = 對話框(9) : End If
    End Sub
    Private Sub 選擇比較()
        Dim SS As Boolean
        For i As Integer = 0 To 捷徑_dgv.Rows.Count - 1
            If 原文_tb.Text = 捷徑_dgv.Rows(i).Cells(3).Value.ToString Then : If 捷徑_dgv.Rows(i).Cells(0).Value = False Then : SS = False : Exit For : Else : SS = True : End If : End If
        Next
        If SS = False Then : 全部選擇_rbt.PerformClick() : Else : 全部取消_rbt.PerformClick() : End If
    End Sub
    Private Sub 業務_bt_Click(sender As Object, e As EventArgs) Handles 業務_bt.Click
        原文_tb.Text = "業務" : 選擇比較()
    End Sub
    Private Sub 採購_bt_Click(sender As Object, e As EventArgs) Handles 採購_bt.Click
        原文_tb.Text = "採購" : 選擇比較()
    End Sub
    Private Sub 倉儲_bt_Click(sender As Object, e As EventArgs) Handles 倉儲_bt.Click
        原文_tb.Text = "倉儲" : 選擇比較()
    End Sub
    Private Sub 工程_bt_Click(sender As Object, e As EventArgs) Handles 工程_bt.Click
        原文_tb.Text = "工程" : 選擇比較()
    End Sub
    Private Sub 財務_bt_Click(sender As Object, e As EventArgs) Handles 財務_bt.Click
        原文_tb.Text = "財務" : 選擇比較()
    End Sub
    Private Sub 人事_bt_Click(sender As Object, e As EventArgs) Handles 人事_bt.Click
        原文_tb.Text = "人事" : 選擇比較()
    End Sub
    Private Sub Cancel_bt_Click(sender As Object, e As EventArgs) Handles Cancel_bt.Click
        Me.Close()
    End Sub
    Private Sub 語轉扭_bt_Click(sender As Object, e As EventArgs) Handles 語轉扭_bt.Click
        介面 = "H003" : 語言轉換讀取() : 計算分流1.Enabled = True
    End Sub

    Dim nOldWndLeft, nOldWndTop, nClickX, nClickY As Integer
    Private Sub PnlTitleBar_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlTitleBar.MouseDown
        '紀錄滑鼠點選時的視窗位置與滑鼠點選位置
        nOldWndLeft = Me.Left : nOldWndTop = Me.Top : nClickX = e.X : nClickY = e.Y
    End Sub
    Private Sub PnlTitleBar_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlTitleBar.MouseMove
        If PnlTitleBar.Capture = True Then        '如果滑鼠按著拖曳
            Me.Top = e.Y + nOldWndTop - nClickY : Me.Left = e.X + nOldWndLeft - nClickX   '設定新的視窗位置
            nOldWndLeft = Me.Left : nOldWndTop = Me.Top   '更新紀錄的視窗位置
        End If
    End Sub
    Private Sub 主題_lb_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 主題_lb.MouseDown
        '紀錄滑鼠點選時的視窗位置與滑鼠點選位置
        nOldWndLeft = Me.Left : nOldWndTop = Me.Top : nClickX = e.X : nClickY = e.Y
    End Sub
    Private Sub 主題_lb_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 主題_lb.MouseMove
        If 主題_lb.Capture = True Then        '如果滑鼠按著拖曳
            Me.Top = e.Y + nOldWndTop - nClickY : Me.Left = e.X + nOldWndLeft - nClickX   '設定新的視窗位置
            nOldWndLeft = Me.Left : nOldWndTop = Me.Top   '更新紀錄的視窗位置
        End If
    End Sub
End Class