VB6.0 the X1,y1,x2,y2 property when making a homemade line control

Source: Internet
Author: User
Tags min
The control line control was the simplest control, but it was too simple to provide some of the events we wanted, and to enhance its functionality, I made a line control myself, and

Give her name as Mline control.

How to make a control see "msdn-visual Basic Document-using the Visual Basic-part tools guide-Creating an ActiveX control" and "

Create ActiveX Controls section.


VB comes with the line control has x1,x2,y1,y2 four properties, no left,top,width,height these four properties, by adjusting the value of X1-y2 four properties, to change the position and shape of lines

。 However, we have only ltwh these four properties in our Mline control, so the key to the Mline control is how to associate Ltwh four properties with X1-y2 four properties.

Obviously, if the line in the Mline control is from the upper-left corner to the lower-right corner, then:
A1=left:b1=top:a2=left+width:b2=top+height
Here I use (A1,B1) to represent the coordinates of the upper-left corner, (A2,B2) to represent the coordinates of the upper-right corner, so that I can determine the area of the control by A1-B2, but not the shape of the line, and

That is, whether the line is skimming or a1-b2, we need a flag variable to record the skew value, as we'll say.

Well, a1-b2 obviously corresponds to the Ltwh one by one, we can write:
Left=a1:top=b1:wdith=a2-a1:height=b2-b1
A1=min (X1,X2): B1=min (y1,y2): A2=max (X1,X2): B2=max (Y1,y2)
Through the above relationship, we associate X1-y2 and Ltwh.
So you do it, basically this line can be drawn, but not too precise, the line near the horizontal or vertical when there will be errors, the reason for the error is that the control has a most

Small width and minimum height! You can create an EXE project. Observe commonly used controls, their height/width minimum can be set to 15 or greater, and UserControl

, which is our control, its height/width minimum can only be set to 30, so if you do not take this factor into account, the Mline control will always be error, if you make

Mline control to (X1,Y1) as the center, let (X2,y2) around it to draw a circle, you will find some subtle changes in the line, this change is intolerable.

So the point of the Mline control is to understand the real shape of the control, not just a rectangular area with a diagonal line, but a look like this:
In its area, there is a redundant area where we can't draw lines, because this redundant area is the smallest area of the control. This redundant area I set it to be a hollow

Rectangle, which is coincident with the entire area of the Mline control, and the hollow area in the middle is where we draw the line. The hollow area should be determined by the minimum width and the minimum height, allowing the Minwid

Th represents the minimum width, minheight represents the minimum height, then the left boundary of the hollow area and control area =MINWIDTH/2, the right boundary =minwidth/2, the upper boundary =minheight/2, the bottom

=MINHEIGHT/2, you should be able to imagine this.


OK, this is the focus of the Mline control, let's adjust the relationship between X1-y2 and Ltwh, of course, I would like to explain the a1-b2 first, so clear:
A1=extender.left+minwidth/2
B1=extender.top+minheight/2
A2=extender.left+extender.width-minwidth/2
B2=extender.top+extender.height-minheight/2

A2=a1+extender.width-minwidth
B2=b1+extender.height-minheight

Extender.left=a1-minwidth/2
Extender.top=b1-minheigth/2
Extender.width=a2-a1+minwidth
Extender.height=b2-b1+minheight

of which a1=min (X1,X2)
B1=min (Y1,y2)
A2=max (X1,X2)
B2=max (Y1,y2)
Did you see it? A1-B2 and Ltwh established one by one corresponding relationships, and through the Min/max method, we can also make x1-y2 and Ltwh to establish the corresponding relationship, but not one by one corresponding.
Why, then? Here we need a flag variable Blnk, which indicates the tilt direction of the line, that is, skim or skew, through blnk, we can make x1-y2 and Ltwh establish

One by one corresponding relationship, that is to say, we can change Ltwh to cause x1-y2 change, or change Ltwh by changing x1-y2, so we get the x1-y2 of attribute get/

The Let method is as follows:
(In practice, I did not use Blnk to record the tilt direction, but PosX1 and PosY1 to record the position of the x1,y1 at Four Corners)

' Customer Area location X1
Public Property Get X1 () as single
If PosX1 = Ls_left Then
X1 = Extender.left + MINWIDTH/2
Else
X1 = Extender.left + EXTENDER.WIDTH-MINWIDTH/2
End If
End Property

Public Property Let X1 (ByVal NewX1 as single)
Dim OldX2 as Single
OldX2 = X2

If NewX1 > OldX2 Then
' New X1 on X2 right.
PosX1 = Ls_right
Extender.left = OLDX2-MINWIDTH/2
Extender.width = newx1-oldx2 + minwidth
Else
' New X1 on X2 left.
PosX1 = Ls_left
Extender.left = NEWX1-MINWIDTH/2
Extender.width = oldx2-newx1 + minwidth
End If
PropertyChanged "X1"
End Property

The X2,y1,y2 property method is similar to this and is no longer redundant.

In the Paint event we use the line method to draw lines, but remember not to draw from X1,y1 to X2,y2, but from X1-extener.left,y-extender.top to x2-extender.left,y2-

Extender.top draw lines.


It is noteworthy that some people may not understand the relationship between attribute and property method Get/let, thus causing many misunderstandings, should understand that the value of X1-y2 is saved in the Get method, every

Secondary read X1-y2 will call get method to obtain its value, note! is to seek! So you can also think that there is no x1-y2 these four variables. And every time you set the X1-y2, you are setting up LTW

H and PosX1, PosY1, I hope you can understand this, some people will be in the Resize/paint event to set X1-y2, and then set x1-y2 in Ltwh, which will cause Resize/pai

NT Event, the middle appears recursive call, although by setting the method of flag variable can prevent infinite recursion, but that is much more complicated, I am sorry to say that I did so at the beginning.

For the X1-y2 let method call, only three cases, the first case is readproperties, at this time will be saved in the form of X1-y2 to set the X1-y2 value;

Developer, the third scenario might be the user.

To understand that each LTWH changes will directly cause x1-y2 changes, know this point, will not go to Resize/paint events to track Ltwh changes.



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.