We help IT Professionals succeed at work.

Why not background?

sccheung
sccheung asked
on
276 Views
Last Modified: 2010-04-06
Hello, thank you for solving my last problem. I've got another problem while writing my component. This is the third question I ask for the same component. I hope that you can help me to finish it. I is the most difficult project I've ever done. I want my component to display the clBtnFace as the background when the property BackgroundPic is empty. But it display the original picture instead. Can anyone help me? Here is the component.

unit ButtonPro;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Menus;

type
  TCurrentState = set of (MActive, KActive, Pressing, Disabled, dDown);
  TButtonPro = class(TCustomControl)
  private
    { Private declarations }
    FNormalPic, FActivePic, FPressingPic, FDisabledPic, FDownPic, FBackPic, FMaskPic: TBitmap;
    FBtnDown, FStretch, FAutosize, FCancel, FDefault: Boolean;
    FTranspColor, FBkColor: TColor;
    FMDResult: TModalResult;
    FOnMouseEnter, FOnMouseLeave: TNotifyEvent;
    CurrentState: TCurrentState;
    procedure SetNormalPic(Value: TBitmap);
    procedure SetActivePic(Value: TBitmap);
    procedure SetPressingPic(Value: TBitmap);
    procedure SetDisabledPic(Value: TBitmap);
    procedure SetDownPic(Value: TBitmap);
    procedure SetBackPic(Value: TBitmap);
    procedure SetMaskPic(Value: TBitmap);
    procedure SetTranspColor(Value: TColor);
    procedure SetBkColor(Value: TColor);
    procedure SetStretch(Value: Boolean);
    procedure SetAutosize(Value: Boolean);
    procedure SetBtnDown(Value: Boolean);
    procedure MaskIt(var TheResult: TBitmap; ThePic, TheMask, TheBackgPic: TBitmap; TheTranspColor: TColor);
  protected
    { Protected declarations }
    procedure Paint; override;
    procedure Click; override;
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
    procedure WMLButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
    procedure WMLButtonUp(var Message: TMessage); message WM_LBUTTONUP;
    procedure GotFocus(var Message: TMessage); message CM_ENTER;
    procedure LostFocus(var Message: TMessage); message CM_EXIT;
    procedure WMChar(var Message: TWMChar); message WM_CHAR;
    procedure CMEnabledChanged(var Message: TWMChar); message CM_ENABLEDCHANGED;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

  published
    { Published declarations }
    Property NormalPicture: TBitmap read FNormalPic write SetNormalPic;
    Property ActivePicture: TBitmap read FActivePic write SetActivePic;
    Property PressingPicture: TBitmap read FPressingPic write SetPressingPic;
    Property DisabledPicture: TBitmap read FDisabledPic write SetDisabledPic;
    Property DownPicture: TBitmap read FDownPic write SetDownPic;
    Property BackgroundPicture: TBitmap read FBackPic write SetBackPic;
    Property MaskPicture: TBitmap read FMaskPic write SetMaskPic;
    Property Down: Boolean read FBtnDown write SetBtnDown;
    Property Stretch: Boolean read FStretch write SetStretch;
    Property TransparentColor: TColor read FTranspColor write SetTranspColor default clWhite;
    Property BackgroundColor: TColor read FBkColor write SetBkColor default clBtnFace;
    Property Height default 50;
    Property Width default 50;
    Property Visible;
    Property Autosize: Boolean read FAutosize write SetAutosize;
    Property TabStop;
    Property TabOrder;
    Property OnEnter;
    Property Cursor;
    Property Enabled;
    Property ModalResult: TModalResult read FMDResult write FMDResult;
    Property ParentShowHint;
    Property ShowHint;
    Property Cancel: Boolean read FCancel write FCancel;
    Property Default: Boolean read FDefault write FDefault;
    Property PopupMenu;
    Property Hint;
    Property OnExit;
    Property OnClick;
    Property OnMouseUp;
    Property OnMouseDown;
    Property OnMouseMove;
    Property OnKeyDown;
    Property OnKeyPress;
    Property OnKeyUp;
    Property OnDragDrop;
    Property OnDragOver;
    Property OnEndDrag;
    Property OnStartDrag;
    Property Tag;
    Property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
    Property OnMOuseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
    Property DragMode;
    Property DragCursor;
    Property Caption;
    Property Font;
    Property ParentFont;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('My Components', [TButtonPro]);
end;

procedure TButtonPro.CMMouseEnter(var Message: TMessage);
begin
  CurrentState := CurrentState + [MActive];
  Invalidate;
  If Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
  inherited;
end;

procedure TButtonPro.CMMouseLeave(var Message: TMessage);
begin
  CurrentState := CurrentState - [MActive];
  Invalidate;
  If Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
  inherited;
end;

procedure TButtonPro.WMLButtonDown(var Message: TMessage);
begin
  inherited;
  CurrentState := CurrentState + [Pressing];
  Invalidate;
end;

procedure TButtonPro.WMLButtonUp(var Message: TMessage);
begin
  inherited;
  CurrentState := CurrentState - [Pressing];
  Invalidate;
end;

constructor TButtonPro.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Height := 50;
  Width := 50;
  FNormalPic := TBitmap.Create;
  FActivePic := TBitmap.Create;
  FPressingPic := TBitmap.Create;
  FDisabledPic := TBitmap.Create;
  FBackPic := TBitmap.Create;
  FBackPic.Canvas.Brush.Color := clBtnFace;
  FDownPic := TBitmap.Create;
  FMaskPic := TBitmap.Create;
  FBtnDown := False;
  FTranspColor := clBackground;
  FBkColor := clBackground;
end;

destructor TButtonPro.Destroy;
begin
  FNormalPic.Free;
  FActivePic.Free;
  FPressingPic.Free;
  FDisabledPic.Free;
  FDownPic.Free;
  inherited Destroy;
end;

procedure TButtonPro.SetBkColor(Value: TColor);
begin
  FBkColor := Value;
  FBackPic.Canvas.Brush.Color := Value;
  Invalidate;
end;

procedure TButtonPro.SetTranspColor(Value: TColor);
begin
  FTranspColor := Value;
  Invalidate;
end;

procedure TButtonPro.SetNormalPic(Value: TBitmap);
begin
  FNormalPic.Assign(Value);
  If (FAutosize) and (not FNormalPic.Empty) then
    begin
      Height := FNormalPic.Height;
      Width := FNormalPic.Width;
    end;
  Invalidate;
end;

procedure TButtonPro.SetActivePic(Value: TBitmap);
begin
  FActivePic.Assign(Value);
  Invalidate;
end;

procedure TButtonPro.SetPressingPic(Value: TBitmap);
begin
  FPressingPic.Assign(Value);
  Invalidate;
end;

procedure TButtonPro.SetDisabledPic(Value: TBitmap);
begin
  FDisabledPic.Assign(Value);
  Invalidate;
end;

procedure TButtonPro.SetDownPic(Value: TBitmap);
begin
  FDownPic.Assign(Value);
  Invalidate;
end;

procedure TButtonPro.Paint;
var TmpRect, BndRect: TRect;
    CurrentPic, TmpPic: TBitmap;
begin
  CurrentPic := TBitmap.Create;
  CurrentPic.Assign(FNormalPic);
  If (MActive in CurrentState) or (KActive in CurrentState) then
    CurrentPic.Assign(FActivePic);
  If Pressing in CurrentState then CurrentPic.Assign(FPressingPic);
  If dDown in CurrentState then CurrentPic.Assign(FDownPic);
  If Disabled in CurrentState then CurrentPic.Assign(FDisabledPic);
  If (FAutosize) and (not CurrentPic.Empty) then
  begin
    Width := CurrentPic.Width;
    Height := CurrentPic.Height;
  end;
  TmpPic := TBitmap.Create;
  TmpPic.Assign(CurrentPic);
  MaskIt(CurrentPic,TmpPic,FMaskPic,FBackPic,FTranspColor);
  TmpPic.Free;
  TmpRect := Rect(0,0,CurrentPic.Width,CurrentPic.Height);
  BndRect := Rect(0,0,Width,Height);
  Canvas.Brush.Color := FBkColor;
  Canvas.Pen.Style := psDash;
  If CurrentPic.Empty then begin
  Canvas.FillRect(BndRect);
  Canvas.TextOut(0,0,'Empty');
  end
  else If FStretch then Canvas.CopyRect(BndRect,CurrentPic.Canvas,TmpRect)
       else Canvas.CopyRect(TmpRect,CurrentPic.Canvas,TmpRect);
  CurrentPic.Free;
end;

procedure TButtonPro.SetStretch(Value: Boolean);
begin
  FStretch := Value;
  If FStretch then FAutosize := False;
  Invalidate;
end;

procedure TButtonPro.SetAutosize(Value: Boolean);
begin
  FAutosize := Value;
  If FAutosize then FStretch := False;
  Invalidate;
end;

procedure TButtonPro.GotFocus(var Message: TMessage);
begin
  CurrentState := CurrentState + [KActive];
  Invalidate;
  inherited
end;

procedure TButtonPro.LostFocus(var Message: TMessage);
begin
  CurrentState := CurrentState - [KActive];
  Invalidate;
  inherited;
end;

procedure TButtonPro.Click;
begin
  inherited;
  If (Owner is TForm) and (FMDResult <> mrNone) then With Owner As TForm do ModalResult := FMDResult;
end;

procedure TButtonPro.WMChar(var Message: TWMChar);
begin
  inherited;
  If (Message.CharCode = 27) and FCancel then Click;
  If (Message.CharCode = 13) and FDefault then Click;
end;

procedure TButtonPro.SetBtnDown(Value: Boolean);
begin
  FBtnDown := Value;
  If FBtnDown then
    CurrentState := CurrentState + [dDown]
  else CurrentState := CurrentState - [dDown];
  Invalidate;
end;

procedure TButtonPro.CMEnabledChanged(var Message: TWMChar);
begin
  If Self.Enabled then CurrentState := CurrentState - [Disabled]
  else CurrentState := CurrentState + [Disabled];
end;

procedure TButtonPro.SetBackPic(Value: TBitmap);
begin
  FBackPic.Assign(Value);
  Invalidate;
end;

procedure TButtonPro.SetMaskPic(Value: TBitmap);
begin
  FMaskPic.Assign(Value);
  Invalidate;
end;

procedure TButtonPro.MaskIt(var TheResult: TBitmap; ThePic, TheMask, TheBackgPic: TBitmap; TheTranspColor: TColor);
var i,j: integer;
    TmpPic1, TmpPic2: TBitmap;
begin
  If (ThePic.Empty) and (not TheBackgPic.Empty) then
      TheResult.Assign(TheBackgPic);
  If (not ThePic.Empty) and (TheBackgPic.Empty) then
    TheBackgPic.Canvas.FillRect(Rect(0,0,ThePic.Width,ThePic.Height));
  If (not ThePic.Empty) and (not TheBackgPic.Empty) then
  begin
    TmpPic1 := TBitmap.Create;
    TmpPic2 := TBitmap.Create;
    If TheMask.Empty then
      begin
        TheMask.Assign(ThePic);
        With TheMask.Canvas do
          for i := 0 to TheMask.Width do
            for j := 0 to TheMask.Height do
              If Pixels[i,j] = TheTranspColor then
                Pixels[i,j] := clBlack
              else Pixels[i,j] := clWhite;
      end;
    TmpPic1.Assign(ThePic);
    With TmpPic1.Canvas do
    begin
      CopyMode := cmSrcAnd;
      CopyRect(Rect(0,0,ThePic.Width,ThePic.Height),TheMask.Canvas,Rect(0,0,TheMask.Width,TheMask.Height));
    end;
    TmpPic2.Assign(TheMask);
    With TmpPic2.Canvas do
    begin
      CopyMode := cmSrcErase;
      CopyRect(Rect(0,0,TheBackgPic.Width,TheBackgPic.Height),TheBackgPic.Canvas,Rect(0,0,TheBackgPic.Width,TheBackgPic.Height));
    end;
    TheResult.Assign(TmpPic1);
    With TheResult.Canvas do
    begin
      CopyMode := cmSrcPaint;
      CopyRect(Rect(0,0,TmpPic1.Width,TmpPic1.Height),TmpPic2.Canvas,Rect(0,0,TmpPic1.Width,TmpPic1.Height));
    end;
    TmpPic1.Free;
    TmpPic2.Free;
  end;
end;

end.
Comment
Watch Question

Commented:
Confirm the points of the question.

For 10 points i can't even read your code.

Wouldn't it be 100 points ?

Author

Commented:
Adjusted points to 50
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.