Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C # use COM components in Visual

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how C # uses COM components in Visual". The explanation in this article is simple and clear and easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "how C # uses COM components in Visual".

Visual C # is a new generation program development language developed by Microsoft. Visual C # development browser is a common software package -. Net FrameWork SDK, which is provided for all .net programming languages by calling .net framework. A large number of rich class libraries are provided in this software package. it can be said that without this software development package, Visual C # will be unable to write even a very functional program. But this also raises the question of whether functions not covered in the .net FrameWork SDK package can be used by Visual C # if they are provided in other third-party COM components. The answer is: it is not possible to use it directly, but these COM components can be converted. This conversion is the conversion from unmanaged code (Unmanaged Code) to managed code (Managed Code).

Let's develop a browser with Visual C # to see how COM components are used in Visual C #.

C # development browser I. software environment for program design and operation

(1)。 Microsoft Windows 2000 Server Edition

(2).. Net FrameWork SDK Beta 2

C # development browser II. Ideas of programming and solutions to key steps

(1)。 Convert a COM component to a WinForm component:

In fact, the implementation of this conversion is very simple, we know that 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, then the location of this component is "c:\ windows\ system". " There are many parameters at the end of the "Aximp.exe" file. You can use "Aximp /?" To learn that you can convert successfully using only the following simple commands in this article:

Aximp c:\ winnt\ system32\ shdocvw.dll

After running the above command, you can implement the conversion and generate "SHDocVw.dll" and "AxSHDocVw.dll" files in the current directory.

(2)。 Use converted components in your program:

The namespace "AxSHDocVw" is defined in "AxSHDocVw.dll", and a "AxWebBrowser" component is defined in this namespace. There are several methods and properties in this component, and Visual C # implements some basic functions of the browser through these methods and properties. The process of using this browser component is the same as using other WinForm components, first import the namespace, then inherit the browser component defined in this namespace in the program, and * set the properties and methods of the inherited component. The details are as follows:

< I >

. Import the namespace as follows:

Using AxSHDocVw

< II>

. Inherit the browser components defined in this namespace as follows:

Private AxWebBrowser axWebBrowser1

(3)。 Some basic functions of the browser are realized through the converted components:

The main function of the browser is to be able to browse information to the specified address, of course, there are some basic functions in the specific browsing, such as: "forward", "backward", "stop", "refresh", "home page" and so on. These functions can be realized through the "AxWebBrowser" component. Here are the details:

< I >

. Browse to the specified address:

In the program, the URL is filled in the component "textbox1", and the "browse to the specified address" function is realized through the program's button "go". Here is the program code after the button "go" is pressed:

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's "forward", "back", "stop", "Refresh", "Home" functions:

There is a specific way to correspond to these functions in the "AxWebBrowser" component, as in the following code:

Private void toolBar1_ButtonClick (object sender, ToolBarButtonClickEventArgs e) {/ / "back" if (e.Button = = tb1) {axWebBrowser1.GoBack (); / / "forward" if (e.Button = = tb2) {axWebBrowser1.GoForward (); / / "stop" if (e.Button = = tb3) {axWebBrowser1.Stop () in browser The "refresh" if in the browser (e.Button = = tb4) {axWebBrowser1.Refresh (); / / the "home page" if in the browser (e.Button = = tb5) {axWebBrowser1.GoHome ();}}

< III >

. Of course, with the above knowledge, you can make a basic browser with Visual C #, but the following is also indispensable, because the following code will make your browser more professional. The role of the following code is to make 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); / locate the go button component to be consistent with the top and right borders of the form textBox1.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right); / / locate the address text box component consistent with the top, left and right borders of the form axWebBrowser1.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right) / / locate the browser component consistent with the top, bottom, left and right borders of the form. Thank you for your reading, which is the content of "how C# uses COM components in Visual". After the study of this article, I believe you have a deeper understanding of how C# uses COM components in Visual. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report