whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

19 Visitors Online


 
...Move a cross in canvas to show X/Y values?
Autor: Daniel Henrique M. Carvalho
Homepage: http://www.delhmc.ubbi.com.br
[ Print tip ]  

Tip Rating (31):  
     


// TPanel, TImage e TLabel components
// Insert Image into Panel...

private
   
BmpH, BmpV : TBitmap;
   OldX, OldY: Integer;
end;

var
  
RectSaved : boolean = false;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  
PanelImage.DoubleBuffered := true; // This prevents that the image is blinking
  
LabelHint.Transparent := true;
  LabelHint.Font.Color := clNave;
end;

procedure TFormMain.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  
Cross(X, Y);
end;

procedure TFormMain.Cross(X, Y: Integer);
begin
  
Image.Canvas.Pen.Color := clBlack;
  // Restore last image to erase line
  
if RectSaved then
  begin
    
Image.Canvas.CopyRect(Rect(OldX - 5, 0, OldX + 5, BmpV.Height),
    BmpV.Canvas, Rect(0,0,BmpV.Width,BmpV.Height));
    Image.Canvas.CopyRect(Rect(0, OldY - 5, BmpH.Width, OldY + 5),
    BmpH.Canvas, Rect(0,0,BmpH.Width,BmpH.Height));
    BmpH.Free;
    BmpV.Free;
  end;
  // Now save the image at new position for each line
  // horizontal line
  
BmpH := TBitmap.Create;
  BmpH.Width := Image.Width;
  BmpH.Height := 10;
  BmpH.Canvas.CopyRect(Rect(0,0,BmpH.Width,BmpH.Height),
  Image.Canvas,Rect(0, Y - 5, BmpH.Width, Y + 5));
  // Vertical line
  
BmpV := TBitmap.Create;
  BmpV.Width := 10;
  BmpV.Height := Image.Height;
  BmpV.Canvas.CopyRect(Rect(0,0,BmpV.Width,BmpV.Height),
                 Image.Canvas,Rect(X - 5, 0, X + 5, BmpV.Height));

  // Now draw the current position
  
Image.Canvas.MoveTo(0, Y);
  Image.Canvas.LineTo(Image.Width, Y);
  Image.Canvas.MoveTo(X, 0);
  Image.Canvas.LineTo(X, Image.Height);

  RectSaved := true;
  OldX := X;
  OldY := Y;

  LabelHint.Left := X + 36;
  LabelHint.Top := Y - 15;
  LabelHint.Caption := '(X:' + IntToStr(X) + ' x Y:' + IntToStr(Y) +')';
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners