community.borland.com

Article #15107: Changing Color of Buttons with Bitmaps

 Technical Information Database

TI107D.txt   Changing Color of Buttons with Bitmaps
Category   :OWL
Platform    :All
Product    :Pascal  All

Description:
{$R bnn}
{ The above resource assumes that you have a dialog name
'dialog_1' that contains three buttons of id 101, 102, 103.
There should also be six bitmaps in the resource of value 1101,
3101, 5101 which hold the bitmaps for the red button and 1102,
3102, 5102 which hold the bitmaps for the blue button.  Clicking
the blue button should change the third button blue and clicking
the red button should change it red.
}

uses WinTypes, WinProcs, WObjects, bwcc;
const
  id_Red    = 101;
  id_Blue   = 102;
  id_Change = 103;

  IsRed: Boolean = False;
  IsBlue: Boolean = False;

type
  TApp = object(TApplication)
    procedure InitMainWindow; virtual;
  end;

  PMyDialog = ^TMyDialog;
  TMyDialog = object(TDialog)
    Red, Blue: array[0..2] of hBitMap;
    constructor Init(AParent :PWindowsObject; AName: PChar);
    destructor Done; virtual;
    procedure ChangeToRed(var Message: TMessage);
                         virtual id_First + id_Red;
    procedure ChangeToBlue(var Message: TMessage);
                         virtual id_First + id_Blue;
  end;

{ TApp }
procedure TApp.InitMainWindow;
begin
  MainWindow := New(PMyDialog, Init(Nil, 'dialog_1'));
end;

{ TMyDialog }
constructor TMyDialog.Init(AParent :PWindowsObject; AName:
PChar);
begin
  TDialog.Init(AParent, AName);
  Red[0] := LoadBitMap(hInstance, PChar(1101));
  Red[1] := LoadBitMap(hInstance, PChar(3101));
  Red[2] := LoadBitMap(hInstance, PChar(5101));

  Blue[0] := LoadBitMap(hInstance, PChar(1102));
  Blue[1] := LoadBitMap(hInstance, PChar(3102));
  Blue[2] := LoadBitMap(hInstance, PChar(5102));
end;

destructor TMyDialog.Done;
var
  I: Integer;
begin
  TDialog.Done;
  if Not IsRed then
    for I := 0 to 2 do
      DeleteObject(Red[I]);
  if Not IsBlue then
    for I := 0 to 2 do
      DeleteObject(Blue[I]);
end;

procedure TMyDialog.ChangeToRed(var Message: TMessage);
begin
  SendDlgItemMessage(HWindow, id_Change, bbm_SetBits, 0,
                     LongInt(
 Red));
  InvalidateRect(GetDlgItem(HWindow, id_Change), Nil, True);
  IsRed := True;
  IsBlue := False;
end;

procedure TMyDialog.ChangeToBlue(var Message: TMessage);
begin
  SendDlgItemMessage(HWindow, id_Change, bbm_SetBits, 0,
                     LongInt(
 Blue));
  InvalidateRect(GetDlgItem(HWindow, id_Change), Nil, True);
  IsRed := False;
  IsBlue := True;
end;

var
  App: TApp;
begin
  App.Init('Test');
  App.Run;
  App.Done;
end.


Reference:


7/16/98 4:33:37 PM
 

Last Modified: 01-SEP-99