How to improve the call Window API functions of programming skills: Gu Dai Hung as a programmer to keep making progress, we are often satisfied with the general functional programming languages offered, always hoping to achieve some special features, which sometimes is not only our program Requirements, sometimes the psychological needs of the programmer (tried, what is more exciting than solving a problem in late night?). VisualBasic, as a currently most popular fast programming environment, give us a platform for implementing these goals, in fact, its unlimited expansion API interface can implement all Windows that can be implemented under Windows. However, for beginners, "vast" API functions are always so magical and unable to figure out, so they cannot be flexible during the practical application. Below I will use two small examples to introduce the steps and features of the VB to call the API function, and summarize how to improve this skill in actual work. The example is compiled with VB6.0 under Windows 98. One. Limit mouse movement - Sometimes our software requires users that the mouse cannot move out of the current active control when not completing a task, and the other words are to limit the mouse to the inside of the control until the task is completed or the user is interrupt. This feature is very clear, and the code is also very concise with the API function. Its idea is to first determine the location of the current mouse; then determine the size of the current active control; finalize the mouse within the active control.
(The process of ignore the project establishment below) ---- 1. Create a new project: New form is form1, add a CommandButton on Form1, set its name = cmbutton1; caption = "Limit the mouse in this button" ---- 2. Copy API Functions and PointAPI Structure: Open the API browser with VB6.0, transfer to Win32API.txt file, copy the following structures and functions declare to Form1 declaration section: Private Type Rectleft As Longtop as LongRight As Longbottom As Longend TypePrivate Type POINTAPIx As Longy As LongEnd TypePrivate Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByValhwnd As Long, lpPoint As POINTAPI) As Long 'is used to determine the current mouse position Private Declare Function ClipCursor Lib "user32" Alias "ClipCursor" (lpRect As any) as long 'is used to limit the range of activities in the current mouse ---- 3. Defines a generic process ConfineToPublic Sub ConfineTo (myCtl As Object) On Error Resume NextDim tmpRect As RECTDim pt As POINTAPIWith myCtlIf TypeOf myCtl Is Screen Then 'locked within the screen tmpRect.Left = 0tmpRect.Top = 0tmpRect.Right = (.Width / Screen.TwipsPerPixelX) tmpRect.Bottom = (.Height / Screen.TwipsPerPixelY) Elseif TypeOf myCtl Is form 'locked in the range form tmpRect.Left = (.Left / Screen.TwipsPerPixelX) tmpRect.Top = (.Top / Screen. TwipsPerPixelY) tmpRect.Right = (.Left .Width) / Screen.TwipsPerPixelXtmpRect.Bottom = (.Top .Height) / Screen.TwipsPerPixelYelsept.X = 0pt.Y = 0Call ClientToScreen (.hWnd, pt) 'get the current control Location tmpRect.left = pt.x 'on the screen TMPRect.top = pt.ypt.x = .widthpt.y = .HeightCall ClientToscreen (.hWnd, Pt) TMPRect.bittom = Pt.ytmpRect.Right = pt.xend ifcall clipcursor (TMPRECT) End Withend Sub ---- 4. Add the following code in cMButton1_Click () Static Cliped as boolean 'static variable to control the status switch if not cliped thenfineto cmbutton1cliped = trueelseconeto screen' Cancel mouse limit CLIPED =
Trueend IF ---- 5. After running, click CMButton1, at which time the mouse can only be locked inside this button, click the button again, limit the cancellation. two. Create a temporary file --- Temporary files are used to save temporary changes during the software running, which is often encountered for people who are familiar with Word and other software. Then how the temporary file is generated, it is actually very simple, only one API function can be required. ---- 1. Copy function declaration (with the former) Private Declare Function GetTempFileName Lib "kernel32" _Alias "GetTempFileNameA" (ByVal lpszPath As String, _ByVal lpPrefixString As String, ByVal wUnique As Long, _ByVal lpTempFileName As String) As Long parameters are defined as follows: lpszPath = Chuan Enter the path to the temporary file, such as "c: / mytemp" lpprefixString = the first three letters starting with the temporary file name to help identify the role of the temporary file source. Wunique = 0, Windows randomly generates the file name; otherwise the security value defines the file name. lpTempFileName = Returns a random file name ---- 2. Return to the temporary file name Private Function GenTempName (sPath As String) Dim sPrefix As StringDim lUnique As LongDim sTempFileName As StringIf IsEmpty (sPath) Then sPath = "c: / temp" sPrefix = " TVB "lUnique = 0 'windows randomized to name sTempFileName = space $ (100) GetTempFileName sPath, sPrefix, lUnique, sTempFileNamesTempFileName = Mid $ (sTempFileName, 1, InStr (sTempFileName, Chr $ (0)) - 1)' remove excess spaces GENTEMPNAME = STEMPFILENAMEEND FUNCTION ---- 3. Add the following code to the Form1_Click () event msgbox gentempname ("c: / temp") ---- 4. Run, mouse click Form, pop-up MSGBox, display generate "c: /TEMP/TVB724.TMP" file, use File Manager View, there is a TVB724.TMP file under C: / Temp, with a length of 0k. ---- What to note is that the incoming path must be a valid path, otherwise the GENTEMPNAME function returns 0, and there is no temporary file name in lptempFileName. III. Conclusion ---- From the above, the API function is indeed simply and efficient, but it is also filled with some high-profile variables. We can't help but ask, when will I use the API function? What functions can I solve my problem? What are the variables in those functions? In fact, for these issues, even if a VB master is also difficult to give a clear answer, because the API function library is too large, everyone can only be familiar with a small part of them (this may also be an endless article of calling the API. Cause. Summarizing your own experience, I think it should be a few points.