Second lesson: handle, rectangle and drawpoint function
First, handle
Today, I will tell you about the API is substantive content. We start from the "handle". As long as you come to the world of API, one of the frequent problems that often encounters is the handle. So what is the handle? If you have never heard of the word "handle", you may first feel a lot of content in your handle. In fact, the so-called handle is actually a data and is a long (long) data. In the API, it is often transmitted to various API functions as a parameter. Such as: Public Declare Function GetWindow & Lib "User32" (BYVAL WCMD As Long)
Among them, hwnd is a handle. In VB, the handle is an attribute that you open the object of the object in VB Take a look at the FORM form or the PictureBox control, etc. Is there a hWnd property. There is. The explanation in VB is such a: Microsoft Windows running environment, identifies them by assigning a handle (or hwnd) to each form and control in the application. The HWnd property is used for Windows API calls. Many Windows running environment functions require the HWnd of the active window as a parameter.
If you want to know your handle, I can tell you that the handle is a pointer to the pointer. We know that the so-called pointer is a memory address. After the application is started, the objects that make up this program are reside. If you simply understand, it seems that we can use this address to access the object at any time as you know the first address of this memory. However, if you really think, then you have a big mistake. We know that Windows is an operating system based on virtual memory. In this system environment, Windows Memory Manager often comes back and forth in memory, which meets the memory needs of various applications. Objects are moved means that its address changes. If the address is always so changed, where do we go to the object?
In order to solve this problem, the Windows operating system makes some internal storage addresses for each application, which is used to register the address changes in the memory, and this address (position of the storage unit) is constant. The Windows Memory Manager informs the object's new address to save the object after moving the object in the memory. This way we only need to remember this handle address, you can indirectly know which location in the memory is specifically in memory. This address is assigned by the system when the object is loaded (LOAD), and when the system is uninstalled (unload) is released to the system. Handle Address (Stable) → Repends on the address in memory - ─ → Object in memory (unstable) → actual object
However, it must be noted that the program is started from the new startup, the system cannot guarantee the handle allocated to this program or the original handle, and most of the cases are different. If we see movies to see movies into the startup operation of an application, the handle to allocate the application is always different, this is always the same as the tickets sold by each cinema. . Get the handle of an object in VB is very simple. To get the handle of the FORM1 form, you can write: form1.hwnd
The handle of the object can also be obtained through the API function, such as:
GetActiveWindow Returns the window handle GetFocus with input focus to get the window handle of the current thread to add the mouse input window handle GetCursor get the handle of the current pointer getWindow from the thread in the front desk. GetWindow get the handle of the current pointer getWindow The handle of a window, which has a specific relationship with a certain source window. "The above function description can be found in the WinApi.hlp file. "
This tutorial provides a demo routine -Play1.vbp, which is to illustrate specific usage of these functions. After running, do something you want to do with the mouse, and observe changes in the data data. Through this program, pay attention to observe the following points: 1, within the thread and the thread. (VB does not support multi-threaded). Other applications are outside the thread. 2, under the Windows95 operating system, each form (including some controls, such as text boxes, picture frames, etc., Microsort are all collectively referred to as forms) have their own mouse pointers. This is very different from the same pointers in various applications under Win16. 3. Each time you start from the new start, the handle of each form varies. During the loading and uninstall of Text5, the handle is always changing. This shows that the phenomenon in the cinema tickets mentioned above is true. There are still a lot of functions that get the object handle, and then introduce them to everyone when you encounter them.
Second, the driver handle
As long as you find what is a handle, especially the window handle, then manipulate an object is more than much. For example, we can easily get the title of a particular window via the getWindowText API function. GetWindowText statement in VB as follows: Private Declare Function GetWindowText & Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As a result, we can get the window title Form1 by the following piece of code . (Create a new project, add a command button, copy the following code, and the above function declaration. You can run)
Private Sub Command1_Click () Dim dl As Long Dim FormCaption As String FormCaption = Space (128) dl & = GetWindowText (Form1.hwnd, FormCaption, 128) Print FormCaptionEnd Sub Note: wherein FormCaption 128 refers to the length of the string variable (also called a buffer Area size)
Is this necessary? In order to get the title of the FORM1 form, why should you write so much code? Is this an API. Yes, it is indeed a line of code in VB with Print Form1.caption. However, if we launched our design application, let's show our app when you use a mouse click, let our app show the title of that form. What should I do? Moreover, we say we In addition, the MICORSOFT WORD is launched. When you click on Word, let the program displays the words "You Select Microsoft Word". Obviously, only VB is not available, but also rely on the API older.
Of course, you have this ability to do this. Let us come together. The key issue is how to get the handle of the Word program. First of all, it is to be recognized that Word is a process external application for VB applications. As you have already thought, we can get its handle with the getForegroundWindow API function used by the previous model. Because, when we click on Word with a mouse, Word will become a front event form. Next? Of course, the getWindowText function shows its title. We can use the Timer control to complete all. I don't have to say more about the rest. The Program2.vbp program comes with this tutorial is preparing for those who are too lazy to write these lines of code, I hope you are not. If you haven't written an API app yet, you can say this is a good chance, or you can move your hand, it will be good. The program is running normally? Ha, this feels the charm of the API? I still want to continue learning!
Third, rectangular and point
With regard to rectangles and points, we briefly mentioned in the previous class. Here, you will do a detailed introduction. First start from the simple point (Point) structure. The structure is as follows: Type Pointapi X as long y as longend type in English, call Point, then why not call Point, and call PointApi? It turns out that there is a Point method in VB, this is just for unnecessary conflicts or heavy name. Point is used to describe a location, of course, a point of position. In the screen coordinates, X refers to the distance from the left side to the specified point, y refers to the distance from the top boundary to the specified point. In addition to the first time, there is not much learning, or the old saying - keep in mind.
The structure and point structure of the rectangle is not to be described, but it is described in two points. Its structure is defined as follows as the following public Type Rect
Left as long top as longund as long bottom as longend Type The following figure describes the meaning of each field of the Rect structure. It can be seen that a rectangular area is described by one point of one point and the lower right corner of the upper left corner of the rectangle. The LEFT and TOP fields describe the position of the massive first corner, the Right and the Bottom field describe the position of the second angle of the rectangle, that is, the lower right corner. In VB, when a rectangular position in which a form or control is described, it is often described in LEFT, TOP, and Width, Height. Among them, Width is a width of a giant area, and Height is height. Here, you should see that this RECT structure is not the case. For example, if you want to get the width, you must lose LEFT from RIGHT.
There are several functions in the API to process rectangular data structures, such as the following table: