?
Solved

How to draw a rectangle on a form using the mouse?

Posted on 2006-05-17
4
Medium Priority
?
505 Views
Last Modified: 2008-02-01
Dear everyone,

I want to let my users create clickable maps in Access. I want to accomplish this by presenting them with a form with an image on it and then letting them draw a rectangle by dragging the mouse across a selection of the image. After they release the mouse, I need to pick up the Top, Left, Right and Bottom values of the rectangle.

Can this be done?

I played around with this Lebans tool a little: http://www.lebans.com/imageclass.htm so I think it can be done, but I'm not clever enough to change his code. I hope you are.

Thanks,
Keimpe

0
Comment
Question by:keimpe
  • 2
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
harfang earned 2000 total points
ID: 16698236
Hello keimpe

I dug something up for you. Create a new form, add an image called "imaSelect" and a rectangle called "recFeedback" (in that order). Then paste the following into the form's module:

---------------------------------------------------------------------------------------->8----------
Option Explicit
'
' visual "selection" of the portion of an image...
'
Dim mfMouseDown As Boolean
Dim mfOrigX As Single
Dim mfOrigY As Single

Private Sub imaSelect_MouseDown( _
    Button As Integer, _
    Shift As Integer, _
    X As Single, _
    Y As Single)

    If Button <> acLeftButton Then Exit Sub
    mfMouseDown = True
    mfOrigX = imaSelect.Left + X
    mfOrigY = imaSelect.Top + Y
    With recFeedback
        .Width = 0
        .Height = 0
        .Left = mfOrigX
        .Top = mfOrigY
        .BorderStyle = 4 ' dots
    End With
   
End Sub

Private Sub imaSelect_MouseMove( _
    Button As Integer, _
    Shift As Integer, _
    X As Single, _
    Y As Single)
   
    Dim sngSpotX As Single
    Dim sngSpotY As Single
   
    If mfMouseDown Then
        If X < 0 Or X > imaSelect.Width Then Exit Sub
        If Y < 0 Or Y > imaSelect.Height Then Exit Sub
        sngSpotX = imaSelect.Left + X
        sngSpotY = imaSelect.Top + Y
        With recFeedback
            .Left = IIf(sngSpotX < mfOrigX, sngSpotX, mfOrigX)
            .Width = Abs(sngSpotX - mfOrigX)
            .Top = IIf(sngSpotY < mfOrigY, sngSpotY, mfOrigY)
            .Height = Abs(sngSpotY - mfOrigY)
        End With
    End If
   
End Sub

Private Sub imaSelect_MouseUp( _
    Button As Integer, _
    Shift As Integer, _
    X As Single, _
    Y As Single)
   
    mfMouseDown = False
    recFeedback.BorderStyle = 1   ' solid...
   
End Sub
---------------------------------------------------------------------------------------->8----------

Have fun!
(°v°)
0
 
LVL 1

Author Comment

by:keimpe
ID: 16698332
That's brilliant Harfang, thanks!

One last thing though: I would like to see the actual drawing happening. With this code I don't see the rectangle until it's done. Do you know a way?
0
 
LVL 1

Author Comment

by:keimpe
ID: 16698356
Got it myself!

Insert a "Me.Repaint" just underneath the "End With" in "imaSelect_MouseMove"
0
 
LVL 58

Expert Comment

by:harfang
ID: 16698508
Hello keimpe

I didn't need that on Access 2k. I believe there is a new form property for later versions, something like "auto redraw", that might play a role here.
Anyway, glad it worked! Good luck with your project!

(°v°)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question