I'm working on a project (SDK 1.1) where we're automating Excel XP. We
have an ExcelService class which is responsible to maintaining Excel
object states and cleanup. The class also acts as a sink for the click
events of several buttons that it creates in Excel Commandbars.
Some of these buttons are required to show WinForms as if they were
launched from Excel (replacement for UserForms). These winforms need
to be modal because of other dependencies.
What is the best way to achieve this? (A try-finally block with
EnableWindow on Excel while the WinForm is loaded as ShowDialog with
SetForegroundWindow from its activate?)
Please ignore... I should've tested my own theory. This seems to work.
In the ExcelService class, I have
try
{
m_frm = new ContractSelectionForm( mode );
m_frm.Closed +=new EventHandler(m_frm_Closed);
EnableWindow( new IntPtr(base.GetExcelhWnd()), false);
m_isWndEnabled = false;
m_frm.ShowDialog();
}
catch (Exception exc)
{
throw new AdapterServiceException(exc);
}
finally
{
if (!m_isWndEnabled)
{
EnableWindow( new IntPtr(base.GetExcelhWnd()), true);
m_isWndEnabled = true;
}
}
and in the form, I have
private void ContractSelectionForm_Activated(object sender,
System.EventArgs e)
{
if( !GetForegroundWindow().Equals( this.Handle ) )
{
ShowWindow(this.Handle, SW_RESTORE);
SetForegroundWindow(this.Handle);
}
}
However, this creates another problem. While the dialog is open and
the user clicks around in the Excel instance, hte mouseClicks seem to
get queued up, only to take place when the dialog is dismissed. Is
there a workaround for this?
Thanks
Thanks
test.file@gmail.com - 14 Jun 2005 13:11 GMT
Well, there's a simpler workaround all this. All I had to do was
implement IWin32Window in my ExcelService class and return Excel's
hWnd.
public IntPtr Handle
{
get
{
return new IntPtr (base.GetExcelhWnd());
}
}
And in ShowDialog, pass the class as the owner.
m_frm.ShowDialog(this);
Even though this is not a form, the interface implementation was enough
to allow me to show the WinForm as modal to C# and also to Excel (and
displayed on top of Excel).
RTFM, I guess.
Thanks