<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>17 of 26 &#187; C++</title>
	<atom:link href="http://17of26.com/category/programming/cplusplus/feed/" rel="self" type="application/rss+xml" />
	<link>http://17of26.com</link>
	<description>the world of programming and technology</description>
	<lastBuildDate>Fri, 11 Dec 2009 01:29:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Host WPF Content in MFC Applications</title>
		<link>http://17of26.com/2009/07/how-to-host-wpf-content-in-mfc-applications/</link>
		<comments>http://17of26.com/2009/07/how-to-host-wpf-content-in-mfc-applications/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 03:12:53 +0000</pubDate>
		<dc:creator>Q</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://17of26.com/?p=300</guid>
		<description><![CDATA[This is something that I figured out a while back but wanted write about it here since I spent a few hours piecing together the information.
There is an MSDN Walkthrough that gets you most of the way there, but there are a couple of key pieces that I found elsewhere. For example, the walkthrough tells [...]]]></description>
			<content:encoded><![CDATA[<p>This is something that I figured out a while back but wanted write about it here since I spent a few hours piecing together the information.</p>
<p>There is an <a rel="nofollow" href="http://msdn.microsoft.com/en-us/library/ms744829.aspx">MSDN Walkthrough</a> that gets you most of the way there, but there are a couple of key pieces that I found elsewhere. For example, the walkthrough tells you to place the line [System::STAThreadAttribute] before the _tWinMain() definition but if you&#8217;re implementing a standard MFC application then you don&#8217;t have _tWinMain() in your source code.</p>
<h4><strong>Step 1: Configure the MFC application to compile with CLR support</strong></h4>
<p>The best way to achieve interoperability between native C++ and managed .NET code is to compile the application as managed C++ rather than native C++. This is done by going to the Configuration Properties of the project. Under General there is an option &#8220;Common Language Runtime support&#8221;. Set this to &#8220;Common Language Runtime Support /clr&#8221;.</p>
<h4><strong>Step 2: Add the WPF assemblies to the project</strong></h4>
<p>Right-click on the project in the Solution Explorer and choose &#8220;References&#8221;. Click &#8220;Add New Reference&#8221;. Under the .NET tab, add WindowsBase, PresentationCore, PresentationFramework, and System. Make sure you Rebuild All after adding any references in order for them to get picked up.</p>
<h4>Step 3: Set STAThreadAttribute on the MFC application</h4>
<p>WPF requires that STAThreadAttribute be set on the main UI thread. Set this by going to Configuration Properties of the project. Under Linker-&gt;Advanced there is an option called &#8220;CLR Thread Attribute&#8221;. Set this to &#8220;STA threading attribute&#8221;.</p>
<h4><strong>Step 4: Create an instance of HwndSource to wrap the WPF component</strong></h4>
<p>System::Windows::Interop::HwndSource is a .NET class that handles the interaction between MFC and .NET components. Create one using the following syntax:</p>
<pre class="syntax-highlight:c++">System::Windows::Interop::HwndSourceParameters^ sourceParams = gcnew     System::Windows::Interop::HwndSourceParameters(&quot;MyWindowName&quot;);
sourceParams-&gt;PositionX = x;
sourceParams-&gt;PositionY = y;
sourceParams-&gt;ParentWindow = System::IntPtr(hWndParent);
sourceParams-&gt;WindowStyle = WS_VISIBLE | WS_CHILD;

System::Windows::Interop::HwndSource^ source = gcnew System::Windows::Interop::HwndSource(*sourceParams);
source-&gt;SizeToContent = System::Windows::SizeToContent::WidthAndHeight;</pre>
<p>Add an HWND member variable to the dialog class and then assign it like this: m_hWnd = (HWND) source-&gt;Handle.ToPointer();  The source object and the associated WPF content will remain in existence until you call ::DestroyWindow(m_hWnd).</p>
<h4><strong>Step 5: Add the WPF control to the HwndSource wrapper</strong></h4>
<pre><strong>
<pre class="syntax-highlight:c++">&lt;/strong&gt;System::Windows::Controls::WebBrowser^ browser = gcnew System::Windows::Controls::WebBrowser();

browser-&gt;Height = height;
browser-&gt;Width = width;
source-&gt;RootVisual = browser;&lt;strong&gt;</pre>
<p></strong></pre>
<h4>Step 6: Keep a reference to the WPF object</h4>
<p>Since the browser variable will go out of scope after we exit the function doing the creation, we need to somehow hold a reference to it. Managed objects cannot be members of unmanaged objects but you can use a wrapper template called gcroot to get the job done.</p>
<p>Add a member variable to the dialog class:</p>
<pre></pre>
<pre class="syntax-highlight:c++">&lt;span&gt;#include &lt;vcclr.h&gt;&lt;/span&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;gcroot&lt;/span&gt;&lt;span&gt;&lt;&lt;/span&gt;&lt;span&gt;System&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;Windows&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;Controls&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span&gt;WebBrowser&lt;/span&gt;&lt;span&gt;^&gt;&lt;/span&gt;&lt;span&gt; m_webBrowser&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/code&gt;</pre>
</pre>
<p>Then add the following line to the code in Step 5:</p>
<pre class="syntax-highlight:c++">&lt;code&gt;&lt;span&gt;m_webBrowser &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; browser&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/code&gt;</pre>
<p>Now you can access properties and methods on the WPF component through m_webBrowser.</p>
]]></content:encoded>
			<wfw:commentRss>http://17of26.com/2009/07/how-to-host-wpf-content-in-mfc-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
