线程中怎么正确的释放Tbitmap-CSDN论坛
1个回答
展开全部
按照下面改,改完你测试下看看,俺测试没啥问题
// 线程类
pic = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure SyncProc; // 同步过程
end;
.....
// 修改了下,确保对象的"谁创建谁销毁"的原则, 这里的对象是 TBitmap
function GetHDCIMG(out aim: TBitmap; SCRhWnd: hWnd): Boolean; //截取指定窗口
var
ScrCanvas: TCanvas;
scrHDC: HDC;
R: TRect;
begin
Result := True;
scrcanvas := Tcanvas.Create;
try
try
scrhdc := GetWindowDC(scrhwnd);
scrcanvas.Handle := scrhdc;
Getwindowrect(scrhwnd, r);
aim.Canvas.Lock;
aim.Width := r.Right - r.Left;
aim.Height := r.Bottom - r.Top;
aim.Canvas.CopyRect(rect(0, 0, aim.Width, aim.Height), scrcanvas, rect(0,
0,
aim.Width, aim.Height));
releasedc(0, scrhdc);
except
Result := False;
end;
finally
aim.Canvas.Unlock;
scrcanvas.Free;
end;
end;
{ pic 实现}
procedure pic.SyncProc;
var
aimg: Tbitmap;
begin
aimg := TBitmap.Create;
while not Terminated do
begin
aimg.Assign(nil);
if GEThdcimg(aimg, 窗口句柄) then
begin
// * * * * * * * * * * * * * * * *
// 处理过程....
// * * * * * * * * * * * * * * * *
sleep(300);
end;
end;
FreeAndNil(aimg);
end;
procedure pic.Execute;
begin
Synchronize(SyncProc);
end;
// 调用
procedure TForm1.btn1Click(Sender: TObject);
var
picThread: pic;
begin
picThread := pic.Create(True);
picThread.FreeOnTerminate := True;
picThread.Resume;
end;
// 线程类
pic = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure SyncProc; // 同步过程
end;
.....
// 修改了下,确保对象的"谁创建谁销毁"的原则, 这里的对象是 TBitmap
function GetHDCIMG(out aim: TBitmap; SCRhWnd: hWnd): Boolean; //截取指定窗口
var
ScrCanvas: TCanvas;
scrHDC: HDC;
R: TRect;
begin
Result := True;
scrcanvas := Tcanvas.Create;
try
try
scrhdc := GetWindowDC(scrhwnd);
scrcanvas.Handle := scrhdc;
Getwindowrect(scrhwnd, r);
aim.Canvas.Lock;
aim.Width := r.Right - r.Left;
aim.Height := r.Bottom - r.Top;
aim.Canvas.CopyRect(rect(0, 0, aim.Width, aim.Height), scrcanvas, rect(0,
0,
aim.Width, aim.Height));
releasedc(0, scrhdc);
except
Result := False;
end;
finally
aim.Canvas.Unlock;
scrcanvas.Free;
end;
end;
{ pic 实现}
procedure pic.SyncProc;
var
aimg: Tbitmap;
begin
aimg := TBitmap.Create;
while not Terminated do
begin
aimg.Assign(nil);
if GEThdcimg(aimg, 窗口句柄) then
begin
// * * * * * * * * * * * * * * * *
// 处理过程....
// * * * * * * * * * * * * * * * *
sleep(300);
end;
end;
FreeAndNil(aimg);
end;
procedure pic.Execute;
begin
Synchronize(SyncProc);
end;
// 调用
procedure TForm1.btn1Click(Sender: TObject);
var
picThread: pic;
begin
picThread := pic.Create(True);
picThread.FreeOnTerminate := True;
picThread.Resume;
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询