Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
TLT Reader - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Public Class Form1
  2.  
  3.     Private Function GetTextFont() As Font
  4.         Dim Result As Font = rtbText.SelectionFont
  5.  
  6.         If Result Is Nothing Then
  7.             Dim Style As FontStyle = 0
  8.             If strBold.Checked Then
  9.                 Style = Style Or FontStyle.Bold
  10.             End If
  11.             If strItalic.Checked Then
  12.                 Style = Style Or FontStyle.Italic
  13.             End If
  14.             If strUnderlined.Checked Then
  15.                 Style = Style Or FontStyle.Underline
  16.             End If
  17.             Result = New Font(cmbFonts.SelectedItem.ToString, CInt(cmbSize.SelectedItem), Style, GraphicsUnit.Point)
  18.             'IIf(strBold.Checked, FontStyle.Bold, 0) Or _
  19.             'IIf(strItalic.Checked, FontStyle.Italic, 0) Or _
  20.             'IIf(strUnderlined.Checked, FontStyle.Underline, 0), GraphicsUnit.Point)
  21.         End If
  22.  
  23.         Return Result
  24.     End Function
  25.  
  26.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  27.         For Each Font As FontFamily In (New Drawing.Text.InstalledFontCollection().Families())
  28.             cmbFonts.Items.Add(Font.Name)
  29.         Next
  30.         cmbFonts.SelectedItem = "Times New Roman"
  31.  
  32.         For I As Int16 = 7 To 72
  33.             cmbSize.Items.Add(I)
  34.         Next
  35.         cmbSize.SelectedIndex = 5
  36.  
  37.         Me.WindowState = FormWindowState.Maximized
  38.     End Sub
  39.  
  40.     Private Sub cmbFonts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFonts.SelectedIndexChanged
  41.         Dim Actual As Font = GetTextFont()
  42.         rtbText.SelectionFont = New Font(CStr(cmbFonts.SelectedItem), Actual.Size, Actual.Style, GraphicsUnit.Point)
  43.     End Sub
  44.  
  45.     Private Sub cmbSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSize.SelectedIndexChanged
  46.         Dim Actual As Font = GetTextFont()
  47.         rtbText.SelectionFont = New Font(Actual.Name, CInt(cmbSize.SelectedItem), Actual.Style)
  48.     End Sub
  49.  
  50.     Private Sub strBold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strBold.CheckedChanged
  51.         Dim Actual As Font = GetTextFont()
  52.         Dim l As Int32 = rtbText.SelectionLength
  53.         If strBold.Checked Then
  54.             If (Not Actual.Bold) Then rtbText.SelectionFont = New Font(Actual, Actual.Style + FontStyle.Bold)
  55.         Else
  56.             If (Actual.Bold) Then rtbText.SelectionFont = New Font(Actual, Actual.Style - FontStyle.Bold)
  57.         End If
  58.     End Sub
  59.  
  60.     Private Sub strItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strItalic.CheckedChanged
  61.         Dim Actual As Font = GetTextFont()
  62.         If strItalic.Checked Then
  63.             If (Not Actual.Italic) Then rtbText.SelectionFont = New Font(Actual, Actual.Style + FontStyle.Italic)
  64.         Else
  65.             If (Actual.Italic) Then rtbText.SelectionFont = New Font(Actual, Actual.Style - FontStyle.Italic)
  66.         End If
  67.     End Sub
  68.  
  69.     Private Sub strUnderlined_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strUnderlined.CheckedChanged
  70.         Dim Actual As Font = GetTextFont()
  71.         If strUnderlined.Checked Then
  72.             If (Not Actual.Underline) Then rtbText.SelectionFont = New Font(Actual, Actual.Style + FontStyle.Underline)
  73.         Else
  74.             If (Actual.Underline) Then rtbText.SelectionFont = New Font(Actual, Actual.Style - FontStyle.Underline)
  75.         End If
  76.     End Sub
  77.  
  78.     Private Sub rtbText_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbText.SelectionChanged
  79.         Dim Actual As Font = rtbText.SelectionFont
  80.  
  81.         If Actual IsNot Nothing Then
  82.             cmbFonts.SelectedItem = Actual.FontFamily.Name
  83.             cmbSize.SelectedItem = CInt(Actual.Size)
  84.             strBold.Checked = Actual.Bold
  85.             strItalic.Checked = Actual.Italic
  86.             strUnderlined.Checked = Actual.Underline
  87.             strColor.BackColor = rtbText.SelectionColor
  88.         End If
  89.     End Sub
  90.  
  91.     Private Sub strColor_BackColorChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strColor.BackColorChanged
  92.         rtbText.SelectionColor = strColor.BackColor
  93.     End Sub
  94.  
  95.     Private Sub strSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSave.Click
  96.         Dim Save As New SaveFileDialog
  97.         Save.Filter = "Totem's Lair Format|*.tlt"
  98.         If Save.ShowDialog = Windows.Forms.DialogResult.OK Then
  99.             Dim W As New TLTFormat.Writer
  100.             rtbText.SelectionStart = 0
  101.             Dim F As Font = rtbText.SelectionFont
  102.             Dim C As Color = rtbText.SelectionColor
  103.             Dim Temp As New System.Text.StringBuilder
  104.             Dim Chunk As New TLTFormat.TextChunk
  105.  
  106.             For I As Int32 = 0 To rtbText.TextLength - 1
  107.                 rtbText.SelectionStart = I
  108.                 If Not rtbText.SelectionFont.Equals(F) Or rtbText.SelectionColor <> C Then
  109.                     Chunk = New TLTFormat.TextChunk
  110.                     With Chunk
  111.                         .Color = C
  112.                         .FontName = F.Name
  113.                         .IsBold = F.Bold
  114.                         .IsItalic = F.Italic
  115.                         .IsUnderlined = F.Underline
  116.                         .Size = F.Size
  117.                         .Text = Temp.ToString
  118.                         W.Chunks.Add(Chunk)
  119.                     End With
  120.                     Temp.Remove(0, Temp.Length)
  121.                     Temp.Append(rtbText.Text(I))
  122.                     F = rtbText.SelectionFont
  123.                     C = rtbText.SelectionColor
  124.                 Else
  125.                     Temp.Append(rtbText.Text(I))
  126.                 End If
  127.             Next
  128.             Chunk = New TLTFormat.TextChunk
  129.             With Chunk
  130.                 .Color = C
  131.                 .FontName = F.Name
  132.                 .IsBold = F.Bold
  133.                 .IsItalic = F.Italic
  134.                 .IsUnderlined = F.Underline
  135.                 .Size = F.Size
  136.                 .Text = Temp.ToString
  137.                 W.Chunks.Add(Chunk)
  138.             End With
  139.  
  140.             W.Write(Save.FileName)
  141.         End If
  142.     End Sub
  143.  
  144.     Private Sub strOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strOpen.Click
  145.         Dim Open As New OpenFileDialog
  146.         Open.Filter = "Totem's Lair Format|*.tlt"
  147.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  148.             Dim R As New TLTFormat.Reader
  149.             R.Read(Open.FileName)
  150.  
  151.             rtbText.Text = ""
  152.             For Each C As TLTFormat.TextChunk In R.Chunks
  153.                 rtbText.AppendText(C.Text)
  154.                 rtbText.Select(rtbText.TextLength - C.Text.Length, C.Text.Length)
  155.                 rtbText.SelectionFont = C.Font
  156.                 rtbText.SelectionColor = C.Color
  157.                 rtbText.DeselectAll()
  158.             Next
  159.         End If
  160.     End Sub
  161.  
  162.     Private Sub strColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strColor.Click
  163.         Dim C As New ColorDialog
  164.         If C.ShowDialog = Windows.Forms.DialogResult.OK Then
  165.             rtbText.SelectionColor = C.Color
  166.         End If
  167.     End Sub
  168. End Class