home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / Code / ch12 / ImageViewer.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-06-15  |  7.6 KB  |  232 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
  3. Begin VB.Form Form1 
  4.    Caption         =   "Image Viewer"
  5.    ClientHeight    =   4275
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   7455
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   4275
  11.    ScaleWidth      =   7455
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog CommonDialog1 
  14.       Left            =   240
  15.       Top             =   3720
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   327680
  19.    End
  20.    Begin VB.PictureBox Picture1 
  21.       Height          =   3495
  22.       Left            =   720
  23.       ScaleHeight     =   3435
  24.       ScaleWidth      =   4995
  25.       TabIndex        =   0
  26.       Top             =   240
  27.       Width           =   5055
  28.    End
  29.    Begin VB.Menu mnuFile 
  30.       Caption         =   "&File"
  31.       Begin VB.Menu mnuFileNew 
  32.          Caption         =   "&New"
  33.          Shortcut        =   ^N
  34.       End
  35.       Begin VB.Menu mnuFileOpen 
  36.          Caption         =   "&Open ..."
  37.          Index           =   2
  38.          Shortcut        =   ^O
  39.       End
  40.       Begin VB.Menu mnuFileList 
  41.          Caption         =   "-"
  42.          Index           =   0
  43.          Visible         =   0   'False
  44.       End
  45.       Begin VB.Menu mnuFileBar 
  46.          Caption         =   "-"
  47.          Index           =   6
  48.       End
  49.       Begin VB.Menu mnuFileExit 
  50.          Caption         =   "E&xit"
  51.          Index           =   9
  52.       End
  53.    End
  54.    Begin VB.Menu mnuEdit 
  55.       Caption         =   "&Edit"
  56.       Begin VB.Menu mnuEditCopy 
  57.          Caption         =   "&Copy"
  58.          Shortcut        =   ^C
  59.       End
  60.       Begin VB.Menu mnuEditPaste 
  61.          Caption         =   "&Paste"
  62.          Shortcut        =   ^V
  63.       End
  64.    End
  65. Attribute VB_Name = "Form1"
  66. Attribute VB_GlobalNameSpace = False
  67. Attribute VB_Creatable = False
  68. Attribute VB_PredeclaredId = True
  69. Attribute VB_Exposed = False
  70. Option Explicit
  71. ' max length of the recent files list (should be <= 9)
  72. Const RECENTFILES_MAX = 9
  73. Dim recentFiles(RECENTFILES_MAX) As String
  74. Private Sub Form_Load()
  75.     ' load the list of recent files
  76.     ReadRecentFiles
  77. End Sub
  78. Private Sub Form_Resize()
  79.     ' resize the picture box along with the form
  80.     Picture1.Move 0, 0, ScaleWidth, ScaleHeight
  81. End Sub
  82. Private Sub Form_Unload(Cancel As Integer)
  83.     ' on exit, save the list of recent files to disk
  84.     WriteRecentFiles
  85. End Sub
  86. Private Sub mnuFileList_Click(Index As Integer)
  87.     ' load a file from the list of recent files
  88.     OpenFile recentFiles(Index)
  89. End Sub
  90. Private Sub mnuFileNew_Click()
  91.     ' clear the picture box
  92.     Set Picture1.Picture = Nothing
  93. End Sub
  94. Private Sub mnuFileOpen_Click(Index As Integer)
  95.     ' query the user for a new picture
  96.     With CommonDialog1
  97.         .Filter = "All Picture Files|*.bmp;*.dib:*.gif;*.wmf;*.emf;*.jpg;*ico;*.cur|" _
  98.             & "Bitmaps (*.bmp;*.dib)|*.bmp;*.dib|" _
  99.             & "Icons (*.ico;*.cur)|*.ico;*.cur|" _
  100.             & "GIF images (*.gif)|*.gif|" _
  101.             & "JPEG images (*.jpg)|*.jpg|" _
  102.             & "Metafiles (*.wmf;*.emf)|*.wmf;*.emf" _
  103.             & "All Files|*.*"
  104.         .Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  105.         .filename = ""
  106.         .ShowOpen
  107.         If .filename <> "" Then
  108.             ' if the user didn't cancel the command, open the image
  109.             OpenFile .filename
  110.         End If
  111.     End With
  112. End Sub
  113. Private Sub mnuFileExit_Click(Index As Integer)
  114.     ' exit the program
  115.     Unload Me
  116. End Sub
  117. Private Sub mnuEditCopy_Click()
  118.     ' copy the current image to the clipboard
  119.     Clipboard.SetData Picture1.Picture
  120. End Sub
  121. Private Sub mnuEditPaste_Click()
  122.     ' paste the image currently in the clipboard
  123.     Set Picture1.Picture = Clipboard.GetData
  124. End Sub
  125. Private Sub OpenFile(filename As String)
  126.     ' load the picture
  127.     Picture1.Picture = LoadPicture(filename)
  128.     ' update the recent file list
  129.     AddToRecentFileList filename
  130. End Sub
  131. Private Function RecentFilePath() As String
  132.     ' return the path of the text file that holds the list
  133.     ' of most recently opened files
  134.     RecentFilePath = App.Path & IIf(Right$(App.Path, 1) <> "\", "\", "") & App.EXEName & ".mru"
  135. End Function
  136. Private Sub ReadRecentFiles()
  137.     ' read the list of recent files, and update the File menu
  138.     Dim fnum As Integer
  139.     Dim fileIsOpened As Boolean
  140.     Dim Index As Integer
  141.     Dim item As String
  142.     On Error GoTo ReadRecentFiles_Err
  143.     fnum = FreeFile()
  144.     Open RecentFilePath For Input As #fnum
  145.     fileIsOpened = True
  146.     Do Until EOF(fnum)
  147.         Line Input #fnum, item
  148.         ' only store non-null strings
  149.         If item <> "" Then
  150.             Index = Index + 1
  151.             recentFiles(Index) = item
  152.         End If
  153.     Loop
  154. ReadRecentFiles_Err:
  155.     If fileIsOpened Then Close #fnum
  156.         
  157.     ' build the menu
  158.     UpdateRecentFileMenu
  159. End Sub
  160. Private Sub WriteRecentFiles()
  161.     ' write the list of recent files
  162.     Dim fnum As Integer
  163.     Dim fileIsOpened As Boolean
  164.     Dim Index As Integer
  165.     On Error GoTo WriteRecentFiles_Err
  166.     fnum = FreeFile()
  167.     Open RecentFilePath For Output As #fnum
  168.     fileIsOpened = True
  169.     For Index = 1 To RECENTFILES_MAX
  170.         ' only store non-blank items
  171.         If recentFiles(Index) <> "" Then
  172.             Print #fnum, recentFiles(Index)
  173.         End If
  174.     Next
  175. WriteRecentFiles_Err:
  176.     If fileIsOpened Then Close #fnum
  177. End Sub
  178. Private Sub UpdateRecentFileMenu()
  179.     ' update the menu with the list of recent files
  180.     Dim Index As Integer
  181.     ' unload any loaded items
  182.     ' except the first one (index=0) that is a static element
  183.     Do While mnuFileList.UBound > 0
  184.         Unload mnuFileList(mnuFileList.UBound)
  185.     Loop
  186.     ' temporarily hide the separator at the
  187.     ' beginning of the list
  188.     mnuFileList(0).Visible = False
  189.     ' load filenames into the menu array
  190.     For Index = 1 To RECENTFILES_MAX
  191.         ' take only non-null items into account
  192.         If recentFiles(Index) = "" Then Exit For
  193.         
  194.         ' load the array item
  195.         Load mnuFileList(Index)
  196.         ' set its caption and hotkey
  197.         mnuFileList(Index).Caption = "&" & Format$(Index) & ". " + recentFiles(Index)
  198.         ' make it visible
  199.         mnuFileList(Index).Visible = True
  200.         ' if at least one item is visible, also the separator
  201.         ' at the beginning of the list should be visible
  202.         mnuFileList(0).Visible = True
  203.     Next
  204. End Sub
  205. Private Sub AddToRecentFileList(ByVal filename As String)
  206.     ' add a new file to the list of the recently opened files
  207.    Dim found As Integer
  208.    Dim Index As Integer
  209.    Dim ercode As Integer
  210.    ' do nothing if the file is already on top of the list
  211.    If filename <> recentFiles(1) Then
  212.         ' check if the file is already in the list
  213.         ' if not found, use the last item of the list
  214.         found = RECENTFILES_MAX
  215.         For Index = 1 To RECENTFILES_MAX - 1
  216.             If recentFiles(Index) = filename Or recentFiles(Index) = "" Then
  217.                 found = Index
  218.                 Exit For
  219.             End If
  220.         Next
  221.         ' move all items in the range [1, found] one
  222.         ' position toward higher indexes
  223.         For Index = found To 2 Step -1
  224.             recentFiles(Index) = recentFiles(Index - 1)
  225.         Next
  226.         ' store the file in the first position
  227.         recentFiles(1) = filename
  228.         ' update the menu
  229.         UpdateRecentFileMenu
  230.    End If
  231. End Sub
  232.