Solved

How can I have two buttons behave as one

Posted on 1998-06-06
1
146 Views
Last Modified: 2010-04-06
I am using the buttons from Magikit.  So I am using 2 buttons with a picture between them.  This in fact looks like it is one button. It looks very nice except for the fact that this buttons also have the property of changing appearance when the cursor is on top of them. So if I am on top of the first button, I would like the second's button' appearance to change as well since they are meant to look like one button. How can I make sure they are in sync that way ?
0
Comment
Question by:skylinka
1 Comment
 
LVL 3

Accepted Solution

by:
Matvey earned 100 total points
ID: 1351228
Hi

Sorry, don't know what is Magikit, so if it's something cool let me know...

The code below works for TSpeedButton, but I'm sure it's adjustable for your buttons. Your buttons might also have events for mouseenter and mouseleave, but here I create them by myself so the code is a bit long.

PS if you want to see even more cool button behavior, post your email and I'll send you a component I worte...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TNewSpeedButton = class(TspeedButton)
  private
    FOnMouseEnter: TNotifyEvent;
    FOnMouseLeave: TNotifyEvent;
  Protected
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  Published
    Property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
    Property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  end;

  TForm2 = class(TForm)
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);

    procedure btn1MouseEnter(Sender: TObject);
    procedure btn1MouseLeave(Sender: TObject);
    procedure btn2MouseEnter(Sender: TObject);
    procedure btn2MouseLeave(Sender: TObject);
  private
    { Private declarations }
  public
    btn1, btn2 : TNewSpeedButton;
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TNewSpeedButton.CMMouseEnter(var Message: TMessage);
Begin
  Inherited;
  if Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
end;

procedure TNewSpeedButton.CMMouseLeave(var Message: TMessage);
Begin
  Inherited;
  if Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
end;

procedure TForm2.btn1MouseEnter(Sender: TObject);
Begin
  Btn2.OnMouseEnter := nil;
 btn2.Perform(CM_MOUSEENTER, 0, 0);
  Btn2.OnMouseEnter := btn2MouseEnter;
end;

procedure TForm2.btn1MouseLeave(Sender: TObject);
Begin
  Btn2.OnMouseLeave := nil;
 btn2.Perform(CM_MOUSELEAVE, 0, 0);
  Btn2.OnMouseLeave := btn2MouseLeave;
end;

procedure TForm2.btn2MouseEnter(Sender: TObject);
Begin
  Btn1.OnMouseEnter := nil;
 btn1.Perform(CM_MOUSEENTER, 0, 0);
  Btn1.OnMouseEnter := btn1MouseEnter;
end;

procedure TForm2.btn2MouseLeave(Sender: TObject);
Begin
  Btn1.OnMouseLeave := nil;
 btn1.Perform(CM_MOUSELEAVE, 0, 0);
  Btn1.OnMouseLeave := btn1MouseLeave;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  Btn1 := TNewSpeedButton.Create(Self);
  Btn2 := TNewSpeedButton.Create(Self);
  Btn1.Parent := Self;
  Btn2.Parent := Self;

  btn1.SetBounds(20, 20, 20, 20);
  btn2.SetBounds(40, 20, 20, 20);
  btn1.Caption := 'A';
  btn2.Caption := 'B';
  btn1.Flat := True;
  btn2.Flat := True;

  Btn1.OnMouseEnter := btn1MouseEnter;
  Btn2.OnMouseEnter := btn2MouseEnter;
  Btn1.OnMouseLeave := btn1MouseLeave;
  Btn2.OnMouseLeave := btn2MouseLeave;
end;

end.


(A code of a simple Delphi unit...)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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