UIElement containing another application's window?
Hello,
I've seen the description (in MSDN docs) of UIElement that says: "UIElement can be considered roughly equivalent to a window handle in Microsoft Win32 programming". Maybe I'm taking this too literally, but *could* a UIElement contain another application's window?
For example, if you've seen the puzzle sample in the Windows SDK (v6.0\Samples\Demos\15Puzzle\Csharp), they do something pretty cool by splitting up different UIElement objects across the pieces of a puzzle, and scrambling the pieces. They have it working for video, some vector (svg-like) animation, 3D cube animation, and a windows form. I was wondering if it was possible to take this a bit further and show, for example, Microsoft Word or even the command-line prompt on that puzzle.
Thanks!
What you really want there is interop, not really UIElement. Specifically, HwndHost. That's how WinForms got in there in the game you saw. WinForms has their own premade HWndHost WindowsFormsHost, so it's even easier for WinForms. HwndHost derives from UIElement, so can be placed into the same content model situations. So long as you can get a window created that supports managed extensions on the outside (ideally CLI), have all the security demands cleared, and can get whatever you want as a child window of that Hwnd you created for interop, I believe something as oddball as a flying command prompt is possible that way. Proviso is that there are a lot of restrictions about how input behaves, trying to share the screen, and a goodly number of 'typical' UIElement/FrameworkElement inherited properties won't "drill in" to a Win32 equivalent property of the hosted window (this last bit is a fact I need to better capture in the HWndHost documentation, BTW).
Hey Wolf,
I am delighted with your answer!
I tried to move this forward a bit. I couldn't figure out how the puzzle sample actually uses HWndHost or WindowsFormsHost. So I decided to add my own HwndHost as an option to show in the puzzle sample. Well, it's not really my own HwndHost; I just "borrowed" the ControlHost from "Walkthrough: Hosting a Simple Win32 Control in a Windows Presentation Foundation Application" (http://msdn2.microsoft.com/en-us/library/ms752055.aspx). Then I added the following case in Puzzle.xaml.cs:
case "Win32Listbox":
{
FrameworkElement elt = new WPF_Hosting_Win32_Control.ControlHost(700, 500);
_elementToChopUp = elt;
_puzzleSize = new Size(700, 500);
}
break;
However, the listbox doesn't show up. I get no error message, or anything to indicate what went wrong. I stepped through the code and the ControlHost() constructor gets called, but ControlHost.BuildWindowCore() never gets called.
Would you have any advice to help diagnose this?
Thanks!