3

graphics32 is a very nice library, but I am having trouble figuring out how to use it properly. For instance LAYERS, they are an awesome feature.

  • I can add layers to an ImageView, select them, move them around, resize them, but, if I want to delete them I have no idea how to do it.

  • Also the layer is selected but I cannot capture any key events on them. I mean I would like to move the layer by a pixel using the arrows on the keyboard, but I cant.

Does anybody know how to solve these problems?

Please help Thank you

user1137313
  • 2,390
  • 9
  • 44
  • 91

1 Answers1

1

The key to capturing the arrow keys is to allow this. For this you need to adjust a public (but not published) property of the underlying TCustomPaintBox32 class.

Something like

ImgView.Options := ImgView.Options + [pboWantArrowKeys];

should allow the TImgView32 class (named ImgView here) to capture arrow keys.

Once enabled you can write a keyboard handler like:

procedure TMainForm.ImgViewKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  Location: TFloatRect;
begin
  if Assigned(FSelection) then
    case Key of
      VK_LEFT:
        FSelection.Location := OffsetRect(FSelection.Location, -1, 0);
...

where OffsetRect adjusts the TFloatRect by adding the deltas (2nd and 3rd argument) to Left/Right and Top/Bottom.

In the above example FSelection is the currently selected layer. It has been stored after selecting the layer (with a mouse click). In addition you may also need to adjust the rubberband location as well, in case you are using a TRubberBandLayer as selector.

Addendum:

Implementation of OffsetRect:

function OffsetRect(const Rct: TFloatRect; const DeltaX, DeltaY: TFloat): TFloatRect;
begin
  Result.TopLeft := OffsetPoint(Rct.TopLeft, DeltaX, DeltaY);
  Result.BottomRight := OffsetPoint(Rct.BottomRight, DeltaX, DeltaY);
end;

alternatively you can directly use code like this:

procedure TMainForm.ImgViewKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  Location: TFloatRect;
begin
  if Assigned(FSelection) then
    case Key of
      VK_LEFT:
        FSelection.Location := FloatRect(FSelection.Location.Left - 1, FSelection.Location.Top, FSelection.Location.Right - 1, FSelection.Location.Bottom);
...

but that looks a bit ugly.

Addendum 2:

For older versions of the library (e.g. 1.9.x) the OffsetPoint function might be missing as well. This is implemented as:

function OffsetPoint(const Pt: TFloatPoint; DeltaX, DeltaY: TFloat): TFloatPoint;
begin
  Result.X := Pt.X + DeltaX;
  Result.Y := Pt.Y + DeltaY;
end;
CWBudde
  • 1,783
  • 1
  • 24
  • 28
  • `There is no overloaded version of OffsetRect that can be called with these arguments` – user1137313 Jan 18 '15 at 15:38
  • You have to write OffsetRect yourself or update the latest trunk (it's in GR32_Geometry). I'll add the implementation I used to the answer. – CWBudde Jan 18 '15 at 15:50
  • I do not understand, I look at the syntax of this function and it all looks ok, except the compiler thinks something's wrong... What is wrong? I tryied calling it as a procedure but same result, compiler error. The procedure is located in gr32.pas – user1137313 Jan 18 '15 at 15:53
  • What do you mean I have to write it myself? – user1137313 Jan 18 '15 at 15:55
  • There is stilla problem with OffsetPoint (undeclared) – user1137313 Jan 18 '15 at 15:58
  • Also, ImageView seems to lose focus, or maybe never gains focus at all? I have an edit box on the form and it's the only focusable control on the form. When I click on a layer in imageview32, i see the focus is still in the editbox. So when I press arrows on keyboard, I see movement in the editbox... (I tryied the alternative code since I do not know what OffsetPoint is, and I could not find it declared in gr32_Geometry either) – user1137313 Jan 18 '15 at 16:02
  • OffsetPoint is declared in unit GR32_Geometry, you may need to add this unit to the uses section otherwise similar functions (from the unit Windows) might be taken instead, where typically there is indeed "no overloaded version of OffsetRect" – CWBudde Jan 18 '15 at 16:06
  • OffsetPoint / OffsetRect might only be present in the trunk, which is already v2.0. The stable version 1.9.x might not feature these functions. I'll add the missing code (for v1.9.1) to the anwser as well now. – CWBudde Jan 18 '15 at 16:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69069/discussion-between-user1137313-and-cwbudde). – user1137313 Jan 18 '15 at 16:09
  • Your answer is perfect. Of course I had to install the latest version of the library from the SVN trunk but now it works perfectly. Thank you very much – user1137313 Jan 19 '15 at 05:40