VB mouse drawing Basic Data summary

Source: Internet
Author: User
VB mouse drawing Basic Data summary

In view of the first half of the program to write a mouse, this will be collected before the summary of the data, to those who used to search the same as me everywhere VB enthusiasts a little convenient, and welcome exchanges and corrections.
Here I try to write wordy and wordy some, I hope you do not blame me. Because we have a lot of information is too theoretical, practical or didactic people how to use, too weak. I wish I could be a little bit more clear.
I am also a VB just getting started people, the limited experience to write out and share with you.

Objective
The mouse drawing can be said that every VB programmer would like to try and exercise a project. Learn the mouse to draw, you will learn the mouse operation of Windows, screen mode mapping mode, drawing mode, Windows GDI (Graphics device interface).
You will know that the computer's devices are limited, that is, resources are limited, the resources here, that is, memory, computer screen, mouse and keyboard, there are some GDI objects, such as brushes, brushes, color palette.
There are some resources that you can use (other apps here) that won't suffice, such as your app capturing the mouse (as for why to catch the mouse, after that), without releasing the mouse, So that other people's applications do not get mouse messages (people who do not understand the Windows Messaging mechanism should learn a bit about Windows messages and the message response mechanism.) The mouse drawing is not windows to the message to our program, and then have our program to deal with it ... , other people's apps have no way to operate with the mouse. So applications that monopolize system resources should be a programmer's effort to avoid. is also not allowed by the operating system. The operating system is a large program for dispensing applications, and it is also a management hardware, the underlying program that allocates resources, its level is higher than your program, so it can control your application. If you want to use system resources, you must request to the operating system and get the allocation before you can use it.
Mouse drawing, that is, on a canvas (the client area of your application), use the mouse to draw. It's a vector-mode graphic, and you can also convert to raster-mode images. Here, you have to understand is: what is the customer area, I believe many people have been programmed for a long time, do not know what is the customer area. What is the vector pattern of graphics, what is the raster pattern image, and so on related issues.
VB programming has an advantage, is the starting point is high, not like VC so what all want you to do, a little things will not, you will be stuck. Control drag and drop is how comfortable ah, every programmer should stand on the shoulders of others, write their own program.
I am in the process of writing a program, is on the basis of other people's procedures, through the improvement or modification, the use of synthesis and analysis of the basic ideas, the preparation of their own procedures. This is called "copycat". Of course, as a programmer, after using someone else's stuff, maintain basic respect for others, such as keeping other people's copyright notices (if any, or you know it); email him, tell him you used his stuff, ask him questions; In the program's dialog box, Express gratitude to those who have provided all kinds of help for free or paid, as well as to declare their copyrights, reserve their rights, give warnings and so on.

Next, I will collect my previous collection of information about the mouse drawing, and to the process of preparing their own experience to summarize.

First, give everyone a directory index.
1. Interface making
2, mouse function and operation
3, Customer Area, window, screen
4. Scrolling plot Area
5, screen mapping mode, drawing mode
, the principle of the rubber band frame
, GDI, and drawing functions
, brushes, brushes, fill mode
, drawing steps
, the preservation of the drawing results
Conclusion

Interface making
The creation of an interface is always annoying, and you may be able to write your code in one day, but your interface may consume you for one weeks. So here I mentioned the problem of interface making. Similarly, making a good interface can be done on the basis of others, or you can do it yourself (if you have time and patience). Since the making of the interface is not a text can be explained, so here I provide my own interface to everyone. It's simple, don't laugh at it. Also provide the source code, do not email me, because I may not have time to reply to your request for source code. Please login to my email: @.com, the password is:. After landing into the delivery box, in the mail address to write down your own mailbox, forwarded to your own mailbox can be. But please do not change the password, after all you are not a person to use.
My interface is shown below:

Mouse function and operation
The mouse generally has two keys, the middle key wheel can not be considered, because in the mouse drawing process almost not.
It is because there are two keys, so you need to move your brain and think about how to use them all. In the program, we use the Color tool, the left click to get the foreground color, right click to get the background color, good use of these two keys.
Its function is to click, drag, move.
You click on, may be to draw a point, how big, just look at your brush, paint the size of the brush. Drag, generally used to draw lines, rectangles, and ellipses. You move it to change the position of your drawing, draw the size of the graphic, or use the mouse to draw randomly.
The operation of the mouse is of course to see the intention of the user, click may be to choose, to draw, drag may be to select the range, drawing graphics; you have to do something else. So you are in the process of use, of course, do not want your mouse does not listen to you, you point, is nothing to point out, you release the mouse, but the program is still drawing (as long as you move the mouse). So we have to completely control their own mouse in the application, according to their own will to work, we have to judge the state of the mouse, is pressed, which key pressed, there is no movement, loosened up no, and so on, and then the correct response to the mouse message. However, because the drawing has a lot of tools, we want to set some variables to flag the tool we are using, so in response to the various messages of the mouse, according to the different tools, draw different effects. such as painting points, airbrush effects, color tools, filler tools, selection tools and so on.

The reason we talk about mouse operation is that every drawing is done in a certain step, and the message of each step is different. In my program, I think it should be in the mouse down the state of the drawing operation. For a line to draw, there are three steps. First, click (in response to mouse down, release the message) to determine a starting point, the second move to the destination (in response to the message of the mouse movement), and the third step to determine the end point (in response to mouse down, released message). It can be seen that in such a simple line operation, there is a response to several mouse messages. So after we have selected each drawing tool, we should consider in detail how to respond to the three basic messages (press, move, loosen) of the mouse in each step, which is a more important issue.

Mouse cursor Problem: I believe some people still have doubts, so here wordy. If you want to use a different Windows default cursor on a control on your window, do this: Mousepointer=vbcustom the control (its value is 99, which means it's user-defined, so you can also use mousepointer=99 in the program) And then change the icon of MouseIcon to your custom cursor file ("Icon ..." button next to the point, then select a cursor file). Of course, you can also respond to this control's MouseMove message to change:
Mousepointer=vbcustom
Mouseicon=loadpicture ("Cursor.cur")
At the same time don't forget to change back in form's MouseMove message, as long as this sentence is enough: mousepointer=vbdefault, otherwise, your form will always have only your own definition of the cursor.


Digression: If you have four mouse, how do you respond to your mouse message, Windows needs your own response? (personally think not, but may need mutually exclusive processing of each mouse message) Maybe one day, you might be drawing with someone else (maybe your lover), so you don't have to grab the mouse ...


client area, window, screen
In the VB program, generally use the PictureBox control to do the plot area. Because of the tool bar, status bar and other controls, as well as the drawing Toolbox, the existence of the color box, PictureBox generally does not fill all the customer area.
As I understand it personally, the client area is the part of the window that users can use freely. In addition to the title bar, menu, scroll bar, Border, toolbar, the status bar outside the area. In addition to these areas, because of how you draw in a drawing program, Windows does not let you draw the graphic to the top of these window sections. The same is true for word processing programs. Because these ranges are not part of the area that the user can freely manipulate, it is called the client area except for the parts of the window outside these areas. Sometimes, the client area may also include a toolbar, and the status bar occupies part of the area, this is because the general program in the toolbar and status bar occupied by the area, will be hidden between the two to users, so I think the client area should include the toolbar and the status bar occupy the part of the window, Because these are free to use for users, after all, there is no toolbar and status bar, we also use the program, and the area of the drawing larger.
Understanding the client area, the window is easy to understand, that is, your application takes up the part of the screen, and nothing in your window is drawn to another application's window. Other people's will not be drawn into your window. That means you can't see the contents of another application's window in the window of your application.
As for the screen, this doesn't have to be explained, you just use one screen (the display part of the display), you certainly can't see anything outside the screen. But if you have two monitors, you move the window to the edge of the screen and you see the part of the window that is not visible on the other screen. If you had three screens, HoHo, that would be awesome.!!! Wordy so much, the purpose is that system resources are limited, each window will occupy a part of the screen, customer area and each control (visible) will also occupy a part of the window. These things are going to run out (I didn't say that).

Scrolling plot Area
I said the plot area, in VB is the PictureBox control, but PictureBox does not support scrolling. The larger part of the picture is not displayed, so we have to let him scroll to show more than PictureBox size content. I don't know what the best way is, but after a few things, I think the best way is to get the job done. Sometimes, you may have wasted a lot of time trying to find an efficient, fast method, or a more technologically advanced method, and you haven't achieved your goal. And in this time, if you use the Stupidest method, perhaps already achieved your function.
Here, Chine is not advocated by me. After all, method skill is not an end, but a means to accomplish a task. I found a way to roll on the line.

Screen mapping mode, drawing mode
The screen mapping mode says the same thing: the measurement problem of the graph display. Your monitor may be a 17-inch flat color display (this 17-inch refers to the length of the diagonal), 17 inches is how many centimeters? 43.18 cm (1 inches equals 2.54 centimeters). So how many pixels is it? If your monitor is using 1024*960 pixel mode, you can calculate the number of pixels in 17 inches, and then calculate if the resolution is 800*600 pixel mode. If your line is 3 centimeters long, but on the screen, the monitor is always measured in pixels, so it's going to be converted to pixel metrics, about. Moreover, the most favorite unit of measurement for foreigners is inches, the width of the controls in VB is always used in twips (twips) (twips: Unit of measurement, equal to "pounds" of 1/20, inches of 1/1440. One centimeter has 567 twips. Pixel (Pixels): the smallest unit of resolution for a monitor or printer. If you right-click on the desktop, select Properties, select "Settings" tab, and click the Advanced button. The DPI setting will appear inside. Typically "normal size (dpi)". The DPI means dpi (Dots per Inch). So we can get the following conversion formula: 1 TWIP = 1440 tpi/96 DPI = Pixels. By the way, other units of measurement:
Points: The unit of measure that refers to the height of the printed character. 1 points equals 1/72 inches, or approximately 1 centimeters of 1/28.
Inches: 2.54 cm
The screen mapping mode is the pixel aspect ratio, if it is 1:1, the pixel is represented as a square, if not, it is rectangular. There is also a mapping pattern mentioned is a centimeter instead of a meter (or more), because our monitor is only so large, to display the world on such a small screen, only by mapping the large length or width of the map to small. This is much like the scale of the map, 1 more than how many million.

The drawing pattern is another story. Used in VB is the PictureBox of the DrawMode property to set. To understand the drawing pattern, you first have to say the foreground and background colors. The foreground is the color of the drawing on the canvas, and the background is the color of the canvas, and if the foreground and background colors are the same, you will not see the drawing. As for how to choose the foreground and background scenery, it depends on your art skills and appreciation of the power. This assumes that the front color is black, the background color is white, when you use the mouse to draw, for copy mode (Drawmode=vbcopypen), you draw a black line is a black line, the foreground color to green, draw a green lines is a green lines; for reverse drawing mode (drawmode= Vbinvert), your foreground color, whether it's black or red, the line you draw will always be the reverse color of the background color--black, that is, regardless of the foreground color, you draw a line, you will see on the screen is black (background color inversion color, back color is white time). Using this feature, we can basically implement the rubber band technology (see the following description).

The principle of the rubber band frame
The function of the rubber band box is like this: Use the starting position of the mouse point, hold down the mouse button, move the mouse, the current position of the mouse deviates from the starting position, then use the line function of the PictureBox, draw a rectangular frame between the starting position of the mouse and the current position. The term "Rubber band" is because the box is named after the mouse moves up and down. When you release the mouse button, the box will remain on the screen. This tip tells you how to create a rubber band box using Visual Basic.

It is easy to draw a box in a form or picture box control using the line method, but the rubber band feature requires you to make the previously drawn box disappear when the mouse moves and the new frame is painted. The way to solve this problem is in the DrawMode attribute. The default setting for DrawMode is Vbcopypen, drawing a solid line, but this does not meet our requirements. Instead, we use the Vbinvert setting, which means that an underscore like the line method uses a hue that is already the color of the form or the picture box control. Therefore, if the background color is white, then Vbinvert will draw a black line, and vice versa. Any color in the palette is like this, which solves two problems:

This box is always visible relative to the background.
If we draw again this box again, the color will turn again, becoming the same value as the original color, so the window disappears.

So, the method is the following:
When the user presses the mouse button, the current coordinates of the mouse are recorded. This will be used for the corner of the position box. Also, set the "Drawing" mark (flag) to True (true).
When the mouse is moved, the previous frame is drawn again and deleted. This step is not in the first time frame, but in the later frame all need this step.
When the mouse is moving, draw a box between the mouse start coordinates and the new coordinates.
When the mouse button is released, set the "Drawing" flag to False (false).



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.