the program below is a command-line utility authored in Delphi (Object
Pascal) to grab a screen part (or all of it) and put it on the clipboard.
You can easily convert it to C if you wish and add code to save to a Windows
Bitmap, then call the command-line utility from J# as you wish (using the
appropriate "System" or "Runtime" class static methods you can execute an
external application). At the Delphi code I don't use {$APPTYPE CONSOLE} at
the start of the code so that it doesn't show a black command-line window
when it's executed from Java or other environment
I have a similar Screen2Printer utility if anyone needs such (to print a
screen part or the whole from Java/J# etc.)
Beware that parts of a window that are out of the screen will be shown as
black (not sure why, something related to the clip region probably, maybe
should update
it first to include the part of the "virtual" desktop to be grabbed, then
grab it)
cheers,
George
//Screen2Clipboard - (C)2001 George Birbilis <birbilis@kagi.com>
//
//This program is FREEWARE!
//Feel free to modify and distribute as long as you give due credit and also
//distribute together with your modified source the original source
unmodified.
//Source parts are from ClipCopy utility used in the "Gaia" educational
software
//
//Version: 5Sep2001
program Screen2Clipboard;
uses Windows,SysUtils;
resourcestring msgSyntax='Syntax: Screen2Clipboard [x1 y1 x2 y2]';
resourcestring msgSyntaxError='Syntax error!';
resourcestring msgNotNumber='x1, y1, x2, y2 must all be numbers!';
function CopyScreenToBitmap(x1,y1, x2,y2:integer):HBITMAP;
var screenDC, memDC : HDC;
bitmap, oldBitmap : HBITMAP;
nWidth, nHeight : integer;
begin
//screenDC := CreateDC('DISPLAY'#0, nil, nil, nil); //the 'DISPLAY' string
must be null-terminated, that's why #0 is appended to it
screenDC := GetWindowDC(0);
memDC := CreateCompatibleDC(screenDC);
nWidth := x2-x1+1;
nHeight := y2-y1+1;
bitmap := CreateCompatibleBitmap(screenDC, nWidth, nHeight);
oldBitmap := HBITMAP(SelectObject(memDC, bitmap));
BitBlt(memDC, 0, 0, nWidth, nHeight, screenDC, x1, y1, SRCCOPY);
bitmap := HBITMAP(SelectObject(memDC, oldBitmap));
DeleteDC(screenDC);
DeleteDC(memDC);
result:=bitmap;
end;
function copyBitmapToClipboard(bitmap:HBITMAP):boolean;
begin
result:=OpenClipboard(0{hwndGaia});
if(result) then
begin
EmptyClipboard();
SetClipboardData(CF_BITMAP, bitmap);
CloseClipboard();
end;
end;
var x1,y1, x2,y2:integer;
screenDC:HDC;
begin
if paramCount=0 then
begin
screenDC:=GetWindowDC(0);
x1:=0;
y1:=0;
x2:=GetDeviceCaps(screenDC,HORZRES)-1;
y2:=GetDeviceCaps(screenDC,VERTRES)-1;
end
else if paramCount=4 then
try
x1:=StrToInt(paramStr(1));
y1:=StrToInt(paramStr(2));
x2:=StrToInt(paramStr(3));
y2:=StrToInt(paramStr(4));
except
on EConvertError do
begin
MessageBox(0,pchar(msgNotNumber),pchar(msgSyntaxError),MB_ICONERROR);
halt(2);
exit; //add this here to avoid warning about x1,y1,x2,y2 possibility of
not being initialized
end;
end
else
begin
MessageBox(0,pchar(msgSyntax),pchar(msgSyntaxError),MB_ICONERROR);
halt(1);
exit; //add this here to avoid warning about x1,y1,x2,y2 possibility of
not being initialized
end;
if CopyBitmapToClipboard(CopyScreenToBitmap(x1,y1,x2,y2))
then halt(0)
else halt(3);
end.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime VCL and ActiveX controls (for PowerPoint/VB/Delphi etc.)
+ Plugs VCL and ActiveX controls (InterProcess/Internet communication)
+ TransFormations, VB6 forms to ASP.net WebForms convertion
http://www.kagi.com/birbilis + Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................