equivalent WindowsFormsHost in C# winform - c#

what is equivalent WindowsFormsHost in C# winform?
i wrote program in C# winform. in my user control(instead of form), i put reporting. when user clicked btnReporting, rptfile is shown.
this is my code in wpf. i don't equivalent WindowsFormsHost in C# winform?
CrystalReportViewer rptViewer = new CrystalReportViewer();
rptViewer.DisplayStatusBar = false;
WindowsFormsHost host = new WindowsFormsHost();
rptViewer.ReportSource = rep;
host.Child = rptViewer;
ReportRpt PageRpt = new ReportRpt();
PageRpt.GrdReport.Children.Add(host);
NavigationService navService = NavigationService.GetNavigationService(this);
navService.Navigate(PageRpt);

For showing crystal report on Winform, you may add the Crystal Viewer from the toolbox on the form and set the crystal report against it.
Please see: Displaying Crystal Reports using WinForms and C#
but if you want to host a WPF control in Winform then you can use ElementHost
A Windows Forms control that can be used to host a Windows
Presentation Foundation (WPF) element.

you dont need a windows forms host in winforms - thats just for backwards compatibility in WPF
just add the reference to crytal reports and place the control on your form

Related

WPF window inside a WinformHost Panel

I am showing a WPF exe window inside another WPF application using winform host.
I have created a panel in main application and set it as child of winformhost.
mHostingPanel = new System.Windows.Forms.Panel()
{
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
};
mWinformHost = new WindowsFormsHost();
mWinformHost.Child = mHostingPanel;
and then I start the other window process and set hosting panel as parent.
WindowsAPI.SetParent(mProcess.MainWindowHandle, mHostingPanel.Handle);
My question is if I launch the application,Who will be rendering my Child WPF window whose parent is a winform panel.Will it be Direct-X or GDI context of Panel?
Also if I set Allowtransparency=True on child WPF application,The UI doesnt show up in hosting panel.
Found the reason.Its called airspace issue (when win32 and WPF trying to share pixels) and I dont think it can be solved by any framework API as microsoft denied it.
https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2644120-bring-back-the-hwndhost-isredirected-and-compositi
Mitigating AirSpace issues

Is it possible to show xaml view inside windows form

I have a windows form which calls other windows form that work within my application. What I want to accomplish is to get away from this whole "windows form" thing and use WPF View (usercontrol) instead. Is there a way I can call a view to show it from my form?
ElementHost host = new ElementHost();
Cars.WPF.Views.DescriptionView descView = new Cars.WPF.Views.DescriptionView();
host.Controls.Add(descView);
host.Dock = DockStyle.Fill;
I get error: --> Argument 1: cannot convert from 'Car.WPF.Views.DescriptionView' to 'System.Windows.Forms.Control'
Add a panel in your winform (lets say panel1)
Define ElementHost at class level, Also define WPF Control at class level
ElementHost host;
Cars.WPF.Views.DescriptionView descView;
In form load event do:
host= new ElementHost();
panel1.Controls.Add(ctrlHost); //Add Element host to panel1
descView = new Cars.WPF.Views.DescriptionView();
descView.InitializeComponent();
host.Child = descView; //Instead of adding WPF control to Winform do this
Also in your project references add:
PresentationCore
PresentationFramework
WindowsBase
Yes, use Element Host.
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx

Classic forms control cannot resize in WPF environment

I've added a regular forms control but I cannot resize it. Instead I have to resize host.
MSDN sample: Hosting a Windows Forms Control in WPF
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
host.Child = mtbDate;
this.grid1.Children.Add(host);
mtbDate.Width = 200; //Not work!
host.Width = 200; //Workaraound...
How can I resize the control, not the host?
This page gives a lot of information about hosting WinForms controls in WPF: http://msdn.microsoft.com/en-us/library/ms744952.aspx
The short story is that you are not supposed to resize the textbox- that will be ignored/overridden. Instead, resize the WindowsFormsHost (either in WPF or via WPF dynamic layout)

Casting Windows.Controls.UserControl to Windows.Forms.Control

Can we cast a WPF User Control to a form control??
I'm sorry you can't. WPF works very differently internally from Winforms: Winforms uses the controls provided by the Windows OS (where each control has a window handle), where WPF uses DirectX to do the painting.
You can host WPF controls inside winforms applications (EDIT)and vice versa (with limitations) but that is perhaps not what you're after.
I tried this out:
TouchScreenWPF touchUI = new TouchScreenWPF();
ElementHost elementHost = new ElementHost();
elementHost.Child = touchUI;
Control userControl = new Control();
userControl.Controls.Add(elementHost);
The form contains the usercontrol, but does not display anything when I include a WPF User control. It works with a single button though... Am I missing something there?

how to launch a default browser instance within a program window?

I am creating console app, involving lots of services. I want to display a webpage within my program window. I know how to launch a new window ( http://dotnetpulse.blogspot.com/2006/04/opening-url-from-within-c-program.html )but I was wondering can I ask a browser to render a webpage withing my program window (in C#) ?
The best solution would likely be to use the WebBrowser control.
This can be placed on a form and allows the web page to appear inside your application.
Here is a nice example of how to go about implementing it http://ryanfarley.com/blog/archive/2004/12/23/1330.aspx
Hope this helps
Use the WebBrowser control and pass it the URL of the web page to render.
In your Windows form, something like (off the top of my head so may need tweaking to get it to compile):
WebBrowser browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
this.Add(browser);
browser.Navigate("http://www.myurl.com");
In a Form you can use WebBrowser Control... in a Console Application there is no way wihout opening a new form.... but you could:
design a form with a Webbrowser Control
hide its border and showintask = false
Open it at a position in your Console-Window

Categories

Resources