devxlogo

Select Areas Within a Graphics Window

Select Areas Within a Graphics Window

Graphics applications sometimes require users to select a rectangular region of a picture or drawing visually. You need to provide a resizing box manipulated by the pointer at run time that only interacts temporarily with the graphics displayed already:

Private Type TRect	Left As Single	Top As Single	Right As Single	Bottom As SingleEnd TypePrivate mbDragging As BooleanPrivate mRect As TRectPrivate Sub Picture1_MouseDown(Button As Integer, _	Shift As Integer, X As Single, Y As Single)	mbDragging = True	Picture1.DrawMode = vbInvert	With mRect		.Left = X		.Top = Y		.Right = X		.Bottom = Y	End WithEnd SubPrivate Sub Picture1_MouseMove(Button As Integer, _	Shift As Integer, X As Single, Y As Single)	If Not mbDragging Then Exit Sub	With mRect		' erase box		Picture1.Line (.Left, .Top)-(.Right, _			.Bottom), , B		.Right = X		.Bottom = Y		' draw box		Picture1.Line (.Left, .Top)-(X, Y), , B	End WithEnd SubPrivate Sub Picture1_MouseUp(Button As Integer, _	Shift As Integer, X As Single, Y As Single)	mbDragging = False	' erase box by redrawing	Picture1.Line (mRect.Left, _		mRect.Top)-(mRect.Right, mRect.Bottom), , B	Picture1.DrawMode = vbCopyPenEnd Sub

By assigning vbInvert to the PictureBox DrawMode property before selection dragging, you can restore the background graphics by redrawing the same rectangle. Once the selection dragging completes, mRect contains the selected rectangle coordinates. You can use the same technique to select a circular region or create the “rubber band” effect.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist