问题1:在窗体上,加入TButton 和TLabel
2:在Button的单击事件中:
Label1->Caption 设为0void __fastcall TForm1::Button1Click(TObject *Sender)
{
   
    for(int i=1;i<=10;i++)
    {
        Label1->Caption=i;
        Sleep(100);
    }
}
目的是想让Label->Caption依次显示1,2,3,.....10
为使 每个数字显示1秒,加入Sleep(1000),相当于线程停止执行1秒,运行结果如下:单击Button后,Label1->Caption无变化,一直为0.等到10秒后,直接由0变为10.这就奇怪了,为什么不依次1,2,3,4,5,6,7,8,9,10,而是依次就变为10?
尽管是BCB,都是相通的.
兄弟们来看看呀???

解决方案 »

  1.   

    你都让界面线程睡着了,怎么还会更新界面呢?
    正确用法是在你的主线程中用API: 
    MsgWaitForMultiObjects等待1秒钟超时(通过先创建一个事件)event = CreateEvent(...); // locked state
    MsgWaitForMultiObject(1, &event, 1000);或 者:
    创建一个新线程,在新线程中运行for循 环 ,代码不变.
      

  2.   

    windows是基于消息的,你Label1->Caption=i;后,系统只是产生一个消息,让Label1去刷新自己,可是你的Button1Click函数一直没有退出,那么消息链里面的消息也得不到响应。
    所以你上面的程序,给Label1产生了10个刷新消息,但实际只执行一个就够了,因为前一个刷新还没得到执行,后一个刷新又来了,windows自动覆盖前一个,这是优化。
      

  3.   

    需要用线程来解决。或者:
    for(int i=1;i<=10;i++)
        {
            Label1->Caption=i;
    //在这里取出消息,再分发消息,让Label1去刷新自己
            Sleep(100);
        }
      

  4.   

    这样看:void __fastcall TForm1::Button1Click(TObject *Sender)
    {
       
        for(int i=1;i<=10;i++)
        {
            Label1->Caption= String(i);
            Label1->Invalidate();
            Label1->Update();
            Sleep(100);
        }
    }