Sometimes, we need to use VB to quickly develop a test data drawing processing program. Changing the mouse cursor in the drawing control to the form of the large cross cursor used in AutoCAD software will achieve better results than the ordinary arrow cursor. So how do we implement such a large cross cursor?
----First of all, let's clarify the effect we want to achieve. If we draw in a Picture control, then when the mouse moves to this control, the mouse cursor immediately changes to a large cross shape, and the horizontal line in the cursor changes from the control's From the left border to the right border, the vertical line goes from the upper border to the lower border of the control, that is, the large cross cursor divides the drawing control into four quadrants. When the mouse moves outside the control, the cursor returns to its original form.
----To implement such a cursor, we have to do it ourselves by drawing lines. If the mouse is in the drawing control, first draw the horizontal and vertical lines of the cursor at the current position of the mouse; when the mouse position moves, first erase the original horizontal and vertical lines of the cursor, and then draw the horizontal lines of the cursor at the new position. Lines and vertical lines, then we have to respond to the MouseMove event of the drawing control. Of course, no matter what content is in the drawing control, we cannot destroy the original content when we erase the cursor line and redraw the cursor line. Therefore, we need to set the DrawMode of the drawing control to vbXorPen (XOR mode) to draw the horizontal line of the cursor. When adding vertical lines, use XOR to combine the horizontal lines and vertical lines. The pixel color is set to the XOR value of the cursor color and the original pixel color, and then the vertical and horizontal lines are drawn at the same position using the XOR method, and the pixels on the horizontal and vertical lines are drawn again. An XOR operation with the cursor color will erase the horizontal and vertical lines of the cursor, and restore the original content in the drawing control.
----We also have to ensure that when the mouse moves into the drawing control, the ordinary mouse cursor disappears and only the large drawn cross cursor appears. Therefore, the MousePointer property of the drawing control should also be set to vbCuntom, which is user-defined. After the MousePointer property of the drawing control is set to vbCustom, the corresponding user-defined graphics should be loaded into its MouseIcon property. Because we hope that the drawing control will only have the cursor we draw and no other cursors, so an empty ( Transparent) cursor graphics. You can find a cursor file, edit it through any resource editor, fill the entire cursor graphic in a transparent manner, and save it as the NoIcon.cur we need.
----Through the above key settings and operations, we can realize the large cross cursor. Using the XOR method to draw, we can also achieve the "rubber band" effect commonly seen in general drawing software, that is, after using the mouse to define a point, dynamically drag the mouse to define another point. During the process of dynamically dragging the mouse, the point to be drawn is The graphics also change dynamically accordingly.
---- Below we use an example to fully implement the large cross cursor in the drawing control, and also demonstrate how to use the "rubber band" effect to draw a rectangle:
----Create a new standard EXE project in VB, add a Picture control to Form1, and set its Name to PicDraw. You can load an image file. The size of PicDraw and the size of the image in it basically cover most of the Form1 is enough. The implementation code is as follows. This program runs in VB5.0.
OptionExplicit
PRivateOld_XAsSingle
PrivateOld_YAsSingle
PrivateisMouseDownAsBoolean
PrivateBox_X0AsSingle
PrivateBox_Y0AsSingle
PrivateBox_X1AsSingle
PrivateBox_Y1AsSingle
PrivatePenColorAsLong
PrivateCrossColorAsLong
PrivateSubForm_Load()
CrossColor=QBColor(8)
PenColor=QBColor(15)
picDraw.DrawMode=vbXorPen
picDraw.MouseIcon=LoadPicture
(App.Path&"/no.cur")
picDraw.MousePointer=vbCustom
isMouseDown=False
Box_X0=Box_X1=Box_Y0=Box_Y1=0
EndSub
PrivateSubpicDraw_MouseDown
(ButtonAsInteger,
ShiftAsInteger,XAsSingle,YAsSingle)
IfisMouseDown=TrueThen
'A point has been previously defined with the mouse
Box_X1=X
Box_Y1=Y
isMouseDown=False
picDraw.DrawMode=vbCopyPen
picDraw.Line(Box_X0,Box_Y0)-
(Box_X1,Box_Y1),
PenColor,B
picDraw.DrawMode=vbXorPen
'Draw a cursor
picDraw.Line(0,Y)-(picDraw.ScaleWidth,Y),
CrossColor
picDraw.Line(X,0)-(X,picDraw.ScaleHeight),
CrossColor
Old_X=X
Old_Y=Y
Else
'Defines the first vertex of a rectangle, then erases the cursor
picDraw.Line(0,Y)-(picDraw.ScaleWidth,Y),
CrossColor
picDraw.Line(X,0)-(X,picDraw.ScaleHeight),
CrossColor
Box_X0=X
Box_Y0=Y
isMouseDown=True
EndIf
EndSub
PrivateSubpicDraw_MouseMove(ButtonAsInteger,
ShiftAsInteger,XAsSingle,YAsSingle)
IfisMouseDown=TrueThen
'Drag the mouse to define another vertex of the rectangle,
At this point erase the previous rectangle and draw a new rectangle
picDraw.Line(Box_X0,Box_Y0)-(Old_X,Old_Y),
PenColor,B
picDraw.Line(Box_X0,Box_Y0)-(X,Y),PenColor,B
Else
'Erase the old cursor line
picDraw.Line(0,Old_Y)-(picDraw.ScaleWidth,Old_Y),
CrossColor
picDraw.Line(Old_X,0)-(Old_X,picDraw.ScaleHeight),
CrossColor
'Draw a new cursor line
picDraw.Line(0,Y)-(picDraw.ScaleWidth,Y),
CrossColor
picDraw.Line(X,0)-(X,picDraw.ScaleHeight),
CrossColor
EndIf
Old_X=X
Old_Y=Y
EndSub->