怎样通过键盘输入打破循环?
大家好:
我有一个问题,我在主窗体中间放置了一个TLabel控件Label1,在它上面有一个小菜单项ok,每当我点击ok菜单时,其菜单处理程序便执行以下程序:
void __fastcall TForm1::ok1Click(TObject *Sender)
{
int i=1;
while(true)
{
Form1->Label1->Caption=IntToStr(i++);
Form1->Label1->Refresh();
Sleep(150);
}
}
即Label1的Caption会从1开始一直显示数字,如果我希望这时我按一下光标键Down,就让这个显示数字过程结束,我应该怎么做呢?
我的初始想法是在OnKeyDown里进行判断,但是突然想起来当程序进入while(true)后,onkeydown是不能让它break的,如果在TForm1中加入一个标志位state(bool型),将while(true)变为while(state),然后在onkeydown中将state设为false,可是主程序一进入while循环后,onkeydown就拿不到控制权了。于是,我想到了用多线程的方法,以下是我用多线程的方法实现的程序。
//mainprog.h 主程序头文件
//---------------------------------------------------------------------------
#ifndef mainprgH
#define mainprgH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TMainMenu *MainMenu1;
TMenuItem *hello1;
TMenuItem *ok1;
void __fastcall FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall ok1Click(TObject *Sender);
private:
// User declarations
public:
bool state;//按理说这个state应放在private段里,但临时使用,所以...
// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//mainprog.cpp 主程序实现文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mainprg.h"
#include "mythread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
state=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==VK_DOWN)
state=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ok1Click(TObject *Sender)
{
Mythread *thread=new Mythread(false);
}
//---------------------------------------------------------------------------
//mythread.h 线程类头文件
#ifndef mythreadH
#define mythreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class Mythread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall Mythread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
//mythread.cpp 线程类实现文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mythread.h"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall Mythread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall Mythread::Mythread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall Mythread::Execute()
{
int i=1;
while(Form1->state)
{
Form1->Label1->Caption=IntToStr(i++);
Form1->Label1->Refresh();
Sleep(150);
}
Form1->Label1->Caption="I'll be back!";
Form1->state=true;
}
//---------------------------------------------------------------------------
我不知道这是不是解决这种问题的唯一方法,因为我最近看一些多线程的东西,所以想到了这招,但应该还有别的方法,请大家指教。
谢谢。
我有一个问题,我在主窗体中间放置了一个TLabel控件Label1,在它上面有一个小菜单项ok,每当我点击ok菜单时,其菜单处理程序便执行以下程序:
void __fastcall TForm1::ok1Click(TObject *Sender)
{
int i=1;
while(true)
{
Form1->Label1->Caption=IntToStr(i++);
Form1->Label1->Refresh();
Sleep(150);
}
}
即Label1的Caption会从1开始一直显示数字,如果我希望这时我按一下光标键Down,就让这个显示数字过程结束,我应该怎么做呢?
我的初始想法是在OnKeyDown里进行判断,但是突然想起来当程序进入while(true)后,onkeydown是不能让它break的,如果在TForm1中加入一个标志位state(bool型),将while(true)变为while(state),然后在onkeydown中将state设为false,可是主程序一进入while循环后,onkeydown就拿不到控制权了。于是,我想到了用多线程的方法,以下是我用多线程的方法实现的程序。
//mainprog.h 主程序头文件
//---------------------------------------------------------------------------
#ifndef mainprgH
#define mainprgH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TMainMenu *MainMenu1;
TMenuItem *hello1;
TMenuItem *ok1;
void __fastcall FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall ok1Click(TObject *Sender);
private:
// User declarations
public:
bool state;//按理说这个state应放在private段里,但临时使用,所以...
// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//mainprog.cpp 主程序实现文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mainprg.h"
#include "mythread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
state=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==VK_DOWN)
state=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ok1Click(TObject *Sender)
{
Mythread *thread=new Mythread(false);
}
//---------------------------------------------------------------------------
//mythread.h 线程类头文件
#ifndef mythreadH
#define mythreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class Mythread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall Mythread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
//mythread.cpp 线程类实现文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mythread.h"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall Mythread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall Mythread::Mythread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall Mythread::Execute()
{
int i=1;
while(Form1->state)
{
Form1->Label1->Caption=IntToStr(i++);
Form1->Label1->Refresh();
Sleep(150);
}
Form1->Label1->Caption="I'll be back!";
Form1->state=true;
}
//---------------------------------------------------------------------------
我不知道这是不是解决这种问题的唯一方法,因为我最近看一些多线程的东西,所以想到了这招,但应该还有别的方法,请大家指教。
谢谢。
...全文
相关推荐
java 详解类加载器的双亲委派及打破双亲委派
主要介绍了java 详解类加载器的双亲委派及打破双亲委派的相关资料,需要的朋友可以参考下
javascript break指定标签打破多层循环示例
break的语法有两种break; 和 break label;下面为大家介绍下直接break掉整个循环嵌套示例
房地产行业深度报告:打破负循环,探寻修复路径.pdf
了解风口行业信息差,行业内部数据报告,营销方案
Python源码:打破循环.zip
探索Python编程的无限可能,这份精心整理的Python案例源码库,是每位编程爱好者与开发者不可多得的宝藏。它不仅涵盖了从基础语法实践到高级项目开发的全方位示例,还融入了机器学习、数据分析、Web开发等热门领域的实战代码。每个案例都如同精心雕琢的模板,旨在帮助学习者快速上手,深入理解Python的强大功能与应用场景。 无论你是初学者,希望通过具体项目巩固知识;还是资深开发者,寻求灵感与优化方案,这份源码库都能为你提供丰富的资源。它像一座桥梁,连接理论与实践,让你的编程之路更加顺畅。立即开启探索之旅,让Python的魔力在指尖绽放,激发你的创造力与潜能,成就非凡的编程梦想!
QuestMobile 电商粉丝经济洞察报告:电商同质化如何突围?用新模型打破恶性循环吧.rar
QuestMobile 电商粉丝经济洞察报告:电商同质化如何突围?用新模型打破恶性循环吧


VCL组件使用和开发
