Controlling other windows from C# app
I'm writing a C# app that will aggregate control of several applications (WMP, Google Earth, etc.). For apps that I am not writing myself, I am launching as a process, so I have their handles (handle = Process.Start("C:\whatever.exe"); is the code, I believe).
For it to work smoothly, I need to be 开发者_StackOverflow社区able to control the minimized/maximized state of each window, including those I launched with Process.Start(). I've seen several methods that claim to do this, usually requiring an import of user32.dll and applying ShowWindow(handle, state).
I ask two questions. Is this the best way to do this? I'm new to .NET/Windows programming (coming from *nix). Further, I haven't been able to get it to work, so are there any quirks that would catch a newbie?
(FYI: VS 2008 on Windows 7 RTM)
Getting the handle of the process is likely the wrong handle if you are looking for a handle of the window.
Back in my VB6 days I used libraries that let you find windows by thier title, then from there you could pass messages to min/max/close/activate etc, but I havent done anything like that since moving to .Net, though I'm sure the same types of libraries exist for .Net
Response to comment:
Yes I see there is a GetMainWindowHandle property, so thats one step down. Next you need to look into windows messaging, this is where you send the "message" to do various windows actions. I dont have the messaging code handy but it is fairly straight forward once you know the codes for each message.
You may also want to check out the Windows Accessibility APIs. Their used extensively within Microsoft for automating UI tests.
If you want a more natural API to do this, you could do worse than White. It's mainly aimed at functional testing (and based on the Windows Accessibility APIs Jim mentioned), but should serve for your purposes. Have a look at this page about working with windows. You'd probably write something like:
var application = Application.Attach(processID);
var window = application.GetWindow(windowName);
window.DisplayState = DisplayState.Minimized;
精彩评论