⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Csharp实例编程百例.rar
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyIEApp
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ToolBarButton toolBarButton2;
		private System.Windows.Forms.ToolBarButton toolBarButton3;
		private System.Windows.Forms.ToolBarButton toolBarButton4;
		private System.Windows.Forms.ToolBarButton toolBarButton5;
		private System.Windows.Forms.ToolBarButton toolBarButton6;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label1;
		private AxSHDocVw.AxWebBrowser MyWebBrowser;
		private System.Windows.Forms.ComboBox MyAddressBox;
		private System.Windows.Forms.ImageList imageList1;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.MyWebBrowser = new AxSHDocVw.AxWebBrowser();
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.panel1 = new System.Windows.Forms.Panel();
			this.MyAddressBox = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.MyWebBrowser)).BeginInit();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// MyWebBrowser
			// 
			this.MyWebBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
			this.MyWebBrowser.Enabled = true;
			this.MyWebBrowser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("MyWebBrowser.OcxState")));
			this.MyWebBrowser.Size = new System.Drawing.Size(360, 277);
			this.MyWebBrowser.TabIndex = 0;
			this.MyWebBrowser.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(this.MyWebBrowser_StatusTextChange);
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.toolBarButton1,
																						this.toolBarButton2,
																						this.toolBarButton3,
																						this.toolBarButton4,
																						this.toolBarButton5,
																						this.toolBarButton6});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageList1;
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(360, 38);
			this.toolBar1.TabIndex = 1;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarButton1
			// 
			this.toolBarButton1.ImageIndex = 0;
			this.toolBarButton1.Text = "Back";
			// 
			// toolBarButton2
			// 
			this.toolBarButton2.ImageIndex = 1;
			this.toolBarButton2.Text = "Forward";
			// 
			// toolBarButton3
			// 
			this.toolBarButton3.ImageIndex = 2;
			this.toolBarButton3.Text = "Stop";
			// 
			// toolBarButton4
			// 
			this.toolBarButton4.ImageIndex = 3;
			this.toolBarButton4.Text = "Refresh";
			// 
			// toolBarButton5
			// 
			this.toolBarButton5.ImageIndex = 4;
			this.toolBarButton5.Text = "Home";
			// 
			// toolBarButton6
			// 
			this.toolBarButton6.ImageIndex = 5;
			this.toolBarButton6.Text = "Search";
			// 
			// imageList1
			// 
			this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 261);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(360, 16);
			this.statusBar1.TabIndex = 4;
			// 
			// panel1
			// 
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.MyAddressBox,
																				 this.label1});
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 38);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(360, 40);
			this.panel1.TabIndex = 5;
			// 
			// MyAddressBox
			// 
			this.MyAddressBox.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.MyAddressBox.Location = new System.Drawing.Point(56, 8);
			this.MyAddressBox.Name = "MyAddressBox";
			this.MyAddressBox.Size = new System.Drawing.Size(298, 20);
			this.MyAddressBox.TabIndex = 5;
			this.MyAddressBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MyAddressBox_KeyPress);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(5, 13);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 4;
			this.label1.Text = "地址:";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(360, 277);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.panel1,
																		  this.statusBar1,
																		  this.toolBar1,
																		  this.MyWebBrowser});
			this.Name = "Form1";
			this.Text = "MyIEApp";
			((System.ComponentModel.ISupportInitialize)(this.MyWebBrowser)).EndInit();
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			try 
			{
				switch (toolBar1.Buttons.IndexOf (e.Button ))
				{
					case 0: 
						MyWebBrowser.GoBack ();		//后退
						break;
					case 1:
						MyWebBrowser.GoForward ();	//前进
						break;
					case 2:
						MyWebBrowser.Stop ();		//停止
						break;
					case 3:
						MyWebBrowser.Refresh ();	//刷新
						break;
					case 4:
						MyWebBrowser.GoHome ();		//主页
						break;
					case 5:
						MyWebBrowser.GoSearch ();	//搜索
						break;
				}
			}
			catch{}	
		}


//		private void MyAddressBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
//		{
//			if ( e.KeyChar == (char)13 )	//回车
//			{
//				MyNavigate();
//				e.Handled = true;
//			}
//		}

		private void MyNavigate()
		{
			object anObject = new object ();
			try
			{
				this.MyWebBrowser.Navigate(MyAddressBox.Text , ref anObject, ref anObject, ref anObject, ref anObject);
			} 
			catch{}
			AddAdress();
			statusBar1.Text = MyWebBrowser.LocationName ;
		}

		private void AddAdress()
		{
			int AdressIndex = MyAddressBox.FindStringExact (MyAddressBox.Text );
			if (AdressIndex < 0)	//如果地址不在ComboBox中,则添加地址
			{
				MyAddressBox.Items.Add (MyAddressBox.Text);
			}
		}

		private void MyAddressBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if ( e.KeyChar == (char)13 )	//回车
			{
				MyNavigate();
				e.Handled = true;
			}
		}

		private void MyWebBrowser_StatusTextChange(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
		{
			statusBar1.Text = e.text;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -