Launch the .Net Win Forms from Excel spreadsheet
Hi,
Actually first I am mentioning some details about my application:
1. It is a Windows Application written in C#.
2. It is an Trading application that has one Main screen (Its a normal
Window Form not MDI Form) from where we can launch Trade Specific screens by
clicking on New and Edit button.
3. We can launch these Trade specific screens from Excel sheet as well but
the precondition for this is that Main screen of Trading application must be
open by that time.
Now the Problem is that:
If any Trade specific Screen is already open then it doesn't open the new
Trade specific Screen from Excel sheet. Actually I have written the following
Snippet of code to open the screen:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
IntPtr lParam);
public bool UploadToTradeScreen( string tradeID, bool saveDebugFile )
{
Process[] myProcesses = Process.GetProcessesByName("TradeApp");
if(myProcesses.Length == 0)
return false;
XmlDocument xmlDoc = FetchXMLDoc( tradeID );
//Other Logic ...
IntPtr ret = SendMessage(myProcesses[0].MainWindowHandle,
WM_USER_TradeApp_OPENTRADE, iNow, (IntPtr)0 );
int iRet = ret.ToInt32( );
if( saveDebugFile == false )
File.Delete( fileName );
return iRet == 0 ? false : true;
}
and I am trapping this Window Message in the following code snippet to open
the screen:
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case ((int) WindowsMessages.WM_USER_TradeApp_OPENTRADE):
{
//..code..
StaticHelperClass.FrameWorkHelper.AppHelper.Displa yTradeData( fileName );
break;
}
case ((int) WindowsMessages.WM_USER_TradeApp_SAVETRADE):
{
//..code..
StaticHelperClass.FrameWorkHelper.AppHelper.SaveTr adeData( fileName );
break;
}
default:
base.WndProc(ref m);
break;
}
}
When the Main screen opens and very first time we launch Trade Specific Screen from Excelsheet then it opens the Trade Specific Screen. But next time it doesn't open the new Trade Specific screen
from Excel sheet.
My guess about this problem is that when we open any Trade Specific Screen then the MainWindowHandle transfers to the Trade Specific Screen from the Main Screen.
Could you please suggest me some solutions to overcome this problem, so that I can open the multiple Trade specific screens from the Excel sheet.
Thanks
AAruni
|