c# 网站嵌套在winform里面 那么如何通过网站怎么关闭winform程序?

我有个网站是嵌套在winform里面的,我想利用里面的网站退出winform程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WebBrowserTest
{
    // This first namespace is required for the ComVisible attribute used on the ScriptManager class.
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    // This is your form.
    public partial class Form1 : Form
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.WebBrowser1 = new System.Windows.Forms.WebBrowser();
            this.SuspendLayout();
            // 
            // WebBrowser1
            // 
            this.WebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.WebBrowser1.Location = new System.Drawing.Point(0, 0);
            this.WebBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.WebBrowser1.Name = "WebBrowser1";
            this.WebBrowser1.Size = new System.Drawing.Size(544, 300);
            this.WebBrowser1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(544, 300);
            this.Controls.Add(this.WebBrowser1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.WebBrowser WebBrowser1;

        // This nested class must be ComVisible for the JavaScript to be able to call it.
        [ComVisible(true)]
        public class ScriptManager
        {
            // Variable to store the form of type Form1.
            private Form1 mForm;

            // Constructor.
            public ScriptManager(Form1 form)
            {
                // Save the form so it can be referenced later.
                mForm = form;
            }

            // This method can be called from JavaScript.
            public void MethodToCallFromScript()
            {
                // Call a method on the form.
                mForm.DoSomething();
            }

            // This method can also be called from JavaScript.
            public void AnotherMethod(string message)
            {
                MessageBox.Show(message);
            }
        }

        // This method will be called by the other method (MethodToCallFromScript) that gets called by JavaScript.
        public void DoSomething()
        {
            // Indicate success.
            MessageBox.Show("I will close!");
            this.Close();
        }

        // Constructor.
        public Form1()
        {
            // Boilerplate code.
            InitializeComponent();

            // Set the WebBrowser to use an instance of the ScriptManager to handle method calls to C#.
            WebBrowser1.ObjectForScripting = new ScriptManager(this);

            // Create the webpage.
            WebBrowser1.DocumentText = @"<html>
                <head>
                <title>Test</title>
                </head>
                <body>
            <input type=""button"" value=""Go!"" onclick=""window.external.MethodToCallFromScript();"" />
                    <br />
                    <input type=""button"" value=""Go Again!"" onclick=""window.external.AnotherMethod('Hello');"" />
                </body>
                </html>";
        }
    }
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2014-04-10
有一套方案。。。你可以试试socket。。winform作为服务端,网页发送socket消息。。。收到消息后关闭自己。。追问

。。。这个理解起来比较困难初学者,有一个案例没有呢?

追答

socket就是通信的意思,就好比QQ聊天,你告诉对方干嘛,对方按照约定好的命令,实现相应的效果,比如网页发送1,winform接收到1之后就认为是要关闭了,就关闭了

第2个回答  2014-04-10
你是不是通过winform的brower默认打开了网站,实现了“”网站嵌套在winform中“的效果?追问

恩恩是的,那我想实现的功能怎么实现呢?

追答

稍等啊。我就是确认一下,待我去模拟一下试试...

楼主想用那种方式推出呢?

随意?

功能已经用一种和简单的方式实现了,但是就等你想用的那种方式是那种了,我在具体实现。

追问

方式无所谓,只要能实现就行了,你加我我们详聊吧:七八七三四八零八六

本回答被提问者采纳
第3个回答  2014-04-10
== 一个bs 一个cs 怎么可能追问

应该可以吧。。

相似回答