Solved

How can I have two buttons behave as one

Posted on 1998-06-06
1
144 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

14 Experts available now in Live!

Get 1:1 Help Now