Solved

Why not background?

Posted on 1997-08-27
3
179 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.
0
Comment
Question by:sccheung
  • 2
3 Comments
 
LVL 4

Expert Comment

by:itamar
ID: 1342706
Confirm the points of the question.

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

Wouldn't it be 100 points ?
0
 

Author Comment

by:sccheung
ID: 1342707
Adjusted points to 50
0
 
LVL 4

Accepted Solution

by:
itamar earned 50 total points
ID: 1342708
Well, if i understood the problem i think you can try changing the code for the Paint method to this (observe the line included):

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.Brush.Color := clBtnFace; <<<< THIS ONE
              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;

Just a comment: Perhaps it could be better using Parent.Color instead of clBtnFace, just because it will have the same efect if someone changes the form's color.

I hope this helps.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now