Joke Collection Website - Blessing messages - What is the function of API and how does it work?

What is the function of API and how does it work?

I hate to say this at first, but maybe you know others don't. In order to take care of novices, I have to say some nonsense here. Please forgive me.

Win32 API is the application programming interface of Microsoft 32-bit platform. All applications running on the Win32 platform can call these functions.

Using Win32 API, applications can fully tap the potential of Windows 32-bit operating system. All 32-bit platforms of Microsoft support a unified API, including functions, structures, messages, macros and interfaces. Using Win32 API can not only develop applications that can run successfully on various platforms, but also make full use of the unique functions and attributes of various platforms.

The above is the introduction of API, but some novices may still not understand the purpose of API after reading it. Please don't worry here. If you have enough patience, please look down slowly.

Second, how to use the API?

It is estimated that this is what everyone really cares about, so how to use API? Before you know the API, open your VB book and turn to the chapter on process functions. Before you know the API, you should know what the process function is! If you don't know how the process works, please don't look down in a hurry, it's easy to take many detours.

Well, when you understand the process function, that is, when you can use the API, don't take it too seriously, just use the API like a process function. First, let's look at a simple API, as follows:

Privately declare the sub-sleep library "kernel32" alias "sleep" (as long as byval dw mills).

The purpose of the above API is to play a delaying role. If you are a novice to API, you may find the writing of API extremely complicated and uncomfortable. Actually, it's nothing. Just get used to it. As for the complicated writing of API, don't worry. When you install VB, Microsoft has brought us an API browser, which can be generated automatically by using the API browser. The location of the API browser is located in [Start Menu-Programs -Microsoft Visual Basic 6.0 Chinese Version -Microsoft Visual Basic 6.0 Chinese Version Tools -API Text Browser]. Open the API browser and enter Sleep in the top text box. At this time, the corresponding API function will be automatically displayed in the list box below, and then click the Add button on the right, and then click the Copy button. At this point, you can use Ctrl+V to add the declared API to the VB code window.

I want to say here that some novices may not understand it. There are generally two modes for the declaration scope of API, one is private and the other is public. Usually, Private is declared in a class module or form class, and Public is declared in a module. When adding an API, there is the declaration scope of the API under the Add button, which can be added according to your own needs. We usually choose private here.

Above we know how to add an API, and then we analyze the API declaration, which is a necessary condition for you to understand the API. Look at the first word, Private. Obviously, as I said just now, this is to declare a private API variable. Look at the second statement. This word helps us tell VB that it is declaring API functions. Generally, this word must be used when declaring external API functions. Third submarine, don't tell me what you don't know. This is what I mean by asking you to learn the process function in VB first. To put it bluntly, there is no echo value. Generally, it is not Sub but function that has echo value. The fourth Lib tells VB which API function in the dll we want to declare, that is, tells VB that we want to declare the API in the fifth word kernel32.dLL. Generally, when writing DLL names, you should enclose them in double quotes, such as "user32" and "shell32.dll". As for the following. DLL, you can accept it or not. Let's look at the sixth alias, which also needs to be used with the latter one. We should connect the sixth and seventh aliases "Sleep", which means that the procedure to be called has another name in the DLL, which is optional. Finally, what is in parentheses is that, like a procedure function, you can pass in the corresponding value.

After analyzing the above API function declaration, we will write the code ourselves. First copy this API to the Form 1 code window, and then write the following code:

Privately declare the sub-sleep library "kernel32" (as long as byval dw mills).

Private Sub-Form _Load ()

Sleep 2000

End joint

To explain, when the form is started, use the Sleep API to delay for 2 seconds, and the following parameter dwMilliseconds indicates the number of seconds you want to delay, which is basically the same as setting the number of seconds in the timer. Look at the way Sleep 2000 is used. Is it the same as using VB process function? Well, our first VB API program has been written, and we can see that it is not difficult to use the API.

Third, how to enhance the interest in learning API?

API, I often regard it as a process function, but everyone has their own views and understanding. As long as his own way of understanding can help him learn and master API better, there is no need to learn from others.

1, make your own MsgBox

It is very important to know how to use API parameters. Here we don't use VB's MsgBox, but use API to pop up the MsgBox message box directly. First, open the API browser and select MessageBox. You can compare this API with the built-in MsgBox in VB. In fact, MsgBox is the abbreviation of MessageBox, but one is API and the other is built in VB, but they all work through API. Ok, select the private declaration mode, paste it into the VB code editing window, and then create a new CommandButton, and write the following code:

Private declaration function MessageBox library "user32" alias "MessageBoxA"

(ByVal hwnd is Long, ByVal lpText is String, ByVal lpCaption is String,

ByVal wType is as long as).

Private subcommand 1_Click ()

MessageBox Me.hwnd, "Here is the content", "Title", 0

End joint

Let's analyze it first. First, consider the first parameter Byval hWnd as a Long. Obviously, this is a long variable, so what we need to pass here is a number. You may find that we are not passing a number, but me. Isn't it strange that hwnd? If you really have this question, it means that you really want to learn API well. Now let's see what Me.hwnd is. The following contents are extracted from the VB help document:

Returns the handle to a form or control.

Statement handle: A unique integer value defined by the operating environment, which is used by programs to identify or switch to an object, such as a form or control.

Now I guess you have almost understood that the hwnd we called is actually a handle integer value. You can use Msgbox Me.hwnd to look at it. For me, this is a keyword that represents the current form object. Such as: me. Caption= "title ",me. BackColor=vbRed, etc.

After the above, we first pass in Me.hwnd, which means that the current window calls MessageBox. Here's a trick for you, that is, whenever you see Byval hwnd as long in the future, you usually need to pass in a handle. As for which object handle is passed in, it depends on how you implement it.

ByVal lpText As String, which is a string variable, indicating that we need to pass in a string. You can see that the variable character lpText belongs to the meaning of Text, that is to say, it is used to display the message text in MsgBox.

ByVal lpCaption is String, which is also a string variable, or a string is passed in. Look at the variable character lpCaption inside, which actually shows the MsgBox title.

ByVal wType As Long, is an integer variable, and you need to pass an integer, or look at the variable character wType inside, which indicates that the MsgBox type is displayed. Here you can use it like VB's MsgBox, such as: vbYesNo, vbOkCancel, etc. If ignored, it can be passed in as 0.

Ok, press F5 to start the program, click Command 1, and then a message box will pop up. Here we have finished the production and analysis of MsgBox. I hope you can learn some knowledge during this period.

Let's do something practical.

Take Windows Task Manager as an example. Here, you can only hide the window in the Task Manager, not the process. Q: Is there any hidden process? What do you want? ), when the program is running, you can't close the program from the window of the task manager, you can only terminate it from the process. Ok, as usual, open the API browser, enter two APIs, GetWindow and ShowWindow, declare that the scope is still private, and copy and paste it into the form code window, huh? Don't worry, it's still an API browser. Select a constant in Combox, enter two API constants GW_OWNER and SW_HIDE, and paste them into the code window. Ask me what these two do. Then keep reading. Write the following code:

Private declaration function getwindowlib "user 32" (byval hwnd as long,

ByVal wCmd is as long as).

Private declaration function show windowlib "user 32" (byval hwnd as long,

ByVal nCmdShow is as long as)

Private constant GW_OWNER = 4

Private constant SW_HIDE = 0

Private Sub-Form _Load ()

Dim lphWnd As Long

lphWnd = GetWindow(Me.hwnd,GW_OWNER)

ShowWindow lphWnd,SW_HIDE

End joint

It's time for analysis again. This is the most exciting time for beginners. Okay, it's me again. Let's take a look at the superficial meaning and transfer value variables of the two APIs.

Look at GetWindow first, on the surface: get the window. Transfer value variables: hWnd plastic handle, wCmd plastic command value.

Look at the ShowWindow again, on the surface: display the window. Passing value variables: hWnd plastic handle, nCmdShow plastic command value.

Then use the code, first look at the sentence LP hwnd = GetWindow (me. Hwnd, GW _ owner), which means to get the owner window handle of the current window. You can see that getwindow is a function process function, which will return the corresponding window handle value after execution, which is a long integer (the same handle). Then call ShowWindow lphWnd, SW_HIDE, which means the window displaying the handle lphWnd. The key sentence is the last SW_HIDE, which is the constant of API function. By setting constants, the system can know how the API should display the window or not. Or hide? Of course it means hidden. Ok, compile it into Exe and open the task manager to view the program window after running. Anything else?

I want to say it again. Some people may not understand why we use these constants GW_OWNER. What are these constants for? How do I know which APIs correspond to which constants? In fact, you only need to pay a little attention to these constants to know what they are. For example, I use GW_OWNER in GetWindow and SW_HIDE in ShowWindow. These constants all have the same feature, that is, they are based on the first letter of an API word. For example, the constant corresponding to GetWindow is Get(G)Window(W)=GW, and the constant corresponding to ShowWindow is often Show(S)Window(W)=SW. These constants can be found in the API browser of VB.

3. I still want to write, but I don't know how to write. Send it out first and see how everyone reacts. If you find any mistakes, please point them out. I accept your criticism. . . .