home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch09 / rtfpad / rtbox.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-07-03  |  12.4 KB  |  362 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
  4. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
  5. Begin VB.Form EditorForm 
  6.    Caption         =   "RTFPad"
  7.    ClientHeight    =   5730
  8.    ClientLeft      =   2040
  9.    ClientTop       =   1965
  10.    ClientWidth     =   11160
  11.    LinkTopic       =   "Form1"
  12.    PaletteMode     =   1  'UseZOrder
  13.    ScaleHeight     =   5730
  14.    ScaleWidth      =   11160
  15.    Begin RichTextLib.RichTextBox RichTextBox1 
  16.       Height          =   3630
  17.       Left            =   150
  18.       TabIndex        =   0
  19.       Top             =   750
  20.       Width           =   10620
  21.       _ExtentX        =   18733
  22.       _ExtentY        =   6403
  23.       _Version        =   393217
  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            =   60
  42.       TabIndex        =   2
  43.       TabStop         =   0   'False
  44.       Top             =   360
  45.       Width           =   10800
  46.       _ExtentX        =   19050
  47.       _ExtentY        =   529
  48.       _Version        =   393216
  49.       LargeChange     =   4
  50.       Max             =   15
  51.    End
  52.    Begin ComctlLib.Slider Slider1 
  53.       Height          =   285
  54.       Left            =   60
  55.       TabIndex        =   1
  56.       TabStop         =   0   'False
  57.       Top             =   120
  58.       Width           =   10800
  59.       _ExtentX        =   19050
  60.       _ExtentY        =   503
  61.       _Version        =   393216
  62.       LargeChange     =   4
  63.       Max             =   15
  64.       TickStyle       =   3
  65.    End
  66.    Begin VB.Frame Frame1 
  67.       Height          =   750
  68.       Left            =   15
  69.       TabIndex        =   3
  70.       Top             =   -30
  71.       Width           =   11040
  72.    End
  73.    Begin MSComDlg.CommonDialog CommonDialog1 
  74.       Left            =   195
  75.       Top             =   4515
  76.       _ExtentX        =   847
  77.       _ExtentY        =   847
  78.       _Version        =   393216
  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 separator1 
  90.          Caption         =   "-"
  91.       End
  92.       Begin VB.Menu FileSave 
  93.          Caption         =   "Save"
  94.       End
  95.       Begin VB.Menu FileSaveAs 
  96.          Caption         =   "Save As"
  97.       End
  98.       Begin VB.Menu separator2 
  99.          Caption         =   "-"
  100.       End
  101.       Begin VB.Menu FilePrint 
  102.          Caption         =   "Print"
  103.       End
  104.       Begin VB.Menu separator3 
  105.          Caption         =   "-"
  106.       End
  107.       Begin VB.Menu FileExit 
  108.          Caption         =   "Exit"
  109.       End
  110.    End
  111.    Begin VB.Menu EditMenu 
  112.       Caption         =   "Edit"
  113.       Begin VB.Menu EditCopy 
  114.          Caption         =   "Copy"
  115.       End
  116.       Begin VB.Menu EditCut 
  117.          Caption         =   "Cut"
  118.       End
  119.       Begin VB.Menu EditPaste 
  120.          Caption         =   "Paste"
  121.       End
  122.       Begin VB.Menu EditSelect 
  123.          Caption         =   "Select All"
  124.       End
  125.       Begin VB.Menu EditSeparator 
  126.          Caption         =   "-"
  127.       End
  128.       Begin VB.Menu EditFind 
  129.          Caption         =   "Find"
  130.       End
  131.    End
  132.    Begin VB.Menu FormatMenu 
  133.       Caption         =   "Format"
  134.       Begin VB.Menu EditFont 
  135.          Caption         =   "Font"
  136.       End
  137.       Begin VB.Menu FormatSeparator 
  138.          Caption         =   "-"
  139.       End
  140.       Begin VB.Menu FormatBold 
  141.          Caption         =   "Bold"
  142.       End
  143.       Begin VB.Menu FormatItalic 
  144.          Caption         =   "Italic"
  145.       End
  146.       Begin VB.Menu FormatUnderline 
  147.          Caption         =   "Underline"
  148.       End
  149.       Begin VB.Menu FormatRegular 
  150.          Caption         =   "Regular"
  151.       End
  152.    End
  153. Attribute VB_Name = "EditorForm"
  154. Attribute VB_GlobalNameSpace = False
  155. Attribute VB_Creatable = False
  156. Attribute VB_PredeclaredId = True
  157. Attribute VB_Exposed = False
  158. '  ******************************
  159. '  ******************************
  160. '  ** MASTERING VB6            **
  161. '  ** by Evangelos Petroutos   **
  162. '  ** SYBEX, 1998              **
  163. '  ******************************
  164. '  ******************************
  165. Private Sub EditCopy_Click()
  166.     Clipboard.SetText RichTextBox1.SelRTF
  167. End Sub
  168. Private Sub EditCut_Click()
  169.     Clipboard.SetText RichTextBox1.SelRTF
  170.     RichTextBox1.SelRTF = ""
  171. End Sub
  172. Private Sub EditFind_Click()
  173.     'SearchForm.Show
  174.     SetWindowPos SearchForm.hwnd, HWND_TOPMOST, Me.CurrentX, Me.CurrentY, 470, 155, SWP_SHOWWINDOW
  175. End Sub
  176. Private Sub EditFont_Click()
  177. On Error Resume Next
  178.     CommonDialog1.Flags = cdlCFBoth
  179.     CommonDialog1.ShowFont
  180.     RichTextBox1.SelFontName = CommonDialog1.FontName
  181.     RichTextBox1.SelBold = CommonDialog1.FontBold
  182.     RichTextBox1.SelItalic = CommonDialog1.FontItalic
  183.     RichTextBox1.SelFontSize = CommonDialog1.FontSize
  184. End Sub
  185. Private Sub EditPaste_Click()
  186.     If Clipboard.GetFormat(vbCFRTF) Then
  187.         RichTextBox1.SelRTF = Clipboard.GetData(vbCFRTF)
  188.     ElseIf Clipboard.GetFormat(vbCFText) Then
  189.         RichTextBox1.SelText = Clipboard.GetText
  190.     End If
  191. End Sub
  192. Private Sub EditSelect_Click()
  193.     RichTextBox1.SelStart = 0
  194.     RichTextBox1.SelLength = Len(RichTextBox1.Text)
  195. End Sub
  196. Private Sub FileNew_Click()
  197.     RichTextBox1.Text = ""
  198. End Sub
  199. Private Sub FileOpen_Click()
  200. Dim txt As String
  201. Dim FNum As Integer
  202. On Error GoTo FileError:
  203.     CommonDialog1.CancelError = True
  204.     CommonDialog1.Flags = cdlOFNFileMustExist
  205.     CommonDialog1.DefaultExt = "RTF"
  206.     CommonDialog1.Filter = "RTF Files|*.RTF|Text Files|*.TXT|All Files|*.*"
  207.     CommonDialog1.InitDir = App.Path
  208.     CommonDialog1.ShowOpen
  209.     If UCase(Right(CommonDialog1.FileName, 3)) = "RTF" Then
  210.         tmode = rtfRTF
  211.     Else
  212.         tmode = rtfText
  213.     End If
  214.     RichTextBox1.LoadFile CommonDialog1.FileName, tmode
  215.     OpenFile = CommonDialog1.FileName
  216.     Exit Sub
  217. FileError:
  218.     If Err.Number = cdlCancel Then Exit Sub
  219.     MsgBox "Unkown error while opening file " & CommonDialog1.FileName
  220.     OpenFile = ""
  221. End Sub
  222. Private Sub FilePrint_Click()
  223. Dim lastPosition As Long, lastSelection As Long
  224.     EditSelect_Click
  225.     RichTextBox1.SelPrint Printer.hDC
  226.     RichTextBox1.SelStart = lastPosition
  227.     RichTextBox1.SelLength = lastSelection
  228. End Sub
  229. Private Sub FileSave_Click()
  230. Dim FNum As Integer
  231. Dim txt As String
  232.     If OpenFile = "" Then
  233.         FileSaveAs_Click
  234.         Exit Sub
  235.     End If
  236. On Error GoTo FileError
  237.     EditSelect_Click
  238.     RichTextBox1.SaveFile OpenFile
  239.     Exit Sub
  240. FileError:
  241.     If Err.Number = cdlCancel Then Exit Sub
  242.     MsgBox "Unkown error while saving file " & OpenFile
  243.     OpenFile = ""
  244. End Sub
  245. Private Sub FileSaveAs_Click()
  246. Dim txt As String
  247. Dim FNum As Integer
  248. On Error GoTo FileError:
  249.     CommonDialog1.CancelError = True
  250.     CommonDialog1.DefaultExt = "RTF"
  251.     CommonDialog1.Filter = "RTF Files|*.RTF|Text Files|*.TXT|All Files|*.*"
  252.     CommonDialog1.ShowSave
  253.     RichTextBox1.SaveFile CommonDialog1.FileName, rtfRTF
  254.     OpenFile = CommonDialog1.FileName
  255.     Exit Sub
  256. FileError:
  257.     If Err.Number = cdlCancel Then Exit Sub
  258.     MsgBox "Unkown error while opening file " & CommonDialog1.FileName
  259.     OpenFile = ""
  260. End Sub
  261. Private Sub Form_Load()
  262. '    Slider1.Width = EditorForm.ScaleX(7.5, vbInches, vbTwips)
  263. '    Slider2.Width = EditorForm.ScaleX(7.5, vbInches, vbTwips)
  264. '    RichTextBox1.Top = EditorForm.Top + Frame1.Height + 100
  265. '    RichTextBox1.Width = EditorForm.ScaleX(7.5, vbInches, vbTwips) + 8 * Screen.TwipsPerPixelX
  266. '    RichTextBox1.Height = EditorForm.Height - Frame1.Height - 12 * Screen.TwipsPerPixelX
  267. '    RichTextBox1.RightMargin = RichTextBox1.Width - 15 * Screen.TwipsPerPixelX
  268. End Sub
  269. Private Sub Form_Resize()
  270.     Slider1.Width = EditorForm.ScaleX(7.5, vbInches, vbTwips) + 6 * Screen.TwipsPerPixelX
  271.     Slider2.Width = EditorForm.ScaleX(7.5, vbInches, vbTwips) + 6 * Screen.TwipsPerPixelX
  272.     RichTextBox1.Top = Frame1.Top + Frame1.Height + 100
  273.     RichTextBox1.Width = EditorForm.ScaleX(7.5, vbInches, vbTwips) + 12 * Screen.TwipsPerPixelX
  274.     RichTextBox1.Height = EditorForm.ScaleHeight - Frame1.Height - 12 * Screen.TwipsPerPixelX
  275.     RichTextBox1.RightMargin = RichTextBox1.Width - 15 * Screen.TwipsPerPixelX
  276. End Sub
  277. Private Sub FormatBold_Click()
  278.     FormatBold.Checked = Not FormatBold.Checked
  279.     RichTextBox1.SelBold = FormatBold.Checked
  280. End Sub
  281. Private Sub FormatItalic_Click()
  282.     FormatItalic.Checked = Not FormatItalic.Checked
  283.     RichTextBox1.SelItalic = FormatItalic.Checked
  284. End Sub
  285. Private Sub FormatRegular_Click()
  286.     FormatBold.Checked = False
  287.     FormatItalic.Checked = False
  288.     FormatUnderline.Checked = False
  289.     RichTextBox1.SelBold = False
  290.     RichTextBox1.SelItalic = False
  291.     RichTextBox1.SelUnderline = False
  292. End Sub
  293. Private Sub FormatUnderline_Click()
  294.     FormatUnderline.Checked = Not FormatUnderline.Checked
  295.     RichTextBox1.SelUnderline = FormatUnderline.Checked
  296. End Sub
  297. Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer)
  298. ' Select word, or sentence
  299.     If Shift = vbCtrlMask Then
  300.         Select Case KeyCode
  301.             ' If Ctrl+S:
  302.             Case vbKeyS
  303.                 RichTextBox1.Span ".?!", False, True
  304.                 SelectionStart = RichTextBox1.SelStart
  305.                 ' Select to the end of the sentence.
  306.                 RichTextBox1.Span ".?!", True, True
  307.                 ' Extend selection to include punctuation.
  308.                 SelectionEnd = RichTextBox1.SelStart + RichTextBox1.SelLength
  309.                 RichTextBox1.SelStart = SelectionStart
  310.                 RichTextBox1.SelLength = SelectionEnd - SelectionStart
  311.                 
  312.             ' If Ctrl+W:
  313.             Case vbKeyW
  314.                 ' Select to the end of the word.
  315.                 RichTextBox1.Span " ,;:.?!", False, True
  316.                 SelectionStart = RichTextBox1.SelStart
  317.                 ' Select to the end of the word
  318.                 RichTextBox1.Span " ,;:.?!", True, True
  319.                 
  320.                 SelectionEnd = RichTextBox1.SelStart + RichTextBox1.SelLength
  321.                 RichTextBox1.SelStart = SelectionStart
  322.                 RichTextBox1.SelLength = SelectionEnd - SelectionStart
  323.                 
  324.             End Select
  325.     End If
  326. ' Move pointer by word or sentence
  327.     If Shift = (vbCtrlMask Or vbShiftMask) Then
  328.         Select Case KeyCode
  329.         Case vbKeyS
  330.             ' Move pointer to end of sentence.
  331.             RichTextBox1.UpTo ".?!", True, False
  332.         Case vbKeyW
  333.             ' Move pointer to end of word.
  334.             RichTextBox1.UpTo " ,;:.?!", True, False
  335.         End Select
  336.     End If
  337. End Sub
  338. Private Sub RichTextBox1_SelChange()
  339.     If Not IsNull(RichTextBox1.SelBold) Then FormatBold.Checked = RichTextBox1.SelBold
  340.     If Not IsNull(RichTextBox1.SelItalic) Then FormatItalic.Checked = RichTextBox1.SelItalic
  341.     If Not IsNull(RichTextBox1.SelUnderline) Then FormatUnderline.Checked = RichTextBox1.SelUnderline
  342. ' Change the sliders' positions according to the selection's indentation
  343.     If IsNull(RichTextBox1.SelIndent) Then
  344.         Slider1.Enabled = False
  345.         Slider2.Enabled = False
  346.         Exit Sub
  347.     Else
  348.         Slider1.Enabled = True
  349.         Slider2.Enabled = True
  350. On Error Resume Next
  351.         Slider1.Value = RichTextBox1.SelIndent * Slider1.Max / RichTextBox1.RightMargin
  352.         Slider2.Value = (RichTextBox1.SelHangingIndent / RichTextBox1.RightMargin) * Slider2.Max + Slider1.Value
  353.     End If
  354. End Sub
  355. Private Sub Slider1_Scroll()
  356.     RichTextBox1.SelIndent = RichTextBox1.RightMargin * (Slider1.Value / Slider1.Max)
  357.     Slider2_Scroll
  358. End Sub
  359. Private Sub Slider2_Scroll()
  360.     RichTextBox1.SelHangingIndent = RichTextBox1.RightMargin * ((Slider2.Value - Slider1.Value) / Slider2.Max)
  361. End Sub
  362.