Windows Forms Control.CreateControl() equivalent in WPF - c#

normally I use UserControls from WinForms and can then have them created with the CreateControl() method and displayed in another application. But now I wanted to try this with WPF. But there I can't find a way to create the UserControl and then get the corresponding handle.
I tried it with
HwndSource hwndSource = PresentationSource.FromVisual(Control) as HwndSource;
if (hwndSource != null)
{
handle = hwndSource.Handle;
}
but i got no handle.
Then there is this possibility, which theoretically works, but only for Windows
IntPtr handle = new WindowInteropHelper(myWindow).EnsureHandle();
Normal Usage with WinForms Controls on a Sample:
MyCoolControl control = new MyCoolControl();
control.CreateControl();
SendHandleToSomethingElse(control.Handle.ToInt32());
and now i need this for WPF :D

Unlike a Windows Forms control, a WPF control doesn't have its own HWND handle.
In WPF only the top-level window has a handle and the controls are rendered as content inside this HWND.
Please refer to the docs for more information about how WPF uses HWNDs.

Related

How to show a WinForms Window in a WPF Grid or Viewbox

In xml file:
<Viewbox name="viewbox1"></Viewbox>
In C# code-behind:
windowformClass window1 = new windowformClass(); // this is the WinForms object, not WPF
viewbox1.Child = window1;
When I assign window1 to viewbox1 it shows an error that cannot convert window form to System.Window.ElementUI
What's the right way to do that?
You should use WPF's interop host control for WinForms called WindowsFormsHost.
The code below was extracted and adapted from MSDN
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create your control.
windowformClass window1 = new windowformClass();
// Assign the control as the host control's child.
host.Child = window1;
// Set the interop host control as the ViewBox child.
viewbox1.Child = host;
A user reported a problem with hosting the WindowsFormHost inside a ViewBox. You should check that out in case you have problems with that too.
As you are (apparently) a newcomer, I'd like to give you a little tip:
There are plenty of resources on this subject on both MSDN and SO that could be found through a simple search on your favourite search engine.
Next time, try to find a solution before posting a question, as to keep only the best answers on the topic and save the moderators the work of closing questions as duplicates.
For more on that, check the help from SO

Enumerate all controls inside window

I'm trying to enumerate all controls inside a window using user32.dll, but I don't know exactly what I need to do.
I'm using EnumWindows and EnumChildWindows, but it doesn't retrieve all controls inside a window.
I want to get the text of some labels in a Delphi application.
I tried using Spy++, but it doesn't list too.
IList<IntPtr> childern = new List<IntPtr>();
WNDENUMPROC enumChildProc = delegate(IntPtr hwnd, IntPtr param)
{
childern.Add(hwnd);
return true;
};
EnumChildWindows(Hwnd, enumChildProc, IntPtr.Zero);
return childern;
which Hwnd is the parent window handle
Delphi labels are non-windowed. That means that you cannot ever hope to obtain window handles for them since they are not windows.
If the Delphi VCL properly supported automation then you'd be able to use UI Automation to inspect the text of these controls. But the VCL only supports automation by dint of the underlying Win32 controls doing so. Since labels aren't windowed controls, again you are out of luck.
The only thing that can realistically hope to read this text is Delphi code inside the process. It does not sound as though that will be viable for you.

How to get Main Window in UserControl

I have got some generic User Control under the Canvas of the WPF window.
How do I can get that window?
I used this approach
void UserControlMethod()
{
WindowInteropHelper WindowInteropHelper = new WindowInteropHelper((Window)((Grid)(((Canvas)this.Parent).Parent)).Parent);
}
Is this correct?
Thank you!
You should use: Window.GetWindow().
The WindowInteropHelper is for interacting with native windows code or API's so that's definitely not what you want.

WPF modal window as tool window in WinForms disappears

I was wondering if anyone can help me with this rather baffling situation. I have a WPF form myForm which I am displaying modally in a WinForm application (without using an ElementHost). Everything works fine if I use the default WindowStyle and it is shown in the taskbar. However now I don't want the form to show in the taskbar or contain the minimize button, therefore I have done the following:
MyForm myForm = new MyForm();
myForm.ShowInTaskbar = false;
myForm.WindowStyle = System.Windows.WindowStyle.ToolWindow;
myForm.WindowStartupLocation =System.Windows.WindowStartupLocation.CenterOwner;
myForm.ShowDialog();
Now, the wpf form displays as expected modally and without the minimize button. If I now select the "parent" winform application in the taskbar, the wpf form disappears and there doesn't seem to be any way of returning to it! I have read this which is similar but not the same (pure WPF application), so I can understand why the main app does not appear in the ALT+TAB menu, but can anyone tell me how I can return to the wpf form?
Thanks in advance.
The use of WindowsInteropHelper allows you to wrap a hosted WPF-form using a Winforms-form and set its parent as a Winforms control, which prevents the WPF form from disappearing (as also pointed out by dowhilefor and Hans Passant)
E.g.:
// Use the interop helper for the specified wpf window hosted by the win32 owner
WindowInteropHelper wih = new WindowInteropHelper(wpfForm);
wih.Owner = this.someWin32FormsControl.Handle;
This is not related to Winforms or WPF, this is the way Windows functions. The only solution that I think you have is to wire up your modality; that is intercept the Activated event on your Winforms form and if your WPF tool window is visible, bring that window forward and give it focus.

How to call native code which draw in WPF Image from C# code?

I have a native library (no sources available) wrapped into C# code.
The following C# declaration exists:
[DllImport(DRIVER_DLL_NAME,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "RenderBitmap")]
private static extern int RenderBitmap(int hWnd);
I need to call this function from my WPF C# project.
I have a working sample code for Windows forms:
System.Windows.Forms.PictureBox DisplayWindow;
...
RenderBitmap(DisplayWindow.Handle.ToInt32());
And I have not found how to do so with WPF System.Windows.Controls.Image instead of System.Windows.Forms.PictureBox - there is no Handle property or something similar.
Moreover I found in "WPF and Win32 Interoperation" the following statement:
"When you create a WPF Window, WPF creates a top-level HWND, and uses an HwndSource to put the Window and its WPF content inside the HWND. The rest of your WPF content in the application shares that singular HWND.".
It seems that HWND handle does not exist at all for Image.
How to call native code which draw in WPF Image from C# code?
WPF Controls does not have handles like in WinForms. Only main window handle is accessible:
For instance, in main window class (or use Application.Current.MainWindow):
var handle = (new WindowInteropHelper(this)).Handle;
var hwnd = HwndSource.FromHwnd(handle);
So looks like you've to consider another approach instead of native calls.
BTW, why you need such a low level drawing functionality? I just can assume that you simply want to reuse already implemented one for WinForms. Perhaps you can achieve the same using built in WPF features.

Categories