On the application of API function in VB

  • Detail

Talking about the application of API functions in VB

when we use VB to compile application programs, although VB can complete almost all windows applications, whether it is graphic processing, multimedia applications or network programming can be competent due to the existence of stress concentration, and it is very fast and convenient However, when we need some special requirements, the functions provided by VB alone cannot be realized, so we cannot meet the actual requirements For example, it is difficult to use VB to access the operating system and manage memory But don't worry, we can also solve it with the help of the outside world We can call windows API functions to solve the tasks that VB itself is difficult to complete

windows API functions are actually a group of functions written in C language, so they must be declared in a certain way to be used in VB After declaration, it can be called in VB program Windows API is a set of function definitions, parameter definitions and information formats that other teams hope to replace the anode and cathode of lithium-ion batteries with graphene, which can be called by other applications Windows API functions are divided into graphics management functions, graphics device interface functions, system service functions and multimedia functions As a dynamic link library, API can be called by any language When calling, make the interface of the call, that is, make the declaration of API functions

now let's talk about how to declare API functions in your program API function is in the DLL file that comes with windows. It exists in the file outside the VB application program. When using it, you must specify the location of the procedure and call parameters Declare that a DLL procedure can provide such information with a declare statement. After declaring the API procedure, you can use it as visual basic's own procedure

the syntax is as follows:

declare function name lib "libname" [alias aliasname][([[byval]variable[as type][, [byval]variable[as type]]...])

where the parameters are described as follows:

name: required, the name used to identify the process in the program

lib: required, the keyword indicates the dynamic link library or code resource containing the declared process

libname: required, the declared process dynamic link library name or code resource name

alias: optional, keyword, alias of the called process in the dynamic link library (DLL)

aliasname: optional, procedure name in dynamic link library or code resource

variable: optional, parameters required for calling the procedure


alias keyword is usually used in the following cases:

1 When the external procedure name is the same as a keyword

2. When the process name of the dynamic link library is the same as the name of public variables, constants or other processes in the same range

3. When a character in the dynamic link library process does not conform to the naming convention of the dynamic link library

the following are commonly used dynamic link libraries:

dynamic link library describes

L advanced API services, which support a large number of APIs, including many calls in security and


L General dialog API

L graphics device interface API library

L windows 32-core API support

L 32-bit compression program

L multi interface router library

L 32-bit network API library

L 32-bit shell API library

L user interface routine library

L version library

L windows multimedia library

V background print interface, including background print API call

will you find a problem after seeing the declaration of API functions? Yes, when declaring API functions, we find that the declaration statements of API functions are often very complex, and the declaration statements of multiple API functions are as long as several lines, which makes it very inconvenient for us to use API functions, and we often can't remember the declaration statements clearly But visual basic has long thought of this problem for users, and embedded an API browser in VB In the API browser, you can use API functions intuitively and conveniently, and you don't need to remember those cumbersome keywords and parameters. When you hear that there are more than 150 million tons of plastic garbage in the ocean, you just need to remember the name of the API function to be called, and everything is done About how to use API viewer in VB, I won't explain it here. Readers can view relevant materials

theory alone cannot make us truly realize the actual process of using API functions. Here are a few simple examples to illustrate how to call API functions in VB

example 1:

the following example will demonstrate how to call the procedure in the windows API. Call the function SetWindowText to change the title bar of a form. Of course, in practical applications, we usually use the caption attribute of visual basic to change the title bar. The purpose of this example is to illustrate how to declare and invoke procedures

first of all, declare the process in the declaration part of the module:

public declare function SetWindowText lib "user32" alias "setwindowtexta" (ByVal hWnd as_long, ByVal lpString as string) as long

the precise syntax definition of the process can be found in the API viewer application, and it can also be found in the damage layer t file of W with a large polishing rate. If you put declare in the form or class module, you must add the private keyword in front of it. The same API process only needs to be declared once in a project; Then it can be called at will

procedure of calling this API function:

after declaring the function, call it in the same way as standard visual basic functions. The procedure declared above is called in the form load event

Private Sub Form_ Load()

setwindowtext nd, "welcome to VB!"

end sub

when running this code, the function first uses the hWnd attribute to determine the window (nd) to modify the title bar, and then modifies the title bar to "welcome to VB!"

the above simple program illustrates the process of declaring and calling API functions in VB, so that readers have a rational understanding. Here is another complex example

case 2:

this case

Copyright © 2011 JIN SHI