|
-
Aug 22nd, 2007, 08:59 AM
#1
Thread Starter
New Member
C# native AppActivate method?
I want to activate an application (Excel) in code. If I reference
microsoft.visualbasic, I could use:
Interaction.AppActivate("Microsoft Excel");
But I wold rather use a native C# approach. I know that Interaction.MsgBox
has a native analog: MessageBox.Show.
Is there one for AppActivate?
-
Aug 22nd, 2007, 03:52 PM
#2
Re: C# native AppActivate method?
I'm not sure but I think this will work:
Code:
System.Windows.Forms.Control.FromHandle(System.Diagnostics.Process.GetProcessesByName("Microsoft Excel")[0].MainWindowHandle).Focus();
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 22nd, 2007, 04:58 PM
#3
Thread Starter
New Member
Re: C# native AppActivate method?
OK, I tried that. I think we're really close but I got "Index was outside the bounds of the array." Clearly, the error is about the ("Microsoft Excel")[0].
-
Aug 22nd, 2007, 05:13 PM
#4
Re: C# native AppActivate method?
try excel.exe
and you should modify the code to make sure the Process.GetProcessesByName returned something
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 22nd, 2007, 06:14 PM
#5
Re: C# native AppActivate method?
The Process.MainWindowHandle property returns an IntPtr and the IntPtr type has no Focus method. You're trying to treat it like it returns a Form reference, which it couldn't because the window isn't a .NET Form anyway.
You do need to get the MainWindowHandle property value, but once you have it you have to go to the Windows API. SetForegroundWindow takes the handle of the window you want to activate. MainWindowHandle is that handle.
-
Aug 22nd, 2007, 07:52 PM
#6
Thread Starter
New Member
Re: C# native AppActivate method?
Thanks everyone. I'll stick to the VB reference.
-
Aug 22nd, 2007, 08:09 PM
#7
Re: C# native AppActivate method?
 Originally Posted by CarlBuddig
Thanks everyone. I'll stick to the VB reference.
That's all well and good but if the app in question is minimised then AppActivate won't restore it anyway, so even in VB you have to use the Windows API if you want that functionality.
-
Aug 22nd, 2007, 11:50 PM
#8
Re: C# native AppActivate method?
The Process.MainWindowHandle property returns an IntPtr and the IntPtr type has no Focus method.
But the Control.FromHandle does
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 22nd, 2007, 11:58 PM
#9
Re: C# native AppActivate method?
 Originally Posted by ComputerJy
But the Control.FromHandle does
Ah, didn't look closely enough did I? That said, a window in an unmanaged application is not a .NET control so you can't just create one from a handle. If the handle is not for a managed control to begin with then it's no help because Control.FromHandle will return null.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|