Lưu trữ việc thiết lập thực đơn trong Registry

Một phần của tài liệu Lập trình trực quan tài liệu lập trình (Trang 153 - 159)

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ữ ngun giâ trị như cũ.

Câch tiện nhất lă chứa giâ trị 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 hệ điều hănh Windows dùng để chứa những dữ kiện liín quan đến Users, Hardware, Configurations, ActiveX Components... dùng trong mây tính. Trong Registry, dữ liệu được sắp đặt theo từng loại. Chúng ta có thể sửa đổi 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 lập trình để chương trình nhớ ln 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 giâ trị Checked của mnuWordWrap vă Left, Top của Form. Chương trình năy ta sẽ để trong Sub Form_QueryUnload vì nó sẽ được thực thi 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", "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 giâ trị của một 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 đổi từ kiểu số sang) lă giâ trị mặc định cho Left vă Top, còn "False" lă giâ trị mặc định của mnuWordWrap.Checked.

Ngoăi ra, ta cũng muốn chương trình nhớ tín của 3 tập tin người dùng sử 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 tập tin, câi mới nhất nằm ở trín cùng. 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 tập tin. 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 tập tin như một xđu ký tự trong Section Settings của Registry. Ta phđn câch tín câc tập tin bằng ký tự đặc biệt “|”.

Mỗi lần người sử dụng mở một tập tin ta sẽ thím tín tập tin ấy văo trong Registry vă bất cứ lúc năo chỉ giữ lại tín của 3 tập tin mới dùng nhất.

Dưới đđy lă đoạn chương trình dùng để thím tín tập tin 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

Vă chương trình 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 câc tập tin văo Registry vă hiển thị tín câc tập tin ấy trong thực đơn. Bín trong clsRecentFiles ta cũng dùng clsString, lă một Class giúp ta ngắt đoạn xđu ký tự trong Registry ra tín của câc tập tin dựa văo chỗ câc ký tự phđn câch |.

' 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) myForm.mnuRFile(i - 1).Visible = True ' Make the MenuItem If i = MaxFiles Then Exit

Next

' Make the rest of the MenuItem array mnuRFile invisible 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 dịng lệnh RegEdit sau khi chọn Start | Run

để xem chi tiết của câc Keys mă chương trình đê chứa trong Sections Location vă Settings của Thư mục 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 tài liệu lập trình (Trang 153 - 159)