I need to hook on to the Application’s OnDeactivate event in C++ Builder. So I need to write my own function to run when the OnDeactivate event fires for the application, but I don’t know where or how to define that function.
Ideally I would like my code to look something like this:
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
try
{
Application->Initialize();
Application->OnDeactivate = myFunction;
Application->Run();
}
later:
void myFunction(TObject *Sender)
{
//Do Stuff
}
When I write it like this in my .cpp file, it complains
cannot convert ‘void(*)(TObject *)’ to ‘TNotifyEvent’
If I change my function to return a TNotifyEvent (which shouldn’t work anyway), it gives me the hilarious error of
Cannot convert ‘TNotifyEvent’ to ‘TNotifyEvent’
So, how should I go about writing a function to hook to my Application property?
You are trying to assign a stand-alone function where a non-static class method is expected instead. You have two choices:
1) Move your event handler into a helper class:
2) Leave the function as a non-class function, but give it an extra parameter to receive the compiler’s
thispointer, and then use aTMethodstruct to help you pass it to the event as a suitableTNotifyEvent:With that said,
TApplication::Run()will exit immediately if a MainForm is not assigned, so the simpliest solution is to just drop aTApplicationEventscomponent onto your MainForm and then you can assign anOnDeactivateevent handler to it at design-time.Update: alternatively, if your project has any
TFormorTDataModuleobjects, you can simply drop aTApplicationEventscomponent on one of them, and assign anOnDeactivateevent handler to it at design time. It will then hook into the Application’sOnDeactivateevent for you.