⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmfill.pas

📁 一套及时通讯的原码
💻 PAS
字号:
{========================================================================}
{=                (c) 1995-98 SwiftSoft Ronald Dittrich                 =}
{========================================================================}
{=                          All Rights Reserved                         =}
{========================================================================}
{=  D 01099 Dresden             = Tel.: +0351-8012255                   =}
{=  Loewenstr.7a                = info@swiftsoft.de                     =}
{========================================================================}
{=  Actual versions on http://www.swiftsoft.de/mmtools.html             =}
{========================================================================}
{=  This code is for reference purposes only and may not be copied or   =}
{=  distributed in any format electronic or otherwise except one copy   =}
{=  for backup purposes.                                                =}
{=                                                                      =}
{=  No Delphi Component Kit or Component individually or in a collection=}
{=  subclassed or otherwise from the code in this unit, or associated   =}
{=  .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed     =}
{=  without express permission from SwiftSoft.                          =}
{=                                                                      =}
{=  For more licence informations please refer to the associated        =}
{=  HelpFile.                                                           =}
{========================================================================}
{=  $Date: 22.02.98 - 01:26:39 $                                        =}
{========================================================================}
unit MMFill;

{$I COMPILER.INC}

interface

uses
    Windows,
    SysUtils,
    Classes,
    Controls,
    Graphics,
    Messages,
    Forms,
    MMObj,
    MMPanel,
    MMUtils,
    MMBmpLst,
    MMHook,
    MMObsrv;

type
    {-- TMMFormFill -----------------------------------------------------------}
    TMMFormFill = class(TMMWndProcComponent)
    private
       FOldOnPaint     : TNotifyEvent;
       FBitmapIndex    : integer;
       FBitmaps        : TMMBitmapList;
       FObserver       : TMMObserver;

       procedure UpdateForm;
{$IFNDEF BUILD_ACTIVEX}
       procedure PaintForm(Sender: TOBject);
{$ENDIF}
       procedure SetBitmaps(aValue: TMMBitmapList);
       procedure BitmapsNotify(Sender, Data: TObject);
       procedure SetBitmapIndex(aValue: integer);

    protected
       procedure Notification(AComponent: TComponent; Operation: TOperation); override;
       procedure HookWndProc(var Message: TMessage); override;
{$IFDEF BUILD_ACTIVEX}
       procedure HookOwner; override;
{$ENDIF}

    public
       constructor Create(aOwner: TComponent); override;
       destructor Destroy; override;

    published
       property BitmapList: TMMBitmapList read FBitmaps write SetBitmaps;
       property BitmapIndex: Integer read FBitmapIndex write SetBitmapIndex default -1;
    end;

    TMMFillPanel = procedure(Sender: TObject; Canvas: TCanvas; R: TRect) of object;

    {-- TMMPanelFill ----------------------------------------------------------}
    TMMPanelFill = class(TMMPanel)
    private
       FBitmapIndex    : integer;
       FBitmapBackIndex: integer;
       FBitmaps        : TMMBitmapList;
       FObserver       : TMMObserver;
       FOnFillPanel    : TMMFillPanel;

       procedure SetBitmaps(aValue: TMMBitmapList);
       procedure BitmapsNotify(Sender, Data: TObject);
       procedure SetBitmapIndex(aValue: integer);
       procedure SetBitmapBackIndex(aValue: integer);
       procedure FillPanel(Sender: TObject; Canvas: TCanvas; aRect: TRect);
       procedure SetFillBevel(aValue: Boolean);
       function  GetFillBevel: Boolean;
       function  GetBitmap: TBitmap;

    protected
       procedure Notification(AComponent: TComponent; Operation: TOperation); override;
       procedure Paint; override;

   public
       constructor Create(aOwner: TComponent); override;
       destructor Destroy; override;

       function BitmapValid: Boolean;
       property Bitmap: TBitmap read GetBitmap;

    published
       property OnFillPanel: TMMFillPanel read FOnFillPanel write FOnFillPanel;
       property BitmapList: TMMBitmapList read FBitmaps write SetBitmaps;
       property BitmapIndex: Integer read FBitmapIndex write SetBitmapIndex default -1;
       property BitmapBackIndex: Integer read FBitmapBackIndex write SetBitmapBackIndex default -1;
       property FillBevel: Boolean read GetFillBevel write SetFillBevel default True;
    end;

implementation

{== TMMFormFill ===============================================================}
constructor TMMFormFill.Create(aOwner: TComponent);
begin
   inherited Create(aOwner);

   FBitmapIndex := -1;
   FBitmaps     := nil;
   FObserver    := TMMObserver.Create;
   FObserver.OnNotify := BitmapsNotify;

   HookOwner;
{$IFNDEF BUILD_ACTIVEX}
   if (OwnerForm <> nil) and not (csDesigning in ComponentState) then
   begin
      FOldOnPaint := OwnerForm.OnPaint;
      OwnerForm.OnPaint := PaintForm;
   end;
{$ENDIF}
end;

{-- TMMFormFill ---------------------------------------------------------------}
destructor TMMFormFill.Destroy;
begin
   UnHookOwner;

   BitmapList := nil;

{$IFNDEF BUILD_ACTIVEX}
   if (OwnerForm <> nil) and not (csDesigning in ComponentState) then
   begin
      OwnerForm.OnPaint := FOldOnPaint;
   end;
{$ENDIF}

   FObserver.Free;

   inherited Destroy;
end;

{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.BitmapsNotify(Sender, Data: TObject);
begin
   if (Data = FBitmaps) then UpdateForm;
end;

{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.SetBitmaps(aValue: TMMBitmapList);
begin
  { bug fix for AX Controls }
  if integer(aValue) = integer(Self) then exit;

  if (FBitmaps <> nil) then FBitmaps.RemoveObserver(FObserver);
  FBitmaps := aValue;
  if (FBitmaps <> nil) then FBitmaps.AddObserver(FObserver);
  UpdateForm;
end;

{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.SetBitmapIndex(aValue: integer);
begin
   if (FBitmapIndex <> aValue) then
   begin
      FBitmapIndex := Max(aValue, -1);
      UpdateForm;
   end;
end;

{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.Notification(AComponent: TComponent; Operation: TOperation);
begin
   inherited Notification(AComponent, Operation);

   if (Operation = opRemove) and (aComponent = BitmapList) then BitmapList := nil;
end;

{-- TMMFormStyler -------------------------------------------------------------}
procedure TMMFormFill.HookWndProc(var Message: TMessage);
{$IFDEF BUILD_ACTIVEX}
var
  PS: TPaintStruct;
  B: TBitmap;
  R: TRect;
begin
  if not (csDesigning in ComponentState) then
    if Assigned(FBitmaps) and (FBitmapIndex >= 0) and (FBitmaps.Count > FBitmapIndex) then
    begin
      B :=FBitmaps[FBitmapIndex];
      case Message.Msg of
        WM_ERASEBKGND:
          begin
            Message.Result := 1;
            exit;
          end;
        WM_PAINT:
          begin
            BeginPaint(HookWnd, PS);
            R := PS.rcPaint;
            if R.Left <> 0 then Dec(R.Left, R.Left mod B.Width);
            if R.Top <> 0 then Dec(R.Top, R.Top mod B.Height);
            TileBlt(PS.hdc, B.Handle, R, SRCCOPY);
            EndPaint(HookWnd, PS);
            Message.Result := 0;
            exit;
          end;
      end
    end;
  inherited;
{$ELSE}
begin
  if Message.Msg = WM_ERASEBKGND then
     Message.Result := 1 else inherited;
{$ENDIF}
end;

{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.UpdateForm;
begin
{$IFNDEF BUILD_ACTIVEX}
   if (OwnerForm <> nil) then OwnerForm.Invalidate;
{$ELSE}
   InvalidateRect(HookWnd, nil, False);
{$ENDIF}
end;

{$IFDEF BUILD_ACTIVEX}
{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.HookOwner;
var
  WasHooked: Boolean;
begin
  WasHooked := FormOK;
  inherited;
  if not WasHooked and FormOK then
    UpdateForm;
end;
{$ENDIF}

{$IFNDEF BUILD_ACTIVEX}
{-- TMMFormFill ---------------------------------------------------------------}
procedure TMMFormFill.PaintForm(Sender: TObject);
begin
   with TForm(Sender) do
   if assigned(FBitmaps) and (FBitmapIndex >= 0) and (FBitmaps.Count > FBitmapIndex) then
   begin
      TileBlt(Canvas.Handle, FBitmaps[FBitmapIndex].Handle, ClientRect,SRCCOPY);
   end
   else
   begin
      Canvas.Brush.Color := Color;
      Canvas.FillRect(ClientRect);
   end;
end;
{$ENDIF}

{== TMMPanelFill ==============================================================}
constructor TMMPanelFill.Create(aOwner: TComponent);
begin
   inherited Create(aOwner);

   FillBevel          := True;
   FBitmapIndex       := -1;
   FBitmapBackIndex   := -1;
   FBitmaps           := nil;
   FObserver          := TMMObserver.Create;
   FObserver.OnNotify := BitmapsNotify;

   OnFill             := FillPanel;
end;

{-- TMMPanelFill --------------------------------------------------------------}
destructor TMMPanelFill.Destroy;
begin
   OnFill := nil;

   BitmapList := nil;

   FObserver.Free;

   inherited Destroy;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.Notification(AComponent: TComponent; Operation: TOperation);
begin
   inherited Notification(AComponent, Operation);

   if (Operation = opRemove) and (aComponent = BitmapList) then BitmapList := nil;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.SetFillBevel(aValue: Boolean);
begin
   inherited FillBevel := not aValue;
end;

{-- TMMPanelFill --------------------------------------------------------------}
function TMMPanelFill.GetFillBevel: Boolean;
begin
   Result := not inherited FillBevel;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.SetBitmaps(aValue: TMMBitmapList);
begin
   { bug fix for AX Controls }
   if integer(aValue) = integer(Self) then exit;

   if (FBitmaps <> nil) then FBitmaps.RemoveObserver(FObserver);
   FBitmaps := aValue;
   if (FBitmaps <> nil) then FBitmaps.AddObserver(FObserver);
   Refresh;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.SetBitmapIndex(aValue: integer);
begin
   if (FBitmapIndex <> aValue) then
   begin
      FBitmapIndex := Max(aValue,-1);
      Refresh;
   end;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.SetBitmapBackIndex(aValue: integer);
begin
   if (FBitmapBackIndex <> aValue) then
   begin
      FBitmapBackIndex := Max(aValue,-1);
      Invalidate;
   end;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.BitmapsNotify(Sender: TObject; Data: TObject);
begin
   Refresh;
end;

{-- TMMPanelFill --------------------------------------------------------------}
function TMMPanelFill.BitmapValid: Boolean;
begin
   Result := (FBitmaps <> nil) and (FBitmapIndex >= 0) and (FBitmapIndex <  FBitmaps.Count);
end;

{-- TMMPanelFill --------------------------------------------------------------}
function TMMPanelFill.GetBitmap: TBitmap;
begin
   if BitmapValid then
      Result := FBitmaps[BitmapIndex]
   else
      Result := nil;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.Paint;
begin
   if assigned(FOnFillPanel) then
   begin
      FOnFillPanel(Self,Canvas,ClientRect);
   end
   else
   begin
      with Canvas do
      begin
         if BitmapValid then
         begin
            {$IFDEF DELPHI3}
            Bitmap.Canvas.Lock;
            {$ENDIF}
            try
               TileBlt(Handle, Bitmap.Handle, ClientRect, SRCCOPY);
            finally
               {$IFDEF DELPHI3}
               Bitmap.Canvas.UnLock;
               {$ENDIF}
            end;
         end
         else
         begin
            Brush.Color := Color;
            Brush.Style := bsSolid;
            FillRect(ClientRect);
         end;
      end;
   end;

   inherited;
end;

{-- TMMPanelFill --------------------------------------------------------------}
procedure TMMPanelFill.FillPanel(Sender: TObject; Canvas: TCanvas; aRect: TRect);
begin
   { only a dummy so the panel doesn't paint the client area }
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -