VB Experience Tips (1)

zhaozj2021-02-11  196

How do I close a running program when I use it? How do I determine if my program is running? How to delay a VB program? How to change the time interval of double-hit the mouse? How to find the XY coordinate of the mouse pointer? How to capture the mouse of the form? How to make my program always in the front of the screen? (ALWAYS ON TOP how to get the number of lines in text box (TextBox)? How to make the program's title strip flashes?

In programming, we often meet such or such problems. Some VB experience skills, and the translation of them, I hope that you will help you, please refer to you, thank you!

The time you record Windows sometimes you need to write down the time that Windows is turned on and off. You can complete this feature, you can put it in the "Start" folder of the Windows Start menu, so when you When entering Windows, this applet will automatically start (invisible), and write down the time in your specified file. When you launch the Windows system, the applet will turn off and write down the time, and turn the record file. Private Sub Form_Load () Left = -10000 TOP = -10000 open "c: /apps/log.txt" for append as # 1 Print # 1, "ON:" & cstr (now) Close # 1 End Sub Private Sub Form_Unload (Cancel As INTEGER) Open "C: /APPS/log.txt" for append as # 1 print # 1, "OFF:" & cstr (now) close # 1 End Sub

How to close a running program You can use the API function FindWindow and PostMessage to find the specified window and close it. The following example teaches you how to find and turn off a CAPTION for the "Caluclator" program. Dim winHwnd As Long Dim RetVal As Long winHwnd = FindWindow (vbNullString, "Calculator") Debug.Print winHwnd If winHwnd <> 0 Then RetVal = PostMessage (winHwnd, WM_CLOSE, 0 &, 0 &) If RetVal = 0 Then MsgBox "message into Error! "End if else msgbox" Calculator is not open! "Endiff In order to work above the code, you must in the module file what the following API functions: declare function findwindow lib" user32 "alias _" FindWindowa "(Byval LpClassName As String , _ ByVal lpWindowName As String) As Long Declare Function PostMessage Lib "user32" Alias ​​_ "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long Public Const WM_CLOSE = & H10 I How to determine if my program is running? Place the following code in the form_load event of the first form: if app.previnstance = true kilobox ("This program is running!", _ Vbexclamation) end Endiff

How to delay a VB program? The delay is very meaningful in VB! For example, sometimes you need to wait for an additional process to complete the code below. The delay enables the program to get rid of the influence of the CPU's calculation speed on the speed of the program, but there is no delay in the VB has an existing function, so rely on the API function, please see the following code: Declare Sub Sleep LIB " Kernel32 "_ (Byval dwmilliseconds as long) 'Delay 1 second Call Sleep (1000)

How to change the time interval of double-hit the mouse? Click two mice in a short time, causing the mouse to double-click event. You can call the API function setDoubleClicktime to change the mouse to double-click the time you need, it only has a parameter and can be accurate to millisecond. Declare function setDoubleClicktime _ lib "user32" AS Long Tip: This change will affect the entire operating system.

How to find the XY coordinate of the mouse pointer? There is a small area in many mapping software to display the cursor position on the current screen, which utilizes the API function very easy to do, the following example will demonstrate how to use the code how to return the coordinate value of the current cursor. Steps:

Create a new project file in VB5, Form1 uses the default settings. Select "Project / Add Module" for the menu to create a new module file "moudule1". Enter the following code to declare the API function. Option Explicit Type PointApi 'Declare Types X As long y as long end type declare function getcursorpos lib "user32" _ (LPPOINT AS POINTAPI) AS Long' Declare API moves focus to Form1, add two label objects (Label) and a timing Object (Timer1), set the interval property of the timer to 1, then double-click any area of ​​Form1, enter: Option Explicit Dim Z as pointapi 'declared Variable Private sub timer1_timer () getCursorpos Z' Get coordinate label1 = "x:" & zx 'gets X coordinate label2 = "Y:" & zy' to get Y coordinate end sub 5, press F5 running program, mobile mouse attention to observe changes in two label objects. How to capture the mouse of the form? This tip will show you how to use the API function of the capture cursor to prevent the mouse pointer from moving out of the form. Pay attention! : If the Border attribute of the form is set to Sizeable (2 or 5), you will "escape" the monitor of the "Escape" program when you change the overall mouse. So you'd better set BorderStyle to 0, 1, 3 or 4. Steps:

Add the following code module of: Option Explicit Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Declare Function ClipCursor Lib "user32" _ (lpRect As Any) As Long Public Sub DisableTrap (CurForm As Form) Dim erg As long 'declaration process variable' Set new coordinate DIM newRect as reference curform.caption = "Release Mouse" with newRect .Left = 0 & .top = 0 & .right = Screen.width / Screen.twipsPerpixelx .bottom = Screen.Height / Screen .TwipsPerPixelY End With erg & = ClipCursor (NewRect) End Sub Public Sub EnableTrap (CurForm As form) Dim x As Long, y As Long, erg As Long 'process variable declaration' provided new coordinates Dim NewRect As RECT 'obtained TwipsperPixel' form Scalemode must be set to twips! ! ! x & = Screen.TwipsPerPixelX y & = Screen.TwipsPerPixelY CurForm.Caption = "Mouse Capture" 'cursor setting range With NewRect .Left = CurForm.Left / x & .Top = CurForm.Top / y & .Right = .Left CurForm.Width / x & .bottom = .top curform.Height / Y & End With Erg & = ClipCursor (NewRect) End Sub 2 Add two command buttons to the form (Command Button). 3. Add the following code as Form1. Private Sub Command1_Click () EnableTrap Form1 End Sub Private Sub Command2_click () DisableTrap Form1 End Sub Private Sub Forml AS Integer 'The mouse is released at the end of the program. Disabletrap Form1 End Sub

How to make my program always in the front of the screen? (ALWAYS ON TOP) If you want your program to be in front, you can use the following code: Form1.zorder mating timer, use this method every time a small time interval can make the form FORM1 in front of the screen, but The user may still make other forms over the form of Form1 in a short period of time. So this method does not enable the form to realize the Always on Top, and the true always on top can use the API function setWindowPos, the code is as follows: 'Declaration function: Declare function setWindowPos lib "user32" _ (byval h%, byval hb%, ByVal x%, ByVal y%, _ ByVal cx%, ByVal cy%, ByVal f%) As Integer Global Const SWP_NOMOVE = 2 Global Const SWP_NOSIZE = 1 Global Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE Global Const HWND_TOPMOST = -1 Global Const hwnd_notopmost = -2 'Put the form in the forefront: res% = setwindowpos (form1.hwnd, hwnd_topmost, _ 0, 0, 0, 0, flags), if res% = 0, generate errors' makes the form Restore normal mode: res% = setwindowpos (form1.hwnd, hwnd_notopmost, _ 0, 0, 0, 0, flags how to get text lines in text box (TextBox)? The number of rows entering the text in the calculation text box can be returned using the sendMessage function. When a line text occurs, it will be treated as a new line, and the number of lines in the text is not simple. Add the following API function to the General Declarations area of ​​the module file, if you are using VB4-32 or VB5, you can also add this declaration to the General Declarations of Form1, and replace all "public" "Private ". Option Explicit Public Declare Function SendMessageLong Lib _ "user32" Alias ​​"SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Public Const EM_GETLINECOUNT = & HBA Form Code Sub TEXT1_CHANGE () DIM LINECUNT AS Long On Local Error ResMe Next 'Get / Display Text Row Linecount = SendMessagelong (Text1.hwnd, Em_getLinecount, 0 &, 0 &) Label1 = Format $ (Linecount, "##, ###") END Sub Note: In order to make this program successfully, set the Multiline property of the text box to True during the design phase.

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

New Post(0)