C++ Builder入门第一例
Project1.cpp:
#include <vcl.h> #include <tchar.h> class Tmyform : public TForm //定义一个窗体类 { __published: //VCL组件成员及事件处理函数 TLabel *Label1,*Label2; TEdit *Edit1; TButton *btnOk,*btnExit; void __fastcall btnOkClick(TObject *Sender); void __fastcall btnExitClick(TObject *Sender); private: public: __fastcall Tmyform(TComponent* Owner); //构造函数 }; __fastcall Tmyform::Tmyform(TComponent* Owner) : TForm(Owner) { btnExit->Enabled=false; btnOk->Default=true; } void __fastcall Tmyform::btnOkClick(TObject *Sender) { this->Label2->Caption="你好,"+this->Edit1->Text; this->btnExit->Enabled=true; } void __fastcall Tmyform::btnExitClick(TObject *Sender) { this->Close(); } #pragma resource "*.dfm" //包含Project1.dfm文件,设置组件的一些属性的值 WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) //相当于DOS程序中的main函数 { Tmyform *f1; Application->Initialize(); //进行一些初始化工作 Application->CreateForm(__classid(Tmyform), &f1); //创建对象f1作为主窗体 Application->Run(); //显示主窗体,进入循环,开始处理消息 return 0; }
Project1.dfm:
object f1: Tmyform Left = 0 Top = 0 Caption = 'Hello C++ Builder' ClientHeight = 263 ClientWidth = 592 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Label1: TLabel Left = 16 Top = 16 Width = 36 Height = 13 Caption = #22995#21517#65306 end object Label2: TLabel Left = 16 Top = 35 Width = 31 Height = 13 Caption = 'Label2' end object Edit1: TEdit Left = 80 Top = 13 Width = 121 Height = 21 TabOrder = 0 end object btnOk: TButton Left = 16 Top = 64 Width = 75 Height = 25 Caption = '&OK' TabOrder = 1 OnClick = btnOkClick end object btnExit: TButton Left = 126 Top = 64 Width = 75 Height = 25 Caption = '&Exit' TabOrder = 2 OnClick = btnExitClick end end
编译方法:
bcc32 -WV Project1.cpp
把这个例子和C++ Builder自动生成的工程对比一下,你会学到一些东西的。
-WV:生成使用VCL的Windows程序
程序运行后的效果如下图:
好的代码像粥一样,都是用时间熬出来的