You are on page 1of 11

Chng Mi - Dng Menu

Menu trong Windows l ni tt c cc commands ca mt program c sp xp th t theo tng loi gip ta dng d dng. C hai loi menu ta thng gp: drop-down (th xung) menu v pop-up (hin ln) menu. Ta dng drop-down menu lm Menu chnh cho chng trnh. Thng thng n nm pha trn chp mn nh. Nm dc theo chiu ngang l Menu Bar, nu ta click ln mt command trong Menu Bar th program s th xung mt menu vi nhng MenuItems nm dc theo chiu thng ng. Nu ta click ln MenuItem no c du hnh tam gic nh bn phi th program s popup mt Menu nh trong hnh di y (khi ta click Format | Make Same Size):

Main Menu Ta dng Menu Editor to hoc sa mt Menu cho program. Menu thuc v mt Form. Do , trc ht ta select mt Form lm vic vi Designer ca n (ch khng phi code ca Form). K ta dng Menu Command Tools | Menu Editor hay click ln icon ca Menu Editor trn Toolbar lm cho Menu Editor hin ra.

u tin c mt vt mu xanh nm trong khung trng ca Menu Editor, ni s hin th Caption ca Menu Command u tin ca Form. Khi ta nh ch &File vo Textbox Caption, n cng hin ra trn vt xanh ni trn. K , bn c th nh tn ca Menu Command vo Textbox Name. D ta cho Menu Command mt tn nhng ta t khi dng n, tr trng hp mun n visible/invisible (hin ra/bin mt). Bnh thng ta dng tn ca MenuItems nhiu hn.

c mt Menu nh trong hnh di y ta cn phi edit thm vo cc MenuItems Open, Save, Close v Exit.

Hnh di y cho thy tt c cc MenuItems ca Menu Command File u nm tht qua bn phi vi bn du chm (....) pha trc. Khi ta click du tn ch qua phi th MenuItem ta ang Edit s c thm bn du chm, tc l tht mt bc trong Menu (Nested).

Tng t nh vy, khi ta click du tn ch qua tri th MenuItem ta ang Edit s mt bn du chm, tc l tri mt bc trong Menu. Nu mun cho User dng Alt key x dng Menu, bn nh thm du & trc character bn mun trong menu Caption. Th d Alt-F s th xung Menu ca Menu Command File. Nu bn t cho MenuItem &Open tn mnuOpen, th khi bn Click ln Caption n trn Form trong lc thit k, VB6 IDE s hin th ci v ca Sub mnuOpen_Click(), ging nh Sub cmdButton_Click() ca mt CommandButton: Private Sub mnuOpen_Click() MsgBox "You clicked mnuOpen" End Sub Trong th d trn ta nh thm mt Statement hin th mt message n gin "You clicked mnuOpen". Bn c th t cho mt MenuItem tn g cng c, nhng ngi ta thng dng prefix mnu d phn bit mt menuItem Event vi mt CommandButton Event. Do , ta c nhng tn mnuFile, mnuOpen, mnuSave, mnuClose, mnuExit. Ci gch ngang gia MenuItems Close v Exit c gi l Menu Separator. Bn c th nht mt Menu Separator bng cch cho Caption n bng du tr ( - ). Ngoi Alt key ta cn c th cho User dng Shortcut ca menuItem. cho MenuItem mt Shortcut, bn chn cho n mt Shortcut t ComboBox Shortcut trong Menu Editor. Trong hnh di y ta chn Ctrl+O cho mnuOpen.

By default, menuItem c Enabled v Visible. Lc thit k bn c th cho MenuItem gi tr khi u ca Enabled v Visible bng cch dng Checkboxes Enabled v Visible. Trong khi chy program (at runtime), bn cng c th thay i cc values Enabled v Visible nh sau: mnuSave.Enabled = False mnuOpen.Visible = False Khi mt MenuItem c Enabled=False th n b m v user khng dng c. Bn dng cc du mi tn ch ln v xung di chuyn MenuItem c selected ln v xung trong danh sch cc MenuItems. Bn dng button Delete hy b MenuItem c selected, Insert nht mt MenuItem mi ngay trn MenuItem c selected v Next chn MenuItem ngay di MenuItem c selected.

Pop-up Menu

i vi User, ang khi lm vic vi mt Object trong Windows tin nht l ta c th lm hin th Context Menu (Menu p dng cho ng tnh hung) bng mt Mouse click. Thng thng l Right Click v ci Context Menu cn c gi l Pop-up Menu. Chnh ci Pop-Up menu tht ra l Dropdown menu ca mt Menu Bar Command. Bnh thng Menu Bar Command y c th visible hay invisible (tn hnh). Trong hnh di y, khi User Right click trn Form, mnuEdit s hin ln. Nu bnh thng bn khng mun cho User dng n trong Main Menu th bn cho n invisible:

Code lm cho Popup menu hin ln c vit trong Event Mousedown ca mt Object m tnh c y l ca chnh ci Form: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ' Popup the Edit Menu if User clicked the Right Button of the Mouse If Button = vbRightButton Then PopupMenu mnuEdit End If End Sub Ngay c khi bn mun cho mnuEdit bnh thng l invisible, bn cng nn cho n visible trong lc u tin b code vo dng x l Click Events ca nhng MenuItems thuc v mnuEdit nh mnuCopy, mnuCut v mnuPaste. Cha menu Settings trong Registry Gi t program bn cho User mt Option WordWrap nh di y:

Bn mun Program nh Option m User chn, ln ti khi User khi ng program th Option WordWrap cn gi nguyn gi tr nh c. Cch tin nht l cha value ca Option WordWrap nh mt Key trong Registry. Registry l mt loi c s d liu c bit ca Windows Operating System dng cha nhng d kin lin h n Users, Hardware, Configurations, ActiveX Components ..v.v. dng trong computer. Trong Registry, data c sp t theo tng loi theo ng cp. Bn c th Edit trc tip tr s cc Keys trong Registry bng cch dng Registry Editor.

Trong program ny ta cng nhn tin bt program nh lun v tr ca Form khi program ngng li, ln ti khi User khi ng program th program s c v tr lc u ging y nh trc. Ta s dng Sub SaveSetting cha Checked value ca mnuWordWrap v Left, Top ca Form. Code y ta s trong Sub Form_QueryUnload v n s c executed trc 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 Ta ca program. Thng thng n l tn ca VB Project, nhng bn c th sa n trong Project Property Dialog (Tab Make) :

Khi cha value ca mt th g (ta gi l Key) vo Registry bn c th sp t cho n nm trong Section no ty . y ta t ra hai Sections tn Location cha Top,Left ca Form v tn Settings cha Key mnuWordWrap.Checked. Mun cho program c cc gi tr ca Keys cha trong Registry khi n khi ng ta ch cn dng Function GetSetting trong Sub Form_Load c vo t Registry nh di 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 Lc u khi cha c g trong Registry th "0" (string "0" c converted bi Val ra 0) l default value cho Left v Top, cn "False" l default value ca mnuWordWrap.Checked. Ngoi ra ta cng mun program nh tn ca ba Files User dng gn y nht. Tc l trong Drop-down ca Menu Command File s c MenuItem Recent Files hin th t mt n ba tn Files, ci mi nht nm trn ht. Trc ht, ta cn to ra 3 SubmenuItem c cng tn mnuRFile nhng mang Index bng 0,1 v 2 (bn nh vo Textbox Index). Ta s dng Captions ca chng hin th tn cc Files. Lc cha c Filename no c th MenuItem Recent Files s b lm m i (tc l mnuRecentFiles.Enabled =

False ). Ta s cha tn cc Files nh mt String trong Section Settings ca Registry. Ta phn cch tn cc Files bng delimiter character |. Th d: "LattestFileName.txt|OldFileName.txt|OldestFilename.txt" Mi ln User Open mt File ta s thm tn File y vo trong Registry v bt c lc no ch gi li tn ca 3 Files mi dng nht.

Di y l code dng thm tn File mi dng nht vo 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 dng trong Sub Form_Load c tn RecentFiles v hin 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 dng mt Class tn clsRecentFiles c bit lo vic cha tn Files vo Registry v hin th tn cc Files y trong Menu. Bn trong clsRecentFiles ta cng dng clsString, l mt Class gip ta ngt khc String trong Registry ra tn ca cc Files da vo ch cc delimiter character |. ' Author: Le Duc Hong http://www.vovisoft.com ' 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() ' Display the most recent Filenames in the menu Dim i As Integer ' If there is no FileNames to display 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 Bn c th chy Line Command RegEdit sau khi click Start | Run

xem chi tit ca cc Keys m program cha trong Sections Location v Settings ca Folder HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Menu

You might also like