Native Win32 window in WPF Control - c#

I am developing an application that needs to a host native Win32 window and somehow i have no clues how to do that.
I need to create a WPF dialog window that could display native Win32 control on it. This dialog window will have WPF controls on it as well, so i am looking for some sort of Grid that i could take HWND of and send it to the unmanaged C++ control, so it could draw on it.
Is that possible ?
I don't need to know what happens within that surface, just need to let C++ dll to draw on it and all i need to do is to pass HWND that has proper size (which i know).
I am kinda new to WPF (used to do win32 programming) and quite lost (but i now how to interface it to C# .NET etc)
Would be great if you could send me any hints :)

you can start by following the instructions/steps here: Hosting Win32 Content in WPF
from the article introduction:
A Walkthrough of Win32 Inside Windows Presentation Framework (HwndHost)
To reuse Win32 content inside WPF applications, use HwndHost, which is
a control that makes HWNDs look like WPF content. Like HwndSource,
HwndHost is straightforward to use: derive from HwndHost and implement
BuildWindowCore and DestroyWindowCore methods, then instantiate your
HwndHost derived class and place it inside your WPF application.
If your Win32 logic is already packaged as a control, then your
BuildWindowCore implementation is little more than a call to
CreateWindow.
then if you have a specific issue ask here in SO and people will help you on specific points.

Related

How do I embed a GLFW3 window into a WPF form?

I have a GLFW3 OpenGL project written in C++. I was looking to create a modern GUI to go with it. I have limited experience with XAML and windows WPF but it is intuitive to work with so I really want this to work.
I found 'WpfUnmanagedOpenGL' template from Github https://github.com/kopaka1822/WpfUnmanagedOpenGL and it runs great with a C++ DLL communicating with the Csharp OpenGL host window that is already working within the form using GLAD, not GLFW.
I managed to create a GLFW window with my existing code and it displays as a second popup window when the WPF form is compiled and run. See:
How can I embed this correctly?
I found this question on StackOverflow: embed window(glfwCreateWindow) as child to C++ MFC parent form .
Essentially it says I can set the GLFW window to be a child window of the WPF form. However I cannot get the WPF parent window handle to set it all up.
PLEASE help.
Thanks,
You may want to try to do the opposite. Rather than embedding your Win32 app inside your WPF form, embed your WPF form within your Win32 app instead.
Official docs:
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-wpf-content-in-win32?view=netframeworkdesktop-4.8
A really neat tutorial:
https://www.codeproject.com/Articles/5253279/Create-An-Awesome-WPF-UI-for-Your-Cplusplus-QT-App
This is for the legacy .NET Framework though. I have no clue if the same technique can be applied in the new .NET Core framework though.

Hosting Win32, WinForms or WPF Window by handle?

I'm investigating if it's possible to host any type of window in WPF by handle. I've been exploring the HwndHost class, although all the examples I've come across is using a Win32 handle. Is it possible to host any window type by handle?
Yes, it is possible. Here is a quick link from MSFT on hosting Win32 controls:
https://msdn.microsoft.com/en-us/library/aa970061.aspx
I've actually worked on a project where we hosted a Java client application (don't ask why) inside a WPF control where we grabbed the Win32 handle from the Java application. Eventing can be tricky, but it can be done.

Examples on using a HWND from C# UserControl in via DirectShow in C++

Does anybody have any simple examples on how to provide C++ code a window that can be used for DirectShow rendering from a C# UserControl?
I am currently able to create a separate and render to a window via a WS_POPUP window and match its size/position to the existing control window I have reserved for the video pane on the WPF control.
As soon as I try to make the window a child of the WPF control or set WS_CHILD on a new or existing window everything stops rendering and I receive no error explaining what is going on.
Ideally I would be able to pass existing HWND from C# through to the C++/DirectShow class and use it there as-is.
P.S.
Before you ask "Why don't you just... do something different", I need to control DirectShow from C++ and have to display the results via a C# (WPF) control. I have no control over these conditions.
C#/WPF (control reserved) <---> C++/CLI <---> C++/HWND/DirectShow
The best way of achieving this would be to use VMR in the windowless mode. The following snippet of code illustrates this(This is for working code. I have removed error checks etc but should give you an idea):
pVMRConfig->SetRenderingMode(VMRMode_Windowless);
m_VMR->QueryInterface(IID_IVMRWindowlessControl9, (void**)&pVMRWindowlessControl);
//displayWnd is what you pass in from your C# app
pVMRWindowlessControl->SetVideoClippingWindow(displayWnd);
RECT rcDest;
GetClientRect(displayWnd, &rcDest);
hr = pVMRWindowlessControl->SetVideoPosition(NULL, &rcDest);

Given a WPF Image control, is it possible to determine its HWND?

I am integrating a webcam in a WPF application. I can see the camera feed in the main window, as I pass its HANDLE on to the DirectShow functions. But this is not what I want.
The main form has a Image control, where I'd like to see the output. However, in order to do this, I need the control's Handle.
Any hint on how to do this?
Thanks in advance,
Gianluca.
An Image Control in WPF, unlike Windows Forms, doesn't actually have an HWND.
WPF works differently than Windows Forms - each control is not a wrapper around a native "window" with a handle, but rather composed together using Direct3D at runtime by the layout system.
If you need to actually host output from a Webcam inside of a WPF window, you should look at using HwndHost (or a subclass). The simplest way is often to just host a Windows Forms control inside of a WindowsFormsHost, though managing an HWND yourself via HwndHost is more efficient.
Using an WindowsFormsHost and a picture box inside is a good solution that I used for a similar problem where I needed a handle to display a video stream. But be careful, in order to work, the Window that is hosting the WindowsFormsHost control must have AllowsTransparency="false"!

integration of wpf into window application

I am new in wpf application..
I am working on window application.it has many module.its one of module is in wpf which is seperate from project.so I want to integrate wpf application into window application project.
Your question is unclear. However, if you are trying to host a WPF object inside of a WinForms application, then yes, that is possible.
A good tutorial can be found here.
Assuming you mean MDI Winform for "windows Application". Here are the steps:
Create a new child Winform.
Add "WPF Interoperability" Element
Add your WPF pages to the WPF interop element
These steps only work in .NET Framework 3.0 or above, where WPF is supported.
You need to add an interop layer between the MDI application and the WPF component that you want to host. This is non-trivial.
1) Determine where the border between your managed and native code will lie. Ensure that you clearly define, delineate, and respect this border, or you will weep tears of pain. This will require use of C++/CLI in any real-world scenario.
I suggest creating a C++/CLI ref class called something like "Launcher" to act as a springboard. It exposes a native API that your native application can consume. The native application provides a pointer to your MDI window and any other req'd information.
2) Use the MDI child pointer and an HwndSource on the WPF component to drop your WPF into the native window.
3) Supply appropriate manual forwarding of window messages from MDI-land to WPF-land via a MessageHook in the HwndSource. Note that you'll manually be handling everything from WM_WINDOWPOSCHANGED to ID_HELP.
Good luck!
You can use ElementHost to add your wpf controls to windows forms. Add ElementHost control to windows form and set your wpf control as ElementHost Child property.
You can find more details here
MSDN
ElementHost

Categories

Resources