home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch4 / RichEdit.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-05  |  25.7 KB  |  746 lines

  1. VERSION 5.00
  2. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "richtx32.ocx"
  3. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
  4. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  5. Begin VB.Form frmRichEdit 
  6.    Caption         =   "RichEdit"
  7.    ClientHeight    =   4440
  8.    ClientLeft      =   165
  9.    ClientTop       =   735
  10.    ClientWidth     =   7680
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   4440
  13.    ScaleWidth      =   7680
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin MSComDlg.CommonDialog dlgFile 
  16.       Left            =   4800
  17.       Top             =   600
  18.       _ExtentX        =   847
  19.       _ExtentY        =   847
  20.       _Version        =   393216
  21.       CancelError     =   -1  'True
  22.       Filter          =   "RTF Files (*.rtf)|*.rtf|All Files (*.*)|*.*"
  23.       FilterIndex     =   1
  24.    End
  25.    Begin ComctlLib.Toolbar tbrButtons 
  26.       Align           =   1  'Align Top
  27.       Height          =   420
  28.       Left            =   0
  29.       TabIndex        =   1
  30.       Top             =   0
  31.       Width           =   7680
  32.       _ExtentX        =   13547
  33.       _ExtentY        =   741
  34.       ButtonWidth     =   609
  35.       ButtonHeight    =   582
  36.       Appearance      =   1
  37.       _Version        =   393216
  38.       BeginProperty Buttons {66833FE8-8583-11D1-B16A-00C0F0283628} 
  39.          NumButtons      =   24
  40.          BeginProperty Button1 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  41.             Key             =   "New"
  42.             Object.ToolTipText     =   "New"
  43.          EndProperty
  44.          BeginProperty Button2 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  45.             Key             =   "Open"
  46.             Object.ToolTipText     =   "Open"
  47.          EndProperty
  48.          BeginProperty Button3 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  49.             Key             =   "Save"
  50.             Object.ToolTipText     =   "Save"
  51.          EndProperty
  52.          BeginProperty Button4 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  53.             Key             =   "SaveAs"
  54.             Object.ToolTipText     =   "Save As"
  55.          EndProperty
  56.          BeginProperty Button5 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  57.             Style           =   3
  58.          EndProperty
  59.          BeginProperty Button6 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  60.             Key             =   "Bold"
  61.             Object.ToolTipText     =   "Bold"
  62.          EndProperty
  63.          BeginProperty Button7 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  64.             Key             =   "Italic"
  65.             Object.ToolTipText     =   "Italic"
  66.          EndProperty
  67.          BeginProperty Button8 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  68.             Key             =   "Underline"
  69.             Object.ToolTipText     =   "Underline"
  70.          EndProperty
  71.          BeginProperty Button9 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  72.             Key             =   "Strikethru"
  73.             Object.ToolTipText     =   "Strikethru"
  74.          EndProperty
  75.          BeginProperty Button10 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  76.             Style           =   3
  77.          EndProperty
  78.          BeginProperty Button11 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  79.             Key             =   "AlignLeft"
  80.             Object.ToolTipText     =   "Align Left"
  81.          EndProperty
  82.          BeginProperty Button12 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  83.             Key             =   "AlignCenter"
  84.             Object.ToolTipText     =   "Align Center"
  85.          EndProperty
  86.          BeginProperty Button13 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  87.             Key             =   "AlignRight"
  88.             Object.ToolTipText     =   "Align Right"
  89.          EndProperty
  90.          BeginProperty Button14 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  91.             Key             =   "Hanging"
  92.             Object.ToolTipText     =   "Hanging Indent"
  93.          EndProperty
  94.          BeginProperty Button15 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  95.             Style           =   3
  96.          EndProperty
  97.          BeginProperty Button16 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  98.             Key             =   "BulletedList"
  99.             Object.ToolTipText     =   "Bulleted List"
  100.          EndProperty
  101.          BeginProperty Button17 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  102.             Style           =   3
  103.          EndProperty
  104.          BeginProperty Button18 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  105.             Key             =   "IncreaseIndentation"
  106.             Object.ToolTipText     =   "Increase Indentation"
  107.          EndProperty
  108.          BeginProperty Button19 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  109.             Key             =   "DecreaseIndentation"
  110.             Object.ToolTipText     =   "Decrease Indentation"
  111.          EndProperty
  112.          BeginProperty Button20 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  113.             Style           =   3
  114.          EndProperty
  115.          BeginProperty Button21 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  116.             Key             =   "Black"
  117.             Object.ToolTipText     =   "Black"
  118.          EndProperty
  119.          BeginProperty Button22 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  120.             Key             =   "Red"
  121.             Object.ToolTipText     =   "Red"
  122.          EndProperty
  123.          BeginProperty Button23 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  124.             Key             =   "Green"
  125.             Object.ToolTipText     =   "Green"
  126.          EndProperty
  127.          BeginProperty Button24 {66833FEA-8583-11D1-B16A-00C0F0283628} 
  128.             Key             =   "Blue"
  129.             Object.ToolTipText     =   "Blue"
  130.          EndProperty
  131.       EndProperty
  132.    End
  133.    Begin ComctlLib.ImageList imlButtons 
  134.       Left            =   4800
  135.       Top             =   1200
  136.       _ExtentX        =   1005
  137.       _ExtentY        =   1005
  138.       BackColor       =   -2147483643
  139.       ImageWidth      =   16
  140.       ImageHeight     =   16
  141.       MaskColor       =   12632256
  142.       _Version        =   393216
  143.       BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
  144.          NumListImages   =   19
  145.          BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  146.             Picture         =   "RichEdit.frx":0000
  147.             Key             =   ""
  148.          EndProperty
  149.          BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  150.             Picture         =   "RichEdit.frx":0112
  151.             Key             =   ""
  152.          EndProperty
  153.          BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  154.             Picture         =   "RichEdit.frx":0224
  155.             Key             =   ""
  156.          EndProperty
  157.          BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  158.             Picture         =   "RichEdit.frx":0336
  159.             Key             =   ""
  160.          EndProperty
  161.          BeginProperty ListImage5 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  162.             Picture         =   "RichEdit.frx":0448
  163.             Key             =   ""
  164.          EndProperty
  165.          BeginProperty ListImage6 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  166.             Picture         =   "RichEdit.frx":055A
  167.             Key             =   ""
  168.          EndProperty
  169.          BeginProperty ListImage7 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  170.             Picture         =   "RichEdit.frx":066C
  171.             Key             =   ""
  172.          EndProperty
  173.          BeginProperty ListImage8 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  174.             Picture         =   "RichEdit.frx":077E
  175.             Key             =   ""
  176.          EndProperty
  177.          BeginProperty ListImage9 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  178.             Picture         =   "RichEdit.frx":0890
  179.             Key             =   ""
  180.          EndProperty
  181.          BeginProperty ListImage10 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  182.             Picture         =   "RichEdit.frx":09A2
  183.             Key             =   ""
  184.          EndProperty
  185.          BeginProperty ListImage11 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  186.             Picture         =   "RichEdit.frx":0AB4
  187.             Key             =   ""
  188.          EndProperty
  189.          BeginProperty ListImage12 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  190.             Picture         =   "RichEdit.frx":0BC6
  191.             Key             =   ""
  192.          EndProperty
  193.          BeginProperty ListImage13 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  194.             Picture         =   "RichEdit.frx":0CD8
  195.             Key             =   ""
  196.          EndProperty
  197.          BeginProperty ListImage14 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  198.             Picture         =   "RichEdit.frx":0DEA
  199.             Key             =   ""
  200.          EndProperty
  201.          BeginProperty ListImage15 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  202.             Picture         =   "RichEdit.frx":0EFC
  203.             Key             =   ""
  204.          EndProperty
  205.          BeginProperty ListImage16 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  206.             Picture         =   "RichEdit.frx":100E
  207.             Key             =   ""
  208.          EndProperty
  209.          BeginProperty ListImage17 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  210.             Picture         =   "RichEdit.frx":1120
  211.             Key             =   ""
  212.          EndProperty
  213.          BeginProperty ListImage18 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  214.             Picture         =   "RichEdit.frx":1232
  215.             Key             =   ""
  216.          EndProperty
  217.          BeginProperty ListImage19 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  218.             Picture         =   "RichEdit.frx":1344
  219.             Key             =   ""
  220.          EndProperty
  221.       EndProperty
  222.    End
  223.    Begin RichTextLib.RichTextBox rchText 
  224.       Height          =   1455
  225.       Left            =   240
  226.       TabIndex        =   0
  227.       Top             =   960
  228.       Width           =   4215
  229.       _ExtentX        =   7435
  230.       _ExtentY        =   2566
  231.       _Version        =   393217
  232.       TextRTF         =   $"RichEdit.frx":1456
  233.    End
  234.    Begin VB.Menu mnuFile 
  235.       Caption         =   "&File"
  236.       Begin VB.Menu mnuFileNew 
  237.          Caption         =   "&New"
  238.          Shortcut        =   ^N
  239.       End
  240.       Begin VB.Menu mnuFileOpen 
  241.          Caption         =   "&Open..."
  242.          Shortcut        =   ^O
  243.       End
  244.       Begin VB.Menu mnuFileSave 
  245.          Caption         =   "&Save"
  246.          Shortcut        =   ^S
  247.       End
  248.       Begin VB.Menu mnuFileSaveAs 
  249.          Caption         =   "Save &As..."
  250.          Shortcut        =   ^A
  251.       End
  252.       Begin VB.Menu mnuFileSep1 
  253.          Caption         =   "-"
  254.       End
  255.       Begin VB.Menu mnuFileRecent 
  256.          Caption         =   "Recent File"
  257.          Index           =   1
  258.          Visible         =   0   'False
  259.       End
  260.       Begin VB.Menu mnuFileRecent 
  261.          Caption         =   "Recent File"
  262.          Index           =   2
  263.          Visible         =   0   'False
  264.       End
  265.       Begin VB.Menu mnuFileRecent 
  266.          Caption         =   "Recent File"
  267.          Index           =   3
  268.          Visible         =   0   'False
  269.       End
  270.       Begin VB.Menu mnuFileRecent 
  271.          Caption         =   "Recent File"
  272.          Index           =   4
  273.          Visible         =   0   'False
  274.       End
  275.       Begin VB.Menu mnuFileSep2 
  276.          Caption         =   "-"
  277.          Visible         =   0   'False
  278.       End
  279.       Begin VB.Menu mnuFileExit 
  280.          Caption         =   "E&xit"
  281.       End
  282.    End
  283.    Begin VB.Menu mnuFont 
  284.       Caption         =   "F&ont"
  285.       Begin VB.Menu mnuFontBold 
  286.          Caption         =   "&Bold"
  287.          Shortcut        =   ^B
  288.       End
  289.       Begin VB.Menu mnuFontItalic 
  290.          Caption         =   "&Italic"
  291.          Shortcut        =   ^I
  292.       End
  293.       Begin VB.Menu mnuFontUnderline 
  294.          Caption         =   "&Underline"
  295.          Shortcut        =   ^U
  296.       End
  297.       Begin VB.Menu mnuFontStrikethru 
  298.          Caption         =   "&Strikethru"
  299.       End
  300.    End
  301.    Begin VB.Menu mnuParagraph 
  302.       Caption         =   "&Paragraph"
  303.       Begin VB.Menu mnuParagraphAlignLeft 
  304.          Caption         =   "Align &Left"
  305.       End
  306.       Begin VB.Menu mnuParagraphAlignCenter 
  307.          Caption         =   "Align &Center"
  308.       End
  309.       Begin VB.Menu mnuParagraphAlignRight 
  310.          Caption         =   "Align &Right"
  311.       End
  312.       Begin VB.Menu mnuParagraphSep1 
  313.          Caption         =   "-"
  314.       End
  315.       Begin VB.Menu mnuParagraphBulletedList 
  316.          Caption         =   "B&ulleted List"
  317.       End
  318.       Begin VB.Menu mnuParagraphSep2 
  319.          Caption         =   "-"
  320.       End
  321.       Begin VB.Menu mnuParagraphIncreaseIndentation 
  322.          Caption         =   "&Increase Indentation"
  323.       End
  324.       Begin VB.Menu mnuParagraphDecreaseIndentation 
  325.          Caption         =   "&Decrease Indentation"
  326.       End
  327.       Begin VB.Menu mnuParagraphHangingIndent 
  328.          Caption         =   "&Hanging Indent"
  329.       End
  330.    End
  331.    Begin VB.Menu mnuColor 
  332.       Caption         =   "&Color"
  333.       Begin VB.Menu mnuSetColor 
  334.          Caption         =   "&Black"
  335.          Index           =   0
  336.       End
  337.       Begin VB.Menu mnuSetColor 
  338.          Caption         =   "&Red"
  339.          Index           =   1
  340.       End
  341.       Begin VB.Menu mnuSetColor 
  342.          Caption         =   "&Green"
  343.          Index           =   2
  344.       End
  345.       Begin VB.Menu mnuSetColor 
  346.          Caption         =   "B&lue"
  347.          Index           =   3
  348.       End
  349.    End
  350. Attribute VB_Name = "frmRichEdit"
  351. Attribute VB_GlobalNameSpace = False
  352. Attribute VB_Creatable = False
  353. Attribute VB_PredeclaredId = True
  354. Attribute VB_Exposed = False
  355. Option Explicit
  356. ' Indicates whether the data has been modified since
  357. ' saved or loaded.
  358. Private DataModified As Boolean
  359. ' The currently loaded file.
  360. Private FileName As String
  361. Private FileTitle As String
  362. ' Return true if it is safe to discard the data.
  363. Private Function DataIsSafe() As Boolean
  364.     If DataModified Then
  365.         Select Case MsgBox("The data has been modified. Do you want to save the changes?", vbYesNoCancel)
  366.             Case vbYes
  367.                 mnuFileSave_Click
  368.                 DataIsSafe = Not DataModified
  369.             Case vbNo
  370.                 DataIsSafe = True
  371.             Case vbCancel
  372.                 DataIsSafe = False
  373.         End Select
  374.     Else
  375.         DataIsSafe = True
  376.     End If
  377. End Function
  378. Private Sub rchText_KeyPress(KeyAscii As Integer)
  379. Const CTRL_B = 2
  380. Const CTRL_I = 9
  381. Const CTRL_U = 21
  382. Const CTRL_S = 19
  383.     If KeyAscii = CTRL_B Then
  384.         rchText.SelBold = Not rchText.SelBold
  385.         KeyAscii = 0
  386.     End If
  387.     If KeyAscii = CTRL_I Then
  388.         rchText.SelItalic = Not rchText.SelItalic
  389.         KeyAscii = 0
  390.     End If
  391.     If KeyAscii = CTRL_U Then
  392.         rchText.SelUnderline = Not rchText.SelUnderline
  393.         KeyAscii = 0
  394.     End If
  395.     If KeyAscii = CTRL_S Then
  396.         rchText.SelStrikeThru = Not rchText.SelStrikeThru
  397.         KeyAscii = 0
  398.     End If
  399.     ' Recheck the check box values.
  400.     If KeyAscii = 0 Then rchText_SelChange
  401. End Sub
  402. ' Set the menu item and button states for the
  403. ' selected text.
  404. Private Sub rchText_SelChange()
  405. Dim i As Integer
  406.     If rchText.SelBold Then
  407.         tbrButtons.Buttons("Bold").value = tbrPressed
  408.         mnuFontBold.Checked = True
  409.     Else
  410.         tbrButtons.Buttons("Bold").value = tbrUnpressed
  411.         mnuFontBold.Checked = False
  412.     End If
  413.     If rchText.SelItalic Then
  414.         tbrButtons.Buttons("Italic").value = tbrPressed
  415.         mnuFontItalic.Checked = True
  416.     Else
  417.         tbrButtons.Buttons("Italic").value = tbrUnpressed
  418.         mnuFontItalic.Checked = False
  419.     End If
  420.     If rchText.SelUnderline Then
  421.         tbrButtons.Buttons("Underline").value = tbrPressed
  422.         mnuFontUnderline.Checked = True
  423.     Else
  424.         tbrButtons.Buttons("Underline").value = tbrUnpressed
  425.         mnuFontUnderline.Checked = False
  426.     End If
  427.     If rchText.SelStrikeThru Then
  428.         tbrButtons.Buttons("Strikethru").value = tbrPressed
  429.         mnuFontStrikethru.Checked = True
  430.     Else
  431.         tbrButtons.Buttons("Strikethru").value = tbrUnpressed
  432.         mnuFontStrikethru.Checked = False
  433.     End If
  434.     If rchText.SelBullet Then
  435.         tbrButtons.Buttons("BulletedList").value = tbrPressed
  436.         mnuParagraphBulletedList.Checked = True
  437.     Else
  438.         tbrButtons.Buttons("BulletedList").value = tbrUnpressed
  439.         mnuParagraphBulletedList.Checked = False
  440.     End If
  441.     If rchText.SelHangingIndent > 0 Then
  442.         tbrButtons.Buttons("Hanging").value = tbrPressed
  443.         mnuParagraphHangingIndent.Checked = True
  444.     Else
  445.         tbrButtons.Buttons("Hanging").value = tbrUnpressed
  446.         mnuParagraphHangingIndent.Checked = False
  447.     End If
  448.     tbrButtons.Buttons("AlignLeft").value = tbrUnpressed
  449.     tbrButtons.Buttons("AlignCenter").value = tbrUnpressed
  450.     tbrButtons.Buttons("AlignRight").value = tbrUnpressed
  451.     mnuParagraphAlignLeft.Checked = False
  452.     mnuParagraphAlignCenter.Checked = False
  453.     mnuParagraphAlignRight.Checked = False
  454.     Select Case rchText.SelAlignment
  455.         Case rtfLeft
  456.             tbrButtons.Buttons("AlignLeft").value = tbrPressed
  457.             mnuParagraphAlignLeft.Checked = True
  458.         Case rtfCenter
  459.             tbrButtons.Buttons("AlignCenter").value = tbrPressed
  460.             mnuParagraphAlignCenter.Checked = True
  461.         Case rtfRight
  462.             tbrButtons.Buttons("AlignRight").value = tbrPressed
  463.             mnuParagraphAlignRight.Checked = True
  464.     End Select
  465.     tbrButtons.Buttons("Black").value = tbrUnpressed
  466.     tbrButtons.Buttons("Red").value = tbrUnpressed
  467.     tbrButtons.Buttons("Green").value = tbrUnpressed
  468.     tbrButtons.Buttons("Blue").value = tbrUnpressed
  469.     For i = 0 To 3
  470.         mnuSetColor(i).Checked = False
  471.     Next i
  472.     Select Case rchText.SelColor
  473.         Case vbBlack
  474.             tbrButtons.Buttons("Black").value = tbrPressed
  475.             mnuSetColor(0).Checked = True
  476.         Case vbRed
  477.             tbrButtons.Buttons("Red").value = tbrPressed
  478.             mnuSetColor(1).Checked = True
  479.         Case vbGreen
  480.             tbrButtons.Buttons("Green").value = tbrPressed
  481.             mnuSetColor(2).Checked = True
  482.         Case vbBlue
  483.             tbrButtons.Buttons("Blue").value = tbrPressed
  484.             mnuSetColor(3).Checked = True
  485.     End Select
  486.     tbrButtons.Refresh
  487. End Sub
  488. ' Mark the data as mofified.
  489. Private Sub SetModified()
  490.     ' Do nothing if the data is already modified.
  491.     If DataModified Then Exit Sub
  492.     DataModified = True
  493.     Caption = "RichEdit*[" & FileTitle & "]"
  494. End Sub
  495. Private Sub Form_Load()
  496. Dim btn As Integer
  497. Dim pic As Integer
  498.     dlgFile.InitDir = App.Path
  499.     ' Prepare the toolbar buttons. This is done here
  500.     ' because it is a hassle to do during design time.
  501.     ' To change the images, you need to disassociate
  502.     ' the toolbar from the image list and then the
  503.     ' toolbar loses this information.
  504.     tbrButtons.ImageList = imlButtons
  505.     pic = 1
  506.     For btn = 1 To tbrButtons.Buttons.Count
  507.         With tbrButtons.Buttons(btn)
  508.             ' See if this button is a button.
  509.             If .Style = tbrDefault Then
  510.                 ' Give this button the next picture.
  511.                 .Image = pic
  512.                 pic = pic + 1
  513.             End If
  514.         End With
  515.     Next btn
  516. End Sub
  517. ' See if it is safe to unload.
  518. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  519.     Cancel = Not DataIsSafe()
  520. End Sub
  521. ' Make the RichTextBox as large as possible.
  522. Private Sub Form_Resize()
  523. Dim hgt As Single
  524.     If WindowState = vbMinimized Then Exit Sub
  525.     hgt = ScaleHeight - tbrButtons.Height
  526.     If hgt < 120 Then hgt = 120
  527.     rchText.Move 0, tbrButtons.Height, ScaleWidth, hgt
  528. End Sub
  529. ' Unload. If the data is not safe, the QueryUnload
  530. ' event handler will stop the unload.
  531. Private Sub mnuFileExit_Click()
  532.     Unload Me
  533. End Sub
  534. ' Start a new file.
  535. Private Sub mnuFileNew_Click()
  536.     ' Make sure the data is safe.
  537.     If Not DataIsSafe() Then Exit Sub
  538.     ' Start over.
  539.     FileTitle = ""
  540.     FileName = ""
  541.     rchText.Text = ""
  542.     Caption = "RichEdit []"
  543.     DataModified = False
  544. End Sub
  545. ' Open a file.
  546. Private Sub mnuFileOpen_Click()
  547.     ' Make sure the data is safe.
  548.     If Not DataIsSafe() Then Exit Sub
  549.     ' Get the file.
  550.     dlgFile.Flags = _
  551.         cdlOFNExplorer + _
  552.         cdlOFNFileMustExist + _
  553.         cdlOFNHideReadOnly + _
  554.         cdlOFNLongNames
  555.     On Error Resume Next
  556.     dlgFile.ShowOpen
  557.     If Err.Number = cdlCancel Then
  558.         Exit Sub
  559.     ElseIf Err.Number > 0 Then
  560.         MsgBox "Error " & Format$(Err.Number) & _
  561.             " selecting file" & vbCrLf & Err.Description
  562.         Exit Sub
  563.     End If
  564.     ' Read the file.
  565.     On Error GoTo ReadErr
  566.     rchText.LoadFile dlgFile.FileName
  567.     On Error GoTo 0
  568.     FileName = dlgFile.FileName
  569.     FileTitle = dlgFile.FileTitle
  570.     Caption = "RichEdit [" & FileTitle & "]"
  571.     DataModified = False
  572.     Exit Sub
  573. ReadErr:
  574.     MsgBox "Error " & Format$(Err.Number) & _
  575.         " reading file '" & dlgFile.FileName & _
  576.         "'" & vbCrLf & Err.Description
  577.     Exit Sub
  578. End Sub
  579. ' Save the file.
  580. Private Sub mnuFileSave_Click()
  581.     ' See if we have a file name.
  582.     If Len(FileTitle) = 0 Then
  583.         mnuFileSaveAs_Click
  584.         Exit Sub
  585.     End If
  586.     ' Save the file.
  587.     On Error GoTo SaveErr
  588.     rchText.SaveFile FileName
  589.     On Error GoTo 0
  590.     Caption = "RichEdit [" & FileTitle & "]"
  591.     DataModified = False
  592.     Exit Sub
  593. SaveErr:
  594.     MsgBox "Error " & Format$(Err.Number) & _
  595.         " saving file '" & FileName & _
  596.         "'" & vbCrLf & Err.Description
  597.     Exit Sub
  598. End Sub
  599. ' Save the file with a new name.
  600. Private Sub mnuFileSaveAs_Click()
  601.     ' Get the file name.
  602.     dlgFile.Flags = _
  603.         cdlOFNExplorer + _
  604.         cdlOFNPathMustExist + _
  605.         cdlOFNHideReadOnly + _
  606.         cdlOFNLongNames + _
  607.         cdlOFNOverwritePrompt
  608.     On Error Resume Next
  609.     dlgFile.ShowSave
  610.     If Err.Number = cdlCancel Then
  611.         Exit Sub
  612.     ElseIf Err.Number > 0 Then
  613.         MsgBox "Error " & Format$(Err.Number) & _
  614.             " selecting file" & vbCrLf & Err.Description
  615.         Exit Sub
  616.     End If
  617.     ' Save the file.
  618.     On Error GoTo SaveAsErr
  619.     rchText.SaveFile dlgFile.FileName
  620.     On Error GoTo 0
  621.     FileName = dlgFile.FileName
  622.     FileTitle = dlgFile.FileTitle
  623.     Caption = "RichEdit [" & FileTitle & "]"
  624.     DataModified = False
  625.     Exit Sub
  626. SaveAsErr:
  627.     MsgBox "Error " & Format$(Err.Number) & _
  628.         " saving file '" & dlgFile.FileName & _
  629.         "'" & vbCrLf & Err.Description
  630.     Exit Sub
  631. End Sub
  632. Private Sub mnuFontBold_Click()
  633.     rchText.SelBold = Not rchText.SelBold
  634.     rchText_SelChange
  635. End Sub
  636. Private Sub mnuFontItalic_Click()
  637.     rchText.SelItalic = Not rchText.SelItalic
  638.     rchText_SelChange
  639. End Sub
  640. Private Sub mnuFontStrikethru_Click()
  641.     rchText.SelStrikeThru = Not rchText.SelStrikeThru
  642.     rchText_SelChange
  643. End Sub
  644. Private Sub mnuFontUnderline_Click()
  645.     rchText.SelUnderline = Not rchText.SelUnderline
  646.     rchText_SelChange
  647. End Sub
  648. Private Sub mnuParagraphAlignCenter_Click()
  649.     rchText.SelAlignment = rtfCenter
  650.     rchText_SelChange
  651. End Sub
  652. Private Sub mnuParagraphAlignLeft_Click()
  653.     rchText.SelAlignment = rtfLeft
  654.     rchText_SelChange
  655. End Sub
  656. Private Sub mnuParagraphAlignRight_Click()
  657.     rchText.SelAlignment = rtfRight
  658.     rchText_SelChange
  659. End Sub
  660. Private Sub mnuParagraphBulletedList_Click()
  661.     If IsNull(rchText.SelBullet) Then
  662.         rchText.SelBullet = True
  663.     Else
  664.         rchText.SelBullet = Not rchText.SelBullet
  665.     End If
  666.     rchText_SelChange
  667. End Sub
  668. Private Sub mnuParagraphDecreaseIndentation_Click()
  669.     rchText.SelIndent = rchText.SelIndent - 240
  670. End Sub
  671. Private Sub mnuParagraphHangingIndent_Click()
  672.     If IsNull(rchText.SelHangingIndent) Then
  673.         rchText.SelHangingIndent = 240
  674.     Else
  675.         rchText.SelHangingIndent = 240 - rchText.SelHangingIndent
  676.     End If
  677.     rchText_SelChange
  678. End Sub
  679. Private Sub mnuParagraphIncreaseIndentation_Click()
  680.     rchText.SelIndent = rchText.SelIndent + 240
  681. End Sub
  682. Private Sub mnuSetColor_Click(Index As Integer)
  683.     Select Case Index
  684.         Case 0
  685.             rchText.SelColor = vbBlack
  686.         Case 1
  687.             rchText.SelColor = vbRed
  688.         Case 2
  689.             rchText.SelColor = vbGreen
  690.         Case 3
  691.             rchText.SelColor = vbBlue
  692.     End Select
  693.     rchText_SelChange
  694. End Sub
  695. ' Mark the data as modified.
  696. Private Sub rchText_Change()
  697.     SetModified
  698. End Sub
  699. ' Execute a command.
  700. Private Sub tbrButtons_ButtonClick(ByVal Button As ComctlLib.Button)
  701.     Select Case Button.Key
  702.         Case "New"
  703.             mnuFileNew_Click
  704.         Case "Open"
  705.             mnuFileOpen_Click
  706.         Case "Save"
  707.             mnuFileSave_Click
  708.         Case "SaveAs"
  709.             mnuFileSaveAs_Click
  710.         Case "Bold"
  711.             mnuFontBold_Click
  712.         Case "Italic"
  713.             mnuFontItalic_Click
  714.         Case "Underline"
  715.             mnuFontUnderline_Click
  716.         Case "Strikethru"
  717.             mnuFontStrikethru_Click
  718.         Case "AlignLeft"
  719.             mnuParagraphAlignLeft_Click
  720.         Case "AlignCenter"
  721.             mnuParagraphAlignCenter_Click
  722.         Case "AlignRight"
  723.             mnuParagraphAlignRight_Click
  724.         Case "Hanging"
  725.             mnuParagraphHangingIndent_Click
  726.         Case "BulletedList"
  727.             mnuParagraphBulletedList_Click
  728.         Case "IncreaseIndentation"
  729.             mnuParagraphIncreaseIndentation_Click
  730.         Case "DecreaseIndentation"
  731.             mnuParagraphDecreaseIndentation_Click
  732.         Case "Black"
  733.             mnuSetColor_Click 0
  734.         Case "Red"
  735.             mnuSetColor_Click 1
  736.         Case "Green"
  737.             mnuSetColor_Click 2
  738.         Case "Blue"
  739.             mnuSetColor_Click 3
  740.         Case Else
  741.             MsgBox "Unknown button '" & Button.Key & _
  742.                 "'"
  743.             Stop
  744.     End Select
  745. End Sub
  746.