Print screen - of selected component

Hi there,

Trying to do something really easy, but just can't do it!

I have a TShape component on my form with a TBevel around it. What I am trying to do is capture the screen but only show the TShape (basically, everything inside the TBevel.

basically

find screen location of TBevel
capture that part of the screen

Going mad :)
LVL 10
wildzeroAsked:
Who is Participating?
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
I don't know how to do it with a TBevel persay, but if you put those things inside a control such as a TPanel, you can use the "PaintTo" method

uses printers;

begin
  if dlgPrint.Execute then
    begin
      with Printer do
      begin
        BeginDoc;
          Panel1.PaintTo(Handle, 10, 10);
        EndDoc;
      end;
    end;
end;
0
 
wildzeroAuthor Commented:
Erm? dlgPrint?


I presume you mean

var
  bmp2 : TBitMap;
begin
  bmp2 := TBitMap.create;
  bmp2.Width := Panel1.Width;
  bmp2.Height := Panel1.Height;
  Panel1.PaintTo(bmp2.Canvas, 0, 0);
  bmp2.SaveToFile('C:\test2.bmp');
0
 
wildzeroAuthor Commented:
Problem is lets say we devided that panel up into 4 equal segments
How could I get the top right 'piece' or the bottom right one using that method?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
wildzeroAuthor Commented:
 bmp2 := TBitMap.create;
  bmp2.Width := Panel1.Width div 2;
  bmp2.Height := Panel1.Height div 2;
  Panel1.PaintTo(bmp2.Canvas, 0, 0);

that gets the top left corner


  bmp2.Width := Panel1.Width div 2;
  bmp2.Height := Panel1.Height div 2;
  Panel1.PaintTo(bmp2.Canvas, -(Panel1.Width div 2), 0);

gets the top right corner



  bmp2.Width := Panel1.Width div 2;
  bmp2.Height := Panel1.Height div 2;
  Panel1.PaintTo(bmp2.Canvas, 0, -(Panel1.Height div 2));

bottom left corner



  bmp2.Width := Panel1.Width div 2;
  bmp2.Height := Panel1.Height div 2;
  Panel1.PaintTo(bmp2.Canvas, -(Panel1.Width div 2), -(Panel1.Height div 2));

bottom right.

Phew!
0
 
wildzeroAuthor Commented:
Any suggestions on that code? =)
0
 
TheRealLokiSenior DeveloperCommented:
no,
by dlgprint, I mean a TPrintDialog (from the dialogs tab) placed on the form
the x and y properties of the "PaintTo" determine where to start printing the control on the printer page
0
 
wildzeroAuthor Commented:
I thought it was odd to use a TPrintDialog when was trying to get the image into a bitmap
0
 
TheRealLokiSenior DeveloperCommented:
hmm ok, your topic says "print screen" but if you just want to capture the area, what you want is something more like

var
    bmp: Tbitmap;
begin
    bmp := TBitmap.create;
    try
        bmp.width := panel1.width;
        bmp.height := panel1.height;
        Panel1.PaintTo(bmp.Canvas.Handle , 0, 0);
        image1.picture.assign(bmp); // just to prove that we got it.  
    finally
        bmp.free;
    end;
end;


don't know why you were doing the div 2 stuff
0
 
TheRealLokiSenior DeveloperCommented:
if you want to do away with the "needing to be in a panel"
you can take a "screenshot" of the area you want like this :-
    var
        DC : HDC;
        bmp: tbitmap;
    begin
        bmp := tbitmap.create;
        try
            bmp.Width  := Bevel1.Width;
            bmp.Height := Bevel1.Height;
            BitBlt(bmp.canvas.handle, 0, 0, bmp.Width,
            bmp.Height,form1.Canvas.handle, Bevel1.Left, Bevel1.Top, SRCCOPY);
            image1.picture.assign(bmp); // show the captured area image
        finally
            bmp.free;
        end;
    end;

if your bevel is inside another control, you will need to change
...... form1.canvas.handle, Bevel1.Left, Bevel1.Top to...
...... form1.canvas.handle, Panel1.Left + Bevel1.Left, Panel1.Top + Bevel1.Top ...
0
 
wildzeroAuthor Commented:
Ahh sorry my mistake :P Was wondering where you got the print bit from

Print Screen (Or PRT Screen) is the button to push when you do a screen shot which is what I ment (to take a screen shot).

I was able to use the first supplied code and modify it to suit my needs perfectly.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.