C++Builder How To Use The HandleMessage Method In Windows Applications

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Use The HandleMessage Method In Windows Applications
By Yilmaz Yoru September 20, 2021

How can I use HandleMessage method? I have loop and I can not click to components on runtime. How can our application receive clicks on the components when I have a loop? How can I receive user inputs on heavy iterations and keep the user interface responsive? Let’s answer these questions.

What is the HandleMessage Method ?​

Для просмотра ссылки Войди или Зарегистрируйся Method (Vcl::Forms::TApplication::HandleMessage) is a VCL method that interrupts the execution of an application while Windows processes a message in the Windows message queue. HandleMessage interrupts the execution of the application so that Windows can process a single message from the Windows message queue before returning control to the application. If the message queue is empty, HandleMessage generates an OnIdle event and starts the process of updating the actions in the application.

Note that If the application goes idle, HandleMessage may take a long time to return. Therefore, do not call HandleMessage when waiting for something message-based while priority actions are also being processed. Instead, call ProcessMessages when processing more than just messages.

What is the syntax of the HandleMessage Method ?​

Here is the syntax of the HandleMessage method,
C++:
void __fastcall HandleMessage();

What is a simple example of using the C++ HandleMessage Method ?

C++:
Application->HandleMessage();

Here is the full example of using the C++ ProcessMessages Method​

C++:
#include <vcl.h>
#pragma hdrstop
 
#include "ProcessMessages_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 
bool loop = true;
unsigned int count;
 
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)      : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)  // START BUTTON
{
    do
    {
 count++;
 // do calculations here
 
 // receive and do form events
 Application->HandleMessage();
 
    } while(loop);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)  // STOP BUTTON
{
 loop = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)  // INFO BUTTON
{
 Memo1->Lines->Add(count);
}