X
xuxiaohan
Unregistered / Unconfirmed
GUEST, unregistred user!
unit GifOleImpl;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActiveX, AxCtrls, GifOleProj_TLB, StdVcl, GifImage, ExtCtrls, StdCtrls;
const
cm_Animate = WM_USER+1;
type
TGifOle = Class;
TDrawThd = class(TThread)
private
Fimage: TGifImage;
FGifOle: TGifOle;
FrameIndex: integer;
procedure DrawImage;
public
Constructor Create(GifOle: TGifOle);
procedure Execute
override;
end;
TGifOle = class(TActiveForm, IGifOle, IpersistStorage)
Image1: TImage;
procedure ActiveFormCreate(Sender: TObject);
procedure ActiveFormDestroy(Sender: TObject);
private
{ Private declarations }
FimageIndex: integer;
DrawThd: TDrawThd;
procedure internalpaintGif;
procedure SetimageIndex(const Value: integer);
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd)
message WM_ERASEBKGND;
procedure WakeMainThread(Sender: TObject);
procedure cmAnimate(var msg: TMessage)
message CM_ANIMATE;
protected
{ Protected declarations }
function IsDirty: HResult
stdcall;
function InitNew(const stg: IStorage): HResult
stdcall;
function Load(const stg: IStorage): HResult
stdcall;
function Save(const stgSave: IStorage
fSameAsLoad: BOOL): HResult;
stdcall;
function SaveCompleted(const stgNew: IStorage): HResult
stdcall;
function HandsOffStorage: HResult
stdcall;
function GetClassID(out classID: TCLSID): HResult
stdcall;
property imageIndex: integer read FimageIndex write SetimageIndex;
// procedure WndProc(var message: TMessage)
override;
// procedure WmPaint(var message: TwmPaint)
message WM_PAINT;
procedure Run;
public
{ Public declarations }
Gif: TGifImage;
procedure LoadFromFile(const FileName: WideString)
safecall;
procedure PaintGif
safeCall;
procedure paint
override;
end;
implementation
uses ComObj, ComServ;
{$R *.DFM}
{ TGifOle }
procedure TGifOle.LoadFromFile(const FileName: WideString);
var
i: integer;
msg: TMsg;
begin
Gif.LoadFromFile(FileName);
ClientWidth := Gif.Width;
ClientHeight := Gif.Height;
// Image1.Picture.Assign(Gif);
//imageIndex := 0;
DrawThd:=TDrawThd.Create(self);
// DrawThd.Resume;
end;
procedure TGifOle.PaintGif
safeCall;
begin
internalpaintGif;
end;
procedure TGifOle.ActiveFormCreate(Sender: TObject);
begin
Classes.WakeMainThread := WakeMainThread;
Gif := TGifImage.Create;
end;
procedure TGifOle.ActiveFormDestroy(Sender: TObject);
begin
if Gif <> nil then Gif.Free;
if self.DrawThd<>nil then
begin
DrawThd.Terminate;
DrawThd.Free;
end;
//Classes.WakeMainThread := nil;
end;
procedure TGifOle.paint;
begin
// inherited;
// PaintGif;
//gif.Draw(canvas, Rect(0, 0, gif.Width, gif.Height));
// beep;
end;
procedure TGifOle.internalpaintGif;
var
R: TRect;
DC: HDC;
cvs: TCanvas;
begin
R:=Rect(0, 0, gif.Width, gif.Height);
if gif.Empty then exit;
[blue]奇怪的是,无法在ActiveForm 的canvas, ActiveForm 中的panel的canvas都无法draw[/blue];
{ gif.Images.SubImages[Imageindex].Draw(canvas, R,
(goTransparent in gif.DrawOptions), (goTile in Gif.DrawOptions));
imageIndex := ImageIndex + 1;
}
[red]在桌面上画就可以,奇怪!!!!;[/red]
Dc:=getDC(0);
Try
cvs:=TCanvas.Create;
cvs.Handle:=Dc;
gif.Images.SubImages[Imageindex].Draw(cvs, R,
(goTransparent in gif.DrawOptions), (goTile in Gif.DrawOptions));
imageIndex := ImageIndex + 1;
finally
cvs.Free;
ReleaseDc(0, Dc);
end;
end;
procedure TGifOle.SetimageIndex(const Value: integer);
begin
if (value >= 0) and (value <> FimageIndex) then
begin
if value > gif.Images.Count - 1 then
FImageIndex := 0
else
FimageIndex := Value;
end;
end;
function TGifOle.HandsOffStorage: HResult;
begin
result := S_OK;
end;
function TGifOle.InitNew(const stg: IStorage): HResult;
begin
Result := S_OK;
end;
function TGifOle.IsDirty: HResult;
begin
Result := S_OK;
end;
function TGifOle.Load(const stg: IStorage): HResult;
var
stmData: IStream;
OS: TOleStream;
begin
if SUCCEEDED(stg.OpenStream('GifData', nil,
STGM_READWRITE or STGM_SHARE_EXCLUSIVE, 0, stmData)) then
begin
OS := TOleStream.Create(stmData);
try
Gif.LoadFromStream(os);
//image1.Picture.Assign(Gif);
finally
OS.Free;
end;
end;
Result := S_OK;
end;
function TGifOle.Save(const stgSave: IStorage
fSameAsLoad: BOOL): HResult;
var
stmData: IStream;
OS: TOleStream;
begin
OleCheck(stgSave.CreateStream('GifData',
STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE, 0, 0, stmData));
OS := TOleStream.Create(stmData);
try
Gif.SaveToStream(OS);
finally
OS.Free;
end;
Result := S_OK;
end;
function TGifOle.SaveCompleted(const stgNew: IStorage): HResult;
begin
result := S_Ok;
end;
function TGifOle.GetClassID(out classID: TCLSID): HResult;
begin
ClassId := CLASS_GifOle;
Result:=S_OK;
end;
procedure TGifOle.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
message.Result:=1;
end;
{
procedure TGifOle.WndProc(var message: TMessage);
begin
// if message.Msg<>WM_PAINT THEN inherited WndProc(message);
if message.Msg = WM_NULL then
begin
CheckSynchronize;
//internalpaintGif;
end
else
inherited WndProc(message);
end;
}
procedure TGifOle.WakeMainThread(Sender: TObject);
begin
if PostMessage(handle, WM_NULL, 0, 0) then //beep;
end;
procedure TGifOle.Run;
var
msg: TMsg;
begin
while PeekMessage(Msg, handle, 0, 0, PM_REMOVE) do
if Msg.message = WM_NULL then
CheckSynchronize
else
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
procedure TGifOle.cmAnimate(var msg: TMessage);
begin
PaintGif;
end;
{
procedure TGifOle.WmPaint(var message: TwmPaint);
begin
inherited;
PaintGif;
beep;
end;
}
{ TDrawThd }
constructor TDrawThd.Create(GifOle: TGifOle);
begin
FGifOle:=GifOle;
FImage:=GifOle.Gif;
FrameIndex:=0;
inherited create(false);
end;
procedure TDrawThd.DrawImage;
var
delay: integer;
R: TRect;
begin
{
R:=Rect(0, 0, Fimage.Width, Fimage.Height);
Fimage.Images.SubImages[Frameindex].Draw(FGifOle.Canvas, R, true, (goTile in Fimage.DrawOptions));
// Fgifole.Canvas.Draw(0, 0, Fimage.Images.SubImages[FrameIndex].Bitmap);
}
// postmessage(FGifole.Handle, CM_INVALIDATE, 0, 0);
// FGifole.Invalidate;
postMessage(FGifole.Handle, CM_ANIMATE, 0, 0);
// InvalidateRect(FHandle, nil, not (csOpaque in ControlStyle))
// self.FGifOle.Invalidate;
delay:=Fimage.Images.SubImages[FrameIndex].GraphicControlExtension.Delay;
if delay=0 then Delay:=10;
sleep(Delay*11);
inc(FrameIndex);
if FrameIndex>Fimage.Images.Count-1 then FrameIndex:=0;
// beep;
end;
procedure TDrawThd.Execute;
begin
while not Terminated do DrawImage;
end;
initialization
TActiveFormFactory.Create(
ComServer,
TActiveFormControl,
TGifOle,
Class_GifOle,
1,
'',
OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
tmApartment);
end.
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActiveX, AxCtrls, GifOleProj_TLB, StdVcl, GifImage, ExtCtrls, StdCtrls;
const
cm_Animate = WM_USER+1;
type
TGifOle = Class;
TDrawThd = class(TThread)
private
Fimage: TGifImage;
FGifOle: TGifOle;
FrameIndex: integer;
procedure DrawImage;
public
Constructor Create(GifOle: TGifOle);
procedure Execute
override;
end;
TGifOle = class(TActiveForm, IGifOle, IpersistStorage)
Image1: TImage;
procedure ActiveFormCreate(Sender: TObject);
procedure ActiveFormDestroy(Sender: TObject);
private
{ Private declarations }
FimageIndex: integer;
DrawThd: TDrawThd;
procedure internalpaintGif;
procedure SetimageIndex(const Value: integer);
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd)
message WM_ERASEBKGND;
procedure WakeMainThread(Sender: TObject);
procedure cmAnimate(var msg: TMessage)
message CM_ANIMATE;
protected
{ Protected declarations }
function IsDirty: HResult
stdcall;
function InitNew(const stg: IStorage): HResult
stdcall;
function Load(const stg: IStorage): HResult
stdcall;
function Save(const stgSave: IStorage
fSameAsLoad: BOOL): HResult;
stdcall;
function SaveCompleted(const stgNew: IStorage): HResult
stdcall;
function HandsOffStorage: HResult
stdcall;
function GetClassID(out classID: TCLSID): HResult
stdcall;
property imageIndex: integer read FimageIndex write SetimageIndex;
// procedure WndProc(var message: TMessage)
override;
// procedure WmPaint(var message: TwmPaint)
message WM_PAINT;
procedure Run;
public
{ Public declarations }
Gif: TGifImage;
procedure LoadFromFile(const FileName: WideString)
safecall;
procedure PaintGif
safeCall;
procedure paint
override;
end;
implementation
uses ComObj, ComServ;
{$R *.DFM}
{ TGifOle }
procedure TGifOle.LoadFromFile(const FileName: WideString);
var
i: integer;
msg: TMsg;
begin
Gif.LoadFromFile(FileName);
ClientWidth := Gif.Width;
ClientHeight := Gif.Height;
// Image1.Picture.Assign(Gif);
//imageIndex := 0;
DrawThd:=TDrawThd.Create(self);
// DrawThd.Resume;
end;
procedure TGifOle.PaintGif
safeCall;
begin
internalpaintGif;
end;
procedure TGifOle.ActiveFormCreate(Sender: TObject);
begin
Classes.WakeMainThread := WakeMainThread;
Gif := TGifImage.Create;
end;
procedure TGifOle.ActiveFormDestroy(Sender: TObject);
begin
if Gif <> nil then Gif.Free;
if self.DrawThd<>nil then
begin
DrawThd.Terminate;
DrawThd.Free;
end;
//Classes.WakeMainThread := nil;
end;
procedure TGifOle.paint;
begin
// inherited;
// PaintGif;
//gif.Draw(canvas, Rect(0, 0, gif.Width, gif.Height));
// beep;
end;
procedure TGifOle.internalpaintGif;
var
R: TRect;
DC: HDC;
cvs: TCanvas;
begin
R:=Rect(0, 0, gif.Width, gif.Height);
if gif.Empty then exit;
[blue]奇怪的是,无法在ActiveForm 的canvas, ActiveForm 中的panel的canvas都无法draw[/blue];
{ gif.Images.SubImages[Imageindex].Draw(canvas, R,
(goTransparent in gif.DrawOptions), (goTile in Gif.DrawOptions));
imageIndex := ImageIndex + 1;
}
[red]在桌面上画就可以,奇怪!!!!;[/red]
Dc:=getDC(0);
Try
cvs:=TCanvas.Create;
cvs.Handle:=Dc;
gif.Images.SubImages[Imageindex].Draw(cvs, R,
(goTransparent in gif.DrawOptions), (goTile in Gif.DrawOptions));
imageIndex := ImageIndex + 1;
finally
cvs.Free;
ReleaseDc(0, Dc);
end;
end;
procedure TGifOle.SetimageIndex(const Value: integer);
begin
if (value >= 0) and (value <> FimageIndex) then
begin
if value > gif.Images.Count - 1 then
FImageIndex := 0
else
FimageIndex := Value;
end;
end;
function TGifOle.HandsOffStorage: HResult;
begin
result := S_OK;
end;
function TGifOle.InitNew(const stg: IStorage): HResult;
begin
Result := S_OK;
end;
function TGifOle.IsDirty: HResult;
begin
Result := S_OK;
end;
function TGifOle.Load(const stg: IStorage): HResult;
var
stmData: IStream;
OS: TOleStream;
begin
if SUCCEEDED(stg.OpenStream('GifData', nil,
STGM_READWRITE or STGM_SHARE_EXCLUSIVE, 0, stmData)) then
begin
OS := TOleStream.Create(stmData);
try
Gif.LoadFromStream(os);
//image1.Picture.Assign(Gif);
finally
OS.Free;
end;
end;
Result := S_OK;
end;
function TGifOle.Save(const stgSave: IStorage
fSameAsLoad: BOOL): HResult;
var
stmData: IStream;
OS: TOleStream;
begin
OleCheck(stgSave.CreateStream('GifData',
STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE, 0, 0, stmData));
OS := TOleStream.Create(stmData);
try
Gif.SaveToStream(OS);
finally
OS.Free;
end;
Result := S_OK;
end;
function TGifOle.SaveCompleted(const stgNew: IStorage): HResult;
begin
result := S_Ok;
end;
function TGifOle.GetClassID(out classID: TCLSID): HResult;
begin
ClassId := CLASS_GifOle;
Result:=S_OK;
end;
procedure TGifOle.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
message.Result:=1;
end;
{
procedure TGifOle.WndProc(var message: TMessage);
begin
// if message.Msg<>WM_PAINT THEN inherited WndProc(message);
if message.Msg = WM_NULL then
begin
CheckSynchronize;
//internalpaintGif;
end
else
inherited WndProc(message);
end;
}
procedure TGifOle.WakeMainThread(Sender: TObject);
begin
if PostMessage(handle, WM_NULL, 0, 0) then //beep;
end;
procedure TGifOle.Run;
var
msg: TMsg;
begin
while PeekMessage(Msg, handle, 0, 0, PM_REMOVE) do
if Msg.message = WM_NULL then
CheckSynchronize
else
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
procedure TGifOle.cmAnimate(var msg: TMessage);
begin
PaintGif;
end;
{
procedure TGifOle.WmPaint(var message: TwmPaint);
begin
inherited;
PaintGif;
beep;
end;
}
{ TDrawThd }
constructor TDrawThd.Create(GifOle: TGifOle);
begin
FGifOle:=GifOle;
FImage:=GifOle.Gif;
FrameIndex:=0;
inherited create(false);
end;
procedure TDrawThd.DrawImage;
var
delay: integer;
R: TRect;
begin
{
R:=Rect(0, 0, Fimage.Width, Fimage.Height);
Fimage.Images.SubImages[Frameindex].Draw(FGifOle.Canvas, R, true, (goTile in Fimage.DrawOptions));
// Fgifole.Canvas.Draw(0, 0, Fimage.Images.SubImages[FrameIndex].Bitmap);
}
// postmessage(FGifole.Handle, CM_INVALIDATE, 0, 0);
// FGifole.Invalidate;
postMessage(FGifole.Handle, CM_ANIMATE, 0, 0);
// InvalidateRect(FHandle, nil, not (csOpaque in ControlStyle))
// self.FGifOle.Invalidate;
delay:=Fimage.Images.SubImages[FrameIndex].GraphicControlExtension.Delay;
if delay=0 then Delay:=10;
sleep(Delay*11);
inc(FrameIndex);
if FrameIndex>Fimage.Images.Count-1 then FrameIndex:=0;
// beep;
end;
procedure TDrawThd.Execute;
begin
while not Terminated do DrawImage;
end;
initialization
TActiveFormFactory.Create(
ComServer,
TActiveFormControl,
TGifOle,
Class_GifOle,
1,
'',
OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
tmApartment);
end.