[인터넷 웹 브라우저를 만들어보자.]
여기서 설명할 내용은 닷넷 COM구성요소에서 제공하는
컨트롤을 이용하여 웹브라우저를 띄워 보는것이다.
그럼 먼저 새프로젝트를 열고 윈도우즈 응용프로그램을 선택하여보자.

먼저 윈도우폼 프로젝트를 하나 새로 만든다. 그리고 왼쪽 도구상자의 메뉴에서 도구 추가삭제를 선택한다.
그러면 아래 쪽에 Microsoft 웹브라우저라는 컨트롤이 있을것이다. 이 컨트롤을 선택한다.선택을 하였으면 폼을 디자인 하여 보자.

단순히 텍스트 박스 하나와 웹브라우저 두개를 집어 넣었다.
그럼 이제 KeyPress이벤트로 일어나는 이벤트를 이용하여 웹브라우저를 작동시켜 보겠다.
먼저 텍스트에 주소를 쓰고 Enter를 누르게 되는 타임을 보도록하겠다.

먼저 텍스트 박스에서 KeyPress라는 이벤트를 만든다. 그리고 코드를 살펴보자.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char keyChr = e.KeyChar;//13 = 엔터값입니다.
if(keyChr == 13)
{
Run();
}
}
키가 눌러지는 순간 누른 Key값의 Value를 체크하게 되고, Run()함수를 부르게 되는것이다.
이제 Run함수를 살펴보기로 하자.
public void Run()
{
// 인자로들어가야 하는 객체들을 초기화합니다.
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
// 커서입니다.
Cursor.Current = Cursors.WaitCursor;
// 첫번째 인자에 URL을 넣어주면 된다는 것만 기억합시다.
axWebBrowser1.Navigate(
textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
Cursor.Current = Cursors.Default;
}
설명을 따로 하지 않아도 Run함수의 주석을 보고 충분히 이해할수 있을것이다.
전체소스
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Explorer
{
/// <summary>
/// Form1에 대한 요약 설명입니다.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows Form 디자이너 지원에 필요합니다.
//
InitializeComponent();
//
// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
//
}
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form 디자이너에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(600, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "http://www.hoonsbara.com";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// axWebBrowser1
//
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(8, 48);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(600, 384);
this.axWebBrowser1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(616, 437);
this.Controls.Add(this.axWebBrowser1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Explorer";
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void Run()
{
// 인자로들어가야 하는 객체들을 초기화합니다.
System.Object nullObject = 0;
string str = "";
System.Object nullObjStr = str;
// 커서입니다.
Cursor.Current = Cursors.WaitCursor;
// 첫번째 인자에 URL을 넣어주면 된다는 것만 기억합시다.
axWebBrowser1.Navigate(
textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
Cursor.Current = Cursors.Default;
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char keyChr = e.KeyChar;//13 = 엔터값입니다.
if(keyChr == 13)
{
Run();
}
}
}
}
|
|
- 움직이는 Text 그래픽을 만들어보자. (0)2007/01/25
- Low-Level 키보드 입력 후킹 (0)2007/01/25
- 인터넷 웹 브라우저를 만들어보자. (0)2007/01/23
- 프로퍼티 그리드 컨트롤을 다루어 보자. (0)2007/01/23
- 윈도우 종료/재부팅 (0)2006/11/24

수안이의 컴퓨터 연구실



Leave your greetings.