User Tools

Site Tools


k13:k13.1:k13.1.2:start

13.1.2 Source code - Module MM. module

Even with a simple menu on the form, the source code for a project quickly becomes confusing, because you have to distinguish between the procedures for generating the menus, the procedures for assigning the program actions to the individual menus, and the procedures that describe the program actions. It is a good idea to place the source code in a module for generating the individual menus.

The source code for the module and the project MenuQM are completely specified and commented in substantial parts.

[1] ' Gambas class file
[2] 
[3] Private mnuMenu1 As Menu 
[4] Private mnuMenu2 As Menu 
[5] Private mnuMenu3 As Menu 
[6] 
[7] Private mnu11Open As Menu 
[8] Private mnu12New As Menu 
[9] Private mnu13Save As Menu 
[10] Private mnu14SaveAs As Menu 
[11] Private mnu15Print As Menu 
[12] Private mnu16Close As Menu 
[13] Private mnu21Optionen As Menu 
[14] Private mnu22SelectColor As Menu 
[15] Private mnu23PreView As Menu 
[16] Private mnu221Palette1 As Menu 
[17] Private mnu222Palette2 As Menu 
[18] Private mnu31Help As Menu 
[19] Private mnu32Information As Menu 
[20] Private mnuSpace As Menu 
[21] 
[22] Public Sub ShowMenu() 
[23] 
[24]   mnuMenu1 = New Menu(FMain, False) ' 1. Eintrag in der Menü-Leiste , sichtbar
[25]   mnuMenu1.Caption = "Bild-Datei" 
[26]   
[27]   mnu11Open = New Menu(mnuMenu1) As "mnuOpen" 
[28]   mnu11Open.Caption = "Bild-Datei Öffnen..." 
[29]   mnu11Open.Shortcut = "CTRL+O" 
[30]   mnu11Open.Picture = Picture["icon:/16/open"]  
[31]   mnu12New = New Menu(mnuMenu1) As "mnuNew" 
[32]   mnu12New.Caption = "Neue Bild-Datei anlegen" 
[33]   mnu12New.Shortcut = "CTRL+N" 
[34]   mnu12New.Picture = Picture["icon:/16/new"]  
[35]   mnu13Save = New Menu(mnuMenu1) As "mnuSave" 
[36]   mnu13Save.Caption = "Speichern" 
[37]   mnu13Save.Shortcut = "CTRL+S" 
[38]   mnu13Save.Picture = Picture["icon:/16/save"] 
[39]   mnu13Save.Enabled = False  
[40]   mnu14SaveAs = New Menu(mnuMenu1) As "mnuSaveAs" 
[41]   mnu14SaveAs.Caption = "Datei speichern als..." 
[42]   mnu14SaveAs.Shortcut = "CTRL+SHIFT+S" 
[43]   mnu14SaveAs.Picture = Picture["icon:/16/save-as"]  
[44]   mnu14SaveAs.Enabled = False  
[45]   mnuSpace = New Menu(mnuMenu1) 
[46]   mnuSpace.Caption = "" 
[47]   mnu15Print = New Menu(mnuMenu1) As "mnuPrint" 
[48]   mnu15Print.Caption = "Bild drucken" 
[49]   mnu15Print.Picture = Picture["icon:/16/print"] 
[50]   mnu15Print.Enabled = False  
[51]   mnuSpace = New Menu(mnuMenu1) 
[52]   mnuSpace.Caption = "" 
[53]   mnu16Close = New Menu(mnuMenu1) As "mnuClose" 
[54]   mnu16Close.Caption = "Beenden" 
[55]   mnu16Close.Action = "FormClose" 
[56]   mnu16Close.Picture = Picture["icon:/16/quit"] 
[57] '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[58]   mnuMenu2 = New Menu(FMain, False) ' 2. Eintrag in der Menü-Leiste , sichtbar
[59]   mnuMenu2.Caption = "Extras" 
[60]   
[61]   mnu21Optionen = New Menu(mnuMenu2) As "mnuOptionen" 
[62]   mnu21Optionen.Caption = "Optionen" 
[63]   mnu21Optionen.Picture = Picture["icon:/16/options"]  
[64]   mnu22SelectColor = New Menu(mnuMenu2) As "mnuSelectColor" 
[65]   mnu22SelectColor.Caption = "Farbpaletten" 
[66]   mnu22SelectColor.Picture = Picture["icon:/16/fill"] 
[67]   mnu221Palette1 = New Menu(mnu22SelectColor) As "mnuPalette1" 
[68]   mnu221Palette1.Caption = "Farbtafel 1" 
[69]   mnu221Palette1.Picture = Picture["icon:/16/pen"] 
[70]   mnuSpace = New Menu(mnu22SelectColor) 
[71]   mnuSpace.Caption = "" 
[72]   mnu222Palette2 = New Menu(mnu22SelectColor) As "mnuPalette2" 
[73]   mnu222Palette2.Caption = "Farbtafel 2" 
[74]   mnu222Palette2.Picture = Picture["icon:/16/color"]   
[75]   mnu23PreView = New Menu(mnuMenu2) As "mnuPreView" 
[76]   mnu23PreView.Caption = "Vorschau" 
[77]   mnu23PreView.Checked = True 
[78]   mnu23PreView.Toggle = True 
[79] '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[80]   mnuMenu3 = New Menu(FMain, False) ' 3. Eintrag in der Menü-Leiste , sichtbar
[81]   mnuMenu3.Caption = "Hilfe" 
[82]   
[83]   mnu31Help = New Menu(mnuMenu3) As "mnuHelpMe" 
[84]   mnu31Help.Caption = "Hilfe" 
[85]   mnu31Help.Shortcut = "F1" 
[86]   mnu31Help.Action = "HelpMe" 
[87]   mnu31Help.Picture = Picture["icon:/16/help"]  
[88]   mnu32Information = New Menu(mnuMenu3) As "mnuInformation" 
[89]   mnu32Information.Caption = "Online-Hilfe" 
[90]   mnu32Information.Picture = Picture["icon:/16/internet"] 
[91] 
[92] End ' ShowMenu() 
[93] 
[94] Public Sub mnuOpen_Click() 
[95]   FMain.ImageOpen() 
[96]   mnu15Print.Enabled = True 
[97] End ' mnuOpen_Click() 
[98] 
[99] Public Sub mnuNew_Click() 
[100]   mnu13Save.Enabled = False 
[101]   mnu14SaveAs.Enabled = True 
[102]   FMain.CreateImage  
[103] End ' mnuNew_Click()  
[104] 
[105] Public Sub mnuSaveAs_Click() 
[106]   FMain.SaveAsDialog  
[107]   mnu13Save.Enabled = True 
[108]   mnu14SaveAs.Enabled = False 
[109] End '  mnuSaveAs_Click() 
[110] 
[111] Public Sub mnuSave_Click() 
[112]   FMain.SaveImage  
[113] End '  mnuSaveAs_Click() 
[114] 
[115] Public Sub mnuPrint_Click() 
[116]   FMain.PrintImage 
[117] End ' mnuPrint_Click() 
[118] 
[119] Public Sub mnuClose_Click() 
[120]   FHelp.Close 
[121]   FMain.Close 
[122] End ' mnuClose_Click() 
[123] 
[124] Public Sub mnuOptionen_Click() 
[125]  FMain.SetOptions 
[126] End ' mnuOptionen_Click() 
[127] 
[128] Public Sub mnuPalette1_Click() 
[129]   FMain.GetColor_1()  
[130] End ' mnuPalette1_Click() 
[131] 
[132] Public Sub mnuPalette2_Click() 
[133]   FMain.GetColor_2()  
[134] End ' mnuPalette2_Click() 
[135] 
[136] Public Sub mnuPreView_Click() 
[137]   If mnu23PreView.Checked = True Then 
[138]      Message.Info("Zukünftig Vorschaubild anzeigen oder" & gb.NewLine & "generieren oder...") 
[139]   Endif ' Checked = True 
[140] End ' mnu23PreView_Click() 
[141] 
[142] Public Sub mnuHelpMe_Click() 
[143]   FHelp.Show 
[144] End ' mnuHelpMe_Click() 
[145] 
[146] Public Sub mnuInformation_Click() 
[147]   Desktop.Open("http://www.gambas-buch.de/dw/doku.php?id=k13:start") 
[148] End ' mnu32Information_Click()
  • In lines 3 to 20, variables of type Menu are declared for the individual menus. Here you should work with care, because changes can only be made later on with effort.
  • At the end of the article you will find Gambas code that allows you to display the names of all icons in a selected size that Gambas provides internally.
  • The procedure ShowMenu () contains the declaration of all required menus and is used later in the main program.
  • In line 24 an instance (menu object) of the class Menu with the name mnuMenu1 is created. The two parameters are Parent of type Object and Hidden of type Boolean (optional). Menu1 is assigned to the form and the value for the optional parameter Hidden is set to False. This means that menu 1 is not hidden on the form - that is, it is displayed. In this case, you could do without the 2nd parameter because the default value for the 2nd parameter is Hidden False.
  • In line 25, menu 1 still has a meaningful label.
  • A further menu is created in lines 40 to 44 and assigned to menu 1 and is placed in the 4th position in the menu list because 3 menus have already been created as submenus for menu 1. The save-as-menu mnu14SaveAs is displayed, but is deactivated when starting the program. It is activated as soon as a new file has been created. You can also call the program action[line 105] assigned to the mnu14SaveAs menu later under the key combination “CTRL+SHIFT+S”. This menu has a suitable icon.
  • A so-called option menu is created in lines 75 to 78. The special features of such a menu are described in chapter 13.3.1' Option menu'. An icon is not allowed at this point, because a checkbox is displayed with a checkmark at the start of the program, if the Checked property is set to True.The program actions assigned to the individual menus are described in lines 94 to 148 in individual procedures.
  • Lines 143 and 147 call local and global help. You can access the local help by pressing F1 and the global help refers to a web page on the Internet. You can find the source code for local help in the repository.

13.1.2 Quelltext - Modul MM.module

Bereits bei einem recht einfachen Menü auf dem Formular wird der Quelltext für ein Projekt sehr schnell unübersichtlich, weil Sie zwischen den Prozeduren für das Generieren der Menüs, den Prozeduren für die Zuordnung der Programmaktionen zu den einzelnen Menüs und den Prozeduren, welche die Programmaktionen beschreiben, unterscheiden müssen. Es ist eine gute Idee, wenn Sie für das Generieren der einzelnen Menüs den Quelltext in ein Modul legen.

Die Quelltexte für das Modul und das Projekt MenuQM werden komplett angegeben und in wesentlichen Teilen kommentiert.

[1] ' Gambas class file
[2] 
[3] Private mnuMenu1 As Menu 
[4] Private mnuMenu2 As Menu 
[5] Private mnuMenu3 As Menu 
[6] 
[7] Private mnu11Open As Menu 
[8] Private mnu12New As Menu 
[9] Private mnu13Save As Menu 
[10] Private mnu14SaveAs As Menu 
[11] Private mnu15Print As Menu 
[12] Private mnu16Close As Menu 
[13] Private mnu21Optionen As Menu 
[14] Private mnu22SelectColor As Menu 
[15] Private mnu23PreView As Menu 
[16] Private mnu221Palette1 As Menu 
[17] Private mnu222Palette2 As Menu 
[18] Private mnu31Help As Menu 
[19] Private mnu32Information As Menu 
[20] Private mnuSpace As Menu 
[21] 
[22] Public Sub ShowMenu() 
[23] 
[24]   mnuMenu1 = New Menu(FMain, False) ' 1. Eintrag in der Menü-Leiste , sichtbar
[25]   mnuMenu1.Caption = "Bild-Datei" 
[26]   
[27]   mnu11Open = New Menu(mnuMenu1) As "mnuOpen" 
[28]   mnu11Open.Caption = "Bild-Datei Öffnen..." 
[29]   mnu11Open.Shortcut = "CTRL+O" 
[30]   mnu11Open.Picture = Picture["icon:/16/open"]  
[31]   mnu12New = New Menu(mnuMenu1) As "mnuNew" 
[32]   mnu12New.Caption = "Neue Bild-Datei anlegen" 
[33]   mnu12New.Shortcut = "CTRL+N" 
[34]   mnu12New.Picture = Picture["icon:/16/new"]  
[35]   mnu13Save = New Menu(mnuMenu1) As "mnuSave" 
[36]   mnu13Save.Caption = "Speichern" 
[37]   mnu13Save.Shortcut = "CTRL+S" 
[38]   mnu13Save.Picture = Picture["icon:/16/save"] 
[39]   mnu13Save.Enabled = False  
[40]   mnu14SaveAs = New Menu(mnuMenu1) As "mnuSaveAs" 
[41]   mnu14SaveAs.Caption = "Datei speichern als..." 
[42]   mnu14SaveAs.Shortcut = "CTRL+SHIFT+S" 
[43]   mnu14SaveAs.Picture = Picture["icon:/16/save-as"]  
[44]   mnu14SaveAs.Enabled = False  
[45]   mnuSpace = New Menu(mnuMenu1) 
[46]   mnuSpace.Caption = "" 
[47]   mnu15Print = New Menu(mnuMenu1) As "mnuPrint" 
[48]   mnu15Print.Caption = "Bild drucken" 
[49]   mnu15Print.Picture = Picture["icon:/16/print"] 
[50]   mnu15Print.Enabled = False  
[51]   mnuSpace = New Menu(mnuMenu1) 
[52]   mnuSpace.Caption = "" 
[53]   mnu16Close = New Menu(mnuMenu1) As "mnuClose" 
[54]   mnu16Close.Caption = "Beenden" 
[55]   mnu16Close.Action = "FormClose" 
[56]   mnu16Close.Picture = Picture["icon:/16/quit"] 
[57] '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[58]   mnuMenu2 = New Menu(FMain, False) ' 2. Eintrag in der Menü-Leiste , sichtbar
[59]   mnuMenu2.Caption = "Extras" 
[60]   
[61]   mnu21Optionen = New Menu(mnuMenu2) As "mnuOptionen" 
[62]   mnu21Optionen.Caption = "Optionen" 
[63]   mnu21Optionen.Picture = Picture["icon:/16/options"]  
[64]   mnu22SelectColor = New Menu(mnuMenu2) As "mnuSelectColor" 
[65]   mnu22SelectColor.Caption = "Farbpaletten" 
[66]   mnu22SelectColor.Picture = Picture["icon:/16/fill"] 
[67]   mnu221Palette1 = New Menu(mnu22SelectColor) As "mnuPalette1" 
[68]   mnu221Palette1.Caption = "Farbtafel 1" 
[69]   mnu221Palette1.Picture = Picture["icon:/16/pen"] 
[70]   mnuSpace = New Menu(mnu22SelectColor) 
[71]   mnuSpace.Caption = "" 
[72]   mnu222Palette2 = New Menu(mnu22SelectColor) As "mnuPalette2" 
[73]   mnu222Palette2.Caption = "Farbtafel 2" 
[74]   mnu222Palette2.Picture = Picture["icon:/16/color"]   
[75]   mnu23PreView = New Menu(mnuMenu2) As "mnuPreView" 
[76]   mnu23PreView.Caption = "Vorschau" 
[77]   mnu23PreView.Checked = True 
[78]   mnu23PreView.Toggle = True 
[79] '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[80]   mnuMenu3 = New Menu(FMain, False) ' 3. Eintrag in der Menü-Leiste , sichtbar
[81]   mnuMenu3.Caption = "Hilfe" 
[82]   
[83]   mnu31Help = New Menu(mnuMenu3) As "mnuHelpMe" 
[84]   mnu31Help.Caption = "Hilfe" 
[85]   mnu31Help.Shortcut = "F1" 
[86]   mnu31Help.Action = "HelpMe" 
[87]   mnu31Help.Picture = Picture["icon:/16/help"]  
[88]   mnu32Information = New Menu(mnuMenu3) As "mnuInformation" 
[89]   mnu32Information.Caption = "Online-Hilfe" 
[90]   mnu32Information.Picture = Picture["icon:/16/internet"] 
[91] 
[92] End ' ShowMenu() 
[93] 
[94] Public Sub mnuOpen_Click() 
[95]   FMain.ImageOpen() 
[96]   mnu15Print.Enabled = True 
[97] End ' mnuOpen_Click() 
[98] 
[99] Public Sub mnuNew_Click() 
[100]   mnu13Save.Enabled = False 
[101]   mnu14SaveAs.Enabled = True 
[102]   FMain.CreateImage  
[103] End ' mnuNew_Click()  
[104] 
[105] Public Sub mnuSaveAs_Click() 
[106]   FMain.SaveAsDialog  
[107]   mnu13Save.Enabled = True 
[108]   mnu14SaveAs.Enabled = False 
[109] End '  mnuSaveAs_Click() 
[110] 
[111] Public Sub mnuSave_Click() 
[112]   FMain.SaveImage  
[113] End '  mnuSaveAs_Click() 
[114] 
[115] Public Sub mnuPrint_Click() 
[116]   FMain.PrintImage 
[117] End ' mnuPrint_Click() 
[118] 
[119] Public Sub mnuClose_Click() 
[120]   FHelp.Close 
[121]   FMain.Close 
[122] End ' mnuClose_Click() 
[123] 
[124] Public Sub mnuOptionen_Click() 
[125]  FMain.SetOptions 
[126] End ' mnuOptionen_Click() 
[127] 
[128] Public Sub mnuPalette1_Click() 
[129]   FMain.GetColor_1()  
[130] End ' mnuPalette1_Click() 
[131] 
[132] Public Sub mnuPalette2_Click() 
[133]   FMain.GetColor_2()  
[134] End ' mnuPalette2_Click() 
[135] 
[136] Public Sub mnuPreView_Click() 
[137]   If mnu23PreView.Checked = True Then 
[138]      Message.Info("Zukünftig Vorschaubild anzeigen oder" & gb.NewLine & "generieren oder...") 
[139]   Endif ' Checked = True 
[140] End ' mnu23PreView_Click() 
[141] 
[142] Public Sub mnuHelpMe_Click() 
[143]   FHelp.Show 
[144] End ' mnuHelpMe_Click() 
[145] 
[146] Public Sub mnuInformation_Click() 
[147]   Desktop.Open("http://www.gambas-buch.de/dw/doku.php?id=k13:start") 
[148] End ' mnu32Information_Click()

Kommentare:

  • In den Zeilen 3 bis 20 werden Variablen vom Typ Menu für die einzelnen Menüs deklariert. Hier sollten Sie mit Sorgfalt arbeiten, weil sich Änderungen später nur mit Aufwand vornehmen lassen.
  • Am Ende des Artikels finden Sie Gambas-Code, mit dem Sie sich die Namen aller Icon in einer gewählten Größe anzeigen lassen können, die Gambas intern zur Verfügung stellt.
  • Die Prozedur ShowMenu() enthält die Deklaration aller benötigten Menüs und wird später im Hauptprogramm eingesetzt.
  • In der Zeile 24 wird eine Instanz (Menü-Objekt) der Klasse Menu mit dem Namen mnuMenu1 angelegt. Die beiden Parameter sind Parent vom Typ Objekt und Hidden vom Typ Boolean (optional). Es wird das Menü1 dem Formular zugewiesen und als Wert für den optionalen Parameter Hidden wird False eingetragen. Das bedeutet, dass das Menü1 auf dem Formular nicht versteckt wird – also angezeigt wird. Sie könnten in diesem Fall auf den 2. Parameter verzichten, weil der Default-Wert für den 2. Parameter Hidden False ist.
  • Das Menü1 erhält in der Zeile 25 noch eine aussagekräftige Beschriftung.
  • In den Zeilen 40 bis 44 wird ein weiteres Menü angelegt und dem Menü1 zugeordnet und steht in der Menü-Liste an 4. Stelle, weil vorher bereits 3 Menüs als Untermenüs für Menü1 angelegt wurden. Das Speichern-als-Menü mnu14SaveAs wird zwar angezeigt, ist aber beim Programmstart deaktiviert. Es wird aktiviert, sobald eine neue Datei angelegt wurde. Sie können die dem Menü mnu14SaveAs zugeordnete Programmaktion [Zeile 105] später auch unter der Tastenkombination “CTRL+SHIFT+S” aufrufen. Diesem Menü wird ein passendes Icon spendiert.
  • Ein sogenanntes Optionsmenü wird in den Zeilen 75 bis 78 angelegt. Die Besonderheiten eines derartigen Menüs werden im Kapitel 13.3.1 'Optionsmenü' beschrieben. Ein Icon verbietet sich an dieser Stelle, weil dafür eine Checkbox angezeigt wird, die mit einem Häkchen beim Programmstart angezeigt wird, wenn die Checked-Eigenschaft auf True gesetzt wurde.
  • Die den einzelnen Menüs zugeordneten Programmaktionen werden in den Zeilen 94 bis 148 in einzelnen Prozeduren beschrieben.
  • In der Zeilen 143 und 147 werden eine lokale und eine globale Hilfe aufgerufen. Die lokale Hilfe erreichen Sie über die Funktionstaste F1 und die globale Hilfe verweist auf eine Webseite im Internet. Den Quelltext zur lokalen Hilfe finden Sie im Projektarchiv.
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k13/k13.1/k13.1.2/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools