Form Focus...

Hey all,
  Quick question.  I have a form that doesn't have a titlebar.  Instead I have a custom image with a label over it that is dragable.  Is there anyway that I can make the font color of that label change when the form loses focus or gains focus, and if so how?
QxmAsked:
Who is Participating?
 
mottorConnect With a Mentor Commented:
Use Application events:

procedure TForm1.FormDeactivate(Sender: TObject);
begin
  Label1.Font.color := clBlue;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  Label1.Font.color := clred;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnActivate := FormActivate;
  Application.OnDeActivate := FormDeActivate;
end;

0
 
mhervaisCommented:
listening
0
 
mhervaisCommented:
listening
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
simonetCommented:
Just do that (change the font) on the OnActive (= form gains focus) and OnDeactivate (=form loses focux) events of the form.

Yours,

Alex
0
 
QxmAuthor Commented:
That didn't work.  I did just what you said and it stayed the same color.
0
 
simonetCommented:
>OnActive

Sorry. That was supposed to read OnActivate. The event handlers should look something like this:

procedure TForm1.FormActivate(Sender: TObject);
begin
   label1.font.color := clActiveCaption;
end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
   label1.font.color := clInactiveCaption;
end;

Make sure you are leaving your form accordingly. Also make sure you are setting the color of the FONT property of the TLabel component.

Yours,

Alex
0
 
QxmAuthor Commented:
That didn't work.  I did just what you said and it stayed the same color.
0
 
LischkeCommented:
Qxm, you need to call TLabel.Update after you changed the color...

Ciao, Mike
0
 
AttarSoftwareCommented:
Or, you can declare a handler for the WM_NCACTIVATE message

  private
    procedure WMNCA( var msg : tMessage ) ; message WM_NCACTIVATE ;


....

procedure tForm1.WMNCA( var msg : tMessage ) ;
begin
  if( msg.wParam = 1 ) then
    label1.font.color := clActiveCaption
  else
    label1.font.color := clInactiveCaption ;
  {and as Mike says ;}
  label1.Update ;
end ;

I have forms with owner-draw captions and that seems to work fine for me in all instances...

(You maight like to look at the help for WM_NCACTIVATE, as my PC is offline at the moment, so I cannot check whether it is wParam that gets the flag for active or inactive...  might be lParam)

Anyways,  good luck...

Tim.
0
 
simonetCommented:
Can you post your code, please? There's no way the suggested solution can be wrong.

Alex
0
 
karooCommented:
Qxm,
try this
regards Ben

....
private
  procedure ColorLabel(Sender: TObject);
....

procedure TForm1.FormCreate(Sender: TObject);
begin
  Screen.OnActiveFormChange:= ColorLabel;
end;

procedure TForm1.ColorLabel(Sender: TObject);
begin
  ...color change code here
end;
0
 
AttarSoftwareCommented:
Surely using the Application.OnActivate solution is fine, so long as you have only one form in the application...  If you have many child windows being created from the same form type (such as in an MDI app), then only the last created form will perform the on activate function?

Probably wrong, I usually am ;O)

Tim.
0
 
QxmAuthor Commented:
Thanks alot mottor!

That worked great for me.

0
 
simonetCommented:
How is that different from the answer I first suggested?

Alex
0
 
QxmAuthor Commented:
I really don't know but your did not work for me.  I tried several times and it just didn't.  I then tried motter's and it worked the very first time.
0
 
mottorCommented:
Thanks Qxm,

Alex,
you didn't explain that he must use Application's event OnActivate, not the Form's OnActivate.
0
All Courses

From novice to tech pro — start learning today.