6
votes

I am trying to create a basic map editor for fun which consists of a scrollbox and paintbox (to draw the grid).

In the OnMouseDown event for the paintbox I create images at runtime and add them inside the scrollbox, the grid paintbox is painted above the images ( because if the grid was at the back it just would not look good ).

Here is a sample screen:

enter image description here

My question would solve two of my problems in one.

  • I need to be able to drag and drop (to move) the images around at runtime.
  • I also need to be able to get the X and Y position of the image to display as information.

Here is where my problem lies, to solve the problems above I first need to get the Image under the mouse cursor. But because I paint my grid (paintbox) above the images, the cursor will only ever 'see' the paintbox grid, and not the underlying images.

I did experiment with copying the paintbox grid to a TImage but it came out all wrong and I was getting out of memory errors. The size of the maps could be quite large and so putting the grid onto a bitmap is not ideal due the memory and limitations etc.

The grid must go on the top, otherwise it would look something like this:

enter image description here

Which hides the grid, and I don't want that to happen.

So, how can I see past the paintbox and get to the images underneath, using FindVCLWindow or something similar?

1
I think I'd avoid storing the map in GUI controls. Store it in a dedicated data structure and make what's on the screen be just a view. Don't have image controls at all. Draw the entire thing in a single paint box.David Heffernan
@DavidHeffernan can paintbox be used to drag and drop the images? i need more time to work with and learn this control but i assumed it was for drawing on and limited to that only, hence i am using images. if i can achieve the same on another paintbox that sounds more practical.user1175743
You'd have to write some drag code but it could be done. One control per sphere seems all wrong to me.David Heffernan
@DavidHeffernan it does sound very inefficient but i believed the only way was using images to move them around and to get the image under mouse, to display there co-ordinates in a label or statusbar for example. Thanks for suggesting an alternative approach.user1175743
Better start as soon as you can. Look what Andreas made for moving triangles for instance. The principle is easy, there's no graphical triangle object at all. All of them are represented by records in the array painted with every trigger OnPaint event and for getting a triangle under the mouse, there's the GetTriangleAt method which you'd have to modify for circles. But that's also easy (to find).TLama

1 Answers

8
votes

Set the Enabled property of the PaintBox to False. That will let the mouse messages go through.

Further:

In the OnMouseDown event for the PaintBox I create images at runtime and add them inside the scrollbox

Change that to the OnMouseDown event on the ScrollBox. Adjust the coordinates by ScrollBox.[Horz/Vert]Scrollbar.Position.