如何在默认浏览器以外的c#Windows窗体应用程序中设计新的Web浏览器 [英] How to design a new Web Browser in c# Windows Forms Application other than default Browser

查看:74
本文介绍了如何在默认浏览器以外的c#Windows窗体应用程序中设计新的Web浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我开发了一个c sharp应用程序,它将执行一个sql Query并获得一个位置的相应坐标(纬度,经度)。一旦程序执行sql查询,它将获得位置坐标。



现在从第一个表格获取坐标后,它将转到第二个表格,并以那个形式嵌入了webbrowser控件。因此它应该打开一个谷歌地图与坐标的位置。



但不幸的是,当我将值从第一种形式传递到第二种形式时。这些值已成功传递到第二个表单。



但网页根本没有显示。只有白屏即将到来。



但是当我直接从主程序调用第二个表单时,它会尝试打开网页并返回Google地图不存在的错误在浏览器中支持。所以我需要在表单中嵌入一个新的浏览器,如chrome或firefox来实现。



主程序: -

Hi,

I have developed a c sharp application which will execute an sql Query and get corresponding Coordinates(latitude, longitude) of a location. Once the program executes the sql query it will get the Location Coordinates.

Now after getting the coordinates from 1st form, It will go to second form and there in that form a webbrowser control is embedded. So it should open a google Map with the location from coordinates.

But unfortunately, when i pass the values from first form to 2nd form. The values are passing to 2nd form successfully.

But the webpage is not displaying at all. Only white screen is coming.

But when i call the 2nd form directly from main Program then it is trying to open the Webpage and returning the error that Google Maps is not supported in the browser. So i need a new browser like chrome or firefox embedded in the form to implement.

Main Program:-

namespace LocationFinder
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new MainForm());  //When this main form is commented out , its executing 2nd form and displaying an error.
            Application.Run(new LocationMap()); //When this 2nd form is commented out , its executing 1st form, then calling 2nd one from 1st and displaying a blank White Page.
        }
    }
}





Form1: -





Form1:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

namespace LocationFinder
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        public void MainForm_Load(object sender, EventArgs e)
        {   
            string old_value = "";

                while (true)
                {
                    String connetionString = null;
                    SqlConnection conn;
                    SqlCommand cmd;
                    String sql = null;
                    SqlDataReader reader;
                    connetionString = "server=(local);database=modelDb;user id=sa;pwd=123456";
                    sql = "DECLARE @var varchar(1000) = (SELECT TOP 1 Text FROM Alarms WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC) DECLARE @start_position int, @end_position int SELECT @start_position = PATINDEX('% at%', @var) SELECT @end_position = PATINDEX('%kilometers%', @var) DECLARE @VALUE VARCHAR(10) = (Select SUBSTRING(@var, @start_position+5,5)) Select Top 1 @VALUE,RouteTable.Latitude,Routetable.Longitude,Alarms.ApplicationTime,RouteTable.StationName,RouteTable.SectionName FROM Alarms INNER JOIN Routetable ON Routetable.Location BETWEEN FLOOR(@VALUE)-1 AND CEILING(@VALUE)+1 WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC";

                    conn = new SqlConnection(connetionString);
                    try
                    {
                        conn.Open();
                        cmd = new SqlCommand(sql, conn);
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {

                            if (old_value.ToString() != reader.GetValue(0).ToString())
                            {
                                MessageBox.Show("Location:-" + "              " + reader.GetValue(0) + Environment.NewLine + "Latitude:-" + "                          " + reader.GetValue(1) + Environment.NewLine + "Longitude:-" + "                       " + reader.GetValue(2) + Environment.NewLine + "Leak Occured Time:-" + "       " + reader.GetValue(3) + Environment.NewLine + "Station Name:-" + "                 " + reader.GetValue(4) + Environment.NewLine + "Section Name:-" + "                " + reader.GetValue(5));
                                old_value = reader.GetValue(0).ToString();
                                string lat = reader.GetValue(1).ToString();
                                string lon = reader.GetValue(2).ToString();
                                LocationMap form1 = new LocationMap(lat, lon);
                                form1.Show();
                            }
                            else
                            {

                            }
                            
                       }
                        reader.Close();
                        cmd.Dispose();
                        conn.Close();
                    }
                    catch (Exception ex)
                    {
                      MessageBox.Show(ex.ToString());
                    }
                    System.Threading.Thread.Sleep(5 * 1000);
                }
            }
        
     private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About_LocationFinder formhelp = new About_LocationFinder();
            formhelp.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}





Form2: -





Form2:-

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

namespace LocationFinder
{
    public partial class LocationMap : Form
    {
        public LocationMap(string lati, string longi)
        {
            InitializeComponent();
            textBox2.Text = longi.ToString();
            textBox1.Text = lati.ToString();
            
        }
        
        private void LocationMap_Load(object sender, EventArgs e)
        {
            string Latitude = textBox1.Text;
            string Longitude = textBox2.Text;
            try
            {
                string address = "https://www.google.co.in/maps/place/";

                if (Latitude != string.Empty && Longitude != string.Empty)
                {
                    string address = address + Latitude + "," + Longitude;
                    webBrowser1.Navigate(address);
                }
                else
                {
                   webBrowser1.Navigate(address);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }  
        }
    }
}





错误当我拨打第二张表格时直接来自主程序的链接如下: -



http://s29.postimg.org/xw4661hav/Error.jpg [ ^ ]

推荐答案

忘记Windows窗体Web浏览器。它基于Internet Explorer的引擎。然而,主要问题是它将自己报告为2006年出现的网页的Internet Explorer 7,所以它几乎不受每个主要网站的支持。



或者,您可以使用基于webkit的Awesomium HTML UI Engine。 http://www.awesomium.com/
Forget the Windows Forms web browser. It's based on Internet Explorer's engine. The main problem however is that it reports itself as Internet Explorer 7 to the web pages, which came out in 2006, so it's pretty much unsupported by every major website.

Alternatively you can use the Awesomium HTML UI Engine, which is based on webkit. http://www.awesomium.com/


这篇关于如何在默认浏览器以外的c#Windows窗体应用程序中设计新的Web浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆