Recent Topics

1 Jan 25, 2008 16:47    

hello everybody, v tried the "tic-tac-toe" example of the multipoint sdk sample which worked just fine..but wen v tried to execute another code with most of the tic-tac-toe 's code v got an excetion saying "TypeInitializationException" ,v tried to throw the exception using try-catch,but all went in vain..dont know how to overcome it..also it says "microsoft.multipoint.multipoinSDK.dll.config not found "..here is our code...v are helpless..could any one go through the code and tell us where v went wrng?

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

// Windows MultiPoint SDK specific headers
using Microsoft.MultiPoint.MultiPointCommonTypes;
using Microsoft.MultiPoint.MultiPointSDK;
using Microsoft.MultiPoint.MultiPointMousePlugIn;
using Microsoft.MultiPoint.MultiPointControls;

namespace multi
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window
{
private static TextBox statustextbox = new TextBox();
private static int[] playerID = new int[2];
private const int INVALID_MOUSE_ID = -1;
private static ResourceDictionary res;
private static int current = 0;
private static Grid Tic_tac_toe_Grid;

private static String MOREMICE =
"More than two mice detected."
+ " Additional mice will be disabled.";
private static String LESSMICE =
"Two mice are required to play Tic-Tac-Toe.";

private static System.Windows.Controls.Image image;
public Window1()
{
InitializeComponent();

this.Loaded += new RoutedEventHandler(TicTacToe_Loaded);
this.KeyDown += new KeyEventHandler(TicTacToe_KeyDown);
}
void TicTacToe_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Application.Current.Shutdown();

}

void TicTacToe_Loaded(object sender, RoutedEventArgs e)
{

try
{
LoadDict();
AddGrid();
AddTicTacToeButtons();

// Initialize the SDK
InitializeSDK();
// Initial check for the mouse count
InitialCheck();
}
catch (MultiPointException ex)
{

MessageBox.Show(ex.ToString());
return;
}

catch (TypeInitializationException exec)
{
MessageBox.Show(exec.ToString());
return;
}
catch (Exception ex)
{

MessageBox.Show(ex.ToString());
return;

}

}

private void AddGrid()
{
// Set height and width for parent grid.
container.Height = 300;
container.Width = 300;
Tic_tac_toe_Grid = new Grid();
Grid.SetColumn(Tic_tac_toe_Grid, 1);

Grid.SetRow(Tic_tac_toe_Grid, 0);
container.Children.Add(Tic_tac_toe_Grid);

}

private void LoadDict()
{
try
{

res = new ResourceDictionary();
res.Source = new Uri(
"/Tic-Tac-Toe;component/Dictionary1.xaml",
UriKind.Relative
);
}
catch (Exception e)
{
statustextbox.Text = e.Message;
}
}

private void AddTicTacToeButtons()
{
// Add the rows.

Tic_tac_toe_Grid.RowDefinitions.Add(new RowDefinition());

// Add the columns.

Tic_tac_toe_Grid.ColumnDefinitions.Add(new ColumnDefinition());

// Add MultiPoint-enabled buttons

MultiPointButton m = new MultiPointButton();

// Apply button style from resource dictionary.
m.Style = (Style)(res["TTTButtons"]);

// Place button inside Tic-Tac-Toe grid.
Grid.SetColumn(m, 1);
Grid.SetRow(m, 1);
Tic_tac_toe_Grid.Children.Add(m);

// Define MultiPoint mouse button handlers.
m.MultiPointClick +=
new RoutedEventHandler(MultiPointButton_MultiPointClick);

}
void MultiPointButton_MultiPointClick(object sender, RoutedEventArgs e)
{

MultiPointButton multipointbutton = (MultiPointButton)sender;

MultiPointMouseEventArgs multipointargs =
(MultiPointMouseEventArgs)e;
current = multipointargs.DeviceInfo.ID;
statustextbox.Text = current.ToString();
}

private void InitializeSDK()
{

MultiPointSDK.SystemCursorPosition = new Point(100, 100);
// Register mouse device.
App.MultiPointObject.RegisterMouseDevice();

// Hide system cursor.
MultiPointSDK.HideSystemCursor();
// Set current window before the device visual is drawn for
// parent window for visuals.
App.MultiPointObject.CurrentWindow = this;
// Draw device visual
App.MultiPointObject.DrawDevices();

// Handling Plugging and unplugging of devices
App.MultiPointObject.DeviceArrivalEvent += new EventHandler<DeviceNotifyEventArgs>(MultiPointObject_DeviceArrivalEvent);
App.MultiPointObject.DeviceRemoveCompleteEvent += new EventHandler<DeviceNotifyEventArgs>(MultiPointObject_DeviceRemoveCompleteEvent);

// assign invalid ID s to both the players initially
playerID[0] = playerID[1] = INVALID_MOUSE_ID;

}

/// <summary>
/// This handler is called when a device is removed from the machine.
/// This function removes the device visual for the player whose mouse is unplugged.
/// If there is already another mouse attached to the machine it will replace the player
/// whose mouse is unplugged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void MultiPointObject_DeviceRemoveCompleteEvent(object sender, DeviceNotifyEventArgs e)
{
//To continue processing only when the device that is unplugged is a mouse device.
if (e.DeviceInfo.DeviceType == DeviceType.Mouse)
{
int tempPlayerID = -1;

// Check whether the currently un-plugged mouse corresponds to
// player 1 or player 2
if (e.DeviceInfo.ID == playerID[0])
{
tempPlayerID = 0;
}
else
{
tempPlayerID = 1;
}

if (tempPlayerID != -1)
{
playerID[tempPlayerID] = INVALID_MOUSE_ID;
//Checking if there the third before removing three mice were already connected to the machine
//In this case the the other mouse which was not assigned to either user will be assigned to the
// player whose mouse is disconnected.
if (App.MultiPointObject.MouseDeviceList.Count >= 2)
{
foreach (DeviceInfo mouse in App.MultiPointObject.MouseDeviceList)
{
if (playerID[1 - tempPlayerID] != mouse.ID)
{
playerID[tempPlayerID] = mouse.ID;
SetCursor(mouse, true);
break;
}
}
}
}
}
}

/// <summary>
/// This function is used to set a cursor visibility and visual
/// </summary>
/// <param name="mouse"></param>
/// <param name="visible"></param>
private void SetCursor(DeviceInfo mouse, bool visible)
{

if (visible)
{
//if the mouse corresponds to player 1 set the correspoding cursor image
if (mouse.ID == playerID[0])
{
((MultiPointMouseDevice)(((DeviceInfo)(mouse)).DeviceVisual)).CursorImage = CreatBitmapImage(Properties.Resources.cursor1);
}
else if (mouse.ID == playerID[1])
{
((MultiPointMouseDevice)(((DeviceInfo)(mouse)).DeviceVisual)).CursorImage = CreatBitmapImage(Properties.Resources.cursor2);
}
}

((MultiPointMouseDevice)mouse.DeviceVisual).Visible = visible;

}

/// <summary>
/// This handler is called when a new device is added to the machine
/// If no mouse is attached to a player this funtion will attach the pligged
/// mouse to the user.
/// If there are already two mouse attached to the machine, this function
/// will simply hide the cursor of the mouse that is plugged in.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void MultiPointObject_DeviceArrivalEvent(object sender, DeviceNotifyEventArgs e)
{
//Continue working only if the device that is plugged in is a mouse
if (e.DeviceInfo.DeviceType == DeviceType.Mouse)
{
//Assign the mouse to the player that does not have a mouse
if (playerID[0] == INVALID_MOUSE_ID)
{
playerID[0] = e.DeviceInfo.ID;
SetCursor(e.DeviceInfo, true);

}
else if (playerID[1] == INVALID_MOUSE_ID)
{
playerID[1] = e.DeviceInfo.ID;
SetCursor(e.DeviceInfo, true);
}
else
{ //if both the players have corresponding mice
// set the new mouse as invisible
SetCursor(e.DeviceInfo, false);
}
}
}

private BitmapImage CreatBitmapImage(System.Drawing.Bitmap b)
{

BitmapImage bmpimg = new BitmapImage();
MemoryStream memStream = new MemoryStream();
bmpimg.BeginInit();
b.MakeTransparent(System.Drawing.Color.White);
b.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
bmpimg.StreamSource = memStream;
bmpimg.EndInit();

return bmpimg;
}

private void InitialCheck()
{

// Throw error if only one mouse is connected. Error for no mice connected
// is thrown by InitializeSDK.
if (App.MultiPointObject.MouseDeviceList.Count < 2)
{
statustextbox.Text = LESSMICE;

}
if (App.MultiPointObject.MouseDeviceList.Count > 2)
{
// Disable extra mice. Only 2 are required.
for (int i = 2; i < App.MultiPointObject.MouseDeviceList.Count; i++)
{

((MultiPointMouseDevice)((DeviceInfo)(App.MultiPointObject.MouseDeviceList[i])).DeviceVisual).Visible = false;

}
statustextbox.Text = MOREMICE;

}

// Applying different cursor images for Mice 1 and 2
try
{
((MultiPointMouseDevice)((DeviceInfo)(App.MultiPointObject.MouseDeviceList[0])).DeviceVisual).CursorImage
= CreatBitmapImage(Properties.Resources.cursor1);
playerID[0] = ((DeviceInfo)(App.MultiPointObject.MouseDeviceList[0])).ID;

((MultiPointMouseDevice)((DeviceInfo)(App.MultiPointObject.MouseDeviceList[1])).DeviceVisual).CursorImage
= CreatBitmapImage(Properties.Resources.cursor2);
playerID[1] = ((DeviceInfo)(App.MultiPointObject.MouseDeviceList[1])).ID;
}
catch (Exception)
{
return;
}

}

}
}


Form is loading...