Easy problem with Message handling

Hi all,

I'm pretty sure this is going to be a very easy question, but i've just started with C++ Builder and don't quite understand it yet.

Basically I need to set the Application->OnActivate to a custom function.

I can't seem to find out how to do this.

Every function declaration I try throws up a declaration error.

If I add in

void __fastcall (__closure *TNotifyEvent)(System::TObject, TObject* Sender);

it compiles, but where do I put the name of this function? And where do I put the body of code.

Any help greatly appreciated.

Who is Participating?
CayceConnect With a Mentor Commented:
/* Just add a function (public) in your main form */

class PACKAGE TForm1
  /* ... */
  void __fastcall ApplicationActivate(TObject* Sender);

void __fastcall TForm1::ApplicationActivate(TObject* Sender)
  /* Your code */

/* And in your constructor */
__fastcall TForm1::TForm1(TComponent Owner) : TForm(Owner)
  Application->OnActivate = ApplicationActivate;

/* __closure is used to create VARIABLES that poing to procedures, for events you create real procedures and assign them to __closure variables (which are called by the event thrower) */
Is the application based on a Form? In that case I would suggest to look at the form in the object explorer. Go to the events tab and write a name to the right of OnActivate. This should create the correct body ready for you to edit.
drb2k2Author Commented:
hmmm ok perhaps my understanding of this is wrong.
But here goes.
It the exe file there is an application pointer. whereby I call
Application->MainForm = A form i've designed

But my problem is that I have written a specific message handler in the Form object. Now when it gets a WM_ACTIVATEAPP message I want it to do something. But it never does. I'm guessing that the messages are getting filtered out somewhere along the line.

I have done some examining and found out that the parent handle to the Form is the application handle. Therefore I assume that my message is getting filtered out in the application message handler.

Since you can set Application->OnActivate to equal a form OnActivate I wanted to design my own custom function to handle this problem.

Note that I can't set application->OnActivate to MainForm->Activate because MainForm is an MDI window that may or may not be needed.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

If MainForm is an MDI window (child?) what window/form is visible all the time to choose a child or open and close them. In my understanding MainForm should be always visible and responsible for this. In that case the difference of Application->OnActivate and MainForm->OnActivate should be marginal. In the case that MainForm is a child form there is a great difference I agree.

For my projects no Application derived class existed. But it should be possible to override the OnActivate for it. Most likely you have to derive a class from TApplication in that case and just override OnActivate with your code and assure that your class is used as global Application instance.
drb2k2Author Commented:
Yes, I feared I would have to do that it seems like a lot work just to write a message handler

George TokasCommented:
From BCB online help:

Occurs when an application becomes active.

__property Classes::TNotifyEvent OnActivate = {read=FOnActivate, write=FOnActivate};


Write an OnActivate event handler to perform special processing when the application becomes active.

An application becomes active when it is initially run or when focus moves from another Windows application back to any window of the application.

That means that you can override this function declaring at Form::Form unit as follows:

Application->OnActivate = MyActivateFunction;
You have also to declare the function on main form's header file at "public" section.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.