Is it possible to host WPF Core (.NET 5.0) content in an MFC application?

Stacy O’Malley 1 Reputation point
2022-01-14T19:41:53.253+00:00

Is it possible to host WPF Core (.NET 5.0) content in an MFC application? I see tutorials on how to host WPF content in Win32 (such as https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-wpf-content-in-win32?view=netframeworkdesktop-4.8) but everything I find is related to the .NET Framework and/or WinForms. I have tried and failed on my own to do this, but I am looking for confirmation that it is not possible, as opposed to me having made mistakes in my attempts.

If I have made mistakes or am being too limited in my thinking, does anyone have any advice to accomplish the task of hosting WPF Core (.NET 5.0) content in an MFC application? Can I host a .NET 5.0 user control in some kind .NET Framework wrapper in some way and then display that in MFC?

Any input is appreciated.

Developer technologies Windows Presentation Foundation
Developer technologies .NET .NET Runtime
Developer technologies C++
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-01-14T20:40:02.5+00:00

    I use ElementHost to embed WPF controls in C++/Win 32 or MFC

    Test with a rotated WPF Button =>

    Set :
    Common Language Runtime Support (/clr)
    Debug Information Format : C7 compatible (/Z7)
    Enable C++ Exceptions : No
    Basic Runtime Checks : Default

    In pch.h :

    #using <PresentationFramework.dll>  
    #using <PresentationCore.dll>  
    #using <WindowsBase.dll>  
      
    #using <System.Windows.Forms.dll>  
    #using <WindowsFormsIntegration.dll>  
    #using <System.dll>  
      
    #using <System.Drawing.dll>  
    

    Test code for the rotated Button :

    	System::Windows::Controls::Canvas^ canvas = gcnew System::Windows::Controls::Canvas();  
    	System::Windows::Controls::Button^ btn1 = gcnew System::Windows::Controls::Button();  
    	btn1->Content = "Test Button";  
    	btn1->SetValue(System::Windows::Controls::Canvas::LeftProperty, 50.0);  
    	btn1->Width = 200;  
    	btn1->Height = 40;  
    	canvas->Children->Add(btn1);  
      
    	System::Windows::Media::RotateTransform^ rotateTransform1 = gcnew System::Windows::Media::RotateTransform(45);  
    	btn1->RenderTransform = rotateTransform1;  
      
    	System::Windows::Forms::Integration::ElementHost^ eh = gcnew System::Windows::Forms::Integration::ElementHost();  
    	eh->Child = canvas;  
    	eh->Location = System::Drawing::Point(100, 100);  
    	eh->Size = System::Drawing::Size(300, 300);  
    	eh->BackgroundImage = gcnew System::Drawing::Bitmap("e:\\blue-floral-background-flower-vector-3286564.jpg");  
      
    	HWND hWndWrapper = (HWND)eh->Handle.ToPointer();  
    	::SetParent(hWndWrapper, m_hWnd);  
    

    165208-mfc-wpf.jpg

    0 comments No comments

  2. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2022-01-17T06:37:59.807+00:00

    Hi, @Stacy O’Malley

    You can also refer to the thread: How do I host WPF content in MFC Applications

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.