C++ Builder关于Application之ProcessMessages方法和Terminated属性——在循环中刷新界面显示、响应点击、关闭程序等

先看一个例子和程序截图:

源代码如下:

Unit1.h文件

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TMemo *Memo1;
    TButton *Button1;
    TButton *Button2;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:	// User declarations
    bool bStop;
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp文件

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
//点击开始按钮
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    bStop=false;
    for(int i=0;;++i)
    {
        Memo1->Lines->Add(IntToStr(i));
        Application->ProcessMessages();
        if(bStop)
            break;
        if(Application->Terminated)
            break;
        Sleep(200);
    }
}
//---------------------------------------------------------------------------
//点击停止按钮
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    bStop=true;    
}
//---------------------------------------------------------------------------

关于TApplication.ProcessMessages方法介绍:

Interrupts the execution of an application so that it can process the message queue.

Call ProcessMessages to permit the application to process messages that are currently in the message queue. ProcessMessages cycles the Windows message loop until it is empty, and then returns control to the application.

Note: Neglecting message processing affects only the application calling ProcessMessages, not other applications. In lengthy operations, calling ProcessMessages periodically allows the application to respond to paint and other messages.

Note: ProcessMessages does not allow the application to go idle, whereas HandleMessage does.

简单地说就是,在漫长的操作如循环计算中,周期性地调用ProcessMessages方法去处理消息队列中的消息,可以让应用程序响应界面刷新和其它消息(如按钮点击等)。

关于TApplication.Terminated属性介绍:

Reports whether the application is in the process of shutting down.

Terminated is used when calling the ProcessMessages method so that the application doesn't perform unnecessary actions after the shut-down process has been triggered. ProcessMessages sets Terminated to true when it receives a WM_QUIT message.

An application usually receives a WM_QUIT message because the main window of the application has closed, or because the Terminate method has been called.

Note: For applications using calculation-intensive loops, call ProcessMessages periodically, and also check Terminated to determine whether to abort the calculation and allow the application to terminate.

简单地说就是,在计算密集的循环中,如果想要关闭程序(点击主窗口的X按钮,系统会向窗口的消息队列中发送WM_QUIT等消息),ProcessMessages方法处理到WM_QUIT消息时会设置Terminated属性为true,这时候可以决定是否终止计算并允许应用程序结束运行。

C++Builder(BCB)学习群(QQ)
https://www.cnblogs.com/ustone/p/16855586.html

posted @ 2021-06-26 12:03  ustone  阅读(856)  评论(0编辑  收藏  举报