全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:19442
推到 Plurk!
推到 Facebook!

BCB6 及 Delphi 7 常用語法對照表

 
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-10-22 08:45:34 IP:211.21.xxx.xxx 未訂閱
以下是最近收集整理的筆記 以 BCB 6 及 Delphi 7.0 實作 ... 會陸續整理出來    
■■開頭區    ●[BCB]    //-----------    #include 
#pragma hdrstop    #include "Unit1.h"
#include "inifiles.hpp" //自己加常用宣告
#include "FileCtrl.hpp" //自己加常用宣告
//-----------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-----------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//-----------    ●[Delphi]    unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, inifiles(自加);    type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}
===========================================================================================
■■簡單事件宣告    ●[BCB]    void __fastcall TForm1::Button1Click(TObject *Sender)
{    }    ●[Delphi]    procedure TForm1.Button1Click(Sender: TObject);
begin    end;
===========================================================================================
■■複雜事件宣告    ●[BCB]    void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
      const TRect &Rect, int DataCol, TColumn *Column,
      TGridDrawState State)
{    }    ●[Delphi]    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin    end;
===========================================================================================
■■動態建立可視物件(一)    ●[BCB]    TLabel *lb[48]; // 宣告 48 個動態 Label 物件陣列 , Global 變數    .....
.....    int x=0; int y=1;
for (int i=0 ; i<48 ; i  ) // i 等於 0 - 47
    {  
      x  ;
      if (x>8)
         {
            x=1;
            y  ;
         }          lb[i]= new TLabel(this);
      lb[i]->Parent=Form1;  //重要
      lb[i]->&lb_MouseDown; //指派動態物件所需的事件
      lb[i]->&lb_MouseUp;     //指派動態物件所需的事件
      lb[i]->AutoSize=false;
      lb[i]->Alignment=taCenter;
      lb[i]->Color=0x00FF8000; 
      lb[i]->Width=96;
      lb[i]->Height=53;
      lb[i]->Left=(x-1)*(lb[i]->Width 1)-0;
      lb[i]->Top=(y-1)*(lb[i]->Height 1)-0;
      lb[i]->Tag=i;
    }    .....        void __fastcall TForm1::lb_MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   //按下 Label 時將 Label 中的 Tag 值取出 
   ShowMessage(IntToStr( ((TLabel *)Sender)->Tag ));
}    .....    for (int i=1 ; i<48 ; i  )
    {
      delete lb[i];  //刪除 Label 物件陣列      
    }    ●[Delphi]    var i,j,k,x,y:integer;
    lb : array[0..47] of TLabel;  // 宣告 48 個動態 Label 物件陣列 , Global 變數    .....
.....
  
x:=0;
y:=1;
for i := 0 to 47 do
    begin          x:=x 1;
      if (x>8) then
         begin
           x:=1;
           y:=y 1;
         end;          lb[i]:=TLabel.Create(self);
      lb[i].Parent:=Form1;  //重要
      lb[i].OnMouseDown:=lb_MouseDown; //指派動態物件所需的事件
      lb[i]->OnMouseUp:=lb_MouseUp;     //指派動態物件所需的事件
      lb[i].AutoSize:=false;
      lb[i].Alignment:=taCenter;
      lb[i].Color:=$00FF8000;
      lb[i].Width:=96;
      lb[i].Height:=53;
      lb[i].Left:=(x-1)*(lb[i].Width 1)-0;
      lb[i].Top:=(y-1)*(lb[i].Height 1)-0;
      lb[i].Tag:=i;
    end;    .....
  
procedure TForm1.lb_MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //按下 Label 時將 Label 中的 Tag 值取出 
  ShowMessage(IntToStr( TLabel(Sender).Tag ));
end;    .....    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  for i := 0 to 47 do
      begin
        lb[i].Free;  //刪除 Label 物件陣列
      end;
end;  
===========================================================================================
■■動態建立可視物件(二)    ●[BCB]    TTable *NewTable;
NewTable = new TTable(this);
....
delete NewTable; //或 NewTable->Free(); //(BCB 建議使用 delete)    ●[Delphi]    var NewTable:TTable;
NewTable:=TTable.Create(self);
....
NewTable.Free;
===========================================================================================
■■動態建立不可視物件    ●[BCB]    TStringList *TmpList;
TmpList = new TStringList;
....
delete TmpList;  //或 TmpList->Free(); //(BCB 建議使用 delete)    ●[Delphi]    var TmpList:TStringList;
TmpList:=TStringList.Create;
....
TmpList.Free;
===========================================================================================
■■動態建立不可視物件 含開檔    ●[BCB]    TIniFile *MyIni;
MyIni = new TIniFile(ChangeFileExt(Application->ExeName , ".ini"));
....
delete MyIni;  //或 MyIni->Free(); //(BCB 建議使用 delete)    ●[Delphi]    var MyIni: TIniFile;
MyIni := TIniFile.Create(ChangeFileExt(Application.ExeName , '.ini'));
....
MyIni.Free;
===========================================================================================
■■流程控制 if ... else 指令    ●[BCB]    if (<邏輯運算式>) // 一定要有()號
   { ...
   }
else if(<邏輯運算式>)
   {...
   }
else
   {...
   }    ●[Delphi]    if <邏輯運算式> then // 不一定要有()號 , 因有 then
   begin
     ...
   end
else if <邏輯運算式> then
   begin
     ....
   end
else
   begin
     ...
   end; {最後一個 end 才要加 ; 分號}
===========================================================================================
■■流程控制 switch/case ... 指令    ●[BCB]    switch ()
       {
         case iX: ... break;
         case iY: ... break;
         default: ...
       }    ●[Delphi]    case of {no "begin" here}
          iX: begin
                ...
              end; {每個end都要;分號}
          iY: begin
                ...
              end;
          else {此處不用;號}
              begin
                ...
              end;
        end; {這個end要;分號}    ===========================================================================================
■■流程控制 for ... loop 指令    ●[BCB]    for (int iCount = 0; iCount <= 10; iCount  )
    { 
      ...      
      continue; // 繼續
      break; // 中斷迴圈
      ...
    }    ●[Delphi]    for iCount := 1 to 10 do
    begin
      ...
      continue; { 繼續 }
      break; { 中斷迴圈 }      
      ...
    end;    ===========================================================================================
■■流程控制 while ... loop 指令 (前測迴圈)    ●[BCB]    while (<邏輯運算式>)
      { 
        ...      
        continue; // 繼續
        break; // 中斷迴圈
        ...
      }    ●[Delphi]    while <邏輯運算式> do
      begin
        ...      
        continue; // 繼續
        break; // 中斷迴圈
        ...
      end;    ===========================================================================================
■■流程控制 do/repeat ... 指令 (後測迴圈)    ●[BCB]    do {
     ...      
     continue; // 繼續
     break; // 中斷迴圈
     ...
   } while(<邏輯運算式>); // 邏輯運算式 false 迴圈終止    ●[Delphi]    repeat
     ...      
     continue; // 繼續
     break; // 中斷迴圈
     ...
until <邏輯運算式>; // 邏輯運算式 true 迴圈終止
===========================================================================================
■■例外處理    ●[BCB]      try
     {            // test for C   exceptions
       ...       
     }
   catch (...)
     {
       ...
     }      try
     {            // test for C   exceptions
       ...       
     }
   __finally
     {
       ...
     }    ●[Delphi]      try     //不用 begin ... end; 指令
      ...
  except
      ...
  end;      try
      ... 
  finally
      ...
  end;
===========================================================================================
■■訊息攔截 - 如何判斷關閉程式的動作    ●[BCB]    void __fastcall TForm1::FormCreate(TObject *Sender)
{
  WindowProc=MyWndProc; //攔截訊息
}    .....    void __fastcall TForm1::MyWndProc(TMessage &Message)
{      if (Message.Msg == WM_SYSCOMMAND)
     {
       if (Message.WParam== SC_CLOSE)  //若Message為 Close Window
          {
            ShowMessage("使用者按了程式右上角 [X] 按鈕");
          }
     }      if (Message.Msg == WM_ENDSESSION)
      ShowMessage("使用者按了螢幕左下角 [關機] 指令");      //將訊息還給 Form 原來處理程序,否則只是收到關閉指令,但不會真正執行關閉動作   
  WndProc(Message);    }    <補充>
通常我們就在以上兩個時機點帶入一個旗標,
到FormCloseQuery()事件中再決定關機與否 ,
但根據我的測試,WM_ENDSESSION 觸發的時機會比 FormCloseQuery() 事件晚到
(WM_SYSCOMMAND 則不會) ,
也就是說錯過了關機判斷時機後,才會有 WM_ENDSESSION 訊息被收到 ,
這時可改判斷是否收到 WM_QUERYENDSESSION 來代替判斷 WM_ENDSESSION ,
程式流程就會正確判斷無誤     ●[Delphi]    procedure TForm1.FormCreate(Sender: TObject);
begin
  WindowProc:=MyWndProc; //攔截訊息
end;    .....    procedure TForm1.MyWndProc(var Message: TMessage);
begin
  if (Message.Msg = WM_SYSCOMMAND) then
     begin
       if (Message.WParam = SC_CLOSE) then //若Message為 Close Window
          begin
            ShowMessage('使用者按了程式右上角 [X] 按鈕');
          end;
     end;      if (Message.Msg = WM_ENDSESSION) then
      ShowMessage('使用者按了螢幕左下角 [關機] 指令');      //將訊息還給 Form 原來處理程序,否則只是收到關閉指令,但不會真正執行關閉動作
  WndProc(Message);
end;    ===========================================================================================
■■訊息攔截 - 阻止 StringGrid 上的滑鼠按鍵例     ●[BCB]    TWndMethod StringGrid_WindowProc;    .....
.....    __fastcall TForm1::TForm1(TComponent* Owner) //放在 FormCreate 也可
        : TForm(Owner)
{
  StringGrid_WindowProc=StringGrid1->WindowProc;
  StringGrid1->WindowProc=MyWindowProc;
}    .....    void __fastcall TForm1::MyWindowProc(TMessage &Message)
{
  if (Message.Msg==WM_NCHITTEST)     //阻止所有滑鼠按鍵動作
      return;      // 呼叫-原訊息處理程序
  StringGrid_WindowProc(Message);
}    ●[Delphi]    StringGrid_WindowProc : TWndMethod;    .....
.....    procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid_WindowProc:=StringGrid1.WindowProc;
  StringGrid1.WindowProc:=MyWindowProc;
end;    .....    procedure TForm1.MyWindowProc(var Message: TMessage);
begin
  if (Message.Msg=WM_NCHITTEST) then    //阻止所有滑鼠按鍵動作
      exit;      // 呼叫-原訊息處理程序
  StringGrid_WindowProc(Message);
end;
發表人 - bruce0211 於 2003/10/22 09:02:57
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-10-27 15:07:46 IP:211.21.xxx.xxx 未訂閱
 
■■將 StringGrid 之 Fixed 行列內容字串置中    ●[BCB]    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
  //判斷是否是 Fixed 行列 
  if (!State.Contains(gdFixed))
     return;      //設定 RowSelected 或 CellFocused 的顏色
  //if (State.Contains(gdSelected)) ((TStringGrid *)Sender)->Canvas->Brush->Color = clWindow;      //設定某一 Row 的顏色(水平 cell 背景變色)
  //if (ARow==4) ((TStringGrid *)Sender)->Canvas->Brush->Color=clYellow;      //填滿每一 Cell 背景 (清掉舊 Cell Text)
  ((TStringGrid *)Sender)->Canvas->FillRect(Rect);      //繪製新的 Cell Text
  TRect FixRect;
  FixRect=Rect; //修正 Rect 座標
  FixRect.Left=Rect.Left 2;
  FixRect.Top =Rect.Top 2;
  DrawText(((TStringGrid *)Sender)->Canvas->Handle, ((TStringGrid *)Sender)->Cells[ACol][ARow].c_str(),
              ((TStringGrid *)Sender)->Cells[ACol][ARow].Length(), &FixRect, DT_CENTER ); //DT_LEFT , DT_CENTER , DT_RIGHT    }    ●[Delphi]    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var FixRect: TRect;
begin      //判斷是否是 Fixed 行列 
  if not (gdFixed in State) then
     exit;      //設定 RowSelected 或 CellFocused 的顏色
  //if (gdSelected in State) then TStringGrid(Sender).Canvas.Brush.Color := clWindow;      //設定某一 Row 的顏色(水平 cell 背景變色)
  //if (ARow=4) then TStringGrid(Sender).Canvas.Brush.Color:=clYellow;      //填滿每一 Cell 背景 (清掉舊 Cell Text)
  TStringGrid(Sender).Canvas.FillRect(Rect);      //繪製新的 Cell Text
  FixRect:=Rect; //修正 Rect 座標
  FixRect.Left:=Rect.Left 2;
  FixRect.Top :=Rect.Top 2;
  DrawText(TStringGrid(Sender).Canvas.Handle, PChar(TStringGrid(Sender).Cells[ACol,ARow]),
              Length(TStringGrid(Sender).Cells[ACol,ARow]), FixRect, DT_CENTER );  //DT_LEFT , DT_CENTER , DT_RIGHT    end;
發表人 - bruce0211 於 2003/10/27 16:45:50
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-10-28 11:18:09 IP:211.21.xxx.xxx 未訂閱
 
■■跟系統要求配置記憶體區塊    ●[BCB]    char tmp_buf[81];
char *pbBuffer;
unsigned long dwBufferLen;
.....
.....
if ((pbBuffer = (char *) malloc(dwBufferLen)) == NULL)
   {
     sprintf(tmp_buf,"記憶體不足!!\n");
     ShowMessage(tmp_buf);
   }
.....
.....
if(pbBuffer) free(pbBuffer);    ●[Delphi]    var 
pbBuffer  : PByte;
dwBufferLen  : DWORD;
.....
.....
GetMem(pbBuffer, dwBufferLen);
if (pbBuffer=nil) then
   begin
     ShowMessage('記憶體不足');
   end;
.....
.....
if(pbBuffer<>nil) then FreeMem(pbBuffer);    ===========================================================================================
■■型態配置      Delphi_Type  Range                          Format               C   implementation        
-------------------------------------------------------------------------------------
Integer             -2147483648..2147483647      signed 32-bit        int
Cardinal     0..4294967295                unsigned 32-bit      unsigned int
-------------------------------------------------------------------------------------
Shortint     -128..127                    signed 8-bit         char
Smallint     -32768..32767                signed 16-bit        short ; short int
Longint             -2147483648..2147483647      signed 32-bit        int ; long        
Int64             -2^63..2^63-1                signed 64-bit
Byte             0..255                       unsigned 8-bit       unsigned char
Word             0..65535                     unsigned 16-bit      unsigned short
Longword     0..4294967295                unsigned 32-bit      unsigned int ; unsigned long
-------------------------------------------------------------------------------------
DWORD                                                          unsigned long
-------------------------------------------------------------------------------------
AnsiChar     8-bit unsigned character                               char        
WideChar     word-sized Unicode character                       wchar_t        
Char         8-bit unsigned character                               char
-------------------------------------------------------------------------------------
String       Delphi AnsiString                                 AnsiString
AnsiString   Delphi AnsiString                                 AnsiString
String[n]    n = 1..255 bytes                                  SmallString
ShortString  255 bytes                                         SmallString<255>
-------------------------------------------------------------------------------------
Single       32-bit floating point number                      float
Real         32-bit floating point number                      
Double       64-bit floating point number                      double
Extended     80-bit floating point number                      long double
-------------------------------------------------------------------------------------
Pointer      32-bit generic pointer                            void *        
PChar        32-bit pointer to characters                      unsigned char *        
PAnsiChar    32-bit pointer to ANSI characters                 unsigned char *        
Comp         64-bit floating point number                      Comp        
OleVariant   OLE variant value                                 OleVariant            ===========================================================================================
■■將 String 轉成以 NULL 結尾字串(Delphi 的 PChar 或 BCB 的 char *)      ●[BCB]    String url="http://www.hinet.net/";
ShellExecute(NULL,NULL,url.c_str(),NULL,NULL,SW_SHOW);    ○StrPCopy example    void __fastcall TForm1::Button1Click(TObject *Sender)
{
  char* szBuffer = new char[Label1->Caption.Length()   Edit1->Text.Length()   1];
  //或是 char szBuffer[80]; //開固定 buffer size 就不需 delete 了
  StrPCopy(szBuffer, Label1->Caption);
  StrCat(szBuffer, Edit1->Text.c_str());
  Label1->Caption = szBuffer;
  Edit1->Clear();
  delete [] szBuffer;
}    ○StrNew, StrDispose example    void __fastcall TForm1::Button1Click(TObject *Sender)    {
  // Allocate memory.
  char* psz = StrNew(Edit1->Text.c_str());
  Application->MessageBox(psz, "StrNew, StrDispose example", MB_OK);
  // Deallocate memory.
  StrDispose(psz);
}    ●[Delphi]    var
String url="http://www.hinet.net/";
begin
  ShellExecute(NIL,NIL,PChar(url),NIL,NIL,SW_SHOW);
end;    ○StrPCopy example    uses SysUtils;
var
  A: array[0..79] of Char;
  S: String;
begin
  S := 'Honk if you know Blaise.';
  StrPCopy(A, S);
  Canvas.TextOut(10, 10, string(A));
end;    ○StrNew, StrDispose example    uses Sysutils;
procedure TForm1.Button1Click(Sender: TObject);
var
  Temp: PChar;
begin
  // Allocate memory.
  Temp := StrNew(PChar(Edit1.Text));
  ShowMessage(Temp, 'StrNew, StrDispose example', MB_OK);
  // Deallocate memory.
  StrDispose(Temp);
end;    ===========================================================================================
■■判斷物件類別    ●[BCB]    if ( dynamic_cast(AControl) || dynamic_cast(AControl))
   {
     .....
     .....
   }    ●[Delphi]    if (AControl is TWinControl) or (AControl is TGraphicControl) then
   begin
     .....
     .....
   end;    
系統時間:2024-11-10 20:52:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!