When does a VCL application get its Application->MainForm->Handle?

Viewed 222

My application populates some panels by means of incoming messages, using the SendStructMessage() function in Message.hpp.

SendStructMessage() need a valid windows handle to send to.

I have encapsulated the SendStrucMessage() in a function, like this:

bool sendAppMessage(ApplicationMessageEnum msgID, void* s)
{
    if(!Application || !Application->MainForm || !Application->MainForm->Handle)
    {
        Log(lError) << "Failed to get a valid handle when trying to send application message";
        return false;
    }
    HWND h = Application->MainForm->Handle;

    AppMessageStruct data;
    data.mMessageEnum = msgID;
    data.mData = s;

    LRESULT res =  SendStructMessage(h, UWM_MESSAGE, 0, &data);
    if(res)
    {
        Log(lError) << "Sending message: "<<msgID<<" was unsuccesful";
        return false;
    }

    return true;
}

Trying to call this from either the MainForm's OnShow or OnCreate event doesn't work, as in either case the Application->MainForm->Handle is still NULL.

My question is, in a VCL application's startup phase, where can one be sure that the Application->MainForm->Handle is actually created?

Currently I kick off a Timer checking for a valid handle:

void __fastcall TMain::WaitForHandleTimerTimer(TObject *Sender)
{
    if(Application->MainForm->Handle)
    {
        WaitForHandleTimer->Enabled = false;

        //Send a message to main ui to update sequence shortcuts
        if(sendAppMessage(abSequencerUpdate) != true)
        {
            Log(lDebug)<<"Sending sequencer update to UI was unsuccesful";
        }
    }
}

Is there a better way?

1 Answers
Related