procedure TForm1.FormCreate(Sender: TObject);
var
x,y,z:integer;
begin
randomize;
x:=random(50);
y:=random(50);
z:=random(2);
if z=1 then
label1.Caption :=inttostr(x);
label3.Caption :=inttostr(y);
if z=1 then
label2.Caption :='+'
else
label2.Caption :='-';
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
answer:integer;
begin
if key=chr(VK_RETURN) then
begin
if label2.Caption :='+' then
answer:=strtoint(label1.Caption)+strtoint(label3.Caption)
else
answer:=strtoint(label1.Caption)-strtoint(label3.Caption);
if answer:=strtoint(edit1.Text)then
begin
showmessage('回答正确');
button1.SetFocus ;
end
else
begin
showmessage('回答错误');
edit1.SetFocus ;
end;
edit1.SelectAll ;
end;
end.
Delphi代码怎么转成C++Builder代码?
答案:2 悬赏:20 手机版
解决时间 2021-01-25 16:59
- 提问者网友:富士山上尢
- 2021-01-25 04:13
最佳答案
- 五星知识达人网友:往事埋风中
- 2021-01-25 05:44
1,窗体的建立事件
2,Edit的按键事件
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
int x,y,z;
randomize();
x = random(50);
y = random(50);
z = random(2);
if (z==1)
Label1->Caption =IntToStr(x);
Label3->Caption =IntToStr(y);
if (z == 1)
Label2->Caption = "+";
else
Label2->Caption = "-";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
int answer;
if (Key == VK_RETURN)
{
if (Label2->Caption =="+")
answer = StrToInt(Label1->Caption)+StrToInt(Label3->Caption);
else
answer = StrToInt(Label1->Caption)-StrToInt(Label3->Caption);
if (answer == StrToInt(Edit1->Text))
{
ShowMessage("回答正确");
Button1->SetFocus() ;
}
else
{
ShowMessage("回答错误");
Edit1->SetFocus() ;
}
Edit1->SelectAll() ;
}
}
2,Edit的按键事件
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
int x,y,z;
randomize();
x = random(50);
y = random(50);
z = random(2);
if (z==1)
Label1->Caption =IntToStr(x);
Label3->Caption =IntToStr(y);
if (z == 1)
Label2->Caption = "+";
else
Label2->Caption = "-";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
int answer;
if (Key == VK_RETURN)
{
if (Label2->Caption =="+")
answer = StrToInt(Label1->Caption)+StrToInt(Label3->Caption);
else
answer = StrToInt(Label1->Caption)-StrToInt(Label3->Caption);
if (answer == StrToInt(Edit1->Text))
{
ShowMessage("回答正确");
Button1->SetFocus() ;
}
else
{
ShowMessage("回答错误");
Edit1->SetFocus() ;
}
Edit1->SelectAll() ;
}
}
全部回答
- 1楼网友:千夜
- 2021-01-25 07:17
delph和bcb虽是一个公司,同为vcl框架,但是毕竟是两种语言,不会像楼上说的.换成->就可以的.
现假设楼主有一定的c++基础,能使用基本的bcb6,改写如下:
1.新建一新工程,加入一memo和一button,控件name为memo1和button1(即默认的名字)
2.向memo1中加入任意内容,双击button1产生单击事件
3.加入如下代码:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
tform1 *form1;
//---------------------------------------------------------------------------
__fastcall tform1::tform1(tcomponent* owner)
: tform(owner)
{
}
//---------------------------------------------------------------------------
void txttojpeg(tmemo* memo1,ansistring filename,tcolor brush_color,tfont* font,tcolor font_color,int width, int height)
{
graphics::tbitmap *temp=new graphics::tbitmap();
temp->height=height;
temp->width=width;
temp->transparent=true;
temp->canvas->brush->color=brush_color;
temp->canvas->font=font;
temp->canvas->font->color=font_color;
int i=0,j=0;
for (i=0;ilines->count;i++){
j = j + 15;
temp->canvas->textouta(10,j,memo1->lines->strings[i]);
}
temp->savetofile(filename);
delete temp;
}
//---------------------------------------------------------------------------
void __fastcall tform1::button1click(tobject *sender)
{
txttojpeg(memo1 ,"12.bmp", clwhite, memo1->font, clblack, memo1->width, memo1->height);
}
//---------------------------------------------------------------------------
4.代码中重复的可省略,程序在bcb6 sp4下编译运行通过
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯