API Tips in VB

zhaozj2021-02-17  51

The skill of the API should be adequately used in VB in VB, and count countless VB enthusiasts. The following is an instance of dozens of API functions in VB for several years, and now write it with everyone, I hope to help everyone. 1, how to make the form in the forefront? * API function declaration Declare Function setWindowPos lib "user32" (Byval Hwnd As Long, Byval X as long, Byval Y, Byval CX As long, Byval Cy As Long, BYVAL WFLAGS AS long : constant declaration Private Const SWP_NOSIZE = & H1 Private Const SWP_NOMOVE = & H2 Private Const HWND_TOPMOST = -1 Private Const HWND_NOTOPMOST = -2 Note: in a form to write: SetWindowPos me.hWnd, WND_TOPMOST, 0,0,0,0, SWP_NOMOVE Note: Or below SETWINDOWPOS ME.HWND, WND_TOPMOST, 0, 0, 0, 0, SWP_NOSize 2, use the API function sendMessage to obtain the rows and columns in the cursor.

Sub getCaretpos (Byval TextWnd &, Lineno &, Colno &) Note: TextWnd is the hWnd property value of TextBox, LINENO is the number of rows, colno is column number DIM I &, J &, K & Note: Get the starting position to the cursor location Number I = SendMessage (TextHWnd, & HB0 &, 0, 0) J = I / 2 ^ 16 Note: Determine LINENO = SendMessage (TexthWnd, & HC9 &, J, 0) 1 Note: Determine the listed K = SendMessage (TexthWnd, & HBB &, -1,0) COLNO = J-K 1 End Sub 3, how to populate a certain area in some color? * API function declaration Private Declare Sub Floodfill LIB "GDI32" _ (Byval HDC As Long, Byval X As Long, Byval Y AS _ long, byval crcolor as long Note: Set (Fillx, Filly) Taken to this area: color is a color FloodFill Picture1.hDC, fillx, filly, color 4, how to turn off the computer * API function declaration declare function ExitWindows Lib "User" (ByVal dwReturnCode as Long, ByVal wReserved as Integer) as Integer Note:? perform Dim DUMMY ? DUMMY = ExitWindows (0,0) 5, how to obtain the Windows directory and System directory Note: copy the following code into a module Public Declare Function GetWindowsDirectory Lib "kernel32" Alias ​​"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Public Declare Function GetSystemDirectory Lib "kernel32" Alias ​​"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long NOTE: call Dim WindowsDirectory As String in the program, SystemDirectory As String, x As Long WindowsDirectory = Space (255 ) SystemDirectory = Space (255) x = getWindowsDirectory (WindowsDirectory, 255) X = getSystemDirectory (SystemDirectory, 255) Msgbox "The installation directory of Windows is:" WindowsDirectory "

The system directory is: " SystemDirectory 6, how to build a simple hyperlink * API function declaration Private Declare Function ShellExecute Lib?" Shell32.dll "Alias" ShellExecute A "(ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, byval lpparameters as string, byval lpdirectory as string, byval nshowcmd a s long) AS Long Note: Open a URL Shellexecute 0, "Open", "http://tyvb.126.com", vbnullstring, vbnullstring, 3 Note: Give a mailbox Email Shellexecute Hwnd, "Open", "Mailto: SST95@21cn.com", VBnullString, VBnullString, 0 7, how to learn all the rows of text in textbox? * API function declaration Declare Function SendMessage Lib "user32" Alias ​​"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Const EM_GETLINECOUNT = & HBA Notes: call LineCnt = SendMessage (ctl.hwnd in the program, EM_GETLINECUNT, 0, 0) Note: Linecnt is the number of rows of TEXTBOX. 8. How to set the horizontal rolling axis of ListBox? * API function declaration const lb_sethorizontalExtent = & H194 Private Declare Function SendMessage LIB "User32" Alias ​​"SendMessagea" _ (Byval Hwnd As Long, Byval WMSG As Long, BYVAL WPARAM AS Long, _ LPARAM AS Any) AS Long Note: Call Call SendMessage (List1.hwnd, LB_SethORIZONTALEXTENT, 400, BYVAL 0 &) Note: Note 400 is in pixels, you can set it according to the situation. 9, how to exchange mouse buttons? * API function declares Declare Function SwapMouseButton & lib "user32" _ (BYVAL BSWAP As long) To exchange mouse buttons, set the BSWAP parameter to True. To restore normal settings, set BSWAP to false. Then call the function to exchange and restore the mouse button.

10. How to make the form of the form of the form flash to cause users to pay attention? Place a Timer Control Timer1 in the form, set its inteval = 200 * API function declaration Private Declare Function Flashwindow lib "user32" (Byval Binvert as long) AS long Note: Write as follows in the form : Private sub timer1_timer () FlashWindow Me.hWnd, True End Sub 11, how to find the XY coordinate of the mouse pointer? * API function declaration TYPE POINTAPI X AS long y as long end Type Declare function getCursorpos lib "user32" (LPPOINT AS POINTAPI) AS Long Call: getCursorpos Z Print Z.x Print Z.Y 12, how to get and change the time interval for double-click the mouse? Obtaining double click interval: Public Declare Function GetDoubleClickTime Lib "user32" Alias ​​_ "GetDoubleClickTime" () As Long obtained double click interval: Declare Function SetDoubleClickTime Lib "user32" Alias ​​"SetDoubleClickTime" (ByVal wCount As Long) As Long NOTE: Note: This change will affect the two functions of the entire operating system, which can be accurate to millisecond.

13. How to open and turn off the light-drive gate in the program? * API function declaration as follows: Private Declare Function mciSendString Lib "winmm.dll" Alias ​​"mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long NOTE: The following code when calling DIM RET As Long Dim Retstr AS String Note: Open the Light Driven Ret = McIndString ("Set CDAUDIO DOOR OPEN", RETSTR, 0, 0) Note: Turn off the light-driven RET = McIndString ("Set CDAudio Door Closed", Retstr, 0, 0) 14 how to get Windows startup mode to add a CommandButton to Form1, a Label, and add the following code:? Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex as Long) as Long Const SM_CLEANBOOT = 67 Private Sub Command1_Click () Select Case getSystemMetrics (SM_CLEANBOOT) Case 1 Label1 = "security mode." Case 2 Label1 = "supports network security mode." Case Else Label1 = "Windows runs in normal mode." End Select End Sub 15, how to make Ctrl-Alt- DELETE is invalid? * API function declaration Private Declare Function SystemParametersInfo Lib "user32" Alias ​​"SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long write the following function: Sub DisableCtrlAltDelete (bDisabled As Boolean) DIM X As Long X = SystemParametersInfo (97, BDISABLED, CSTR (1), 0) End Sub makes Ctrl-alt-delete (TRUE) Restore Ctrl-Alt-delete (false) 16, how to move Without the window of the title bar? We generally use the mouse to press the title bar of the window, then move the window, when the window does not have the title bar,

We can use the following methods to move a window: * API function declaration: Declare Function ReleaseCapture Lib "user32" () As Long Declare Function SendMessage Lib "user32" Alias ​​"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Const HTCAPTION = 2 Public Const WM_NCLBUTTONDOWN = & HA1 in Form_MouseDown event: Private Sub Form_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single) ReleaseCapture SendMessage hwnd, WM_NCLBUTTONDOWN, How to use a delay function in HTCAPTION, 0 & END SUB 17, VB? * API function declaration: Declare Sub Sleep LIB "Keer Sub Sleep LIB" Kernel 32 "Call: Note: Delay 1 Second Call Sleep (1000) 18, call to modify the window protection password: Private Declare Function PWDChangePassword lib" MPR "Alias "PwdChangePasswordA" (ByVal lpcRegkeyname As String, ByVal hwnd As Long, ByVal uiReserved1 As Long, ByVal uiReserved2 As Long) As Long call: call PwdChangePassword ( "SCRSAVE", Me.hwnd, 0, 0) 19, the Windows start screen Saver : * API function declaration Private declare function SendMessage Lib "user32" Alias ​​"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Const WM_SYSCOMMAND = & H112 & Const SC_SCREENSAVE = & HF140 & Notes: Call Dim Result As Long Result = SendMessage (Form1.hWnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0 & 20 &) 20. How to change the Windows desktop background? * API function declaration const spi_setdeskwallpaper = 20 const spif_updateinifile = &

H1 Declare Function SystemParametersInfo Lib "user32" Alias ​​"SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long NOTE: Call Call SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, "C: windowsClouds. BMP ", SPIF_UPDATEINIFILE 21, how to determine if the system has a sound card installed? * API function declaration: Declare Function WaveoutGetNumdevs LIB "Winmm.dll" Alias ​​"WaveoutGetNumdevs" () As long code is as follows: DIM i as integer i = WaveoutGetNumdevs () IF i> 0 Then Msgbox "Your system can play sound.", vbinformation, "Sound Card Detection" Else Msgbox "Your system cannot play sound.

", vbinformation," Sound Card Detection "end if 22, how to find the disk number of the CD-ROM drive? The following function will check if all the drives of your computer see if it is a CD-ROM, if you return the drive number, if you don't return null character Public Function GetCDROMDrive () As String Dim lType As Long, i As Integer, tmpDrive as String, found as Boolean On Error GoTo errL For i = 0 To 25 tmpDrive = Chr (65 i) & ":" lType = GetDriveType (tmpDrive) Notes: Win32 API function If (lType = DRIVE_CDROM) Then Notes: Win32 API constants found = True Exit For End If Next If Not found Then tmpDrive = "" BI_GetCDROMDrive = tmpDrive exit function errL: msgbox error $ End function 23, how to file into the recycle bin? ** API function declaration Public Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As Long End Type Public declare function SHFileOperation Lib _ "shell32 .dll "alias" SHFIL eOperationA "(lpFileOp As SHFILEOPSTRUCT) As Long Public Const FO_DELETE = & H3 Public Const FOF_ALLOWUNDO = & H40 NOTE: call Dim SHop As SHFILEOPSTRUCT, strFile as string With SHop .wFunc = FO_DELETE .pFrom = strFile Chr (0) .fFlags = FOF_ALLOWUNDO End How does WITH 24, VB use un installed fonts? Declare Function AddFontResource LIB "GDI32" Alias ​​"AddFontResourceA" (Byval LPFILENAME AS STRING) AS Long Declare Function RemoveFontResource LIB "GDI32" Alias ​​"RemoveFontResourcea"

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

New Post(0)