Delphi Graphics32 relative mouse position (to the layer)

前端 未结 2 478
清歌不尽
清歌不尽 2021-01-14 23:09

I have a ImgView32, that is anchored to all form margins. The form is maximized.

The bitmap of ImgView is not fixed (it can be of different sizes)

I am tryin

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 23:44

    Ok, I solved it. Here is the final (relevant) code:

    procedure TForm5.ImgViewResize(Sender: TObject);
    begin
      OffsX := (ImgView.ClientWidth - imwidth) div 2;
      OffsY := (ImgView.ClientHeight - imheight) div 2;
      BL.Location := GR32.FloatRect(OffsX, OffsY, imwidth+OffsX, imheight+OffsY);
    end;
    
    procedure TForm5.SwapBuffers32;
    begin
        TransparentBlt(
          BL.Bitmap.Canvas.Handle, 0, 0, BL.Bitmap.Width, BL.Bitmap.Height,
          bm32.Canvas.Handle, 0, 0, bm32.Width, bm32.Height, clWhite);
    end;
    
    
    procedure TForm5.LayerMouseDown(Sender: TObject; Buttons: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      FStartPoint := Point(X-OffsX, Y-OffsY);
      FDrawingLine := true;
    end;
    
    procedure TForm5.LayerMouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);
    begin
      if FDrawingLine then
      begin
        SwapBuffers32;
          BL.Bitmap.Canvas.Pen.Color := pencolor;
          BL.Bitmap.Canvas.MoveTo(FStartPoint.X, FStartPoint.Y);
          BL.Bitmap.Canvas.LineTo(X-OffsX, Y-OffsY);
      end;
    end;
    
    
    procedure TForm5.LayerMouseUp(Sender: TObject; Buttons: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      FDrawingLine := false;
      FEndPoint := Point(X-OffsX, Y-OffsY);
      AddLineToLayer;
      SwapBuffers32;
    end;
    
    procedure TForm5.LayerOnPaint(Sender: TObject; Buffer: TBitmap32);
    begin
      SwapBuffers32;
    end;
    
    procedure TForm5.AddLineToLayer;
    begin
      bm32.Canvas.Pen.Color := pencolor;
      bm32.Canvas.Pen.Width := penwidth;
      bm32.Canvas.MoveTo(FStartPoint.X, FStartPoint.Y);
      bm32.Canvas.LineTo(FEndPoint.X, FEndPoint.Y);
    end;
    

    With this code, everything works as expected. The drawing of lines can only happen within the boundaries

    Thank you

提交回复
热议问题