using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SHDocVw;
using mshtml;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
InternetExplorer ie = new InternetExplorer();
IWebBrowserApp webBrowser = (IWebBrowserApp)ie;
// 브라우져 크기 지정
webBrowser.Height = 700;
webBrowser.Width = 900;
webBrowser.Visible = true;
webBrowser.Navigate("http://www.google.com");
Thread.Sleep(3000);
IHTMLDocument2 doc2 = (IHTMLDocument2)webBrowser.Document;
IHTMLDocument3 doc3 = (IHTMLDocument3)webBrowser.Document;
// Document 속성 읽기
string title = doc2.title;
string url = doc2.url;
textBox1.Text = url;
// 특정 Element 컨트롤
IHTMLElement q = doc3.getElementsByName("q").item("q", 0);
q.setAttribute("value", "microsoft");
IHTMLFormElement form = doc2.forms.item(Type.Missing, 0);
form.submit();
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
// 특정 URL을 갖는 IE 찾기
SHDocVw.WebBrowser wb = FindIE("https://www.google.co.kr/");
// URL을 다른 것으로 변경
if (wb != null)
wb.Navigate("www.bing.com");
}
catch (System.Exception)
{
throw;
}
}
static SHDocVw.WebBrowser FindIE(string url)
{
Uri uri = new Uri(url);
var shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.WebBrowser wb in shellWindows)
{
Uri wbUri = new Uri(wb.LocationURL);
if (wbUri.Equals(uri))
{
return wb;
}
}
return null;
}
}
}