Take advantage of the Windows API extension Delphi function

zhaozj2021-02-11  194

Borland Delphi is a visual development tool similar to Visual Basic, but its function is more powerful than VB. For example, Delphi supports assembly language and pointer operations, fully supports the use of Windows API functions and external DLLs, making it a broader application range. Even if used to prepare some smaller applications, Delphi has no phase compared to VB, but because Delphi is a compile type language, its compiled EXE file can be truly out of Delphi's environment, and VB is generated The exe file leaves a lot of DLLs, VBX, and other files that leave the VB package may not be executed. More importantly, general users often don't really know which DLL-based files have been developed by the VB program they have developed. In order to ensure the normal operation of the program, it may contain a lot of facts in the final installation disc. document. Therefore, general users use VB difficult to develop real practical software, no wonder that VB is "first sweet and bitter". Delphi can actually be regarded as VP (Visual Pascal), so it can be comparable to VB. This year, Delphi2.0 can develop 32-bit programs for Windows 95, Window S NT, making the advantage of VB 4.0 no longer exist. It seems that more people will develop Windows applications in the future. People developing software under DOS knows that INT 2LH is important. Similar to this, the Windows application is also inseparable from the Windows API, many of the next level of operation must be implemented by the API function. To this end, both C , VB or Delp Hi has no exception to support the call of the API function, only VB is limited by itself, only partial API functions, while C , Delphi fully supports the API function. In contrast, Delphi is the simplest call to the API function, reaching the degree of hardness with its own internal functions (as needed to add Windows in the User segment in Interfaces, and this step Delphi will automatically You do), VB is quite cumbersome. Taking the API function of the WINDOWS system subdirectory as an example, let's take a look at the difference: API function format Description: Word getSystemDirectory (lpbuffer, nsize) VB 4.0 Call first, you must declare: Declare Function GetSystemDirectory LIB "kernel32" (ByVal lpBuffer as String, ByValnSize as Long) as Long before using: Dim lpBuffer as String * 255 Size & = GetSystemDirectory (lpBuffer, 255) and Delphi just like calling an internal function as: var lpBuffer: PChar; {API The function must use the string of NULL} Size: Word; {Returned system subdirectory string actual length} begin lpbuffer: = stralloc (255); {Reserved string length} size: = GetSystemDirectory (lpbuffer, 255); This can be seen that Delphi can make full use of the Windows API to extend its own function. Of course, in most cases, the function function of Delphi itself is sufficiently used. However, in some cases, you need to use the API function to implement some special features or improve some of Delphi. Two instances are provided below to illustrate this. First, the information box function MSGBOX information box is a dialogue mechanism that is used in Windows applications, which are widely used for prompts, selecting program tours, is an important program control. The information box function of Delphi is stronger. The maximum advantage is that the button can be set to set the information box (using a collection type), but its disadvantage is also very obvious, mainly in the title string cannot be set by the user; Silent silence when the window pops up; the button cannot display different texts with the version of Windows's different languages ​​(eg, "OK", "Yes", such as "OK", "Yes") in different languages.

These advantages and disadvantages are because it is Delphi you implement, rather than by calling the system API function, so you can't use the system's language environment, multimedia and other resources. In this way, the developed program is very uncoordinated with the surrounding window when the information is displayed in the Chinese version. And VB is well done in this regard, so we customize a message frame function MSGBox in accordance with VB's format. The following functions are assumed to be defined in TFORML. To use convenience, we first define some symbol constants in the interfaces segment: const {first group: button content selection} Okonly = 0; {"OK" button} okcancel = 1; {Show "OK" and "Cancel" Buttons} ABORTRETRYIGNORE = 2; {"Abort" "Retry" "Abandon"} yesnocancel = 3; {"Yes" "No" "Cancel"} yesno = 4; {"Yes" and "No"} Retrycancel = 5; {"Retry" "Cancel"} {second group: display icon selection} critical = 16; {"stop" icon} Question = 32; "?" Icon} eXCALAMATION = 48; {"!" Icon} information = 64; {"I" icon} {third group: default pointer position (activation status)} defaultButton1 = 0; {1 button} defaultButton2 = 256 {second button} defaultButton3 = 512 {third button} {fourth Group: Information Mode} ApplicationModal = 0; {Application Method} SystemModal = 4096; {System Mode} Then, establish a function MSGBox, because the string used in the API function must end with MULL, so another custom function is used STRTOPCH to convert the PASCAL type string into a string ending with MULL. Parameter Description MSG is the message box title variable; MbType is a variable for the specified information box type, how is the "MBTYPE = button content icon default pointer information box mode". If you want to get a message containing "OK" and "Cancel", with "?" Icon, the default pointer points to the second button (ie "cancel"), then set this setting: MbType: = Okcancel Question DefaultButton2; Title Displays the variable of the text in the information box. The return value of MSGBOX is exactly the same as the return value of the MessageDlg function, for example, returns MRYES indicates that "YES" or "Yes" button is pressed, and mRNO means "no" or "No" button is pressed. Refer to the MessageDLG function.

Below is the code of the MSGBox function in Delphi by calling the getActive Windows function and the Messagebox function of the Windows API: {message Function} Function TFORM1.MSGBOX (MSG: String; MbType: Word; Title: Stri Ng): Word; var hWnd: HWND; 1pText, pCaption: Pchar; begin 1pText:! = StrToPch (title); 1pCaption = StrToPch (msg); hWnd: = GetActiveWindow (); MsgBox: = MessageBox (hWnd, 1pText, 1pCaption, mbType); end {Convert the PASCAL string into a NULL end string function} function tform1.strtopch (str: string): pchar; var A: pchar; begin A: = stralloc (length (str) 1); strpcopy (A, STR) STRTOPCH: = a;

转载请注明原文地址:https://www.9cbs.com/read-4219.html

New Post(0)