我的方法没用吗?我都是这样用的呀
////////////////////////////////////////
// DFM 文件
///////////////////////////////////////
object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 296
Top = 160
Width = 384
Height = 288
end
object Image2: TImage
Left = 296
Top = 160
Width = 384
Height = 288
Transparent = True
OnMouseDown = Image2MouseDown
OnMouseMove = Image2MouseMove
OnMouseUp = Image2MouseUp
end
end
///////////////////////////////////////////////
// Pas 文件
///////////////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage; // 一定要放到 Image1 前面,并设其 TransParent 为 True;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Image2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image2MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image2MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses VFW;
var
wndCap: HWND;
info: TBitmapInfo;
bmpCap: TBitmap;
canProc: Boolean;
x0, x1, y0, y1: integer;
procedure FrameToBitmap(Bitmap: TBitmap; FrameBuffer: pointer; BitmapInfo: TBitmapInfo);
begin
if bitmapInfo.bmiHeader.BiCompression = bi_RGB then
with Bitmap do
setDiBits(canvas.handle, handle, 0, BitmapInfo.bmiHeader.biheight,
FrameBuffer, BitmapInfo, DIB_RGB_COLORS);
end;
function myProc(hWnd:HWND; lpVHdr
VIDEOHDR):LongInt; stdcall;
begin
result := 0;
FrameToBitmap(bmpCap, lpvhdr^.lpData, info);
Form1.Image1.Picture.Bitmap := bmpCap;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := true;
canProc := False;
bmpCap := TBitmap.Create;
with bmpCap do
begin
Width := 384;
Height := 288;
PixelFormat := pf24bit;
end;
with Image2.Picture.Bitmap do
begin
Width := Image2.Width;
Height := Image2.Height;
PixelFormat := pf24bit;
Canvas.Pen.Style := psDot;
Canvas.Brush.Style := bsClear;
end;
wndCap := capCreateCaptureWindow('a', ws_visible or ws_child, 0, 0, 4, 3, Handle, 100);
capDriverConnect(wndCap, 0);
capPreview(wndCap, true);
capPreviewRate(wndCap, 40);
capGetVideoFormat(wndCap, @info, sizeof(TBitmapInfo));
capSetCallbackOnFrame(wndCap, myProc);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DestroyWindow(wndCap);
bmpCap.Free;
end;
procedure TForm1.Image2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
canProc := True;
x0 := x;
y0 := y;
x1 := x;
y1 := y;
end;
procedure TForm1.Image2MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if canProc then
begin
with Image2.Picture.Bitmap.Canvas do
begin
Pen.Color := clWhite; // Pen.Mode := pmXor 及 DrawFocusRect 都不好用
Rectangle(x0, y0, x1, y1);
x1 := x;
y1 := y;
Pen.Color := clBlack;
Rectangle(x0, y0, x, y);
end;
end;
end;
procedure TForm1.Image2MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
canProc := False;
x1 := x;
y1 := y;
bitblt(Canvas.Handle, 0, 0, x1 - x0, y1 - y0,
Image1.Canvas.Handle, x0, y0, SRCCOPY);
end;
end.