|
using System;
using System.Runtime.InteropServices.Automation;
using System.Windows;
using System.Windows.Controls;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
dynamic m_ComDrawerContoller;
AutomationEvent m_DrawerChangeEventHandler;
public MainPage()
{
InitializeComponent();
// the COM object will only be successfully created if the application is currently running
// out of browser and in elevated trust.
if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
{
RegisterComObject();
}
}
private void RegisterComObject()
{
try
{
// Create the object. If ProgId attribute is not added to the COM class, the
// string to use is assembly.class
m_ComDrawerContoller = AutomationFactory.CreateObject("ComExample.Application");
// Creates an object to manage the event from the com object.
m_DrawerChangeEventHandler = AutomationFactory.GetEvent(m_ComDrawerContoller, "DrawerStateChange");
// Adds a handler for the event actually being raised.
m_DrawerChangeEventHandler.EventRaised += new EventHandler<AutomationEventArgs>(HandleDrawerStateChange);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// Handles the click from any of the "Open Drawer x" buttons.
/// The drawer number is in the tag property of the button.
/// </summary>
private void Button_Click(object sender, RoutedEventArgs e)
{
int drawerNumber = Convert.ToInt32(((Button)sender).Tag);
m_ComDrawerContoller.OpenDrawer(drawerNumber);
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
m_ComDrawerContoller.CloseAllOpenDrawers();
}
/// <summary>
/// Handles the event raised by the com object.
/// </summary>
private void HandleDrawerStateChange(object sender, AutomationEventArgs e)
{
switch ((int)e.Arguments[0])
{
case 1:
Drawer1Status.Text = (bool)e.Arguments[1] ? "Open" : "Closed";
break;
case 2:
Drawer2Status.Text = (bool)e.Arguments[1] ? "Open" : "Closed";
break;
case 3:
Drawer3Status.Text = (bool)e.Arguments[1] ? "Open" : "Closed";
break;
}
}
private void check_click(object sender, System.Windows.RoutedEventArgs e)
{
int drawerNumberToCheck;
int.TryParse(txtDrawerToCheck.Text, out drawerNumberToCheck);
if (drawerNumberToCheck > 0)
{
bool isOpen = m_ComDrawerContoller.IsDrawerOpen(drawerNumberToCheck);
if (isOpen) lblCheckStatus.Text = "Open"; else lblCheckStatus.Text = "Closed";
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.