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

如何將 ini 物件枹裝成 VCL

尚未結案
bruce0211
版主


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

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-05 22:28:04 IP:61.227.xxx.xxx 未訂閱
我常在程式中加入  
 
TIniFile *MyIni;    Form1Create()時加入
MyIni = new TIniFile(ChangeFileExt(Application->ExeName , ".ini"));
....
....
Form1Close()時加入
....
delete MyIni;  
但我百思不得其解,BORLAND 為何不把 TINIFILE 寫成原件 只要拉到畫面上就可以使用 INI 這個 VCL 所以自力救濟 但 TINIFile 好像無法繼承為 vcl 元件 所以只好繼承TComponent TMyIni 元件程式碼如下(可以用) 但如此寫(繼承TComponent),我必需將 tinifile 全部的方法屬性 public 出來 覺得不正統 試問,為何不能將 tinifile 直接計承為新的 vcl ?? 然後我值接在其 create 中 inherited 並 指定 ini 檔名 ChangeFileExt(Application->ExeName , ".ini"));
 
unit MyIni;    interface    uses
  SysUtils, Classes, inifiles, Forms;    type
  TMyIni = class(TComponent)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy; override;      published
    { Published declarations }
  end;    procedure Register;    implementation    var Ini:TIniFile;    constructor TMyIni.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  Ini := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  Ini.WriteInteger('test','test',2);
end;    destructor TMyIni.Destroy;
begin
  inherited Destroy;
  Ini.Free;
end;    procedure Register;
begin
  RegisterComponents('TEST', [TMyIni]);
end;    end.
 
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-06 08:47:29 IP:210.61.xxx.xxx 未訂閱
Delphi7 的 TValueListEditor ?    您的想法宣告成這樣
  TMyIni=class(TComponent)
  private
    FIniFile:TIniFile;
  public
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
    property IniFile:TIniFile read FIniFile write FIniFile;
  end;
就可以操弄IniFile的所有公開方法及屬性
bruce0211
版主


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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-06 09:15:44 IP:211.21.xxx.xxx 未訂閱
可是這樣產生出來的 MyIni 無法直接使用 如 MyIni.WriteString() 而必須這樣用 MyIni1.IniFile.WriteString() >_<    另外,我想將 TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 寫到元件內 這個 MyIni 一產生,就指向與專案同名的檔案上(如 Project1.ini) 但我似乎無法override create()的內容 ....
constructor TMyIni.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);//把 AOwner 改為 ChangeFileExt(Application.ExeName,'.ini' ??
end;
ha0009
版主


發表:16
回覆:507
積分:639
註冊:2002-03-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-11-06 09:33:02 IP:61.30.xxx.xxx 未訂閱
你好:     不了解樓上那篇文章的意思,給你參考看看 < class="code"> TMyIni = class(TComponent) private Ini:TIniFile; // 應該宣告為私有變數,以避免混淆。 protected { Protected declarations } public { Public declarations } constructor Create(AOwner: TComponent; FileName : String); virtual; destructor Destroy; override; published { Published declarations } end; implementation { TMyIni } constructor TMyIni.Create(AOwner: TComponent; FileName: String); begin inherited Create(AOwner); Ini := TIniFile.Create(FileName); Ini.WriteInteger('test','test',2); end; destructor TMyIni.Destroy; begin FreeAndNil (Ini); // 先釋放較保險 inherited; end;
shaofu
高階會員


發表:5
回覆:136
積分:103
註冊:2003-01-07

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-11-06 09:51:41 IP:210.243.xxx.xxx 未訂閱
想了一下, 這個問題的確好像不是很好弄.. 不論是繼承 TIniFile 或是繼承 TComponent, 似乎要動手的地方還是很多 不過換了另個角度想, 以 > 所以小弟引用 class="code"> type TForm = class(Forms.TForm) private FIniFile: TIniFile; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property IniFile: TIniFile read FIniFile write FIniFile; end; TForm1 = class(TForm) ... end; constructor TForm.Create(AOwner: TComponent); begin inherited; FIniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); end; destructor TForm.Destroy; begin FIniFile.Free; inherited; end; procedure TForm1.Button1Click(Sender: TObject); begin IniFile.WriteString('General', 'Test', 'Hello World!'); end; procedure TForm1.Button2Click(Sender: TObject); begin ShowMessage(IniFile.ReadString('General', 'Test', '')); end; 這個假 TForm 的實作可以放到一個獨立的 Unit, 有需要在 use 就好 (一定要放在原本 Forms 這個 Unit 的後面, 以便能覆蓋他), 這樣用似乎也蠻方便的 ^^ 參考看看 發表人 -
bruce0211
版主


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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-11-06 10:21:14 IP:211.21.xxx.xxx 未訂閱
謝謝各位長官的回應 不過還是無法達到我所希望的目的 我希望 1.這個 MyIni 一產生,就指向與專案同名的檔案上,也就是 TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));是寫在元件內部的,檔名不需如ha0009兄的做法,不用由外部傳FileName進去 (不過此設定可保留以便將來同一 Project 同時開多個不同的INI檔) 2.MyIni 的所有用法是在外部設定的,且用法與 TIniFile 用法相同 在元件內部不用任何 Ini.WriteInteger('test','test',2); 或類似的動作(所以 WriteInteger 這些 TIniFile 方法都要 Public 出去讓 user 設定) 3.最好直接使用如 MyIni.WriteString(),而不是間接使用 MyIni1.IniFile.WriteString() 4.寫成元件就是希望簡單化,shaofu兄的方法或許可行,但用一假的Form,會把事情複雜化,況且若寫好的元件要交給別人用,人家問我元件為何帶一假Form,我會不知如何回答 乍看之下,把 TIniFile 寫成 vcl 我以為很簡單,現才知 Borland 為何沒把 TIniFile 做成 VCL 有其考量跟困難度 .... 發表人 - bruce0211 於 2003/11/06 10:25:16
shaofu
高階會員


發表:5
回覆:136
積分:103
註冊:2003-01-07

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-11-06 10:28:41 IP:210.243.xxx.xxx 未訂閱
引言: 乍看之下,把 TIniFile 寫成 vcl 我以為很簡單,現才知 Borland 為何沒把 TIniFile 做成 VCL 有其考量跟困難度 ....
唔.. 對 Borland 來說難度不高吧 事實上 > (將
bruce0211
版主


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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-11-06 11:30:49 IP:211.21.xxx.xxx 未訂閱
引言: .... 唔.. 對 Borland 來說難度不高吧 事實上 > (將 >< face="Verdana, Arial, Helvetica"> 嗚~ 不想預期的事還是發生了 就如我第一個範例 將所有的 TIniFile 屬性方法全部抄一遍 再藉由 TComponent Public 出來 ... 這樣就可直接用 MyIni.WriteString()... 若其它長官還有方法,還是要跟我說一下啊 我去做苦工了,先結案...
bruce0211
版主


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

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-11-06 12:48:23 IP:211.21.xxx.xxx 未訂閱
引言: ... 我希望 1.這個 MyIni 一產生,就指向與專案同名的檔案上,也就是 TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));是寫在元件內部的... 2.MyIni 的所有用法是在外部設定的,且用法與 TIniFile 用法相同.... 3....直接使用如 MyIni.WriteString()......
我寫好了 TMyIni , 初步以達成上數要求,目前只 Public 出以下標準 TIniFile 幾個常用的方法
procedure WriteString(const Section, Ident, Value: String);
    procedure WriteInteger(const Section, Ident: String; Value: Longint);
    procedure WriteBool(const Section, Ident: String; Value: Boolean);
    function  ReadString(const Section, Ident, Default: String): String;
    function  ReadInteger(const Section, Ident: String; Default: Longint): Longint;
    function  ReadBool(const Section, Ident: String; Default: Boolean): Boolean;
 
TMyIni 用法很簡單,安裝完這個元件後,放到你主 Form 中,執行你的程式後,背後就自動產生一個與執行檔同名的ini,如你的執行檔叫 Project1.exe , 就自動產生 Project1.ini ; 你的執行檔叫 Project2.exe , 就自動產生 Project2.ini,當然,要曾經呼叫過 WriteString() 等方法後 , 該 ini 檔才會第一次產生 , 寫入資料就直接 MyIni1.WriteString(...),取出資料就MyIni1.ReadString(...),用法完全跟 TIniFile 相同 TMyIni 程式碼如下,給有需要的人參考
 
unit MyIni;    interface    uses
  SysUtils, Classes, inifiles, Forms;    type
  TMyIni = class(TComponent)
  private
    { Private declarations }
    //FIniFile:TIniFile;      protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy; override;
    //property IniFile:TIniFile read FIniFile write FIniFile;        procedure WriteString(const Section, Ident, Value: String);
    procedure WriteInteger(const Section, Ident: String; Value: Longint);
    procedure WriteBool(const Section, Ident: String; Value: Boolean);
    function  ReadString(const Section, Ident, Default: String): String;
    function  ReadInteger(const Section, Ident: String; Default: Longint): Longint;
    function  ReadBool(const Section, Ident: String; Default: Boolean): Boolean;      published
    { Published declarations }
  end;    procedure Register;    implementation    var TmpIni:TIniFile;    //---------------------------------------------------------------------------
constructor TMyIni.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  TmpIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
end;
//---------------------------------------------------------------------------
destructor TMyIni.Destroy;
begin
  FreeAndNil (TmpIni); // 先釋放較保險
  inherited Destroy;
  //Ini.Free;
end;
//---------------------------------------------------------------------------
procedure TMyIni.WriteString(const Section, Ident, Value: String);
begin
  TmpIni.WriteString(Section, Ident,Value);
end;
//---------------------------------------------------------------------------
procedure TMyIni.WriteInteger(const Section, Ident: String; Value: Longint);
begin
  TmpIni.WriteInteger(Section, Ident,Value);
end;
//---------------------------------------------------------------------------
procedure TMyIni.WriteBool(const Section, Ident: String; Value: Boolean);
begin
  TmpIni.WriteBool(Section, Ident,Value);
end;
//---------------------------------------------------------------------------
function TMyIni.ReadString(const Section, Ident, Default: String): String;
begin
  result:=TmpIni.ReadString(Section, Ident,Default);
end;
//---------------------------------------------------------------------------
function TMyIni.ReadInteger(const Section, Ident: String; Default: Longint): Longint;
begin
  result:=TmpIni.ReadInteger(Section, Ident,Default);
end;
//---------------------------------------------------------------------------
function TMyIni.ReadBool(const Section, Ident: String; Default: Boolean): Boolean;
begin
  result:=TmpIni.ReadBool(Section, Ident,Default);
end;
//---------------------------------------------------------------------------
procedure Register;
begin
  RegisterComponents('MyWay', [TMyIni]);
end;
//---------------------------------------------------------------------------
end.
發表人 - bruce0211 於 2003/11/06 12:56:45
系統時間:2024-06-02 4:50:14
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!