Macro Express Forums

# Programmatically use Mouse Locator

## Recommended Posts

Is it possible to use the mouse locator in a macro?

I would like to get the x, y and size of a rectangle the user draws on screen.

Also be able to modify the location and size of rectangle, just like the mouse locator.

##### Share on other sites

I don't think you can directly obtain information about a rectangle on the screen.

However, it might be possible to calculate the position of a rectangle of a known colour.

If I were to take on a project like this, I would try to simplify the problem by, for example, making the rectangle one colour, assuming the mouse pointer is manually placed inside the rectangle, and working out the dimensions of the rectangle, not its position.

The logic might look something like this:

Assume the rectangle is a solid colour, 123456.

Assume the mouse pointer is somewhere inside the rectangle.

Get Mouse Cursor Position [StartX, StartY]

Count = 0

Repeat Until Pixel Colour is NOT 123456

Get Pixel Colour

Move Mouse Cursor UP one pixel

Count = Count + 1

End Repeat

Top y coordinate is Count

Move Mouse Cursor to [StartX, StartY]

Count = 0

Repeat Until Pixel Colour is NOT 123456

Get Pixel Colour

Move Mouse Cursor DOWN one pixel

Count = Count + 1

End Repeat

Bottom y coordinate is Count

Repeat twice: to work out left x coordinate and right x coordinate.

Here's another way that might also work, by finding and tracing the perimeter of a rectangle.

Repeatedly move UP up by 1 pixel until the colour is not 123456

Move down one pixel.

Repeatedly move RIGHT by 1 pixel until the colour is not 123456

Move left one pixel.

Repeatedly move DOWN by 1 pixel until the colour is not 123456

Move up one pixel.

Repeatedly move LEFT by 1 pixel until the colour is not 123456

This could be a fun project to take on to sharpen one's Macro Express skills. The script will likely be computationally intense, and might run slowly! Aiming for an approximate answer by checking every 5 or 10 or 100 pixels could save a lot of time.

##### Share on other sites

Get Mouse Position into (x_coordinate, y_coordinate) Relative to Screen

##### Share on other sites

Your user would not be able to draw a rectangle on the screen. You could start a macro and get the coordinates. Then pause the macro and prompt them to move to anther location and hit Enter to close the MessageBox and read in the next coordinates.

##### Share on other sites

After reading the three very different responses to your question, there's one thing that is very clear: We all have different ideas of what you're trying to achieve!

Could you provide more details?

##### Share on other sites

I wrote a macro years ago that has proved very useful. On activation with its hot key it asks me to click two positions on the screen. Then it tells me the width and height of the rectangle. Usually it’s just the horizontal or vertical difference between those clicked positions that I’m seeking. The macro uses the same simple command as the example offered by @rberq.

Like others I’m unclear about your exact objective, but let me know if you want my macro’s code.

Terry

##### Share on other sites

What I was attempting to do was automate the screen snipping tool to be able to repeat the same snipping area over and over.

I found a better tool, it's called Movavi - Screen Capture.

I automated this with ME so the F8 key captures an area of the screen and saves it to a file.

In Movavi you can set the area of the screen you want to capture.

Thanks for all the help.

##### Share on other sites

Pleased to hear you got it sorted, and thanks for the feedback.

I do exactly the same thing with Snagit.

I’m sure it could be also be done with an MX Pro macro, but if there’s another tool at hand that does it more quickly and reliably then it’s no contest. 🙂

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.