200分求,屏幕对象抓图。(200分)

  • 主题发起人 主题发起人 cbdiy
  • 开始时间 开始时间
C

cbdiy

Unregistered / Unconfirmed
GUEST, unregistred user!
200分求,屏幕对象抓图。象HyperSnap那样可以在屏幕对象四周画矩形。清除,点击后抓取。

提供思路也可。 谢谢。
 
用 WINAPI 的 BitBlt 函数就可以实现
例子

uses WinTypes, WinProcs, Forms, Controls, Classes, Graphics;

function CaptureScreenRect( ARect: TRect ): TBitmap;
var
ScreenDC: HDC;
begin
Result := TBitmap.Create;
with Result, ARect do
begin
Width := Right - Left;
Height := Bottom - Top;
ScreenDC := GetDC( 0 );
try
BitBlt( Canvas.Handle, 0, 0, Width, Height, ScreenDC,
Left, Top, SRCCOPY );
finally
ReleaseDC( 0, ScreenDC );
end;
end;
end;

思路是这样的

parameter 是一个 TRect, 也就是一个 4 方形,你可以设定,函数是这样的
TRect defines a rectangle.
Unit
Types
Delphi syntax:
type
TRect = packed record
case Integer of
0: (Left, Top, Right, Bottom: Integer);
1: (TopLeft, BottomRight: TPoint);
end;

返回一个 Bitmap 也就是图像拉
创建一个新的 bitmap instance
HDC 是一个 device context (DC),也就可以利用 BitBlt 把windows 图像转到 bitmap 里了。
完整代码在这里,朋友可以直接调用

unit ScrnCap;

interface
uses WinTypes, WinProcs, Forms, Controls, Classes, Graphics;

function CaptureScreenRect( ARect: TRect ): TBitmap;
function CaptureScreen: TBitmap;
function CaptureClientImage( Control: TControl ): TBitmap;
function CaptureControlImage( Control: TControl ): TBitmap;
function CaptureWindowImage( Wnd: HWND ): TBitmap;

implementation

{==============================================================================}
{ Use this to capture a rectangle on the screen... }
function CaptureScreenRect( ARect: TRect ): TBitmap;
{==============================================================================}
var
ScreenDC: HDC;
begin
Result := TBitmap.Create;
with Result, ARect do
begin
Width := Right - Left;
Height := Bottom - Top;
ScreenDC := GetDC( 0 );
try
BitBlt( Canvas.Handle, 0, 0, Width, Height, ScreenDC,
Left, Top, SRCCOPY );
finally
ReleaseDC( 0, ScreenDC );
end;
end;
end;

{==============================================================================}
{ Use this to capture the entire screen... }
function CaptureScreen: TBitmap;
{==============================================================================}
begin
with Screen do
Result := CaptureScreenRect( Rect( 0, 0, Width, Height ));
end;

{==============================================================================}
{ Use this to capture just the client area of a form or control... }
function CaptureClientImage( Control: TControl ): TBitmap;
{==============================================================================}
begin
with Control, Control.ClientOrigin do
Result := CaptureScreenRect( Bounds( X, Y, ClientWidth,
ClientHeight ));
end;

{==============================================================================}
{ Use this to capture an entire form or control... }
function CaptureControlImage( Control: TControl ): TBitmap;
{==============================================================================}
begin
with Control do
if Parent = nil then
Result := CaptureScreenRect( Bounds( Left, Top, Width,
Height ))
else
with Parent.ClientToScreen( Point( Left, Top )) do
Result := CaptureScreenRect( Bounds( X, Y, Width,
Height ));
end;

{==============================================================================}
{ Use this to capture an entire form or control paased as an API hWnd... }
function CaptureWindowImage( Wnd: HWND ): TBitmap;
{==============================================================================}
var
R: TRect;
begin
GetWindowRect(Wnd, R);
Result := CaptureScreenRect(R);
end;


end.


 
谢.幕后黑手.
以上的方法可以抓幕。但我的问题不在这。
我的问题:object抓图,appliction最小化后。mouse在屏幕上移动。当移入有些工具栏时画的矩形无法正常清除(采用异或)??因为工具栏的button也重画了?

另一个想法:把屏幕先copy到窗体上,mouse在窗体上移动。但无法GetWindowRect(Wnd, R);得到矩形。因为它始终是form??有没有其它办法???
 
思路:做一个Mouse Hook Dll记录下鼠标拉的框就可以了,别的什么画矩形都不用你管。
 
可以用全局鼠标钩子判断 lefdown 然后开始记录 position 再算一下不会太难吧
 
得到矩形没问题 。
可是清矩形有问题 。最好是在静态的form上画。别的 抓图软件好象都是这样做的。
 
我有现存的原代码,怎么给你.
 
jianguobu :
cbdiy@163.com thanks
 
已发出,注意查收
 
up一下,
问题还未能得到解决.
 
...........
没收到邮件吗?
 
谢谢jianguobu。收到了,你那只有矩形和全屏捉图。
 
to:jianguobu
我也正在研究这方面的东西,也给我发一个,谢了
xiaoxin183@21cn.com
 
陈宽达好像有个例子
 
To jianguobu:
能发给我一份吗?谢谢.
yckxzjj@163.com
 
谢谢,各位啦。我已想到办法
 
To jianguobu:
我也要一份,可以吗?谢谢!!
bankemi78@126.com
 
多人接受答案了。
 
后退
顶部