简体   繁体   English

如何在网页中打开Windows窗体控件?

[英]how to open windows form control in a web page?

I'm developing a web site as well as windows application and i'm going to use same web page for windows form application as well as for web site. 我正在开发一个网站以及Windows应用程序,并且我将对Windows窗体应用程序和网站使用同一网页。

so i want to call the windows form control after clicking on the link which is placed on the web page. 因此,我想在单击放在网页上的链接后调用Windows窗体控件。 And i want to show the windows form control as a pop up form. 我想将Windows窗体控件显示为弹出窗体。

How to do this? 这个怎么做? Or provide some document related to this issue. 或提供一些与此问题有关的文件。

Assuming you mean that within your WinForms application you're hosting an instance of the WebBrowser class, you can provide an object to its ObjectForScripting property. 假设您的意思是在WinForms应用程序中托管WebBrowser类的实例,则可以为其ObjectForScripting属性提供一个对象。 that provides a method to call to trigger your WinForms code. 提供了一种方法来触发WinForms代码。 For example: 例如:

public partial class MyWindowsFormsForm()
{
    public MyWindowsFormsForm()
    {
        this.WebBrowserControl.ObjectForScripting = this;
    }

    public void DoSomething()
    {
        MyOtherForm f = new MyOtherForm();
        f.Show();
    }
}

Then, in your page: 然后,在您的页面中:

<script language="javascript" type="text/javascript">
function loadOtherForm()
{
    if (RunningInWinFormsApplication())
    {
        window.external.DoSomething();
    }
    else
    {
        // Code to do something when NOT running inside the WinForms app could go here
    }
}

function RunningInWinFormsApplication()
{
   return (window.external.DoSomething != undefined);
}
</script>

<button onclick="loadOtherForm();">Call into WinForms app</button>

There's the obvious caveat that you'll need to have code in your web page that checks to ensure that window.external.DoSomething is actually there, so likely your onclick (in this example) would call a helper method that either calls into WinForms, or does whatever needs doing in the event that the page isn't being hosted inside your application. 有一个明显的警告,您需要在网页中包含代码以检查是否确实存在window.external.DoSomething ,因此您的onclick (在此示例中)可能会调用一个辅助方法,该方法要么调用WinForms,或在页面未托管在您的应用程序内部的情况下需要做的事情。

You can display Windows Form in Web Browser (Internet Explorer Preferred) by using WPF Browser Application which have a facility to embed Windows Forms (or similar like .NET Components). 您可以使用WPF浏览器应用程序在Web浏览器(首选Internet Explorer)中显示Windows窗体,应用程序具有嵌入Windows窗体(或类似.NET组件的功能)的功能。

This is a valid Question because there are lots of Legacy .NET Applications which company don't have resources to be developed for Web HTML 5 format (using AJAX etc). 这是一个有效的问题,因为有许多旧版.NET应用程序,该公司没有资源来开发Web HTML 5格式 (使用AJAX等)。 Such legacy applications can be easily displayed in a web browser with a minor initial setup required. 这样的遗留应用程序可以轻松地在Web浏览器中显示,而只需进行少量的初始设置即可。

  1. Open your Windows Form Application in your existing Visual Studio IDE 在现有的Visual Studio IDE中打开Windows窗体应用程序
  2. Go to Project Properties and in Application Settings Set output Type to 'Class Library' 转到项目属性,然后在“应用程序设置”中将输出类型设置为“类库” 在此处输入图片说明
  3. Add a new project of Type 'WPF Browser Application' as shown below 如下所示添加“ WPF浏览器应用程序”类型的新项目 在此处输入图片说明
  4. Reference your Windows Form (Now as DLL) along with two important assemblies of WindowsFormIntegration and System.Windows.Forms 引用Windows窗体(现在为DLL)以及WindowsFormIntegrationSystem.Windows.Forms的两个重要程序集
  5. Write the code to call Windows Form in XAML and XAML.CS page as shown in this tutorial link . 本教程链接所示编写代码以在XAML和XAML.CS页面中调用Windows窗体。
  6. Rebuild the Solution and Click on Publish and Save select File System as shown below. 重建解决方案,然后单击“发布并保存”,选择文件系统,如下所示。 在此处输入图片说明
  7. Go to Publish Folder and double click file with extension '.xbap. 转到“发布文件夹”,然后双击扩展名为“ .xbap”的文件。 It should get automatically opened in Internet Explorer with permissions asking for the first time. 它应该在Internet Explorer中自动打开,并具有首次询问权限。
  8. You should be able to display the output in Web Browser. 您应该能够在Web浏览器中显示输出。 在此处输入图片说明

Let me know if you want me to create a video tutorial to achieve the same. 让我知道是否要创建一个视频教程来实现相同的目的。

I think your getting confused - or we are. 我认为您感到困惑-还是我们。

"I have web page which i designed for web application and i'm going to open the same web page in the web browser control which will be placed on the one of form of windows form application. And if i open the form which is having my web site page and i clicked on the link from the web page then that time it should open the another form from the windows application" “我有一个专为Web应用程序设计的网页,我将在Web浏览器控件中打开相同的网页,该网页将放置在Windows窗体应用程序的窗体之一上。如果我打开正在使用的窗体我的网站页面,然后我单击了该页面上的链接,然后应该从Windows应用程序中打开另一个表单。”

So you know the link that will be clicked? 这样您知道将要单击的链接吗? Then in the WebBrowser Navigating Method check if its the link you were expecting and open the WinForm, eg: 然后在“ WebBrowser导航方法”中检查其链接是否与您期望的一样,然后打开WinForm,例如:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.Tostring() == "http://localhost/LinkThatOpensFormTwo.htm")
{
Form2 f = new Form2();
f.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM