線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1444
推到 Plurk!
推到 Facebook!

panel, label 存檔

尚未結案
wsxcv123
一般會員


發表:36
回覆:27
積分:12
註冊:2004-11-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-30 01:22:46 IP:218.168.xxx.xxx 未訂閱
在我的程式,user 可以自己產生 panel 和 label ,更改 panel 和 label 的一些屬性。我想把 user 產生的東西存起來。存檔要怎ㄇ做比較好呢?
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-30 10:47:39 IP:210.243.xxx.xxx 未訂閱
給你一個小範例, 測試流程如下: 1. 開啟程式, 按Button1將動態產生的Panel1, Label1放到Form1上 2. 按Button3將Panel1, Label1寫入c:\1.dat, 關閉程式. 3. 重新啟動程式, 直接按Button4, 自c:\1.dat讀取Panel1, Label1並放到Form1上.
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;    type
  TForm1 = class(TForm)
    Button3: TButton;
    Button4: TButton;
    Button1: TButton;
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;      // 程式會動態產生 一個Panel, 一個Label.
  Panel1: TPanel;
  Label1: TLabel;    implementation    {$R *.DFM}    procedure TForm1.Button3Click(Sender: TObject);
var
  OutputStream: TFileStream;    begin
  // 將 Panel1, Label1寫入c:\1.dat
  OutputStream := TFileStream.Create('c:\1.dat', fmCreate);
  try
    OutputStream.WriteComponent(Panel1);
    OutputStream.WriteComponent(Label1);
  finally
    OutputStream.Free;
  end;
end;    procedure TForm1.Button4Click(Sender: TObject);
var
  InputStream: TFileStream;    begin
  // 從c:\1.dat讀取 Panel1, Label1 property, 並放到Form1上
  InputStream := TFileStream.Create('c:\1.dat', fmOpenRead);
  try
    InputStream.ReadComponent(Panel1);
    InputStream.ReadComponent(Label1);
    InsertControl(Panel1);
    InsertControl(Label1);
  finally
    InputStream.Free;
  end;
end;    procedure TForm1.Button1Click(Sender: TObject);
begin
  // 設定 Panel1 Property
  Panel1.Left := 100;
  Panel1.Top := 100;
  Panel1.Width := 100;
  Panel1.Height := 100;
  Panel1.Caption := 'TestPanel';
  InsertControl(Panel1);      // 設定 Label1 Property
  Label1.Left := 10;
  Label1.Top := 10;
  Label1.Caption := 'TestLabel';
  InsertControl(Label1);
end;    procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Assigned(Panel1) then Panel1.Free;
  if Assigned(Label1) then Label1.Free;
end;    procedure TForm1.FormCreate(Sender: TObject);
begin
  // 動態產生 一個Panel, 一個Label.
  Panel1 := TPanel.Create(Self);
  Label1 := TLabel.Create(Self);
end;    end.
Fishman
尊榮會員


發表:120
回覆:1949
積分:2163
註冊:2006-10-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-30 10:54:18 IP:210.65.xxx.xxx 未訂閱
Hi wsxcv123,    以下有幾篇分享,你也可以參可看看    如何存取元件屬性,不是用文字檔或資料庫的方法的方法 http://delphi.ktop.com.tw/topic.php?topic_id=50270 網咖管理金剛組合版 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=28622 網咖介面for BCB step1 http://delphi.ktop.com.tw/topic.php?topic_id=44023 網咖介面for BCB step2 http://delphi.ktop.com.tw/topic.php?topic_id=44034 網咖介面for BCB step3 http://delphi.ktop.com.tw/topic.php?topic_id=44056 網咖介面for bcb final http://delphi.ktop.com.tw/topic.php?topic_id=44039 網咖介面 【大家一起來】版本大集合 http://delphi.ktop.com.tw/topic.php?topic_id=44077 ---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-30 13:09:03 IP:210.243.xxx.xxx 未訂閱
抱歉我發現我搞錯版了, 重貼一個BCB的版本 
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;    // 程式會動態產生 一個Panel, 一個Label.
TPanel* Panel1;
TLabel* Label1;    //---------------------------------------------------------------------------    void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // 動態產生 一個Panel, 一個Label.
  Panel1 = new TPanel(this);
  Label1 = new TLabel(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  if (Panel1) delete Panel1;
  if (Label1) delete Label1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // 設定 Panel1 Property
  Panel1->Left = 100;
  Panel1->Top = 100;
  Panel1->Width = 100;
  Panel1->Height = 100;
  Panel1->Caption = "TestPanel";
  InsertControl(Panel1);      // 設定 Label1 Property
  Label1->Left = 10;
  Label1->Top = 10;
  Label1->Caption = "TestLabel";
  InsertControl(Label1);    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  // 將 Panel1, Label1寫入c:\1.dat
  TFileStream* OutputStream = new TFileStream("c:\\1.dat", fmCreate);
  __try
  {
    OutputStream->WriteComponent(Panel1);
    OutputStream->WriteComponent(Label1);
  }
  __finally
  {
    delete OutputStream;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
  // 從c:\1.dat讀取 Panel1, Label1 property, 並放到Form1上
  TFileStream* InputStream = new TFileStream("c:\\1.dat", fmOpenRead);
  __try
  {
    InputStream->ReadComponent(Panel1);
    InputStream->ReadComponent(Label1);
    InsertControl(Panel1);
    InsertControl(Label1);
  }
  __finally
  {
    delete InputStream;
  }
}
//---------------------------------------------------------------------------
wsxcv123
一般會員


發表:36
回覆:27
積分:12
註冊:2004-11-10

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-30 19:08:57 IP:61.222.xxx.xxx 未訂閱
哇自己寫的 component 也可以用ㄜ。 存出來的檔案有點大說,一個 panel就有 1kb.    另外請問, 我有一個 component, CAObject inherits CBaseObject。 CBaseObject 又 inherits TCustomControl。 我把一些共用的東西如: TLabel 定義在 CBaseObject.    CAObject *abc; abc = new CAObject(this) abc->Parent = this    WriteComponent(abc) ... 好像ok ReadComponent(abc)...就掛了 (error: Label 找不到)     
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-12-31 11:31:23 IP:61.64.xxx.xxx 未訂閱
引言: 哇自己寫的 component 也可以用ㄜ。 存出來的檔案有點大說,一個 panel就有 1kb. 另外請問, 我有一個 component, CAObject inherits CBaseObject。 CBaseObject 又 inherits TCustomControl。 我把一些共用的東西如: TLabel 定義在 CBaseObject. CAObject *abc; abc = new CAObject(this) abc->Parent = this WriteComponent(abc) ... 好像ok ReadComponent(abc)...就掛了 (error: Label 找不到) < face="Verdana, Arial, Helvetica"> 我試了一下的確如此, 不過我目前沒空去查原因, 有興趣你可以去看Delphi的Source Code. 我用了一個小技巧可以避開這種錯誤, 程式碼如下, 這個範例我定義了一個Panel, 這個Panel裡內建了一個TLabel, 其它詳情請看程式註解 CPP檔貼下篇
// Unit1.h
//---------------------------------------------------------------------------    #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
  TButton *Button1;
  TButton *Button3;
  TButton *Button4;
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall FormDestroy(TObject *Sender);
  void __fastcall Button1Click(TObject *Sender);
  void __fastcall Button3Click(TObject *Sender);
  void __fastcall Button4Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
  __fastcall TForm1(TComponent* Owner);
};    class TestPanel: public TPanel
{
  public:
    TLabel* TestLabel;
    inline __fastcall virtual TestPanel(TComponent* Owner): TPanel(Owner)
    {
      TestLabel = new TLabel(this);
      TestLabel->Left = 10;
      TestLabel->Top = 10;
      TestLabel->Caption = "TestLabel";
      InsertControl(TestLabel);
    }        inline __fastcall virtual ~TestPanel()
    {
      if (TestLabel) delete TestLabel;
    }
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-12-31 11:35:36 IP:61.64.xxx.xxx 未訂閱
續上篇, 測試流程如下 1. 開啟程式, 按Button1將動態產生的Panel1, Label1放到Form1上 2. 按Button3將Panel1, Label1寫入c:\1.dat, 關閉程式. 3. 重新啟動程式, 直接按Button4, 自c:\1.dat讀取Panel1, Label1並放到Form1上.    
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TestPanel* Panel1;    //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------    void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Panel1 = new TestPanel(this);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  if (Panel1) delete Panel1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Panel1->Top = 100;
  Panel1->Left = 100;
  Panel1->Width = 100;
  Panel1->Height = 100;
  Panel1->Caption = "TestPanel";
  InsertControl(Panel1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  // 將 Panel1, Label1寫入c:\1.dat
  TFileStream* OutputStream = new TFileStream("c:\\1.dat", fmCreate);
  __try
  {
    // 先將子元件寫入
    OutputStream->WriteComponent(Panel1->TestLabel);
    // 子元件寫入後把它的Parent設為Form1, 避免寫入Panel時再次寫入子元件.
    Panel1->TestLabel->Parent = Form1;
    // 寫入Panel Properties
    OutputStream->WriteComponent(Panel1);
    // 還原子元件Parent.
    Panel1->TestLabel->Parent = Panel1;
  }
  __finally
  {
    delete OutputStream;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
  // 從c:\1.dat讀取 Panel1, Label1 property, 並放到Form1上
  TFileStream* InputStream = new TFileStream("c:\\1.dat", fmOpenRead);
  __try
  {
    // 先讀取子元件Properties
    InputStream->ReadComponent(Panel1->TestLabel);
    // 再讀取Panel Properties
    InputStream->ReadComponent(Panel1);
    InsertControl(Panel1);
  }
  __finally
  {
    delete InputStream;
  }
}
//---------------------------------------------------------------------------
系統時間:2024-04-24 5:33:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!