Sample Programs > Main |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Attachmate.Reflection.Emulation.IbmHosts; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Web.Msie; using Attachmate.Reflection.UserInterface; namespace HostAndWeb { public partial class Main : Form { private Attachmate.Reflection.Framework.Application m_application; private Guid m_reflectionInstance; private IIbmTerminal m_terminal; private IIbmScreen m_screen; private IWebControl m_webControl; private IWebDocument m_webDocument; public Main() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { m_application = MyReflection.CreateApplication("myWorkspace", true); RowTextBox.Text = "13"; ColumnTextBox.Text = "18"; LengthTextBox.Text = "19"; SearchWebButton.Enabled = false; SearchPosLabel.Enabled = false; RowLabel.Enabled = false; ColumnLabel.Enabled = false; LengthLabel.Enabled = false; RowTextBox.Enabled = false; ColumnTextBox.Enabled = false; LengthTextBox.Enabled = false; base.OnLoad(e); } protected override void OnClosed(EventArgs e) { if (m_reflectionInstance != null && m_reflectionInstance != Guid.Empty) m_application.Close(ApplicationCloseOption.CloseNoSave); base.OnClosed(e); } private void InitSessionsButton_Click(object sender, EventArgs e) { if (m_application != null) { m_terminal = (IIbmTerminal)m_application.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\[session filename]"); if (m_terminal != null) { m_terminal.Connect(); //You can also use AfterConnect event to wait for the connection. while (!m_terminal.IsConnected) { System.Threading.Thread.Sleep(500); } m_screen = m_terminal.Screen; m_screen.WaitForHostSettle(4000, 2000); } else MessageBox.Show("Failed to create the host session. Please check the session file path."); m_webControl = (IWebControl)m_application.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\[filename].urlx"); if (m_webControl != null) { while (m_webControl.IsBusy) { System.Threading.Thread.Sleep(500); } } else MessageBox.Show("Failed to create the Web session. Please check the Web session file path."); //This makes a session visible in the Reflection application. IView terminalSessionView; IView webSessionView; IFrame theFrame = (IFrame)m_application.GetObject("Frame"); if (theFrame != null) { terminalSessionView = theFrame.CreateView(m_terminal); webSessionView = theFrame.CreateView(m_webControl); m_webControl.Navigate("http://www.bing.com"); } else Console.WriteLine("Fatal error. Check Reflection installation."); InitSessionsButton.Enabled = false; SearchWebButton.Enabled = true; SearchPosLabel.Enabled = true; RowLabel.Enabled = true; ColumnLabel.Enabled = true; LengthLabel.Enabled = true; RowTextBox.Enabled = true; ColumnTextBox.Enabled = true; LengthTextBox.Enabled = true; } } private void SearchWebButton_Click(object sender, EventArgs e) { string hostText = GetHostText(); if (MessageBox.Show("The host text to search on the Web : " + hostText + " \r\n Are you sure you would like to search this text on the Web?","Text to Search", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (!string.IsNullOrEmpty(hostText)) { if (InputHostTextToSearchBox(hostText)) { if (SearchWeb()) { string searchResult = GetSearchResult(); if (searchResult == String.Empty) { MessageBox.Show("Did not find any reference on the Web."); } else MessageBox.Show("Found " + searchResult + " references on the Web."); } } } else MessageBox.Show("Invalid search text."); } } private string GetSearchResult() { if (m_webDocument != null) { IWebElement resultElement = m_webDocument.GetElementById("count"); { string result = resultElement.InnerText; if (!String.IsNullOrEmpty(result)) { int resultStringIndex = -1; int ofStringIndex = -1; if (((resultStringIndex = result.IndexOf("results")) != -1) && (( ofStringIndex = result.IndexOf("of")) != -1 )) { string searchResult = result.Substring(ofStringIndex + 2, resultStringIndex - ofStringIndex - 2); return searchResult; } } } } else { MessageBox.Show("Invalid WebDocument."); return String.Empty; } return String.Empty; } private bool SearchWeb() { if (m_webDocument != null) { IWebElement searchButton = FindSearchButton(); if (searchButton != null) { searchButton.Click(); ProgressBox pb = new ProgressBox(); pb.Show(this); while (m_webControl.IsBusy) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(500); } pb.Visible = false; pb.Dispose(); return true; } else { MessageBox.Show("Can not find the Search button on the Web page"); return false; } } else return false; } private IWebElement FindSearchButton() { IWebElement[] inputElements; inputElements = m_webDocument.GetElementsByTagName("input"); if (inputElements != null && inputElements.Length > 0 ) { string inputName; string inputValue; foreach( IWebElement webElement in inputElements ) { inputName = webElement.GetAttribute("name"); if( !String.IsNullOrEmpty(inputName)) { if( inputName.Equals("go", StringComparison.CurrentCultureIgnoreCase)) { inputValue = webElement.GetAttribute("title"); if(!String.IsNullOrEmpty(inputValue)) { if( inputValue.Equals("Search", StringComparison.CurrentCultureIgnoreCase)) { return webElement; } } } } } } return null; } private bool InputHostTextToSearchBox(string textToSearch) { m_webDocument = m_webControl.Document; if (m_webDocument != null) { IWebElement searchBox = m_webDocument.GetElementById("q"); if (searchBox != null) { searchBox.PutText(textToSearch); return true; } else { MessageBox.Show("Can not find the Search box on the Web page"); return false; } } else { MessageBox.Show("Invalid WebDocument"); return false; } } private string GetHostText() { string hostText = String.Empty; int row; int column; int length; if (!Int32.TryParse(RowTextBox.Text, out row)) { MessageBox.Show("Please input a valid row number."); return String.Empty; } else if (row > m_screen.Rows || row < 0) { MessageBox.Show("Row is out of the valid range."); return String.Empty; } if (!Int32.TryParse(ColumnTextBox.Text, out column)) { MessageBox.Show("Please input a valid column number."); return String.Empty; } else if (column > m_screen.Columns || column < 0) { MessageBox.Show("Column is out of the valid range."); return String.Empty; } if (!Int32.TryParse(LengthTextBox.Text, out length)) { MessageBox.Show("Please input a valid length number."); return String.Empty; } else if (length < 0) { MessageBox.Show("Length is out of the valid range."); return String.Empty; } if (m_screen != null) { hostText = m_screen.GetText(row, column, length); } return hostText; } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Web.Msie; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection.Emulation.OpenSystems; namespace HostAndWeb { public partial class Main : Form { private Attachmate.Reflection.Framework.Application m_application; private Guid m_reflectionInstance; private ITerminal m_terminal; private IScreen m_screen; private IWebControl m_webControl; private IWebDocument m_webDocument; public Main() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { m_application = MyReflection.CreateApplication("myWorkspace", true); RowTextBox.Text = "13"; ColumnTextBox.Text = "18"; LengthTextBox.Text = "19"; SearchWebButton.Enabled = false; SearchPosLabel.Enabled = false; RowLabel.Enabled = false; ColumnLabel.Enabled = false; LengthLabel.Enabled = false; RowTextBox.Enabled = false; ColumnTextBox.Enabled = false; LengthTextBox.Enabled = false; base.OnLoad(e); } protected override void OnClosed(EventArgs e) { if (m_reflectionInstance != null && m_reflectionInstance != Guid.Empty) m_application.Close(ApplicationCloseOption.CloseNoSave); base.OnClosed(e); } private void InitSessionsButton_Click(object sender, EventArgs e) { if (m_application != null) { m_terminal = (ITerminal)m_application.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\[session filename]"); if (m_terminal != null) { m_terminal.Connect(); //You can also use AfterConnect event to wait for the connection. while (!m_terminal.IsConnected) { System.Threading.Thread.Sleep(500); } m_screen = m_terminal.Screen; // m_screen.WaitForHostSettle(4000, 2000); } else MessageBox.Show("Failed to create the host session. Please check the session file path."); m_webControl = (IWebControl)m_application.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\[filename].urlx"); if (m_webControl != null) { while (m_webControl.IsBusy) { System.Threading.Thread.Sleep(500); } } else MessageBox.Show("Failed to create the web session. Please check the web session file path"); //This makes a session visible in the Reflection application. IView terminalSessionView; IView webSessionView; IFrame theFrame = (IFrame)m_application.GetObject("Frame"); if (theFrame != null) { terminalSessionView = theFrame.CreateView(m_terminal); webSessionView = theFrame.CreateView(m_webControl); m_webControl.Navigate("http://www.bing.com"); } else Console.WriteLine("Fatal error. Check Reflection installation."); InitSessionsButton.Enabled = false; SearchWebButton.Enabled = true; SearchPosLabel.Enabled = true; RowLabel.Enabled = true; ColumnLabel.Enabled = true; LengthLabel.Enabled = true; RowTextBox.Enabled = true; ColumnTextBox.Enabled = true; LengthTextBox.Enabled = true; } } private void SearchWebButton_Click(object sender, EventArgs e) { string hostText = GetHostText(); if (MessageBox.Show("The host text to search on the Web : " + hostText + " \r\n Are you sure you would like to search this text on the Web?","Text to Search", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (!string.IsNullOrEmpty(hostText)) { if (InputHostTextToSearchBox(hostText)) { if (SearchWeb()) { string searchResult = GetSearchResult(); if (searchResult == String.Empty) { MessageBox.Show("Did not find any reference on the Web."); } else MessageBox.Show("Found " + searchResult + " references on the Web."); } } } else MessageBox.Show("Invalid search text."); } } private string GetSearchResult() { if (m_webDocument != null) { IWebElement resultElement = m_webDocument.GetElementById("count"); { string result = resultElement.InnerText; if (!String.IsNullOrEmpty(result)) { int resultStringIndex = -1; int ofStringIndex = -1; if (((resultStringIndex = result.IndexOf("results")) != -1) && (( ofStringIndex = result.IndexOf("of")) != -1 )) { string searchResult = result.Substring(ofStringIndex + 2, resultStringIndex - ofStringIndex - 2); return searchResult; } } } } else { MessageBox.Show("Invalid WebDocument."); return String.Empty; } return String.Empty; } private bool SearchWeb() { if (m_webDocument != null) { IWebElement searchButton = FindSearchButton(); if (searchButton != null) { searchButton.Click(); ProgressBox pb = new ProgressBox(); pb.Show(this); while (m_webControl.IsBusy) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(500); } pb.Visible = false; pb.Dispose(); return true; } else { MessageBox.Show("Can not find the Search button on the Web page"); return false; } } else return false; } private IWebElement FindSearchButton() { IWebElement[] inputElements; inputElements = m_webDocument.GetElementsByTagName("input"); if (inputElements != null && inputElements.Length > 0 ) { string inputName; string inputValue; foreach( IWebElement webElement in inputElements ) { inputName = webElement.GetAttribute("name"); if( !String.IsNullOrEmpty( inputName)) { if( inputName.Equals("go", StringComparison.CurrentCultureIgnoreCase)) { inputValue = webElement.GetAttribute("title"); if(!String.IsNullOrEmpty(inputValue)) { if( inputValue.Equals("Search", StringComparison.CurrentCultureIgnoreCase)) { return webElement; } } } } } } return null; } private bool InputHostTextToSearchBox(string textToSearch) { m_webDocument = m_webControl.Document; if (m_webDocument != null) { IWebElement searchBox = m_webDocument.GetElementById("q"); if (searchBox != null) { searchBox.PutText(textToSearch); return true; } else { MessageBox.Show("Can not find the Search box on the Web page"); return false; } } else { MessageBox.Show("Invalid WebDocument"); return false; } } private string GetHostText() { string hostText = String.Empty; int row; int column; int length; if (!Int32.TryParse(RowTextBox.Text, out row)) { MessageBox.Show("Please input a valid row number."); return String.Empty; } else if (row > m_screen.DisplayRows || row < 0) { MessageBox.Show("Row is out of the valid range."); return String.Empty; } if (!Int32.TryParse(ColumnTextBox.Text, out column)) { MessageBox.Show("Please input a valid column number."); return String.Empty; } else if (column > m_screen.DisplayColumns || column < 0) { MessageBox.Show("Column is out of the valid range."); return String.Empty; } if (!Int32.TryParse(LengthTextBox.Text, out length)) { MessageBox.Show("Please input a valid length number."); return String.Empty; } else if (length < 0) { MessageBox.Show("Length is out of the valid range."); return String.Empty; } if (m_screen != null) { hostText = m_screen.GetText(row, column, length); } return hostText; } } }