VB's API programming essence (2) Visual Basic is known for its friendly and easy-learning visualization development environment, and has become a preferred language for people to learn computer programming. At present, there are about more than 3 million people around the world use Visual Basic language. If you want to go out of this exclusive, you have to learn how API (Application Program Lnterface, Windows Application Programming Interface) is programmed. Don't understand the API, you can't make a master. Section 1: The API Foundation API said that the bottom is a series of underlying functions, and the system is provided to the user to enter the operating system core, advanced programming. Windows APIs can be accessed by declaring external processes in the Visual Basic application. After declaring the process, the method calling it is the same as the process of calling Visual Basic. To declare a DLL process, you need to add a declare statement in the "Declaration" section of the code window. If the process returns a value, it should be declared as function. For example: declare function publicname lib "libname" [alias "alias"] [([Byval] variable [as type] [, [byval] variable [as type]] ...)] AS TYPE If the process does not return value It can be declared as SUB. By default, the DLL process declared in the standard module can call anywhere in the application. The DLL process defined in other types of modules is the module private, and must be added to the private keyword to show the distinction. Try to note that the process names in 32-bit Visual Basic are case sensitive. In the previous 16th versions, it is not case sensitive, which is a place where beginners are easy to make mistakes. The lib clause in the DECLARE statement is used to tell Visual Basic how to find a DLL file that contains the process. If the referenced process belongs to the Windows core library (User32, Kernel32 or GDI32), the file extension can be not included. For example: Declare function gettickcount lib "kernel32" alias "gettickcount" () as long. For other DLLs, the lib clarion must specify the path and extension of the file. If the calling Windows API process is used to use a string, you must add an Alias clause in the declaration statement to specify the correct character set. A Windows API function containing strings, there are two Genu ANSI formats Unicode format. Therefore, in the Windows header file, each function containing a string has an ANSI version and Unicode version. For example, the following is two C language describing the setWindowText function.
It can be seen that the first description defines the function as setWindowTexta, the "A" of the tail indicates that it is an ANSI function: setWindowTexta (hwnd hwnd, lpcstr lpstring); second description defines it as setWindowTextw, "W" of the tail Indicates that it is a Unicode function: setWindowTextw (hwnd hwnd, lpcwstr lpstring); because the actual name of the two functions is not "setWindow text", you must add an alias clause: Private Declare Function SetWindowText lib "User32 "Alias" setWindowTexta "(Byval HWnd As String) AS LONG Please note that the string behind the Alias clause must be the true name of the process, which must be case sensitive. In fact, you only need to remember that only Windows NT supports Unicode format, and Windows 95 only supports ANSI format. As for the difference between the two, it is not necessary for general application development. VB5 Professional Edition provides information about the API with several files in a VB directory / WinAPI subdirectory. The win32api.txt file contains a 32-bit Windows API functionality declaration and a value of the global constant. Users can easily use Win32API using the external program "API browser" with the VB itself. TXT, as shown below: Click "Loading text files" from the menu file item to select "Win32API.txt" from the WINAPI directory under the VB directory, you can view the declaration, constant definition and data type of the API function of the Windows 95 system. . For example, we intend to view the declaration of the function inverRect (). First, click the "Search" button to enter the string "InverRect". In the Optional bar, the brightness of the blue light will move on the "InverRect" item. Press the "Add" button again, "InverRect" declaration in Visual Basic appears in "Selection". Next, you will naturally click the "Copy" button and then switch the window to the Visual Basic development environment, which is required to declare the API function. Ctrl V (Paste). Although the declaration method mentioned above is simple, only the API function using Win Dows itself can be like this. For a third-party dynamic link library (DLL) you only knock on the use of keyboards. In the second section: The cow knife is now readers must try itself, and the examples of the two practical applications will make everyone experience the API! 1. Make a form that always keeps on the screen. We know that the VB itself comes with a function that is difficult to complete this feature, we can call the Windows API function: SetWindowPos reaches our requirements.
The steps are as follows: (1) Start VB5 to establish a new project, add a module (moudel) in this project, and add the function declaration and constant declaration of the API function as described above with the above "API API" part: 'API function declaration declare function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long 'constant declaration Global Const SWP_HIDEWINDOW = & H80Global Const SWP_NOACTIVATE = & H10Global Const SWP_NOCOPYBITS = & H100Global Const SWP_NOMOVE = & H2Global Const SWP_NOOWNERZORDER = & H200Global Const SWP_NOREDRAW = & H8Global Const SWP_NOREPOSITION = SWP_NOOWNERZORDERGlobal Const SWP_NOSIZE = & H1Global Const SWP_NOZORDER = & H4Global Const SWP_SHOWWINDOW = & H40Global Const HWND_BOTTOM = 1Global Const HWND_BROADCAST = & HFFFF & Global Const HWND_DESKTOP = 0Global Const HWND_NOTOPMOST = -2Global Const HWND_TOPMOST = -1Global Const HWND_TOP = 0Global Const Flags = SWP_NOMOVE Or SWP_NOSIZE here for constants starting with "SWP_" is a form has style, these constants Can be combined with the "or" operator in VB. The constant starting with "hwnd_" represents the location of the form on the desktop. In the sense of English words from these constants, the reader should be able to understand the style they have. Therefore, the author will not explain it. As for why you want to add these constants instead of this, you want to go to see the help documentation for the Windows SDK about the function. Of course, there is a certain difficulty for beginners, but don't fear, just want to take care of it carefully. Because these API functions are written for C and C , it is easy to understand if you know a little C . (2) Now that you can call this function when you want this function, call methods such as: DIM SUCCESS AS Longsuccess = setWindowPos (Me.hwnd. Hwnd_topmost, 0,0,0,0,0, flags) If you want The returned value is not equal to zero, indicating that the call is successful. For example, add the two lines of code described above in a LOAD event in a form, you can reach the top of the screen at the top of the screen.
The careful readers may have discovered that the modules in the above example declare several constants, but why only use three? Now you can try to change the second parameter or constant Flags in the API function "SetWindowPOS", see what effect appears in your form? 2. How to block the Ctrl_Alt_Del, Ctrl_esc, and Alt_Tab, Alt_Tab, Alt_Tab, Alt_Tab, Alt_Tab, and Alt_Tab, Alt_Tab, and Alt_Tab. First create a new project; add a form and a module in this project; drag and drop two buttons on the form, name "cmddisable", "cmdenable", "copy"; COPY is in the following code into the module: Public Declare Function SystemParametersInfo LIB "user32" Ahias "SystemParametersInfoA" (ByVal uAction as Long, ByVal uParam as Long, lpvParam as Any, ByVal fuWinIni as Long) aS LongPublic Const SPI_SCREENSAVERRUNNING = 97 Copy the following code in the form of the code compilation: 'so that the three groups hotkey failure Private Sub cmdDisable_click () SystemParametersInfoSPI_SCREENSAVERRUNNING, True, byVal 1 &, 0End SubPrivate Sub Form_Unload (Cancel As Integer) 'before the program exits the hotkey is effective CndEnable_ClickEnd that your screen saver Sub If the combination of this function and the screen saver together, Many have been added. The simple call example of the API function is so easy. I believe that you now call the API's call no longer feel mysterious. Let's take a look at a more complex application. Section III: Examples of the Admission About the API on the API are just to take you to the WIN DOWS API world to explore. I believe that you have explored a little eyebrow and want to achieve something more "fun". it is good! Let you introduce you to a very "fun" at the same below, will make your program look more professional an API call. I believe that there must be "Jinshan Word" on your machine, try to start it you found? It was "gone" after the startup screen. Move the mouse to the lower right corner of the desktop, "hidden in" Windows tray "in the form of an icon. Right click in the mouse to pop up a menu function item for you to choose.
Now you have to put your own procedure to the tray so your program has a professional level! Below is the implementation step of this feature: 1. Here we call the API function is: "shell_notifyicon", add the following function declaration and constant declaration in your module: 'The following constants tell the system what happened on your icon in your icon' Constant statement public const wm_mousemove = & H200 'move the mouse on the icon Public Const WM_LBUTTONDOWN = & H201' mouse button pressed Public Const WM_LBUTTONUP = & H202 'left mouse button is released Public Const WM_LBUTTONDBLCLK = & H203' double click Public Const WM_RBUTTONDOWN = & H204 'right mouse down Public Const WM_RBUTTONUP = & H205 'the right mouse button is released Public Const WM_RBUTTONDBLCLK = & H206' double-click the right mouse button Public Const WM_SETHOTKEY = & H32 'response hotkey you define' API function declaration Public declare function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long , LPDATA AS NOTIFYICONDATA) AS long 'Customization A call API shell_notifyicon To use the type "Notifyicondata" PUBLIC TYPE NOTIFYICONDATACDSI As long HWND AS long "The name of your application form UID As long' app The ID number of the program icon resource is used to make those parameters. It is the combination of 'nif_message, nif_icon, nif_tip, three groups of Nif_tip, and the combination UCALLBACKMESSAGE AS long' mouse to send this message Hicon. As long 'icon Handle SZTIP AS STRING * 64' When the mouse is displayed on the icon, the Tip text end type 'is an enumeration type it tells API shell_notifyicon what to do what to do PUBLIC ENUM ENM_NIM_SHELLNIM_ADD = & H40' in "Gold Disc" Add a icon nim_modify = & h1 'Modify the icon nim_delete = & h2' in "Gold Disc" NIM_DELETE = & H2 'in "Gold Disc" Nif_Message = & H1' Make UcallbackMessage in Type "Notifyicondata" Make Type "Notifyicondata" 'szTip effective WM_MOUSEMOVE that the type "NOTIFYICONDATA" in = & H200' in hIcon effective NIF_TIP = & H4 moving the mouse message is valid End Enum 'defines a "NOTIFYICONDATA" type variable Public nidProgramData As NOTIFYICONDATA upper function and the constant declaration and custom One type variable, the following is the call method of this API function: