Visual C # create a "Browser"

Source: Internet
Author: User
Abstract: Visual C # creating a "Browser"

Visual C # is a new-generation programming language released by Microsoft. Visual C # implements many functions through calling. net Framework for all. A public software package provided by the net programming language --.. NET Framework SDK. This software package provides a large number of and rich class libraries. It can be said that without this software development kit, Visual C # cannot be used, and even a program with very powerful functions cannot be compiled. However, this will also cause a problem.. NET Framework SDK package does not involve functions, but is provided in other third-party COM components, so can these components be used by Visual C. The answer is: direct use is not acceptable, but these COM components can be converted. This conversion is the conversion from unmanaged code to managed code. These COM components are generally not managed code, but the class library to be used when compiling the Visual C # file can only be managed code ), this means that to use unmanaged code components in Visual C #, you must convert these unmanaged code components into managed code components. In the .netframework, a special program named “aximp.exe is used to convert the COM component to the winform component. So where is this file? If you install the. NET Framework SDK on the "c" disk, you can find this file in the "C: \ Program Files \ Microsoft. NET \ frameworksdk \ bin" directory. If you install the. NET Framework SDK in another directory or disk, you can find the file in the preceding directory order.
The following uses Visual C # as a "Browser" to see how the COM component is used in Visual C.
I. software environment for program design and operation in this article
(1). Microsoft Windows 2000 Server Edition
(2). Net Framework SDK beta 2
Ii. Ideas of program design and solutions to key steps
(1) convert the COM component into a winform component:
In fact, this conversion is very simple. We know that the COM component of Microsoft Web browser is named "shdocvw. DLL, because we use Windows 2000, this file is stored in the "C: \ winnt \ System32" directory. If you are using Windows 98 or Windows ME, the component is stored in "C: \ WINDOWS \ SYSTEM ". The “aximp.exe file contains many parameters. You can use "aximp /?" To understand, in this article, you can use only the following simple commands to successfully convert:
Aximp c: \ winnt \ system32 \ shdocvw. dll
After running the preceding command, you can implement the conversion and generate two files, "shdocvw. dll" and "axshdocvw. dll", in the current directory. For example:

Figure 01: Convert COM component to winform component
(2). Use the converted component in the program:
In "axshdocvw. DLL defines the namespace "axshdocvw" and defines an "axwebbrowser" component in this namespace, which contains several methods and attributes, visual C # implements some basic functions of the browser through these methods and attributes. The process of using this browser component is the same as that of using other winform components. First, you must import the namespace and then inherit the browser components defined in this namespace in the program, finally, set the attributes and methods of the inherited component. The details are as follows:
<I>. The code for importing a namespace is as follows:
Using axshdocvw;
<II>. inherit the browser components defined in this namespace. The Code is as follows:
Private axwebbrowser axwebbrowser1;
(3). implement some basic functions of the browser by converting components:
The main function of a browser is to browse information at a specified address. Of course, there are some basic functions in the browser, such: these functions can be implemented through the axwebbrowser component, such as "forward", "back", "stop", "refresh", and "home page. The following is a detailed introduction:
<I>. Browse the specified address:
In the program, the URL is filled in the component "textbox1". The "Browse specified address" function is implemented by the "go to" button of the program. The following is the program code after the button "go to" is clicked:
Private void button#click (Object sender, system. eventargs E)
{
System. Object nullobject = 0;
String STR = "";
System. Object nullobjstr = STR;
Cursor. Current = cursors. waitcursor;
Axwebbrowser1.navigate (textbox1.text, ref nullobject, ref nullobjstr );
Cursor. Current = cursors. default;
}
<II>. "forward", "backward", "stop", "refresh", and "Homepage" functions of the browser:
In the "axwebbrowser" component, there is a specific method for these functions, as shown in the following code:
Private void toolbar1_buttonclick (Object sender, toolbarbuttonclickeventargs E)
{
// "Back" in the browser"
If (E. Button = tb1)
{
Axwebbrowser1.goback ();
}
// Forward in the browser"
If (E. Button = tb2)
{
Axwebbrowser1.goforward ();
}
// "Stop" in the browser"
If (E. Button = fig)
{
Axwebbrowser1.stop ();
}
// "Refresh" in the browser"
If (E. Button = tb4)
{
Axwebbrowser1.refresh ();
}
// "Homepage" in the browser"
If (E. Button = tb5)
{
Axwebbrowser1.gohome ();
}

}
<III>. of course, with the above knowledge, you can use Visual C # To make a basic browser, but the following are also indispensable, the code below will make your browser more professional. The following code changes the browser interface with the form, and the buttons and text boxes also change with the form.
Button1.anchor = (anchorstyles. Top | anchorstyles. Right );
// Locate the "go to" button. The widget is consistent with the top and right borders of the form.
Textbox1.anchor = (anchorstyles. Top | anchorstyles. Left)
| Anchorstyles. Right );
// The positioning address text box component is consistent with the upper, left, and right borders of the form
Axwebbrowser1.anchor = (anchorstyles. Top | anchorstyles. Bottom)
| Anchorstyles. Left)
| Anchorstyles. Right );
// Locate the browser component and keep the top, bottom, left, and right borders consistent with the form
3. Source Code (Brower. CS)
After learning about the above, you can easily compile a browser of your own. The following is the source code of the browser program using Visual C, he has some common functions of IE browser.
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using axshdocvw;
Public class form1: Form
{
Private toolbar toolbar1;
Private toolbarbutton tb1;
Private toolbarbutton tb2;
Private toolbarbutton fig;
Private toolbarbutton tb4;
Private toolbarbutton tb5;
Private Label label1;
Private textbox textbox1;
Private button button1;
Private axwebbrowser axwebbrowser1;
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
Initializecomponent ();
}
// Clear resources used in the program
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
// Initialize components in the form
Private void initializecomponent ()
{
Tb1 = new toolbarbutton ();
Tb2 = new toolbarbutton ();
Int32 = new toolbarbutton ();
Toolbar1 = new toolbar ();
Tb4 = new toolbarbutton ();
Tb5 = new toolbarbutton ();
Button1 = new button ();
Textbox1 = new Textbox ();
Axwebbrowser1 = new axwebbrowser ();
Label1 = new label ();
(System. componentmodel. isupportinitialize) (This. axwebbrowser1). begininit ();
This. suspendlayout ();

Tb1.text = "backward ";
Tb2.text = "Forward ";
Tb3.text = "stop ";
Tb4.text = "refresh ";
Tb5.text = "Homepage ";

Toolbar1.appearance = toolbarappearance. Flat;
Toolbar1.borderstyle = system. Windows. Forms. borderstyle. fixedsingle;
// Add a button to the toolbar
Toolbar1.buttons. Add (tb1 );
Toolbar1.buttons. Add (tb2 );
Toolbar1.buttons. Add (int32 );
Toolbar1.buttons. Add (tb4 );
Toolbar1.buttons. Add (tb5 );
Toolbar1.dropdownarrows = true;
Toolbar1.name = "toolbar1 ";
Toolbar1.showtooltips = true;
Toolbar1.size = new system. Drawing. Size (612, 39 );
Toolbar1.tabindex = 0;
Toolbar1.buttonclick + = new toolbarbuttonclickeventhandler (toolbar1_buttonclick );
// Locate the "go to" button. The widget is consistent with the top and right borders of the form.
Button1.anchor = (anchorstyles. Top | anchorstyles. Right );
Button1.dialogresult = dialogresult. OK;
Button1.location = new system. Fig. Point (544, 45 );
Button1.name = "button1 ";
Button1.size = new system. Drawing. Size (40, 23 );
Button1.tabindex = 3;
Button1.text = "go ";
Button1.click + = new system. eventhandler (button#click );
// The positioning address text box component is consistent with the upper, left, and right borders of the form
Textbox1.anchor = (anchorstyles. Top | anchorstyles. Left)
| Anchorstyles. Right );
Textbox1.location = new system. Drawing. Point (64, 47 );
Textbox1.name = "textbox1 ";
Textbox1.size = new system. Drawing. Size (464, 21 );
Textbox1.tabindex = 2;
Textbox1.text = "";
// Locate the browser component and keep the top, bottom, left, and right borders consistent with the form
Axwebbrowser1.anchor = (anchorstyles. Top | anchorstyles. Bottom)
| Anchorstyles. Left)
| Anchorstyles. Right );
Axwebbrowser1.enabled = true;
Axwebbrowser1.location = new system. Drawing. Point (0, 72 );
Axwebbrowser1.size = new system. Drawing. Size (608,358 );
Axwebbrowser1.tabindex = 4;

Label1.location = new system. Drawing. Point (16, 48 );
Label1.name = "label1 ";
Label1.size = new system. Drawing. Size (48, 16 );
Label1.tabindex = 1;
Label1.text = "Address :";

This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (612,433 );

This. Controls. Add (axwebbrowser1 );
This. Controls. Add (button1 );
This. Controls. Add (textbox1 );
This. Controls. Add (label1 );
This. Controls. Add (toolbar1 );
This. formborderstyle = formborderstyle. fixedsingle;
This. Name = "form1 ";
This. Text = "Visual C # Web Browser ";
(System. componentmodel. isupportinitialize) (This. axwebbrowser1). endinit ();
This. resumelayout (false );

}
Static void main ()
{
Application. Run (New form1 ());
}
// Implement the main functions of the browser
Private void toolbar1_buttonclick (Object sender, toolbarbuttonclickeventargs E)
{
// "Back" in the browser"
If (E. Button = tb1)
{
Axwebbrowser1.goback ();
}
// Forward in the browser"
If (E. Button = tb2)
{
Axwebbrowser1.goforward ();
}
// "Stop" in the browser"
If (E. Button = fig)
{
Axwebbrowser1.stop ();
}
// "Refresh" in the browser"
If (E. Button = tb4)
{
Axwebbrowser1.refresh ();
}
// "Homepage" in the browser"
If (E. Button = tb5)
{
Axwebbrowser1.gohome ();
}

}
// Browse the specified web address
Private void button#click (Object sender, system. eventargs E)
{
System. Object nullobject = 0;
String STR = "";
System. Object nullobjstr = STR;
Cursor. Current = cursors. waitcursor;
Axwebbrowser1.navigate (textbox1.text, ref nullobject, ref nullobjstr );
Cursor. Current = cursors. default;
}
}
4. Compile the source program and run the compiled execution Program Interface
After the following command is compiled, you can get your own browser.
CSC/T: winexe/R: axshdocvw. dll/R: shdocvw. dll/R: system. dll
/R: system. Windows. Forms. dll/R: system. Drawing. dll Brower. CS
Figure 02: "Browser" running interface using Visual C #
V. Summary
Now, even if a "Browser" with relatively complete functions is complete, the process of using Visual C # as a "Browser" is actually the process of using COM components in Visual C. After learning how to use COM components in Visual C #, you can use Visual C # To write more powerful and adaptable software. However, the compilation process is very simple.

From: http://www.yomi.cn/computer/A200508/2005-08-07/186394.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.