home *** CD-ROM | disk | FTP | other *** search
- unit DBTransparentImage;
-
- {***************************************************************}
- { }
- { Transparent DBImage control for Delphi 4 }
- { Paints the bitmap transparently onto its background. Note }
- { that its background is not itself see through, but has the }
- { colour specified in its Color property. }
- { }
- { Freeware by Deborah Pate }
- { Version 1, November 1998 }
- { }
- { Uses the Transparent property of a TGraphic, so can't be used }
- { with Delphi 2 (sorry). Maybe with Delphi 3? I went straight }
- { from 2 to 4, so I can't check it. }
- { }
- { Please send improvements, bug reports etc to me at: }
- { dpate@hotmail.com }
- { }
- {***************************************************************}
-
-
- interface
-
- uses Windows, SysUtils, Messages, Classes, Controls, Forms,
- Graphics, Db, DBCtrls;
-
- type
-
- TDBTransparentImage = class(TDBImage)
- private
- fTransparent: boolean;
- protected
- procedure Paint; override;
- procedure SetTransparent(const On: boolean);
- public
- constructor Create(AOwner: TComponent); override;
- published
- property Transparent: boolean read fTransparent
- write SetTransparent default True;
- end;
-
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('Data Controls', [TDBTransparentImage]);
- end;
-
- { TDBTransparentImage }
-
- constructor TDBTransparentImage.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- fTransparent := True;
- ControlStyle := ControlStyle - [csOpaque];
- end;
-
- procedure TDBTransparentImage.SetTransparent(const On: boolean);
- begin
- fTransparent := On;
- if On then
- ControlStyle := ControlStyle - [csOpaque]
- else ControlStyle := ControlStyle + [csOpaque];
- Invalidate;
- end;
-
- procedure TDBTransparentImage.Paint;
- var
- Size: TSize;
- R: TRect;
- S: string;
- DrawPict: TPicture;
- Form: TCustomForm;
- Pal: HPalette;
- begin
- with Canvas do begin
- Brush.Style := bsClear;
- Brush.Color := Color;
- if ((Picture.Graphic <> nil) or (csPaintCopy in ControlState)) then begin
- DrawPict := TPicture.Create;
- Pal := 0;
- try
- if (csPaintCopy in ControlState) and
- (Field <> nil) and Field.IsBlob then begin
- DrawPict.Assign(Field);
- if DrawPict.Graphic is TBitmap then
- DrawPict.Bitmap.IgnorePalette := QuickDraw;
- end
- else begin
- DrawPict.Assign(Picture);
- if Focused and (DrawPict.Graphic <> nil)
- and (DrawPict.Graphic.Palette <> 0) then begin
- { Control has focus, so realize the bitmap palette in foreground }
- Pal := SelectPalette(Handle, DrawPict.Graphic.Palette, False);
- RealizePalette(Handle);
- end;
- end;
-
- if (DrawPict.Graphic <> nil) then
- DrawPict.Graphic.Transparent := fTransparent;
-
- if Stretch then
- if (DrawPict.Graphic = nil) or DrawPict.Graphic.Empty then
- FillRect(ClientRect)
- else StretchDraw(ClientRect, DrawPict.Graphic)
- else begin
- SetRect(R, 0, 0, DrawPict.Width, DrawPict.Height);
- if Center then OffsetRect(R, (ClientWidth - DrawPict.Width) div 2,
- (ClientHeight - DrawPict.Height) div 2);
- StretchDraw(R, DrawPict.Graphic);
- ExcludeClipRect(Handle, R.Left, R.Top, R.Right, R.Bottom);
- FillRect(ClientRect);
- SelectClipRgn(Handle, 0);
- end;
- finally
- if Pal <> 0 then SelectPalette(Handle, Pal, True);
- DrawPict.Free;
- end;
- end
- else begin
- Font := Self.Font;
- if (Field <> nil) then
- S := Field.DisplayLabel
- else S := Name;
- S := '(' + S + ')';
- Size := TextExtent(S);
- R := ClientRect;
- TextRect(R, (R.Right - Size.cx) div 2, (R.Bottom - Size.cy) div 2, S);
- end;
-
- Form := GetParentForm(Self);
- if (Form <> nil) and (Form.ActiveControl = Self) and
- not (csDesigning in ComponentState) and
- not (csPaintCopy in ControlState) then
- begin
- Brush.Color := clWindowFrame;
- FrameRect(ClientRect);
- end;
- end;
- end;
-
- end.
-