Win32API lecture 4

xiaoxiao2021-03-06  40

Lesson 4: Mouse, Insert and System Functions

First, the pointer is cut

What is a pointer? I think everyone knows, there is no need to talk more. It is only clear that the pointer is a pointer, the mouse is a mouse, and the mouse controls the pointer. In WIN16, the pointer only one is running in the system to share this pointer. However, in Win32, each form has its own pointer. This is not said that there are several pointers on the screen, but each form has its own style and some characteristic pointers. When the pointer moves to a form, the pointer automatically turns into the pointer style of that form. Another point to the pointer needs to be recognized is that the position of the pointer is specified in pixel screen coordinates. this point is very important. Next, we entered the topic of this section.

As long as you are careful, you can find that the mouse pointer is generally unable to exceed the screen (Display Screen). But this is not absolute, you will understand it for a while. The pointer is generally controlled within the screen, which is the fact. From this fact we can know that the pointer is limited to a certain area. The process of limiting the activity of the pointer is called a pointer shear. There are two functions related to pointer shearing. One is a getClipCursor function that allows the coating area of ​​the current mouse pointer. This function has only one Rect structure parameter. After the function call is completed, the data of this RECT structure is loaded with the rectangular shear area of ​​the current mouse. If you call this function in general, you probably get the screen size.

Another function is that the SetClipCursor function is to set the pointer shear area of ​​the specified size. The pointer size to be set is transmitted in the data mounted in a RECT structure. These two functions are generally used. Use the getClipCursor function before setting the new pointer clip area to get the current pointer shear area to save its value, and then call the SetClipCursor function to set the new shear area. When this new shear area is no longer needed, send the previously saved area value to the setClipCursor function, and can be restored to the original pointer shear area. This tutorial provides this Program1.vbp demo. When I design this program, I forgot to restore the original pointer area, and the result pointer cannot be detached from the newly set frame (naturally, it will not be able to disappear the button), and have to use Ctrl Alt delete to force the VB designer. I will also become a experience.

Second, the pointer position function

The pointer position function, simple and pointer shear functions are similar. Windows also provides two functions for this, one is getCursorpos, and the other is setcursorpos. The GetCursorpos function has only one parameter to load a POINTAPI structure, which is description of the coordinates of the X and Y of the current pointer. However, the SetCursorpos function is different, it does not rely on the PointAPI structure, need to pass the two parameters of the X and Y of the pointer directly. The program2.vbp provided in this tutorial is existed to the use of these two functions to demonstrate automatic movement of a pointer.

The GetCursorpos function is relatively high in these two functions. As a wonderful example, I simplified my own No. 4 presentation into the program6.vbp of this tutorial. This program demonstrates how to drag the form where the title bar. The program also uses some window functions learned in the first few classrooms, as a good review material, welcome to read. In program2.vbp, a little mathematical knowledge used in the program is afraid that some friends are difficult to see (note: not learning rocky Yue rural), in this brief description. First, the program calculates the start position of the mouse pointer and the NEWPOINT. The pointer needs to move on the straight line of the two points. We know that if set (x, y) refers to a point on this straight line, then this linear equation can be written like this :y = ax b

Here, A and B are constants. If we know A and B, then as long as the X is increased by a certain amount of unit, then Y's value can be known. How is the problem? What is the A and B here? Solution is: set, the starting point coordinates of the mouse is: X1, Y1 mouse end point coordinates: x2, y2 :y1 = ax1 B 1Y2 = ax2 b 2 from 1 available :B = Y1-AX1 3 3) 2 can be obtained, Y2 = ax2 y1-ax1 4 from 4 available :a = (Y2-Y1) ÷ (x2-x1) to get the rest after A You can use 1 or 2 to obtain B. (This is actually the first year of the junior high school! But the first-year student may not necessarily know that it is straight. Analyze the geometry seems to be in the second grade of the high school. You can also use the analysis of the geometry.)

Third, other pointer functions

There are still the following related pointer functions

Function CreateCursor Create a pointer DestroyCursor on the basis of two bitmaps to clear a pointer, and release the system resource it occupies getCursor gets the handle of the current pointer getDoubleClicktime learns that two consecutive mouse clicks are viewed as double-click event. Time Length LoadCursor Generates a Mouse Simulation Resource Mouse_Event Generates a Mouse Simulation Event SetCursor Select the Pointer SetDoubleClickTime Set Two Continuous Click Double-click Time Length SetSystemCursor to change any System Standard Poincursor Control Pointer Vision SwapMouseButton switches the role of the left and right mouse buttons

Many functions written here are not too much. Of course, you just find these functions, then this may be a very discovered discovery for you. You have already seen that for some function functions, VB itself has already owned. In these various functions, I like the mouse_even function, it is necessary to introduce you to you. If you haven't seen Program2.vbp yet, it is best to start and run it. The purpose of the program is to move the movement of the mouse pointer to move to you "xing to you" this button. When the mouse pointer moves to this button, the button is not pressed. Of course, this program is not designed to press That but call the Command2_Click process directly. This is of course fake, caught people.

With the mouse_even function, we can replace the design ideas. That is, when the mouse pointer moves to "xing to you" button, do not call the Command2_Click process directly, but analog to generate a click operation of the button. Program3.vbp has moved this design idea to the actual program. A procedure has also been used in the program that hasn't learned API functions Sleep. This function is used to wait for a certain period of time and time in milliseconds. If you want to wait 20 milliseconds, you can write as: Sleep 20. Since it is often necessary, first talk about it here. Please open and run Program3.vbp, now whether the demonstration process is like a matter?

Fourth, about insert

As a resource, the plug is usually used to represent a location in the text editor to enter text. The appearance is generally a flickering line segment or a slot block. However, in fact, the insert can be any style. Its pattern can be processed by bitmap. However, since the bitmap is the later learning content, this tutorial does not give a demo. When you learn a bitmap, these are very relaxed. What you need to know now is an insert, which is created, how to fix the location, how to display, etc. to apply in your own program. Function Configuring CreateCaret Create and select an Insert DESTROYCARET Clear Insert GetCareTBlinkTime Getting Insert Flashing Frequency GetCareTPOS Understanding Inserts In Logical Coordinate Hide Inserts SetCareTblinkTime Setting Inserts Flashing Rate SetCareTPOS Set Insert in logic coordinates The position of the medium showcaret makes the inserts visible

In terms of reason, the insert function should be created and initialized in the GotFocus event of the control. In other words, when the control is focused. Unfortunately, the Gotfocus event is triggered after the control is lost in the control, and after the other control in the current application, the Gotfocus event will be triggered. You can start the Program4.vbp test. The specific method is that the application starts a VB designer after starting, so that the VB designer becomes an active form. At this time, the application window will not be seen behind the VB. After confirming that the flashing insert spectrum is seen in the VB designer, click the application task bar from the taskbar to make the application form a active window. At this time, the application form will jump from the VB designer to the front desk and display. At this time you can observe that the cursor of the Picture1 control above disappears. Clicking on the control with a mouse also has no response. Unless you click on the Text Control, then click the Picture control, the cursor will not appear. Of course, you can use a timer to detect the control of the control, but this method is obviously troublesome. For this problem I generally write a row code in the Click event of the control (see the program in the GotFocus event (see the program, and put the code Picture1_gotfocus in the private sub dressure1_click () event).

On the book, the insert should be clear in the Lostfocus event. But I think there is nothing necessary, which will produce some functions. For example, in the program4 provided by this tutorial, if the code DL & = DestroyCaret within the LostFocus event is valid, the result is not inserted when we click the Text control. So I think that you can create it when you want to use insert. I don't want to see it, I use the hidecaret function to hide. The presentation program Program4.vbp summarizes some commonly used insert functions.

V. Keyboard control functions

To use the keyboard control function, you must first recognize what is scanning code, what is a virtual key code. The so-called scan code is a code determined by the hardware of the computer keyboard, which can say that different types of keyboards have different standard scanks. If we use the scan code directly to design the program, it is probably a very boring thing, and it is difficult to ensure the compatibility of the program. Unlike the scan code, the virtual keys are the same for each computer. Here, the so-called key code is not other, just which data is used to indicate which button is issued. In terms of working principle, the computer keyboard sent to the computer scanning code, then the scan code from the keyboard will be converted to a virtual key code, who is going to do this conversion, we don't have to care. Finally, Windows converts virtual keys to ASCII codes or characters. As I saw, the key code delivered from the key of Visual Basic's keypressed is the ASCII code. In addition, the concept of Unicode character sets and ANSI character sets also needs to be mastered. Time relationship, the following directly put a wonderful discussion called "VB core technology" to everyone, the author is Bruce McKinney. If you want to learn now, please click here. Now, the key disk handler is listed to the following table, and everyone will first look at what functions have functions:

Keyboard processing API function

Function, ActivateKeyboardLayout, in the system-defined keyboard layout, select a list of enumsystemCodePages to get system-supported or installed all code pages GetAcp Get the status getasyncKeyState for the current Used ANSI code page specified when the virtual key is specified when the function call is specified. GetCPinfo get information GetKeyboardLayout judgment relating to a code page a specified thread keyboard layout GetkeyboardLayoutList get keyboard type GetKeyNameText present a list of keyboard layouts available GetKeyboardLayoutName get the name GetKeyboardState keyboard layout has made the current state 256 virtual keys GetKeyboardType determine the current use of To determine a specific virtual key, getKeyState obtains the status of the specified virtual key getoemcp to get the identifier of the OEM code page currently in use. KeyBD_Event generates a new keyboard layout loadKeyboardLayout loads a new keyboard layout MapVirtualKey in characters and virtual keys, The scan code and the ASCII code are converted between MapVirtualKeyex to convert the ASCII code to the OEM scan code setKeyboardState to convert the status of 256 virtual keys to Windows. TOASCIITOASCIIEX converts a scan code and SHIFT state into a corresponding ASCII code. Character TOUNICODE converts a scan code and SHIFT state into a corresponding Unicode character unloadKeyboardLayout uninstalls the keyboard layout of the LoadKeyboardLayout function loaded, VKKEYSCanex converts the ASCII code into a virtual key code and SHIFT key status.

I have seen many keyboard control functions. However, these functions are basically limited to virtual code and scanning code, that is, you can provide virtual code and scan code for these functions, which must be able to master and use. There is 256 virtual keys, it is difficult to give here, but I don't think it is necessary to give it a brief description. The virtual key code is started with "VK_", such as the Ctrl key for VK_Control, the Alt key is VK_MENU. As long as you open the API text! "VK_" is typed after selecting a constant (consts), then you can see a large pile of virtual keys starting with "VK_" from the list box. Another skill is that VB itself also has its own code. VK_Control is probably VBKEYCONTROL in VB, you remove the words "VBKEY", then change "VK_", it is estimated that the relevant virtual key name can also be found. (As if the value is also the same, if the value of VBKeyContol is 17 (decimal), and the value of VK_Control is & H11 (16). I don't have to guarantee, there is not much time, I am interested, I can Try it directly with VBKEYXXXXX.). As for the scan code, it can be converted from a virtual key code from a virtual key code. In order to help you understand the practical application of these functions, this tutorial comes with two routines, one is Program4.vbp, and the other is Program8.vbp. The previous program is transformed from one of my demo programs, the main demo button detection and setting shortcuts. This program may have another one. For example, I don't understand what the virtual key name of the "Print Screen Sysrq" button on the keyboard is. Of course, there is a virtual key code table, but just as the average person only knows the "Ctrl" key name without knowing "Control" key name (the two is actually the same), I can't find "Print Screen Sysrq" on the list. The virtual key name is. Later I started my program, press "Print Screen Sysrq", the program indicates that its virtual key value is equal to 40 (decimal), then take the calculator provided by Windows, and its 16-based data is & H2C. Ha, this is good, look at the list, the virtual key called "vk_snapshot" is "vk_snapshot", very interesting?

The second program is that I once answered a micrographer when I did. Unfortunately, the issue mentioned by this netizen is lost in my last hard disk failure. Probably this is like this, very like a question in the test volume: "There is only one text box in the form. After entering some text in the text box, click on the text editor program window that has been launched Word, Windows notebook, at this time The content in the text box is pasted directly into these editing pages, instead of pressing any other keys (such as Ctrl V). "My program is written as Word, to successfully run it in advance, you must start you must start Word Editor and open a new editing page.

Six, system functions

For system functions with Windows, I don't think there is no need to conduct special instructions, because these functions are not particularly particularly Just, these functions are mainly used to obtain and set the system information. For example, setting the desktop wallpaper, the default form, the command button, or what you are, or you can see more. As follows:

Letter number Description Beep the PC speaker sound ExitWindowsEx in various ways exit and restart an environmental component Environmental string block WINDOWSExpandEnvironmentStrings string FreeEnvironmentStrings release distributed by GetEnvironmentStrings of GetCommandLine get command line used to start the application GetCOmputerName get the current computer Name GetEnvironmentString gets a environment block that provides a separate environment variable GERLASTERROR to get a separate environment variable gerlasterror in the environment block get the extension error message of the previous API function call. In Visual Basic, use the LastDLLLERROR attribute of ERR object to get this value getSyscolor to get this value getSyscolor Setting GetSystemInfo gets the information related to the hardware platform GetSystemMetrics to determine a series of system style information; such as a menu bar or form header, vertical scrolling The height of the strip, the smallest size of the window, and the like. GetSystemPowerStatus obtain relevant information GetUserName the power supply and system status get the current user name GetVersion, Windows and DOS of GetVersionEx determine the running version MessageBeep generate a standard system sound SetComputerName set the computer name SetEnvironmentVariable set an environment block of environment variables SetSysColors set Windows The current color of the object SystemParametersInfo is a powerful function to obtain a wide range of system parameters such as protection programs, desktop wallpapers, keyboard delays, and repetition rates. Of course, from these functions, some functions are suddenly selected, and a presentation program (Program7.vbp) is also made. When analyzing the code, please master the usage of System_info and OsoSysteminfo structures.

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

New Post(0)