Visual C # is a new generation of software development languages launched by Microsoft, and Visual C # implements many functions by calling the. NET Framework a common package for all of the. NET Program Development languages ——. Net framework SDK. In this package provides a large and very rich class library, you can say, without the software development package, Visual C # is unable to write even a very functional program. However, there is also a problem with whether these components can be used by Visual C # if they are not covered in the. Net FrameWork SDK Package and are provided in other third-party COM components. The answer is: direct use is not possible, but these COM components after a certain conversion can be. This conversion is the conversion of the unmanaged code (unmanaged) to the managed code (Managed). Because these COM components are generally unmanaged (unmanaged code), the class library to be used when compiling the Visual C # file is only managed code (Managed code), which means that you want to use those unmanaged components in Visual C #. You must convert these unmanaged code components into a managed code component. A program "Aximp.exe" is specifically provided in the. NET Framework to implement transformations from COM components to WinForm components. 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 a different directory or disk, you can find the file in the order listed above.
Let's use Visual C # as a "browser" to see how COM components are used in Visual C #.
A The software environment for programming and running in this paper
(1) Microsoft company Windows 2000 Server Edition
(2). Net FrameWork SDK Beta 2
Two The idea of programming and the solution of the key steps
(1). Convert COM component to WinForm component:
In fact, the implementation of this conversion is very simple, we know that the Microsoft Web browser COM component name is "Shdocvw.dll", because we are using Windows 2000, so this file is stored in the "C:\Winnt\System32" directory, If you are using Windows 98 or Windows Me, the location of this component is "C:\Windows\System". There are a lot of parameters behind the "Aximp.exe" file, you can go through "aximp/?" To understand that you can successfully convert this article using only the following simple commands:
AxImp C:\winnt\system32\shdocvw.dll
After you run the above command, you can implement the transformation and generate "SHDocVw.dll" and "AxSHDocVw.dll" two files in the current directory. The following figure:
Figure 01: Converting COM components to WinForm components
(2). Use a converted component in a program:
Namespace "AXSHDOCVW" is defined in "AxSHDocVw.dll", in which a "axwebbrowser" component is defined with several methods and properties, Visual C # is through these methods and attributes to achieve some of the basic browser functionality. Using this browser component is the same as using other WinForm components, you first import the namespace, then inherit the browser components defined in this namespace in your program, and finally set the properties and methods of the inherited component. Specifically as follows:
< I > Import namespaces, the specific code is as follows:
Using AxSHDocVw;
< ii>. Inherits the browser components defined in this namespace, with the following specific code:
Private Axwebbrowser AxWebBrowser1;
(3). Implement some basic functions of the browser by converting the components:
The main function of the browser is to be able to browse the information to the specified address, of course, there are some basic functions in the specific browsing, such as: "Forward", "back", "Stop", "refresh", "home", etc., these features can be implemented through the "Axwebbrowser" component. Here is a specific description:
< I > Browse the specified address:
In the program, the URL is filled in the component "TextBox1", "Browse specified address" function is through the program's button "go" to achieve. Following is the button "go" to press the program code:
private void Button1_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, ref NULLOBJSTR, ref NULLOBJSTR);
Cursor.current = Cursors.Default;
}
< II >. Browser "Forward", "back", "Stop", "refresh", "Home" feature:
There is a specific approach to these features in the "Axwebbrowser" component, 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 = = tb3)
{
Axwebbrowser1.stop ();
}
"Refresh" in the browser
if (E.button = = TB4)
{
Axwebbrowser1.refresh ();
}
"Home" in the browser
if (E.button = = tb5)
{
Axwebbrowser1.gohome ();
}
}
< III >. Of course, with the knowledge above, you can use Visual C # to make a basic browser, but these are also indispensable, because the following code will make you do the browser more professional. The following code works by making the browsing interface change as the form changes, and the buttons and text boxes change as the form changes.
Button1. Anchor = (Anchorstyles.top | Anchorstyles.right);
Position the Go To button component to match the top and right border of the form
Textbox1.anchor = ((Anchorstyles.top | Anchorstyles.left)
| Anchorstyles.right);
The Position Address text box component is aligned with the top, left, and right border of the form
Axwebbrowser1.anchor = ((Anchorstyles.top | Anchorstyles.bottom)
| Anchorstyles.left)
| Anchorstyles.right);
Locate the browser component in accordance with the top, bottom, left, and right border of the form
Three SOURCE program code (Brower.cs)
Understand that with these above, it is easier to write a browser of their own, the following is the use of Visual C # Browser source code, he has some common IE browser features.
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 tb3;
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 ();
}
To purge resources used in a program
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Initializing individual components in a form
private void InitializeComponent ()
{
TB1 = new ToolBarButton ();
TB2 = new ToolBarButton ();
TB3 = 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 = "Back";
TB2. Text = "forward";
Tb3. Text = "Stop";
TB4. Text = "Refresh";
Tb5. Text = "Home page";
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 (TB3);
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);
Position the Go To button component to match the top and right border of the form
Button1. Anchor = (Anchorstyles.top | Anchorstyles.right);
Button1. DialogResult = DialogResult.OK;
Button1. Location = new System.Drawing.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 (button1_click);
The Position Address text box component is aligned with the top, left, and right border 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 in accordance with the top, bottom, left, and right border of 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);
}
static void Main ()
{
Application.Run (New Form1 ());
}
Implement browser main function
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 = = tb3)
{
Axwebbrowser1.stop ();
}
"Refresh" in the browser
if (E.button = = TB4)
{
Axwebbrowser1.refresh ();
}
"Home" in the browser
if (E.button = = tb5)
{
Axwebbrowser1.gohome ();
}
}
Browse for the specified web address
private void Button1_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, ref NULLOBJSTR, ref NULLOBJSTR);
Cursor.current = Cursors.Default;
}
}
Four The running interface for compiling the source and compiled execution programs
After compiling with the following command, 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: The Run interface of "browser" with Visual C #
Five Summarize
So far a relatively complete "browser" is done, in fact, using Visual C # as a "browser" process, which is the process of using COM components in Visual C #. With the knowledge that COM components are used in Visual C #, you can use Visual C # to write more powerful, more adaptable software, but the process is simple to write.
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.