home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch07 / rtfpad / rtbox.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-02-20  |  10.9 KB  |  324 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
  3. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.1#0"; "RICHTX32.OCX"
  4. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.1#0"; "COMCTL32.OCX"
  5. Begin VB.Form Form1 
  6.    Caption         =   "RTFPad"
  7.    ClientHeight    =   5730
  8.    ClientLeft      =   2040
  9.    ClientTop       =   1965
  10.    ClientWidth     =   9255
  11.    LinkTopic       =   "Form1"
  12.    PaletteMode     =   1  'UseZOrder
  13.    ScaleHeight     =   5730
  14.    ScaleWidth      =   9255
  15.    Begin RichTextLib.RichTextBox RichTextBox1 
  16.       Height          =   3630
  17.       Left            =   255
  18.       TabIndex        =   0
  19.       Top             =   780
  20.       Width           =   8790
  21.       _ExtentX        =   15505
  22.       _ExtentY        =   6403
  23.       _Version        =   327680
  24.       Enabled         =   -1  'True
  25.       HideSelection   =   0   'False
  26.       ScrollBars      =   2
  27.       RightMargin     =   4989.764
  28.       TextRTF         =   $"RTBox.frx":0000
  29.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  30.          Name            =   "Tahoma"
  31.          Size            =   9.75
  32.          Charset         =   0
  33.          Weight          =   400
  34.          Underline       =   0   'False
  35.          Italic          =   0   'False
  36.          Strikethrough   =   0   'False
  37.       EndProperty
  38.    End
  39.    Begin ComctlLib.Slider Slider2 
  40.       Height          =   300
  41.       Left            =   165
  42.       TabIndex        =   2
  43.       TabStop         =   0   'False
  44.       Top             =   390
  45.       Width           =   8940
  46.       _ExtentX        =   15769
  47.       _ExtentY        =   529
  48.       _Version        =   327680
  49.       LargeChange     =   4
  50.       Max             =   12
  51.    End
  52.    Begin ComctlLib.Slider Slider1 
  53.       Height          =   285
  54.       Left            =   165
  55.       TabIndex        =   1
  56.       TabStop         =   0   'False
  57.       Top             =   150
  58.       Width           =   8940
  59.       _ExtentX        =   15769
  60.       _ExtentY        =   503
  61.       _Version        =   327680
  62.       LargeChange     =   4
  63.       Max             =   12
  64.       TickStyle       =   3
  65.    End
  66.    Begin VB.Frame Frame1 
  67.       Height          =   750
  68.       Left            =   75
  69.       TabIndex        =   3
  70.       Top             =   0
  71.       Width           =   9075
  72.    End
  73.    Begin MSComDlg.CommonDialog CommonDialog1 
  74.       Left            =   105
  75.       Top             =   0
  76.       _ExtentX        =   847
  77.       _ExtentY        =   847
  78.       _Version        =   327680
  79.       FontSize        =   1.17491e-38
  80.    End
  81.    Begin VB.Menu FileMenu 
  82.       Caption         =   "File"
  83.       Begin VB.Menu FileNew 
  84.          Caption         =   "New"
  85.       End
  86.       Begin VB.Menu FileOpen 
  87.          Caption         =   "Open"
  88.       End
  89.       Begin VB.Menu FileSave 
  90.          Caption         =   "Save"
  91.       End
  92.       Begin VB.Menu FileSaveAs 
  93.          Caption         =   "Save As"
  94.       End
  95.       Begin VB.Menu FileExit 
  96.          Caption         =   "Exit"
  97.       End
  98.    End
  99.    Begin VB.Menu EditMenu 
  100.       Caption         =   "Edit"
  101.       Begin VB.Menu EditCopy 
  102.          Caption         =   "Copy"
  103.       End
  104.       Begin VB.Menu EditCut 
  105.          Caption         =   "Cut"
  106.       End
  107.       Begin VB.Menu EditPaste 
  108.          Caption         =   "Paste"
  109.       End
  110.       Begin VB.Menu EditSelect 
  111.          Caption         =   "Select All"
  112.       End
  113.       Begin VB.Menu EditSeparator 
  114.          Caption         =   "-"
  115.       End
  116.       Begin VB.Menu EditFind 
  117.          Caption         =   "Find"
  118.       End
  119.    End
  120.    Begin VB.Menu FormatMenu 
  121.       Caption         =   "Format"
  122.       Begin VB.Menu EditFont 
  123.          Caption         =   "Font"
  124.       End
  125.       Begin VB.Menu FormatSeparator 
  126.          Caption         =   "-"
  127.       End
  128.       Begin VB.Menu FormatBold 
  129.          Caption         =   "Bold"
  130.       End
  131.       Begin VB.Menu FormatItalic 
  132.          Caption         =   "Italic"
  133.       End
  134.       Begin VB.Menu FormatUnderline 
  135.          Caption         =   "Underline"
  136.       End
  137.       Begin VB.Menu FormatRegular 
  138.          Caption         =   "Regular"
  139.       End
  140.    End
  141. Attribute VB_Name = "Form1"
  142. Attribute VB_GlobalNameSpace = False
  143. Attribute VB_Creatable = False
  144. Attribute VB_PredeclaredId = True
  145. Attribute VB_Exposed = False
  146. Private Sub EditCopy_Click()
  147.     Clipboard.SetText RichTextBox1.SelRTF
  148. End Sub
  149. Private Sub EditCut_Click()
  150.     Clipboard.SetText RichTextBox1.SelRTF
  151.     RichTextBox1.SelRTF = ""
  152. End Sub
  153. Private Sub EditFind_Click()
  154.     Form2.Show
  155. End Sub
  156. Private Sub EditFont_Click()
  157. On Error Resume Next
  158.     CommonDialog1.Flags = cdlCFBoth
  159.     CommonDialog1.ShowFont
  160.     RichTextBox1.SelFontName = CommonDialog1.FontName
  161.     RichTextBox1.SelBold = CommonDialog1.FontBold
  162.     RichTextBox1.SelItalic = CommonDialog1.FontItalic
  163.     RichTextBox1.SelFontSize = CommonDialog1.FontSize
  164. End Sub
  165. Private Sub EditPaste_Click()
  166.     RichTextBox1.SelRTF = Clipboard.GetText()
  167. End Sub
  168. Private Sub EditSelect_Click()
  169.     RichTextBox1.SelStart = 0
  170.     RichTextBox1.SelLength = Len(RichTextBox1.Text)
  171. End Sub
  172. Private Sub FileNew_Click()
  173.     RichTextBox1.Text = ""
  174. End Sub
  175. Private Sub FileOpen_Click()
  176. Dim txt As String
  177. Dim FNum As Integer
  178. 'On Error GoTo FileError:
  179.     CommonDialog1.CancelError = True
  180.     CommonDialog1.Flags = cdlOFNFileMustExist
  181.     CommonDialog1.DefaultExt = "RTF"
  182.     CommonDialog1.Filter = "RTF Files|*.RTF|Text Files|*.TXT|All Files|*.*"
  183.     CommonDialog1.ShowOpen
  184.     If UCase(Right(CommonDialog1.filename, 3)) = "RTF" Then
  185.         tmode = rtfRTF
  186.     Else
  187.         tmode = rtfText
  188.     End If
  189.         
  190.     RichTextBox1.LoadFile CommonDialog1.filename, tmode
  191.     OpenFile = CommonDialog1.filename
  192.     Exit Sub
  193. FileError:
  194.     If Err.Number = cdlCancel Then Exit Sub
  195.     MsgBox "Unkown error while opening file " & CommonDialog1.filename
  196.     OpenFile = ""
  197. End Sub
  198. Private Sub FileSave_Click()
  199. Dim FNum As Integer
  200. Dim txt As String
  201.     If OpenFile = "" Then
  202.         FileSaveAs_Click
  203.         Exit Sub
  204.     End If
  205. On Error GoTo FileError
  206.     EditSelect_Click
  207.     RichTextBox1.SaveFile OpenFile
  208.     Exit Sub
  209. FileError:
  210.     If Err.Number = cdlCancel Then Exit Sub
  211.     MsgBox "Unkown error while saving file " & OpenFile
  212.     OpenFile = ""
  213. End Sub
  214. Private Sub FileSaveAs_Click()
  215. Dim txt As String
  216. Dim FNum As Integer
  217. On Error GoTo FileError:
  218.     CommonDialog1.CancelError = True
  219.     CommonDialog1.DefaultExt = "RTF"
  220.     CommonDialog1.Filter = "RTF Files|*.RTF|Text Files|*.TXT|All Files|*.*"
  221.     CommonDialog1.ShowSave
  222.     RichTextBox1.SaveFile CommonDialog1.filename, rtfRTF
  223.     OpenFile = CommonDialog1.filename
  224.     Exit Sub
  225. FileError:
  226.     If Err.Number = cdlCancel Then Exit Sub
  227.     MsgBox "Unkown error while opening file " & CommonDialog1.filename
  228.     OpenFile = ""
  229. End Sub
  230. Private Sub Form_Load()
  231.     RichTextBox1.RightMargin = RichTextBox1.Width - 100
  232.     Frame1.Width = RichTextBox1.Width + 200
  233.     Slider1.Width = Form1.ScaleX(6, vbInches, vbTwips)
  234.     Slider2.Width = Form1.ScaleX(6, vbInches, vbTwips)
  235.     RichTextBox1.Top = Form1.Top + Frame1.Height + 100
  236.     RichTextBox1.Width = Form1.ScaleX(6, vbInches, vbTwips)
  237.     RichTextBox1.Height = Form1.Height - Frame1.Height - 1400
  238.     RichTextBox1.RightMargin = RichTextBox1.Width - 100
  239. End Sub
  240. Private Sub Form_Resize()
  241.     RichTextBox1.RightMargin = RichTextBox1.Width - 100
  242.     Frame1.Width = RichTextBox1.Width + 200
  243.     Slider1.Width = Form1.ScaleX(6, vbInches, vbTwips)
  244.     Slider2.Width = Form1.ScaleX(6, vbInches, vbTwips)
  245.     RichTextBox1.Top = Frame1.Top + Frame1.Height + 100
  246.     RichTextBox1.Width = Form1.ScaleX(6, vbInches, vbTwips)
  247.     RichTextBox1.Height = Form1.Height - Frame1.Height - 1800
  248.     RichTextBox1.RightMargin = RichTextBox1.Width - 100
  249. End Sub
  250. Private Sub FormatBold_Click()
  251.     FormatBold.Checked = Not FormatBold.Checked
  252.     RichTextBox1.SelBold = FormatBold.Checked
  253. End Sub
  254. Private Sub FormatItalic_Click()
  255.     FormatItalic.Checked = Not FormatItalic.Checked
  256.     RichTextBox1.SelItalic = FormatItalic.Checked
  257. End Sub
  258. Private Sub FormatRegular_Click()
  259.     FormatBold.Checked = False
  260.     FormatItalic.Checked = False
  261.     FormatUnderline.Checked = False
  262.     RichTextBox1.SelBold = False
  263.     RichTextBox1.SelItalic = False
  264.     RichTextBox1.SelUnderline = False
  265. End Sub
  266. Private Sub FormatUnderline_Click()
  267.     FormatUnderline.Checked = Not FormatUnderline.Checked
  268.     RichTextBox1.SelUnderline = FormatUnderline.Checked
  269. End Sub
  270. Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer)
  271.         
  272. ' Select word, or sentence
  273.     If Shift = vbCtrlMask Then
  274.         Select Case KeyCode
  275.             ' If Ctrl+S:
  276.             Case vbKeyS
  277.                 RichTextBox1.Span ".?!", False, True
  278.                 SelectionStart = RichTextBox1.SelStart
  279.                 ' Select to the end of the sentence.
  280.                 RichTextBox1.Span ".?!", True, True
  281.                 ' Extend selection to include punctuation.
  282.                 SelectionEnd = RichTextBox1.SelStart + RichTextBox1.SelLength
  283.                 RichTextBox1.SelStart = SelectionStart
  284.                 RichTextBox1.SelLength = SelectionEnd - SelectionStart
  285.                 
  286.             ' If Ctrl+W:
  287.             Case vbKeyW
  288.                 ' Select to the end of the word.
  289.                 RichTextBox1.Span " ,;:.?!", False, True
  290.                 SelectionStart = RichTextBox1.SelStart
  291.                 ' Select to the end of the word
  292.                 RichTextBox1.Span " ,;:.?!", True, True
  293.                 
  294.                 SelectionEnd = RichTextBox1.SelStart + RichTextBox1.SelLength
  295.                 RichTextBox1.SelStart = SelectionStart
  296.                 RichTextBox1.SelLength = SelectionEnd - SelectionStart
  297.                 
  298.             End Select
  299.     End If
  300. ' Move pointer by word or sentence
  301.     If Shift = (vbCtrlMask Or vbShiftMask) Then
  302.         Select Case KeyCode
  303.         Case vbKeyS
  304.             ' Move pointer to end of sentence.
  305.             RichTextBox1.UpTo ".?!", True, False
  306.         Case vbKeyW
  307.             ' Move pointer to end of word.
  308.             RichTextBox1.UpTo " ,;:.?!", True, False
  309.         End Select
  310.     End If
  311. End Sub
  312. Private Sub RichTextBox1_SelChange()
  313.     If Not IsNull(RichTextBox1.SelBold) Then FormatBold.Checked = RichTextBox1.SelBold
  314.     If Not IsNull(RichTextBox1.SelItalic) Then FormatItalic.Checked = RichTextBox1.SelItalic
  315.     If Not IsNull(RichTextBox1.SelUnderline) Then FormatUnderline.Checked = RichTextBox1.SelUnderline
  316. End Sub
  317. Private Sub Slider1_Scroll()
  318.     RichTextBox1.SelIndent = RichTextBox1.RightMargin * (Slider1.Value / Slider1.Max)
  319.     Slider2_Scroll
  320. End Sub
  321. Private Sub Slider2_Scroll()
  322.     RichTextBox1.SelHangingIndent = RichTextBox1.RightMargin * ((Slider2.Value - Slider1.Value) / Slider2.Max)
  323. End Sub
  324.