6

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?

  • 2
    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 Sep 23 '13 at 06:25
  • @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. –  Sep 23 '13 at 07:30
  • 1
    You'd have to write some drag code but it could be done. One control per sphere seems all wrong to me. – David Heffernan Sep 23 '13 at 07:39
  • @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. –  Sep 23 '13 at 07:51
  • 1
    Better start as soon as you can. Look what Andreas made for [`moving triangles`](http://stackoverflow.com/a/7224075/960757) 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 Sep 23 '13 at 08:07
  • @TLama my images could be anything, not just spheres - they could be any type of image. The easy way is for me to use TImages and limit the size of the map, at least I have a better idea of what I am doing, but as David Heffernan suggests using another paintbox to draw everything as a 'view' rather than a physical object would be more effecient. I think this would be better asked in another question because I really don't know what to do right now. –  Sep 23 '13 at 08:18
  • He suggested what I linked you to with a specific example. It's not meant to be another paint box. It's meant to be the collection which you'll just draw. Exactly as Andreas did in his example. You should do something similar. Anyway, you know that you won't be able to select a specific shape precisely at this time. You'll be able to select just a rectangle. Never mind. Take it as suggestion of one demotivated user :-) Good luck! – TLama Sep 23 '13 at 09:04
  • @TLama i perhaps initially misunderstood what you meant, I have looked at the compiled sample by Andreas and it does look promising. The rectangle around each image would be enough for me to drag around, I don't need pixel perfect selection - it's just as if it was is in a TImage and you are dragging the rectangle around it. –  Sep 23 '13 at 09:07

1 Answers1

8

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.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • Perfect thanks, `Enabled` could have perhaps been better named as I would of completely overlooked it, only until you mentioned it I found information from the Delphi online help: `Controls whether the control responds to mouse, keyboard, and timer events. ` I like this because it also allows me to `lock` the paintbox :) –  Sep 23 '13 at 08:22