www.pudn.com > code0003文本编辑器.zip > textedit.frm


VERSION 5.00 
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX" 
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX" 
Begin VB.Form Form1  
   Caption         =   "TextEdit" 
   ClientHeight    =   6630 
   ClientLeft      =   1440 
   ClientTop       =   1560 
   ClientWidth     =   9645 
   LinkTopic       =   "Form1" 
   PaletteMode     =   1  'UseZOrder 
   ScaleHeight     =   6630 
   ScaleWidth      =   9645 
   Begin MSComDlg.CommonDialog CmdOpen  
      Left            =   6720 
      Top             =   420 
      _ExtentX        =   847 
      _ExtentY        =   847 
      _Version        =   393216 
      DefaultExt      =   "txt" 
      Filter          =   "RTF Files|*.rtf|Text Files|*.txt|All files|*.*" 
   End 
   Begin RichTextLib.RichTextBox RichTextBox1  
      Height          =   6495 
      Left            =   0 
      TabIndex        =   0 
      Top             =   0 
      Width           =   9615 
      _ExtentX        =   16960 
      _ExtentY        =   11456 
      _Version        =   393217 
      BorderStyle     =   0 
      ScrollBars      =   2 
      TextRTF         =   $"textedit.frx":0000 
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}  
         Name            =   "宋体" 
         Size            =   12 
         Charset         =   134 
         Weight          =   400 
         Underline       =   0   'False 
         Italic          =   0   'False 
         Strikethrough   =   0   'False 
      EndProperty 
   End 
   Begin VB.Menu mnuFile  
      Caption         =   "文件&F" 
      Begin VB.Menu mnuNew  
         Caption         =   "新建&N" 
         Shortcut        =   ^N 
      End 
      Begin VB.Menu mnuOpen  
         Caption         =   "打开&O" 
         Shortcut        =   ^O 
      End 
      Begin VB.Menu mnuSave  
         Caption         =   "保存&S" 
         Shortcut        =   ^S 
      End 
      Begin VB.Menu Sep1  
         Caption         =   "-" 
      End 
      Begin VB.Menu mnuFont  
         Caption         =   "字体&T" 
      End 
      Begin VB.Menu mnuPrint  
         Caption         =   "打印&P" 
         Shortcut        =   ^P 
      End 
      Begin VB.Menu Sep2  
         Caption         =   "-" 
      End 
      Begin VB.Menu mnuExit  
         Caption         =   "退出&X" 
      End 
   End 
   Begin VB.Menu mnuEdit  
      Caption         =   "编辑&E" 
      Begin VB.Menu mnuFind  
         Caption         =   "查找&F" 
         Shortcut        =   ^F 
      End 
      Begin VB.Menu mnuNext  
         Caption         =   "查找下一个&N" 
         Shortcut        =   {F3} 
      End 
   End 
End 
Attribute VB_Name = "Form1" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = False 
Attribute VB_PredeclaredId = True 
Attribute VB_Exposed = False 
'-------------------------------------- 
'   一个由RichTextBox实现的文本编辑器 
'-------------------------------------- 
'            洪恩在线 求知无限 
'-------------------------------------- 
'------名称-------------作用------------ 
'       Form1           主窗体 
'       CmdOpen         通用对话框 
'       RichTextBox1    RichTextBox 
'       mnuNew          “新建”菜单项 
'       mnuOpen         “打开”菜单项 
'       mnuSave         “保存”菜单项 
'       mnuExit         “退出”菜单项 
'       mnuFont         “字体”菜单项 
'       mnuPrint        “打印”菜单项 
'       mnuFind         “查找”菜单项 
'       mnuNext         “查找下一个”菜单项 
'------变量-------------作用------------- 
'       sFind           待查找的字符串 
'--------------------------------------- 
Option Explicit 
Public sFind As String 
 
Private Sub Form_Resize() 
 
'如果窗体不处于最小化RichTextBox1状态,改变RichTextBox1大小以适应窗体大小变化 
If Form1.WindowState <> 1 Then 
RichTextBox1.Width = Form1.Width - 135 
If Form1.Height < 1200 Then 
         Form1.Height = 1200 
End If 
RichTextBox1.Height = Form1.Height - 675 
End If 
End Sub 
 
'当“退出”菜单项被点击时 
Private Sub mnuExit_Click() 
Unload Me 
End 
End Sub 
 
'当“查找”菜单项被点击时 
Private Sub mnuFind_Click() 
'InputBox("弹出的输入框的标题",[默认值], [返回的值]) 
'语法:InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context]) 
sFind = InputBox("Find what?", , sFind) 
'RichTextBox1.Find 是一个方法,根据给定的字符串,在 RichTextBox 控件中搜索文本 
RichTextBox1.Find sFind 
End Sub 
 
'当“字体”菜单项被点击时 
Private Sub mnuFont_Click() 
'显示“字体”对话框 
'使用指定的方法,CommonDialog 控件能够显示下列对话。 
'---------------------------------------- 
'方法         所显示的对话框 
'---------------------------------------- 
'ShowOpen    显示“打开”对话框 
'ShowSave    显示“另存为”对话框 
'ShowColor   显示“颜色”对话框 
'ShowFont    显示“字体”对话框 
'ShowPrinter 显示“打印”或“打印选项”对话框 
'ShowHelp    调用 Windows 帮助引擎 
'---------------------------------------- 
CmdOpen.Flags = cdlCFBoth + cdlCFEffects 
CmdOpen.ShowFont 
'将RichTextBox1的属性根据“字体”对话框的变化作相应设置 
'---------------------------------------- 
'要改变 RichTextBox 控件中的字体特性,可以使用 
'SelFontName、SelFontSize 和 SelFontColor 属性。 
'---------------------------------------- 
With RichTextBox1 
    .SelFontName = CmdOpen.FontName 
    .SelFontSize = CmdOpen.FontSize 
    .SelBold = CmdOpen.FontBold 
    .SelItalic = CmdOpen.FontItalic 
    .SelStrikeThru = CmdOpen.FontStrikethru 
    .SelUnderline = CmdOpen.FontUnderline 
End With 
End Sub 
 
'当“新建”菜单项被点击时,设置为空 
Private Sub mnuNew_Click() 
RichTextBox1.Text = "" 
End Sub 
 
'当“查找下一个”菜单项被点击时 
Private Sub mnuNext_Click() 
'SelStart属性-返回或设置所选择的文本的起始点;如果没有文本被选中,则指出插入点的位置。 
RichTextBox1.SelStart = RichTextBox1.SelStart + RichTextBox1.SelLength + 1 
'object.Find(string, start, end, options) 
'------------------------------------------ 
'Find 方法的语法包含下面部分: 
'部分        描述 
'----------------------------------------- 
'object     必需的。对象表达式,其值是“应用于”列表中的一个对象。 
'string     必需的。要在控件中查找的字符串表达式。 
'start      可选的。决定从哪儿开始搜索的整数字符索引。控件中的每一个字符都有一个可唯一标识的整数索引。控件中文本的第一个字符的索引是 0。 
'end        可选的。决定在哪儿结束搜索的整数字符索引。 
'options    可选的。用来指定一个或多个可选功能常数的和。所指定的功能如“设置值”中所述。 
'----------------------------------------- 
RichTextBox1.Find sFind, , Len(RichTextBox1) 
End Sub 
 
'当“打开”菜单项被点击时 
Private Sub mnuOpen_Click() 
'参看上面CommonDialog方法 
CmdOpen.ShowOpen 
'RichTextBox的LoadFile方法 
RichTextBox1.LoadFile (CmdOpen.FileName) 
End Sub 
 
'当“打印”菜单项被点击时 
Private Sub mnuPrint_Click() 
CmdOpen.Flags = cdlPDReturnDC + cdlPDNoPageNums 
If RichTextBox1.SelLength = 0 Then 
    CmdOpen.Flags = CmdOpen.Flags + cdlPDAllPages 
Else 
    CmdOpen.Flags = CmdOpen.Flags + cdlPDSelection 
End If 
'参看上面CommonDialog方法 
CmdOpen.ShowPrinter 
'将 RichTextBox 控件中格式化文本发送给设备进行打印。 
'语法 
'object.SelPrint (hDC) 
'SelPrint 方法的语法包含下面部分: 
'部分         描述 
'----------------------------------------------- 
'object       对象表达式,其值是“应用于”列表中的一个对象。 
'hdc          设备描述体,是准备用来打印控件内容的设备。 
'----------------------------------------------- 
RichTextBox1.SelPrint CmdOpen.hDC 
End Sub 
 
'当“保存”菜单项被点击时 
Private Sub mnuSave_Click() 
CmdOpen.ShowSave 
'RichTextBox的SaveFile方法,保存文本 
RichTextBox1.SaveFile (CmdOpen.FileName) 
End Sub