Click here to Skip to main content
15,883,887 members
Please Sign up or sign in to vote.
4.38/5 (6 votes)
Got an interesting request here. in This Article there are is an "embedded" program in a form, now, old iexplore, and notepad, works fine in VS2010 with some editing. Now that is all fine, but when I try "embedding" Chrome, MS mathematics, and VLC it simply opens it up, and not "embedding" it. I am New to C# so I cannot solve this on my own, could anybody give me some help, keep in mind I'm a newbie?

If anybody have something doing the same thing just using WPF and/or VS2010, I'd like to see the code.

FYI: This is for a proof-of-consept program, I'm using to teach myself C#.

Thanks for any and all help!



EDIT:
Found something nice that could possibly give me what I need:
http://www.codeproject.com/KB/cs/WindowTabifier.aspx
Any thoughts?

-frank
Posted
Updated 18-Jan-11 10:34am
v3
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 15:20pm    
Interesting reference and question - my 5.
Henry Minute 18-Jan-11 15:21pm    
Which OS are you using?
Frank R. Haugen 18-Jan-11 16:19pm    
Win7 x64

Frank, I appreciate your question.

I took a look at the article. It is very interesting. Even though it is short, but the problem is not so trivial. At the same time, looking with my unarmed eye I can see, it simply cannot be absolutely universal, so success is based upon some assumption on the child process. Perhaps, this is so because it is to hard to make it more universal.

I would ask Jay Nelson directly as the most competent person on the topic right now. I suggest you prepare minimalistic code to manifest the problem.
 
Share this answer
 
v2
Comments
Espen Harlinn 18-Jan-11 15:36pm    
VLC is another nice open source project: http://www.videolan.org/vlc/
Sergey Alexandrovich Kryukov 18-Jan-11 16:31pm    
Thank you, Espen, I did not recognized the acronym and mixed up, answer modified. Yes, I know this thing, it became more stable and can help in very difficult situations.
Sergey Alexandrovich Kryukov 18-Jan-11 22:15pm    
By the way, Espen, thank you again: when you reminded me about VLC, I immediately remembered about another question by Dalek Dave which I did not know exactly how to approach in effective way ("Web Cam Image on Web Page"). I did not use VLC for streaming but decided to search and found something that might solve a problem. I'll let Dalek to check up if it work for him.
Hei Frank :)

Take a look at http://msdn.microsoft.com/en-us/library/ms742522.aspx[^]

and

http://en.wikipedia.org/wiki/Desktop_Window_Manager[^]

Offers an explanation on what's going on.

SetParent(appWin, this.Handle);


Gets into trouble with WPF based applications (Top level) see: Technology Regions Overview[^]. In this case WPF is not rendering to a GDI device context, but uses accellerated graphics rendering.

(Ble en del linker, men jeg tror det dekker det vesentligste)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 16:39pm    
Espen, I think this time your references are irrelevant. Of course WPF can host WinForm control (and visa versa), but... did you see Jay's work? It allows to host a different process, which is very different. Same think about DWM -- that's not it.
Espen Harlinn 18-Jan-11 16:45pm    
You're right, but you made a slight oversight - WPF *top level* windows bypasses GDI
Sergey Alexandrovich Kryukov 18-Jan-11 17:12pm    
Interesting note, thank you. I think I did not realize it.
Hi,

hm, does it really open a "new" Window for Chrome and VLC Player?
I'm also hosting WindowsForms with Espen Harlinns Solution (SetParent), tried it with VLC and Chrome and it gets emmbedded in window or wpf page successfully.
I read the interesting links from SAKryukov and Harlinn and now I don't know if it's because of the missing rendering that I'm not able to resize the window at the moment.
Don't tried it with serveral WindowsFormsHost as in Tabifier Article.
However, I was wondering why it's not working for you to embedd exactly the applications you wrote.
I embedded it this way:

XAML

XML
<TabItem Name="tab1" Header="Tab 1" Foreground="White" FontWeight="Bold" FontSize="20">
                   <my:Window_Host_Helper x:Name="host"></my:Window_Host_Helper>
               </TabItem>


C#
public class Window_Host_Helper : System.Windows.Controls.ContentControl
    {
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
           int Y, int cx, int cy, uint uFlags);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
          public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
        
        const UInt32 SWP_FRAMECHANGED = 0x0020;  
        static readonly int GWL_STYLE = (-16);
        public const uint WS_CAPTION = 0x00C00000;
        public const uint WS_THICKFRAME = 0x00040000;
        public const uint WS_CLIPCHILDREN = 0x02000000;
        public const uint WS_OVERLAPPED = 0x00000000;
        public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);

        private WindowsFormsHost host = new WindowsFormsHost();
        private System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
        private IntPtr windowHandle;

        public System.Diagnostics.Process Process
        {
            set
            {
                try
                {
                    value.Start();
                   
                    if (!value.WaitForInputIdle(3000))
                    {
                        throw new ArgumentException();   
                    }
                    //Important to let the thread sleep for a while
                    System.Threading.Thread.Sleep(1000);
                    this.windowHandle = value.MainWindowHandle;
                    int dwStyle = GetWindowLong(this.windowHandle, GWL_STYLE);
                    SetWindowLong(this.windowHandle, GWL_STYLE, new IntPtr(dwStyle &
                    ~WS_CAPTION & ~WS_THICKFRAME));
                    SetWindowPos(this.windowHandle, IntPtr.Zero, 0, 0, Convert.ToInt32(Math.Floor(this.ActualWidth)), Convert.ToInt32(Math.Floor(this.ActualHeight)), SWP_FRAMECHANGED);
                    SetParent(this.windowHandle, panel.Handle);
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                }
            }
        }

        public Window_Host_Helper()
        {
            this.Content = host;
            this.panel.CreateControl();
            host.Child = this.panel;
        }

        protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
        }
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeBounds)
        {
            base.ArrangeOverride(arrangeBounds);
            SetWindowPos(this.windowHandle, IntPtr.Zero, 0, 0, Convert.ToInt32(Math.Floor(arrangeBounds.Width)), Convert.ToInt32(Math.Floor(arrangeBounds.Height)), 0);
            return arrangeBounds;
        }
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            this.Content = this.host;
            
        }

        
    }


Usage:
C#
Process p = new Process();
           p.StartInfo = new ProcessStartInfo("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe");
           host.Process = p;




Best Regards
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900