martin2k

ForumsVB6GamesSoftwareDirectoryHTMLArticlesBlogContact

File Viewer


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Program

The File Viewer program is able to display any file as text.


vb_63_file_viewer.zip
(3.96 KB)

This program uses the following ActiveX controls:

  • Allen OCX (allenocx.ocx)
  • Microsoft Common Dialog Control 6.0 (comdlg32.ocx)
  • Microsoft Rich Textbox Control 6.0 (SP4) (richtx32.ocx)

This project contains one Form.  The Form contains a RichTextBox control (named 'Main') for displaying the contents of the file.  The Box is resized to fit the size of the Form when the Form is resized by the user.

There are also two menus.  The 'File' menu contains 'Open...', 'Refresh' (for reloading the currently opened file and 'Exit'.  The 'Options' menu allows for customising the appearance of the program and includes 'Text Colour...', Background Colour...' and 'Font...'.

File_Viewer

File_Viewer contains the following code:

Public ADrive As String

Public Function Textload(File As String)
CD.Flags = 4102
Me.Caption = FV & " - " & File

Open File For Binary As #1
    Main.Text = StrConv(InputB(LOF(1), 1), vbUnicode) 'load the file and display it in the RichTextBox control 'Main'
Close #1

End Function


Private Sub Form_Load()
On Error Resume Next 'move to the next line if an error is encountered when retrieving the appearance settings from the registry
Main.SelFontName = GetSetting(MA, FV, "Font")
Main.SelFontSize = GetSetting(MA, FV, "FontSize")
Main.SelBold = GetSetting(MA, FV, "FontBold")
Main.SelItalic = GetSetting(MA, FV, "FontItalic")
Main.SelStrikeThru = GetSetting(MA, FV, "FontStrikeThrough")
Main.SelUnderline = GetSetting(MA, FV, "FontUnderline")
Main.SelColor = GetSetting(MA, FV, "ForeColour")
Main.BackColor = GetSetting(MA, FV, "BackColour")

If Not Command() = "" Then 'if the command line argument is not blank, then load the file specified
    If Allen.FExists(Replace(Command(), Chr(34), "")) = True Then
        CD.FileName = Replace(Command(), Chr(34), "")
        Textload Replace(Command(), Chr(34), "")
    End If
End If
End Sub


Private Sub Form_Resize()
On Error Resume Next 'prevent program crashing when the user minimises
Main.Height = File_Viewer.ScaleHeight
Main.Width = File_Viewer.ScaleWidth
End Sub


Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next 'ignore errors when saving appearance values to the registry
SaveSetting MA, FV, "Font", Main.SelFontName
SaveSetting MA, FV, "FontSize", Main.SelFontSize
SaveSetting MA, FV, "FontBold", Main.SelBold
SaveSetting MA, FV, "FontItalic", Main.SelItalic
SaveSetting MA, FV, "FontStrikeThrough", Main.SelStrikeThru
SaveSetting MA, FV, "FontUnderline", Main.SelUnderline
SaveSetting MA, FV, "ForeColour", Main.SelColor
SaveSetting MA, FV, "BackColour", Main.BackColor
End Sub


Private Sub mnuBackground_Click()
On Error GoTo Error
CD.Flags = 3
CD.Color = Main.BackColor
CD.ShowColor
Main.BackColor = CD.Color
Exit Sub
Error:
End Sub


Private Sub mnuText_Click()
Dim i As Long, j As Long
CD.Flags = 3
On Error GoTo Error
i = Main.SelStart
j = Main.SelLength

CD.Color = Main.SelColor
CD.ShowColor

Main.Visible = False
Main.SelStart = 0 'select all text to apply new format
Main.SelLength = Len(Main.Text)

Main.SelColor = CD.Color
CD.Color = CD.Color

Main.SelStart = i 'restore original cursor position
Main.SelLength = j
Error:
Main.Visible = True
End Sub


Private Sub mnuExit_Click()
Unload Me
End Sub


Private Sub mnuFont_Click()
Dim i As Long, j As Long
On Error GoTo Error
CD.Flags = 259
i = Main.SelStart
j = Main.SelLength

CD.FontName = Main.SelFontName
CD.FontSize = Main.SelFontSize
CD.FontBold = Main.SelBold
CD.FontItalic = Main.SelItalic
CD.FontStrikethru = Main.SelStrikeThru
CD.FontUnderline = Main.SelUnderline
CD.Color = Main.SelColor

CD.ShowFont

Main.Visible = False
Main.SelStart = 0 'select all text to apply new format
Main.SelLength = Len(Main.Text)

Main.SelFontName = CD.FontName
Main.SelFontSize = CD.FontSize
Main.SelBold = CD.FontBold
Main.SelItalic = CD.FontItalic
Main.SelStrikeThru = CD.FontStrikethru
Main.SelUnderline = CD.FontUnderline
Main.SelColor = CD.Color
CD.Color = CD.Color

Error:
Main.SelStart = i 'restore original cursor position
Main.SelLength = j
Main.Visible = True
End Sub


Private Sub mnuOpen_Click()
On Error GoTo Error
CD.Flags = 4102
CD.ShowOpen
Textload CD.FileName
Exit Sub
Error:
End Sub


Private Sub mnuRefresh_Click()
On Error GoTo Error
Textload CD.FileName
Error:
End Sub

The 'Textload' routine opens the file and then loads the contents into the 'Main' RichTextBox control.

The 'File_Viewer' Form appears like this:

The 'File_Viewer' Form
The 'File_Viewer' Form

The RichTextBox control needs to be called 'Main' and must have its Top and Left properties set to 0.  You must also use an Allen control, called 'Allen' and a CommonDialog control named 'CD'.  The CommonDialog control is used to allow the user to open files, change the font of the text in the 'Main' control and change the text and background colour.


blog comments powered by Disqus
Martin Allen 1999 - 2011.  Last updated Saturday 13 August 2011 12:10:00 AM +0100.