以下是最近收集整理的筆記
以 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