Walkthroughs > Customize the User Interface > Define Context Menus |
You can define context menus and assign them at runtime. You can also load context menus or switch from one menu to another when specific screens are loaded.
This sample opens InfoConnect, creates a new session, and defines and assigns a context menu.
Create a context menu |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Text; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.IbmHosts; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; namespace ContextMenus { class Program { static void Main(string[] args) { //Start a visible instance of InfoConnect or get the instance running at the given channel name Attachmate.Reflection.Framework.Application app = MyReflection.CreateApplication("myWorkspace", true); //Create a Ibm 3270 terminal IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}")); //Specify the host name or IP address and connect. terminal.HostAddress = "demo:ibm3270.sim"; terminal.Port = 623; terminal.Connect(); //Make the session visible IFrame frame = (IFrame)app.GetObject("Frame"); frame.CreateView(terminal); //Get the collection of context menus defined for this session IContextMenus menus = (IContextMenus)terminal.ContextMenus; //Create a menu and assign it to a copy of the existing default menu ContextMenu AlternateMenu = new ContextMenu("alternateMenu"); AlternateMenu = (ContextMenu)menus.DefaultContextMenu; //Create an action and add it to a menu item InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null); InputMapActionSequence sequence = new InputMapActionSequence(); sequence.Add(action); ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence); //Add a line separator and the menu item to the alternate menu AlternateMenu.AddSeparator(); AlternateMenu.AddMenuItem(item); //Remove the Cut item in the alternate menu based on its position in the menu AlternateMenu.RemoveItem(2); //assign the Alternate menu as the context menu for this session menus.DefaultContextMenu = AlternateMenu; Console.ReadKey(); } } } |
Create a context menu |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Text; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.OpenSystems; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; namespace ContextMenus { class Program { static void Main(string[] args) { //Start a visible instance of InfoConnect or get the instance running at the given channel name Attachmate.Reflection.Framework.Application app = MyReflection.CreateApplication("myWorkspace", true); //Create an Open Systems terminal ITerminal terminal = (ITerminal)app.CreateControl(new Guid("{BE835A80-CAB2-40d2-AFC0-6848E486BF58}")); //Specify the host name or IP address and connect. IConnectionSettingsTelnet conn = (IConnectionSettingsTelnet)terminal.ConnectionSettings; conn.HostAddress = "yourHostName"; terminal.Connect(); //Make the session visible IFrame frame = (IFrame)app.GetObject("Frame"); frame.CreateView(terminal); //Get the collection of context menus defined for this session IContextMenus menus = (IContextMenus)terminal.ContextMenus; //Initialize a menus as a copy of the existing default menu ContextMenu AlternateMenu = new ContextMenu("alternateMenu"); AlternateMenu = (ContextMenu)menus.DefaultContextMenu; //Create an action and add it to a menu item InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null); InputMapActionSequence sequence = new InputMapActionSequence(); sequence.Add(action); ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence); //Add the menu item to the alternate menu AlternateMenu.AddSeparator(); AlternateMenu.AddMenuItem(item); //Remove an item in the alternate based its position in the menu AlternateMenu.RemoveItem(2); //Assign the Default menu for the session to the new Alternate menu menus.DefaultContextMenu = AlternateMenu; Console.ReadKey(); } } } |
You can create an alternate menu and use only on specific screens.
Add properties for menus |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Text; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.IbmHosts; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection; namespace ContextMenus { class Program { private IContextMenu _alternateMenu; private IContextMenu _defaultMenu; public IContextMenu AlternateMenu { get { return this._alternateMenu; } set { this._alternateMenu = value; } } public IContextMenu DefaultMenu { get { return this._defaultMenu; } set { this._defaultMenu = value; } } static void Main(string[] args) { } } } |
Create the menus |
Copy Code
|
---|---|
static void Main(string[] args) { Program SwitchMenus = new Program(); //Start a visible instance of InfoConnect or get the instance running at the given channel name Attachmate.Reflection.Framework.Application app = MyReflection.CreateApplication("myWorkspace", true); //Create an IBM 3270 terminal string sessionFilePath = System.Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rd3x"; IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(sessionFilePath); //Make the session visible IFrame frame = (IFrame)app.GetObject("Frame"); frame.CreateView(terminal); //Get the collection of context menus defined for this session IContextMenus menus = (IContextMenus)terminal.ContextMenus; //Initialize two menus as copies of the existing default menu SwitchMenus.AlternateMenu = new ContextMenu("alternateMenu"); SwitchMenus.AlternateMenu = menus.DefaultContextMenu; SwitchMenus.DefaultMenu = new ContextMenu("defaultMenu"); SwitchMenus.DefaultMenu = menus.DefaultContextMenu; //Create an action and add it to a menu item InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null); InputMapActionSequence sequence = new InputMapActionSequence(); sequence.Add(action); ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence); //Add the menu item to the alternate menu SwitchMenus.AlternateMenu.AddSeparator(); SwitchMenus.AlternateMenu.AddMenuItem(item); //Remove an item in the alternate menu based its position in the menu SwitchMenus.AlternateMenu.RemoveItem(2); //Set event handler for NewScreenReady event so we can identify screens and swap menus for specific screens IIbmScreen screen = terminal.Screen; screen.NewScreenReady += SwitchMenus.screen_NewScreenReady; //Set event handler for BeforeDisconnect event to make sure session is saved with the correct default menu terminal.BeforeDisconnect += SwitchMenus.terminal_BeforeDisconnect; Console.ReadKey(); } |
Create the menus |
Copy Code
|
---|---|
static void Main(string[] args) { Program SwitchMenus = new Program(); //Create an application object Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication("myWorkspace", true); //Create a VT terminal string sessionPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rdox"; ITerminal terminal = (ITerminal)reflectionApplication.CreateControl(sessionPath); //Make the session visible IFrame frame = (IFrame)reflectionApplication.GetObject("Frame"); frame.CreateView(terminal); //Get the collection of context menus defined for this session IContextMenus menus = (IContextMenus)terminal.ContextMenus; //Initialize two menus as copies of the existing default menu SwitchMenus.AlternateMenu = new ContextMenu("alternateMenu"); SwitchMenus.AlternateMenu = menus.DefaultContextMenu; SwitchMenus.DefaultMenu = new ContextMenu("defaultMenu"); SwitchMenus.DefaultMenu = menus.DefaultContextMenu; //Create an action and add it to a menu item InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null); InputMapActionSequence sequence = new InputMapActionSequence(); sequence.Add(action); ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence); //Add the menu item to the alternate menu SwitchMenus.AlternateMenu.AddSeparator(); SwitchMenus.AlternateMenu.AddMenuItem(item); //Remove an item in the alternate based its position in the menu SwitchMenus.AlternateMenu.RemoveItem(2); //Set event handler for ControlKeySending event so we can identify screens and swap menus for specific screens IScreen screen = terminal.Screen; screen.ControlKeySending += SwitchMenus.screen_ControlKeySending; //Set event handler for Closing event to make sure session is saved with the correct default menu terminal.Closing += SwitchMenus.terminal_Closing; Console.ReadKey(); } |
Handle NewScreenReady event to swap menus |
Copy Code
|
---|---|
public void screen_NewScreenReady(object sender, EventArgs e) { IIbmScreen screen = (IIbmScreen)sender; IIbmTerminal terminal = screen.Parent; //Get a string that is unique to the second screen in the demo and use it to identify this screen string screenID1 = screen.GetText(1, 1, 5); //Use the alternate menu only on this screen if (screenID1 == "LOGON") { terminal.ContextMenus.DefaultContextMenu = this.AlternateMenu; } else { terminal.ContextMenus.DefaultContextMenu = this.DefaultMenu; } } |
Handle ControlKeySending event to swap menus |
Copy Code
|
---|---|
public void screen_ControlKeySending(object sender, ControlKeySendingEventArgs e) { IScreen screen = (IScreen)sender; ITerminal terminal = screen.Parent; //Apply the alternate menu only after the 'demodata' command is entered if (screen.GetText(screen.CursorRow, 1, 25).Contains("demodata")) { terminal.ContextMenus.DefaultContextMenu = AlternateMenu; } else { terminal.ContextMenus.DefaultContextMenu = DefaultMenu; } } |
Save before disconnecting |
Copy Code
|
---|---|
//Set the session to use the default menu and save it before disconnecting or closing private void terminal_BeforeDisconnect(object sender, EventArgs e) { IIbmTerminal terminal = (IIbmTerminal)sender; terminal.ContextMenus.DefaultContextMenu = this.DefaultMenu; terminal.Save(); } |
Save before closing |
Copy Code
|
---|---|
//Set the session to use the default menu and save it before closing private void terminal_Closing(object sender, CancelableEventArgs e) { ITerminal terminal = (ITerminal)sender; terminal.ContextMenus.DefaultContextMenu = this.DefaultMenu; terminal.Save(); } |