DELPHI from Borland Company (now renamed INPRISE Company) is currently the most convenient Windows program.
One of the planning tools. Many people think that DELPHI appears as a database development tool. In fact, DELPHI
Efficient Windows programs can be developed at breakneck speed.
Now we will use DELPHI to write a practical screen copy program. Look, the picture below
This is an example of copying the regional screen after running the programmed program. Not bad!
The talented designers of Borland Company use the canvas (Tcanvas) object to encapsulate most of the graphics in Windows.
Output function, which allows us to deal with the Windows screen in a more intuitive way,
And don't have to worry about the headache of Windows API functions. The following short program can realize the entire
The screen image is copied.
var //variable declaration
Fullscreen:Tbitmap;
FullscreenCanvas:TCanvas;
dc:HDC;
//------------------------------------------------ ----------
DC := GetDC (0); //Get the DC of the screen, parameter 0 refers to the screen
FullscreenCanvas := TCanvas.Create; //Create a CANVAS object
FullscreenCanvas.Handle := DC; //Assign the DC of the screen to HANDLE
Fullscreen.Canvas.CopyRect
(Rect (0, 0, screen.Width,screen.Height),
fullscreenCanvas,
Rect (0, 0, Screen.Width, Screen.Height));
//Copy the entire screen to BITMAP
FullscreenCanvas.Free; //Release CANVAS object
ReleaseDC (0, DC); //Release DC
//SCREEN object is a screen object predefined by DELPHI, just use it directly.
After reading the above code, you will find that it is indeed very simple to write a screen copy program with DELPHI.
Of course, you need to write a practical screen copy program. The above code alone is not enough. Let’s talk about it below.
The following are the main programming ideas:
1. Implementation of full-screen copy
First hide the screen copying program, and after a certain period of time, use the above program to achieve screen copying.
copy.
2. Implementation of regional copy
To achieve area copy, you need to use a little trick. First call the full screen copy program to copy the entire screen.
download, and then display the copied image on the screen, and then allow the user to
Select the required area, and finally copy the user-selected area.
Programming implementation:
1. First open a project with DELPHI3.
2. Place a TPANEL component on the FORM, set ALIGN=ALTOP, and then select the component bar ADDITIONAL
TSCROLLBOX, place it on the FORM, set ALIGN=ALCLIENT, and then place a
TIMAGE object.
3. Place 4 buttons on the PANEL, namely FULL SCREEN, REGIN, SAVE, and EXIT.
4. Do what is easy to do first and write the code in the CLICK event of the EXIT button
procedure TForm1.ExitClick(Sender: TObject);
begin
close;
end;
5. Next is to realize full-screen copy. Place a timer TTIMER on FROM and set ENABLED to
FALSE, INTERVAL is set to 500, which means it is activated once every half second. Double-click the TIMER component and write the following
code.
procedure TForm1.Timer1Timer(Sender: TObject);
var
Fullscreen:Tbitmap;
FullscreenCanvas:TCanvas;
dc:HDC;
begin
timer1.Enabled:=false; //Cancel the clock
Fullscreen := TBitmap.Create; //Create a BITMAP to store images
Fullscreen.Width := screen.width;
Fullscreen.Height := screen.Height;
DC := GetDC (0); //Get the DC of the screen, parameter 0 refers to the screen
FullscreenCanvas := TCanvas.Create; //Create a CANVAS object
FullscreenCanvas.Handle := DC;
Fullscreen.Canvas.CopyRect
(Rect (0, 0, screen.Width, screen.Height), fullscreenCanvas,
Rect (0, 0, Screen.Width, Screen.Height));
//Copy the entire screen to BITMAP
FullscreenCanvas.Free; //Release CANVAS object
ReleaseDC (0, DC); //Release DC
//******************************
image1.picture.Bitmap:=fullscreen;//Assign the copied image to the IMAGE object
image1.Width:=fullscreen.Width;
image1.Height:=fullscreen.Height;
fullscreen.free; //Release bitmap
form1.WindowState:=wsNormal; //Restore window state
form1.show; //Display window
messagebeep(1); //BEEP beeps once to report that the image has been captured.
end;
6. The next code on the FULLSCREEN button is very simple.
procedure TForm1.FullscreenClick(Sender: TObject);
begin
form1.WindowState:=wsMinimized; //Minimize the program window
form1.hide; //Hide the program
timer1.enabled:=true; //Open the timer
end;
7. After copying the image, of course it needs to be saved. The SAVE button comes into play. We write the following:
Download the code.
procedure TForm1.Save1Click(Sender: TObject);
begin
if savedialog1.Execute then
begin
form1.Image1.Picture.SaveToFile(savedialog1.filename)
end;
end;
8. The following is the implementation of zone copy. Then New a FORM, set BorderStype to bsNone, so that it can be displayed
For full screen, place a TIMAGE component on it, set ALIGN to ALCLIENT, and place a TTIMER
widget, the program of the TIMER widget is very similar to the above, because the first thing it needs to achieve is full-screen copying.
cowry.
procedure TForm2.Timer1Timer(Sender: TObject);
var
Fullscreen:Tbitmap;
FullscreenCanvas:TCanvas;
dc:HDC;
begin
timer1.Enabled:=false;
Fullscreen := TBitmap.Create;
Fullscreen.Width := screen.width;
Fullscreen.Height := screen.Height;
DC := GetDC (0);
FullscreenCanvas := TCanvas.Create;
FullscreenCanvas.Handle := DC;
Fullscreen.Canvas.CopyRect (Rect
(0, 0, screen.Width, screen.Height), fullscreenCanvas,
Rect (0, 0, Screen.Width, Screen.Height));
FullscreenCanvas.Free;
ReleaseDC (0, DC);
image1.picture.Bitmap:=fullscreen;
image1.Width:=fullscreen.Width;
image1.Height:=fullscreen.Height;
fullscreen.free;
form2.WindowState:=wsMaximized;
form2.show;
messagebeep(1);
foldx:=-1;
foldy:=-1;
image1.Canvas.Pen.mode:=pmnot; //The pen mode is inverted
image1.canvas.pen.color:=clblack; //The pen is black
image1.canvas.brush.Style:=bsclear; //Blank brush
flag:=true;
end;
9. There are two event programs on the TIMAGE component that need to be written, one is ONMOUSEDOWN and the other
It's ONMOUSEMOVE.
10. You can look back at the idea of region copying. At this time, we have already
is obtained and is also displayed on the screen. Press the left mouse button to be the origin of the area. Then move the mouse to
There is a rectangle between the origin and the mouse. It changes as the mouse moves. Press the mouse again.
Click the left button of the target. At this time, the area contained by the rectangle is the image we want to get.
11. So MOUSEDOWN has two response processing, see the following procedure.
procedure TForm2.Image1MouseDown
(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
width,height:integer;
newbitmap:Tbitmap;
begin
if (trace=false) then // TRACE indicates whether the mouse is being tracked
begin //Click the left mouse button for the first time to start tracking the mouse.
flag:=false;
with image1.canvas do
begin
moveTo(foldx,0);
LineTo(foldx,screen.height);
moveto(0,foldy);
lineto(screen.width,foldy);
end;
x1:=x;
y1:=y;
oldx:=x;
oldy:=y;
trace:=true;
image1.Canvas.Pen.mode:=pmnot; //The pen mode is inverted
//Draw the rectangle again in the original place, which is equivalent to erasing the rectangle.
image1.canvas.pen.color:=clblack; //The pen is black
image1.canvas.brush.Style:=bsclear;//Blank brush
end
else
begin //The second click indicates that the rectangle has been obtained.
//Copy it to the IMAGE component in FORM1.
x2:=x;
y2:=y;
trace:=false;
image1.canvas.rectangle(x1,y1,oldx,oldy);
width:=abs(x2-x1);
height:=abs(y2-y1);
form1.image1.Width:=Width;
form1.image1.Height:=Height;
newbitmap:=Tbitmap.create;
newbitmap.width:=width;
newbitmap.height:=height;
newbitmap.Canvas.CopyRect
(Rect (0, 0, width, Height),form2.image1.canvas,
Rect (x1, y1,x2,y2)); //Copy
form1.image1.picture.bitmap:=newbitmap; //Put it on the IMAGE of the FORM
newbitmap.free;
form2.hide;
form1.show;
end;
end;
12. The processing of MOUSEMOVE is to continuously draw and erase rectangles between the origin and the current position of the mouse.
Except rectangle.
procedure TForm2.Image1MouseMove
(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if trace=true then //Are you tracking the mouse?
begin //Yes, erase the old rectangle and draw a new one
with image1.canvas do
begin
rectangle(x1,y1,oldx,oldy);
Rectangle(x1,y1,x,y);
oldx:=x;
oldy:=y;
end;
end
else if flag=true then //Draw a cross where the mouse is
begin
with image1.canvas do
begin
moveTo(foldx,0); //Erase the old cross
LineTo(foldx,screen.height);
moveto(0,foldy);
lineto(screen.width,foldy);
moveTo(x,0); //Draw a new cross
LineTo(x,screen.height);
moveto(0,y);
lineto(screen.width,y);
foldx:=x;
foldy:=y;
end;
end;
end;
13. Okay, let's go back to writing the code for the REGION button.
procedure TForm1.RegionClick(Sender: TObject);
begin
form1.Hide;
form2.hide;
form2.Timer1.Enabled:=true;
end;
Okay, we've finally finished it successfully. Let's run it again and copy the beautiful screen! Look
DELPHI is not only an excellent database development tool, but also an excellent tool for writing WINDOWS
A good helper for the program. Let us not help but admire: Great DELPHI!
Jia Xuejie, Youhe Lane, Ningbo City