Main.cs (IBM)
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)
{
try
{
m_application = MyReflection.CreateApplication("Reflection");
}
catch
{
try
{
m_reflectionInstance = MyReflection.StartProgram();
m_application = MyReflection.CreateApplication("Reflection");
}
catch (ApplicationException ex2)
{
MessageBox.Show(ex2.Message);
MessageBox.Show("Fatal error. You may want to exit.");
}
}
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)
MyReflection.StopProgram(m_reflectionInstance);
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();
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.");
.
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.msn.com");
}
else
Console.WriteLine("Fatal error. Check Reflection 2008 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 a 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 searchDiv = m_webDocument.GetElementById("search_header");
if (searchDiv != null)
{
IWebElement[] divElements = searchDiv.GetElementsByTagName("h5");
if (divElements != null && divElements.Length > 0)
{
IWebElement h5 = divElements[0];
if (h5 != null)
{
string result = h5.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 inputType;
string inputValue;
foreach( IWebElement webElement in inputElements )
{
inputType = webElement.GetAttribute("type");
if( !String.IsNullOrEmpty( inputType))
{
if( inputType.Equals("submit", StringComparison.CurrentCultureIgnoreCase))
{
inputValue = webElement.GetAttribute("value");
if(!String.IsNullOrEmpty(inputValue))
{
if( inputValue.Equals("Search Web", 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("f1");
if (searchBox != null)
{
searchBox.PutText(textToSearch);
return true;
}
else
{
MessageBox.Show("Can not find the search text 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;
}
}
}
|