我要实现的是:想这个GIF动画在第一次单击Image时,先运行0-90度后停,第二次单击Image时运行90-180度,以些类推最后是270-0度 , 也就是单击一下Image,GIF变化一部分,再单击Image,GIF再变化.帮看看注解一下这些代码,能实现上面的要求,但是又发现一个问题:0-90-180-270-0本来应该是这样转的,可是现在转到270时,就变成应该在0时才变的图的啦,也就是少转了270-0这部分,好像是在定义时把270度时当成0度设置了,我不知道这部分代码是在哪里实现的?这些代码是我参考别人的。请帮帮忙,谢谢 !
{ TMyImage }procedure TMyImage.CalcButtonLayout(Canvas: TCanvas; const Client: TRect;
const Offset: TPoint; const Caption: string; Layout: TButtonLayout;
Margin, Spacing: Integer; var GlyphPos: TPoint; var TextBounds: TRect;
BiDiFlags: Integer);
var
TextPos: TPoint;
ClientSize, GlyphSize, TextSize: TPoint;
TotalSize: TPoint;
begin
if (BiDiFlags and DT_RIGHT) = DT_RIGHT then
if Layout = blGlyphLeft then Layout := blGlyphRight
else
if Layout = blGlyphRight then Layout := blGlyphLeft;
{ calculate the item sizes }
ClientSize := Point(Client.Right - Client.Left, Client.Bottom -
Client.Top); GlyphSize := Point(0, 0); if Length(Caption) > 0 then
begin
TextBounds := Rect(0, 0, Client.Right - Client.Left, 0);
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds,
DT_CALCRECT or BiDiFlags);
TextSize := Point(TextBounds.Right - TextBounds.Left, TextBounds.Bottom -
TextBounds.Top);
end
else
begin
TextBounds := Rect(0, 0, 0, 0);
TextSize := Point(0,0);
end; { If the layout has the glyph on the right or the left, then both the
text and the glyph are centered vertically. If the glyph is on the top
or the bottom, then both the text and the glyph are centered horizontally.}
if Layout in [blGlyphLeft, blGlyphRight] then
begin
GlyphPos.Y := (ClientSize.Y - GlyphSize.Y + 1) div 2;
TextPos.Y := (ClientSize.Y - TextSize.Y + 1) div 2;
end
else
begin
GlyphPos.X := (ClientSize.X - GlyphSize.X + 1) div 2;
TextPos.X := (ClientSize.X - TextSize.X + 1) div 2;
end; { if there is no text or no bitmap, then Spacing is irrelevant }
if (TextSize.X = 0) or (GlyphSize.X = 0) then
Spacing := 0; { adjust Margin and Spacing }
if Margin = -1 then
begin
if Spacing = -1 then
begin
TotalSize := Point(GlyphSize.X + TextSize.X, GlyphSize.Y + TextSize.Y);
if Layout in [blGlyphLeft, blGlyphRight] then
Margin := (ClientSize.X - TotalSize.X) div 3
else
Margin := (ClientSize.Y - TotalSize.Y) div 3;
Spacing := Margin;
end
else
begin
TotalSize := Point(GlyphSize.X + Spacing + TextSize.X, GlyphSize.Y +
Spacing + TextSize.Y);
if Layout in [blGlyphLeft, blGlyphRight] then
Margin := (ClientSize.X - TotalSize.X + 1) div 2
else
Margin := (ClientSize.Y - TotalSize.Y + 1) div 2;
end;
end
else
begin
if Spacing = -1 then
begin
TotalSize := Point(ClientSize.X - (Margin + GlyphSize.X), ClientSize.Y -
(Margin + GlyphSize.Y));
if Layout in [blGlyphLeft, blGlyphRight] then
Spacing := (TotalSize.X - TextSize.X) div 2
else
Spacing := (TotalSize.Y - TextSize.Y) div 2;
end;
end; case Layout of
blGlyphLeft:
begin
GlyphPos.X := Margin;
TextPos.X := GlyphPos.X + GlyphSize.X + Spacing;
end;
blGlyphRight:
begin
GlyphPos.X := ClientSize.X - Margin - GlyphSize.X;
TextPos.X := GlyphPos.X - Spacing - TextSize.X;
end;
blGlyphTop:
begin
GlyphPos.Y := Margin;
TextPos.Y := GlyphPos.Y + GlyphSize.Y + Spacing;
end;
blGlyphBottom:
begin
GlyphPos.Y := ClientSize.Y - Margin - GlyphSize.Y;
TextPos.Y := GlyphPos.Y - Spacing - TextSize.Y;
end;
end; { fixup the result variables }
with GlyphPos do
begin
Inc(X, Client.Left + Offset.X);
Inc(Y, Client.Top + Offset.Y);
end; { Themed text is not shifted, but gets a different color. }
if ThemeServices.ThemesEnabled then
OffsetRect(TextBounds, TextPos.X + Client.Left, TextPos.Y + Client.Top)
else
OffsetRect(TextBounds, TextPos.X + Client.Left + Offset.X, TextPos.Y + Client.Top + Offset.Y);end;procedure TMyImage.CMTextChanged(var Message: TMessage);
begin
Invalidate;
end;constructor TMyImage.Create(AOwner: TComponent);
begin
FGIFImage := TGIFImage.Create;
inherited;
FPlace := 1;
end;destructor TMyImage.Destroy;
begin inherited;
FGIFImage.Free;
end;procedure TMyImage.Paint;
var
PaintRect: TRect;
procedure DrawImage(Index: Integer);
begin
Index := Min(Index, FGIFImage.Images.Count - 1);
if Index >= 0 then
FGIFImage.Images[Index].Draw(Canvas, PaintRect, FTransparent, False)
else
Canvas.Rectangle(PaintRect);
end;
var
Bitmap: TBitmap;
Offset: TPoint;
GlyphPos: TPoint;
TextBounds: TRect;begin
inherited;
PaintRect := Rect(0, 0, Width, Height);
if csDesigning in ComponentState then
begin
Canvas.Pen.Style := psDash;
Canvas.Brush.Style := bsClear;
DrawImage(0);
end else begin
Canvas.Pen.Style := psClear;
Canvas.Brush.Style := bsClear;
if FPlace<>0 then
begin
DrawImage(FIndex);
end else begin
Bitmap:=TBitmap.Create;
if FState=1 then
begin
Bitmap.LoadFromResourceName(hInstance, 'WHEEL_01');
end
else
if FState=2 then
begin
Bitmap.LoadFromResourceName(hInstance, 'WHEEL_02');
end;
Canvas.StretchDraw(PaintRect, Bitmap);
Bitmap.Free;
end;
end;
Canvas.Font := Self.Font;
Offset.X := 0;
Offset.Y := 0;
CalcButtonLayout(Canvas, PaintRect, Offset, Caption, blGlyphLeft, -1, 4,
GlyphPos, TextBounds, DrawTextBiDiModeFlags(0));
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds,
DT_CENTER or DT_VCENTER or DrawTextBiDiModeFlags(0));
end;procedure TMyImage.RunToPlace;
begin
if FPlace <> 0 then
begin
if FIndex <> (FPlace - 1) * (FGIFImage.Images.Count div 4) then
begin
Inc(FIndex);
FIndex := FIndex mod FGIFImage.Images.Count;
end;
end;
Invalidate;
end;procedure TMyImage.SetGIFImage(const Value: TGIFImage);
begin
FGIFImage.Assign(Value);
Invalidate;
end;procedure TMyImage.SetIndex(const Value: Integer);
begin
FIndex := Value;
Invalidate;
end;procedure TMyImage.SetState(const Value: Integer);
begin
FState := Value;
case FState of
1:
begin
FRects[1] := Rect(184, 98, 0, 0);
FRects[2] := Rect(103, 179, 0, 0);
FRects[3] := Rect(23, 98, 0, 0);
FRects[4] := Rect(103, 19, 0, 0);
end;
2:
begin
FRects[1] := Rect(184, 98, 0, 0);
FRects[2] := Rect(-100, 0, 0, 0);
FRects[3] := Rect(23, 98, 0, 0);
FRects[4] := Rect(-100, 0, 0, 0);
end;
else
begin
FRects[1] := Rect(-100, 0, 0, 0);
FRects[2] := Rect(-100, 0, 0, 0);
FRects[3] := Rect(-100, 0, 0, 0);
FRects[4] := Rect(-100, 0, 0, 0);
end;
end;
Invalidate;
end;
事件代码如下:
procedure TForm1.MyImage2Click(Sender: TObject);
begin
TMyImage(Sender).ImagePlace:=(TMyImage(Sender).ImagePlace+1+Ord((TMyImage(Sender).ImageState=2) and (TMyImage(Sender).ImagePlace<>0))) mod 5;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
MyImage2.RunToPlace;
end;
{ TMyImage }procedure TMyImage.CalcButtonLayout(Canvas: TCanvas; const Client: TRect;
const Offset: TPoint; const Caption: string; Layout: TButtonLayout;
Margin, Spacing: Integer; var GlyphPos: TPoint; var TextBounds: TRect;
BiDiFlags: Integer);
var
TextPos: TPoint;
ClientSize, GlyphSize, TextSize: TPoint;
TotalSize: TPoint;
begin
if (BiDiFlags and DT_RIGHT) = DT_RIGHT then
if Layout = blGlyphLeft then Layout := blGlyphRight
else
if Layout = blGlyphRight then Layout := blGlyphLeft;
{ calculate the item sizes }
ClientSize := Point(Client.Right - Client.Left, Client.Bottom -
Client.Top); GlyphSize := Point(0, 0); if Length(Caption) > 0 then
begin
TextBounds := Rect(0, 0, Client.Right - Client.Left, 0);
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds,
DT_CALCRECT or BiDiFlags);
TextSize := Point(TextBounds.Right - TextBounds.Left, TextBounds.Bottom -
TextBounds.Top);
end
else
begin
TextBounds := Rect(0, 0, 0, 0);
TextSize := Point(0,0);
end; { If the layout has the glyph on the right or the left, then both the
text and the glyph are centered vertically. If the glyph is on the top
or the bottom, then both the text and the glyph are centered horizontally.}
if Layout in [blGlyphLeft, blGlyphRight] then
begin
GlyphPos.Y := (ClientSize.Y - GlyphSize.Y + 1) div 2;
TextPos.Y := (ClientSize.Y - TextSize.Y + 1) div 2;
end
else
begin
GlyphPos.X := (ClientSize.X - GlyphSize.X + 1) div 2;
TextPos.X := (ClientSize.X - TextSize.X + 1) div 2;
end; { if there is no text or no bitmap, then Spacing is irrelevant }
if (TextSize.X = 0) or (GlyphSize.X = 0) then
Spacing := 0; { adjust Margin and Spacing }
if Margin = -1 then
begin
if Spacing = -1 then
begin
TotalSize := Point(GlyphSize.X + TextSize.X, GlyphSize.Y + TextSize.Y);
if Layout in [blGlyphLeft, blGlyphRight] then
Margin := (ClientSize.X - TotalSize.X) div 3
else
Margin := (ClientSize.Y - TotalSize.Y) div 3;
Spacing := Margin;
end
else
begin
TotalSize := Point(GlyphSize.X + Spacing + TextSize.X, GlyphSize.Y +
Spacing + TextSize.Y);
if Layout in [blGlyphLeft, blGlyphRight] then
Margin := (ClientSize.X - TotalSize.X + 1) div 2
else
Margin := (ClientSize.Y - TotalSize.Y + 1) div 2;
end;
end
else
begin
if Spacing = -1 then
begin
TotalSize := Point(ClientSize.X - (Margin + GlyphSize.X), ClientSize.Y -
(Margin + GlyphSize.Y));
if Layout in [blGlyphLeft, blGlyphRight] then
Spacing := (TotalSize.X - TextSize.X) div 2
else
Spacing := (TotalSize.Y - TextSize.Y) div 2;
end;
end; case Layout of
blGlyphLeft:
begin
GlyphPos.X := Margin;
TextPos.X := GlyphPos.X + GlyphSize.X + Spacing;
end;
blGlyphRight:
begin
GlyphPos.X := ClientSize.X - Margin - GlyphSize.X;
TextPos.X := GlyphPos.X - Spacing - TextSize.X;
end;
blGlyphTop:
begin
GlyphPos.Y := Margin;
TextPos.Y := GlyphPos.Y + GlyphSize.Y + Spacing;
end;
blGlyphBottom:
begin
GlyphPos.Y := ClientSize.Y - Margin - GlyphSize.Y;
TextPos.Y := GlyphPos.Y - Spacing - TextSize.Y;
end;
end; { fixup the result variables }
with GlyphPos do
begin
Inc(X, Client.Left + Offset.X);
Inc(Y, Client.Top + Offset.Y);
end; { Themed text is not shifted, but gets a different color. }
if ThemeServices.ThemesEnabled then
OffsetRect(TextBounds, TextPos.X + Client.Left, TextPos.Y + Client.Top)
else
OffsetRect(TextBounds, TextPos.X + Client.Left + Offset.X, TextPos.Y + Client.Top + Offset.Y);end;procedure TMyImage.CMTextChanged(var Message: TMessage);
begin
Invalidate;
end;constructor TMyImage.Create(AOwner: TComponent);
begin
FGIFImage := TGIFImage.Create;
inherited;
FPlace := 1;
end;destructor TMyImage.Destroy;
begin inherited;
FGIFImage.Free;
end;procedure TMyImage.Paint;
var
PaintRect: TRect;
procedure DrawImage(Index: Integer);
begin
Index := Min(Index, FGIFImage.Images.Count - 1);
if Index >= 0 then
FGIFImage.Images[Index].Draw(Canvas, PaintRect, FTransparent, False)
else
Canvas.Rectangle(PaintRect);
end;
var
Bitmap: TBitmap;
Offset: TPoint;
GlyphPos: TPoint;
TextBounds: TRect;begin
inherited;
PaintRect := Rect(0, 0, Width, Height);
if csDesigning in ComponentState then
begin
Canvas.Pen.Style := psDash;
Canvas.Brush.Style := bsClear;
DrawImage(0);
end else begin
Canvas.Pen.Style := psClear;
Canvas.Brush.Style := bsClear;
if FPlace<>0 then
begin
DrawImage(FIndex);
end else begin
Bitmap:=TBitmap.Create;
if FState=1 then
begin
Bitmap.LoadFromResourceName(hInstance, 'WHEEL_01');
end
else
if FState=2 then
begin
Bitmap.LoadFromResourceName(hInstance, 'WHEEL_02');
end;
Canvas.StretchDraw(PaintRect, Bitmap);
Bitmap.Free;
end;
end;
Canvas.Font := Self.Font;
Offset.X := 0;
Offset.Y := 0;
CalcButtonLayout(Canvas, PaintRect, Offset, Caption, blGlyphLeft, -1, 4,
GlyphPos, TextBounds, DrawTextBiDiModeFlags(0));
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds,
DT_CENTER or DT_VCENTER or DrawTextBiDiModeFlags(0));
end;procedure TMyImage.RunToPlace;
begin
if FPlace <> 0 then
begin
if FIndex <> (FPlace - 1) * (FGIFImage.Images.Count div 4) then
begin
Inc(FIndex);
FIndex := FIndex mod FGIFImage.Images.Count;
end;
end;
Invalidate;
end;procedure TMyImage.SetGIFImage(const Value: TGIFImage);
begin
FGIFImage.Assign(Value);
Invalidate;
end;procedure TMyImage.SetIndex(const Value: Integer);
begin
FIndex := Value;
Invalidate;
end;procedure TMyImage.SetState(const Value: Integer);
begin
FState := Value;
case FState of
1:
begin
FRects[1] := Rect(184, 98, 0, 0);
FRects[2] := Rect(103, 179, 0, 0);
FRects[3] := Rect(23, 98, 0, 0);
FRects[4] := Rect(103, 19, 0, 0);
end;
2:
begin
FRects[1] := Rect(184, 98, 0, 0);
FRects[2] := Rect(-100, 0, 0, 0);
FRects[3] := Rect(23, 98, 0, 0);
FRects[4] := Rect(-100, 0, 0, 0);
end;
else
begin
FRects[1] := Rect(-100, 0, 0, 0);
FRects[2] := Rect(-100, 0, 0, 0);
FRects[3] := Rect(-100, 0, 0, 0);
FRects[4] := Rect(-100, 0, 0, 0);
end;
end;
Invalidate;
end;
事件代码如下:
procedure TForm1.MyImage2Click(Sender: TObject);
begin
TMyImage(Sender).ImagePlace:=(TMyImage(Sender).ImagePlace+1+Ord((TMyImage(Sender).ImageState=2) and (TMyImage(Sender).ImagePlace<>0))) mod 5;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
MyImage2.RunToPlace;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货