Chứa menu Settings trong Registry

Một phần của tài liệu LẬP TRÌNH TRỰC QUAN (Trang 160 - 168)

Giả sử chương trình chúng ta cho người sử dụng một Option WordWrap như dưới đây:

Chúng ta muốn chương trình nhớ Option mà người sử dụng đã chọn, để lần tới khi người sử dụng khởi động chương trình thì Option WordWrap còn giữ nguyên giá trị như cũ.

Cách tiện nhất là chứa value của Option WordWrap như một Key trong Registry. Registry là một loại cơ sở dữ liệu đặc biệt của Windows Operating System dùng để chứa những dữ kiện liên hệ đến Users, Hardware, Configurations, ActiveX Components ..v.v. dùng trong computer. Trong Registry, data được sắp đặt theo từng loại theo đẳng cấp. Chúng ta có thể Edit trực tiếp trị số các Keys trong Registry bằng cách dùng Registry Editor.

Trong chương trình này ta cũng nhân tiện bắt chương trình nhớ luôn vị trí của Form khi chương trình ngừng lại, để lần tới khi người sử dụng khởi động chương trình thì chương trình sẽ có vị trí lúc đầu giống y như trước.

Ta sẽ dùng Sub SaveSetting để chứa Checked value của mnuWordWrap và Left, Top của Form. Code ấy ta sẽ để trong Sub Form_QueryUnload vì nó sẽ được executed trước khi Form Unload.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

SaveSettings End Sub

Private Sub SaveSettings() ' Save Location of the form

SaveSetting App.Title, "Location", "Left", Me.Left SaveSetting App.Title, "Location", "Top", Me.Top ' Save the setting of WordWrap in menu

SaveSetting App.Title, "Settings", "WordWrap", mnuWordWrap.Checked

End Sub

App.Title là tiêu đề của chương trình. Thông thường nó là tên của VB Project, nhưng chúng ta có thể sữa nó trong Project Property Dialog (Tab Make) :

Khi chứa value của một thứ gì (ta gọi là Key) vào Registry chúng ta có thể sắp đặt cho nó nằm trong Section nào tùy ý. Ở đây ta đặt ra hai Sections tên Location để chứa Top, Left của Form và tên Settings để chứa Key mnuWordWrap.Checked.

Muốn cho chương trình có các giá trị của Keys chứa trong Registry khi nó khởi động ta chỉ cần dùng Function GetSetting trong Sub Form_Load để đọc vào từ Registry như dưới đây:

Private Sub Form_Load()

' Initialise Location of the form by reading the Settings from the Registry

Me.Left = Val(GetSetting(App.Title, "Location", "Left", "0")) Me.Top = Val(GetSetting(App.Title, "Location", "Top", "0")) ' Initialise setting of WordWrap in the menu

mnuWordWrap.Checked = ( GetSetting(App.Title, "Settings",

"WordWrap", "False") = "True" ) End Sub

Lúc đầu khi chưa có gì trong Registry thì "0" (string "0" được converted bởi Val ra 0) là default value cho Left và Top, còn "False" là default value của mnuWordWrap.Checked.

Ngoài ra ta cũng muốn chương trình nhớ tên của ba Files User dùng gần đây nhất. Tức là trong Drop-down của Menu Command File sẽ có MenuItem Recent Files để hiển thị từ một đến ba tên Files, cái mới nhất nằm trên hết. Trước hết, ta cần tạo ra 3 SubmenuItem có cùng tên mnuRFile nhưng mang Index bằng 0,1 và 2 (chúng ta đánh vào Textbox Index). Ta sẽ dùng Captions của chúng để hiển thị tên các Files. Lúc chưa có Filename nào cả thì MenuItem Recent Files sẽ bị làm mờ đi (tức là mnuRecentFiles.Enabled = False ).

Ta sẽ chứa tên các Files như một String trong Section Settings của Registry. Ta phân cách tên các Files bằng delimiter character |. Ví dụ:

"LattestFileName.txt|OldFileName.txt|OldestFilename.txt"

Mỗi lần người sử dụng Open một File ta sẽ thêm tên File ấy vào trong Registry và bất cứ lúc nào chỉ giữ lại tên của 3 Files mới dùng nhất.

Dưới đây là code dùng để thêm tên File mới dùng nhất vào Registry:

Private Sub mnuOpen_Click()

' Initialise Folder in Common Dialog CommonDialog1.InitDir = App.Path ' Launch the dialog

CommonDialog1.ShowOpen

' Save the Filename in the Registry, using Object myRecentFiles

myRecentFiles.AddFile CommonDialog1.FileName End Sub

Code dùng trong Sub Form_Load để đọc tên RecentFiles và hiển thị trong Menu:

'

Set myRecentFiles = New clsRecentFiles ' Pass the form handle to it

' This effectively loads the most recently used FileNames to menu

myRecentFiles.Init Me

Ta sẽ dùng một Class tên clsRecentFiles để đặc biệt lo việc chứa tên Files vào Registry và hiển thị tên các Files ấy trong Menu. Bên trong clsRecentFiles ta cũng dùng clsString, là một Class giúp ta ngắt khúc String trong Registry ra tên của các Files dựa vào chỗ các delimiter character |.

' Class Name: clsRecentFiles

' This Class saves the most Recent FileNames used in the Registry ' in form of a String delimited by |.

' Up to MaxFiles Filenames maybe stored.

' You need to pass the Form that contains the menu to it.

' The assumption is that you have created an array of MenuItems ' named mnuRFile to display the FileNames

'

Const MaxFiles = 3 ' Maximum number of FileNames to remember Private myForm As Form

Private RecentFiles As clsString Public Sub Init(TForm As frmMenu) Set myForm = TForm

Set RecentFiles = New clsString

' Read the Most Recent Filename String from the Registry RecentFiles.Text = GetSetting(App.Title, "Settings",

"RecentFiles", "")

' Assign the Delimiter character and tokennise the String (i.e. split it) into FileNames

RecentFiles.Delimiter = "|"

UpdateMenu End Sub

Public Sub AddFile(FileName As String)

' Add the latest FileName to the list and update the Registry ' Prefix the FileName to the existing MostRecentFileName

String

RecentFiles.Text = FileName & "|" & RecentFiles.Text

' Discard the oldest FileNames if the total number is greater than MaxFiles

If RecentFiles.TokenCount > MaxFiles Then Dim TStr As String

Dim i As Integer

' Reconstitute the String that contains only the most recent MaxFiles FileNames

For i = 1 To MaxFiles

TStr = TStr & RecentFiles.TokenAt(i) & "|"

Next

' Remove the last delimiter character on the right RecentFiles.Text = Left(TStr, Len(TStr) - 1)

End If

' Update the String in the Registry

SaveSetting App.Title, "Settings", "RecentFiles", RecentFiles.Text

UpdateMenu End Sub

Private Sub UpdateMenu()

' Hiển thị the most recent Filenames in the menu Dim i As Integer

' If there is no FileNames to hiển thị then disable the MenuItem entry

If RecentFiles.TokenCount = 0 Then

myForm.mnuRecentFiles.Enabled = False Exit Sub

Else

' Otherwise enable the MenuItem entry myForm.mnuRecentFiles.Enabled = True

End If

' Assign FileName to Caption of mnuRFile array and make the MenuItem elements visible

For i = 1 To RecentFiles.TokenCount

myForm.mnuRFile(i - 1).Caption = RecentFiles.TokenAt(i) ' Assign to Caption

myForm.mnuRFile(i - 1).Visible = True ' Make the MenuItem visible

If i = MaxFiles Then Exit For ' This line maybe unnecessary

Next

' Make the rest of the MenuItem array mnuRFile invisible if there are less than MaxFiles

If RecentFiles.TokenCount < MaxFiles Then

For i = RecentFiles.TokenCount To MaxFiles - 1 myForm.mnuRFile(i).Visible = False

Next End If End Sub

Chúng ta có thể chạy Line Command RegEdit sau khi click Start | Run

để xem chi tiết của các Keys mà chương trình đã chứa trong Sections LocationSettings của Folder HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Menu

Một phần của tài liệu LẬP TRÌNH TRỰC QUAN (Trang 160 - 168)

Tải bản đầy đủ (PDF)

(251 trang)