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

VCL How To - Additional 元件篇

 
bruce0211
版主


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

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-08-07 13:14:47 IP:210.208.xxx.xxx 未訂閱
元件名稱:Chart (圖表元件) 文件版本:for BCB 5.0 文件作者:bruce0211 2002.05 ●前言 首先先在 Form 中放一個 Tchar 物件 什麼屬性都不要設 再放一個 button , 然後在 Button 的 OnClick 事件中加入下段程式碼 你就知道大概如何手動控制 Tchar 了 , 其他請舉一反三 void __fastcall TForm1::Button1Click(TObject *Sender) { Chart1->RemoveAllSeries(); // 清除Chart1上所有舊 Series Chart1->View3D=false; // 不要 3D 立體 Chart1->Legend->Visible=false; // 不秀圖例說明 ●設定此 char Title 名稱 Title 是 StringList 型態 , 不是 String 型態 , 所以不能用 Chart1->Title->Text="xxx" ... Chart1->Title->Text->Clear(); Chart1->Title->Text->Add("test"); ●動態宣告一個 THorizBarSeries 型態的 Series 其它的 Series 型態有哪些 請看 TChartSeries 之 Help Series1=new THorizBarSeries(Chart1) ; Series1->ParentChart=Chart1; Series1->Marks->Visible=true; // 設定要提示說明 Series1->Marks->Style=smsValue; // 提示說明內容為 Label //(註) Series1->Marks->Style 內容請參考 TSeriesMarksStyle 之 Help Series1->SeriesColor=clBlue; // 設線條1為藍色,不設則自動給色 ●輸入假資料 int V; // Value String L; //Label for (int i = 1; i <= 8; i ) { V=i; // Series 值 L="V" IntToStr(i); // 軸名稱 Series1->Add( V , L , clTeeColor ); } 發表人 - bruce0211 於 2002/08/11 15:51:25
bruce0211
版主


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-08-09 22:06:11 IP:163.32.xxx.xxx 未訂閱
元件名稱:StringGrid 文件版本:for Delphi 文件作者:bruce0211 1998/01     ●固定行及固定列:       StringGrid.FixedCols:=固定行之數;       StringGrid.FixedRows:=固定列之數;       StringGrid. FixedColor:=固定行列之顏色;       StringGrid.Color:=資料區之顏色;    ●資料行列之寬高度:       StringGrid.DefaultColWidth:=內定全部之寬度;       StringGrid.DefaultRowHeight:=內定全部之高度;       StringGrid.ColWidths[Index:Longint]:=某一行整行之寬度;        StringGrid.RowHeights[Index:Longint]:=某一列整列之高度;    ●資料區(CELL)指定:       將某一行列停在畫面之資料區最左上角:           StringGrid.LeftCol:=某一行號;           StringGrid.TopRow:=某一列號;       焦點移至某一格(CELL)內:                  StringGrid.Row:=?;           StringGrid.Col:=?;       設定資料行列數:(包含固定行、列亦算在內)                  StringGrid.RowCount:=?;           StringGrid.ColCount:=?;       寫一字串至某一格(CELL)內:           StringGrid.Cells[Col值 , Row值]:=字串;       判斷滑鼠指標目前在哪一格(CELL)範圍內:           在StringGrid之Mouse事件中(UP,DOWN或MOVE)下:              VAR C , R : Longint;              Begin              StringGrid.MouseToCell(X,Y,C,R); {X,Y由MOUSE事件傳入}              {取回 C , R 即為目前之Col , Row值 }               ......    ●StringGrid之Options屬性:         若要於程式執行中開啟或關閉Options某一功能如 ‘goTABS’             開: StringGrid.Options:= StringGrid.Options + [goTABS];             關: StringGrid.Options:= StringGrid.Options - [goTABS];       goFixedHorzLine       固定列間之水平線    goFixedVertLine       固定行間之垂直線    goHorzLine             資料格間水平線    goVertLine             資料格間垂直線    goRangeSelect          滑鼠可多重選擇    goDrawFocusSelected   多重選擇時,第一資料項反白    goRowSizing           滑鼠可改變列高    goColSizing           滑鼠可改變行寬    goRowMoving           滑鼠可搬資料列    goColMoving           滑鼠可搬資料行    goEditing              可編輯(與滑鼠可多重選擇互斥)    goAlwaysShowEditor   須有goEditing,不用按F4或ENTER即有等待輸入游標    goTabs                 允許TAB及Shift-TAB移動游標    goRowSelect           用滑鼠點一下可選取整列(亦與滑鼠可多重選擇互斥)    goThumbTracking      捲軸動時GRID跟著動,否則捲軸動完放開,GRID才動     ●其它應用:    ◎用ENTER鍵替代TAB鍵在格(CELL)內移動:              在StringGrid之KeyDown 事件中下                 if KEY=13 then                    SendMessage(StringGrid.Handle,WM_KEYDOWN,VK_TAB,0);      ◎若要在StringGrid最後一行最後一列按ENTER鍵時增加一列:           在StringGrid之KeyDown 事件中下:              if KEY=13 then                 begin                   if (StringGrid.ROW= StringGrid.ROWCOUNT-1) and                       (StringGrid.COL= StringGrid.COLCOUNT-1) then                       begin                          StringGrid.RowCount:= StringGrid.RowCount +1;                         SendMessage(StringGrid.Handle,WM_KEYDOWN,VK_TAB,0);                       End;                 end;      ◎在格(CELL)內畫圖線:              在StringGrid之DrawCell 事件中下:( Rect變數 由事件產生)              StringGrid.Canvas.Pen.Color:=clBlack;  {指定顏色}              StringGrid.Canvas.Rectangle( Rect.Left, Rect.Top,                                                 Rect.Right, Rect.Bottom);    ◎要準確的抓取每一CELL之RECT座標值要用CellRect方法             不要藉用OnDrawCell事件:             例 procedure TForm1.Button1Click(Sender: TObject);                var Rectangle: TRect;                begin                  Rectangle := StringGrid1.CellRect(3, 2);                  Label1.Caption := IntToStr(Rectangle.Top) + ' is the                                        top';                  Label2.Caption := IntToStr(Rectangle.Bottom) + ' is the                                        bottom';                  Label3.Caption := IntToStr(Rectangle.Left) + ' is the                                        left side';                  Label4.Caption := IntToStr(Rectangle.Right) + ' is the                                        right side';                end;    ◎若要偵測CELL新位置變動 ( 即希望有一OnCellChange事件 ) 可由           OnSelectCell事件中抓其傳回之 Col , Row            不可抓 StringGrid.Col 及StringGrid.Row , 因會慢半拍           但若跳出此StringGrid 再跳回此StringGrid 之原 Cell 位置           則不會觸發此OnSelectCell事件   OnEnter 時所抓之StringGrid.Col 及StringGrid.Row 非滑鼠所點之   位置,而是未進入此StringGrid 前所停留之 Cell 位置    ●附錄 ——— 將StringGrid內容存檔或取出之程序寫法    <存檔> 例:_StrGridSaveToFile(StringGrid1,’C:\TEST.TXT’); <取檔> 例:_StrGridLoadFromFile(StringGrid1,’C:\TEST.TXT’);    (程序原始碼如後)      procedure _StrGridSaveToFile(StrG: TStringGrid ; FileName: String); var   i, j: LongInt;   ss: string;   f: TextFile; begin   AssignFile(f, FileName);   Rewrite(f);   ss := IntToStr(StrG.ColCount) + ',' + IntToStr(StrG.RowCount);   Writeln(f, ss);      for i := 0 to (StrG.RowCount - 1) do   begin     for j := 0 to (StrG.ColCount - 1) do     begin       if _RTRIM(StrG.Cells[j, i]) <> '' then       begin                 ss := IntToStr(j) + ',' + IntToStr(i) + ',' + StrG.Cells[j, i];                 Writeln(f, ss);       end;     end;   end;   CloseFile(f); end;      procedure _StrGridLoadFromFile(StrG: TStringGrid ; FileName: String); var   X, Y: Integer;   ss, ss2: string;   f: TextFile; begin   if not FileExists(FileName) then exit;      AssignFile(f, FileName);   Reset(f);   Readln(f, ss);   if ss <> '' then   begin     StrG.ColCount := _strtoint(_strseg(ss,1));     StrG.RowCount := _strtoint(_strseg(ss,2));     _StrGridClear(StrG);   end;      while not Eof(f) do   begin     Readln(f, ss);     StrG.Cells[_strtoint(_strseg(ss,1)),                _strtoint(_strseg(ss,2))] :=_strseg(ss,3);   end;   CloseFile(f); end;    {以下為上例中會用到的自寫函式}    {-------------------------------------------------------------------------------} FUNCTION _STRTOINT(S:STRING):LONGINT; {與 DELPHI 之 STRTOINT() 不同,格式不對其傳回 0 , 而不是錯誤訊息} BEGIN RESULT:=_INT(_VALUE(S));    END; {-------------------------------------------------------------------------------} FUNCTION _INT(R:REAL):LONGINT; {與 DELPHI 之 INT() 不同,其傳回 LONGINT , 而非 REAL ( 如123.0 ) } VAR RR:STRING; BEGIN   RR:=FLOATTOSTR(INT(R));   RESULT:=STRTOINT(RR); END; {-------------------------------------------------------------------------------} FUNCTION _VALUE(S:STRING):EXTENDED; VAR I:LONGINT;     {88.01.20 修改}     CODE:INTEGER;     SS:STRING; begin S:=_ALLTRIM(S);  {將字串前後空白去掉}    IF _STRNUM(S,'.')=0 THEN    BEGIN      VAL(S,I,CODE);      RESULT:=I;      EXIT;    END ELSE    BEGIN      IF _STRNUM(S,'.')>1 THEN         BEGIN         RESULT:=0;         EXIT;         END;         SS:=S;      DELETE(SS,(POS('.',SS)),1);         VAL(SS,I,CODE);         IF I=0 THEN         BEGIN         RESULT:=0;         EXIT;         END;        RESULT:=STRTOFLOAT(S);       END; end; {-------------------------------------------------------------------------------} FUNCTION _AllTrim(cText: String): String; { 將字串中左右所有的空白字元刪除 } var    i, nlen: integer; begin    {Result := '';    nlen := Length(cText);    for i := 1 to nlen do        if cText[i] <> ' ' then           Result := Result + cText[i]; }       RESULT := _LTRIM(_RTRIM(cTEXT)); end; {-------------------------------------------------------------------------------} FUNCTION _LTRIM(S:STRING):STRING;  { 去字串 左空白} VAR I:INTEGER;     R:STRING; BEGIN   R:='';   IF (S<>'') AND (S<>' ') THEN      BEGIN        FOR I :=1 TO LENGTH(S) DO            IF COPY(S,I,1)<>' ' THEN BREAK;         R:=COPY(S,I,(LENGTH(S)-I+1));      IF R=' ' THEN R:='';         END;   RESULT:=R; END; {-------------------------------------------------------------------------------} FUNCTION _RTRIM(S:STRING):STRING;  {同 Clipper RTRIM() 去掉字串右邊空白} VAR I:INTEGER;     R:STRING; BEGIN   R:='';   IF (S<>'') AND (S<>' ') THEN      BEGIN        FOR I :=LENGTH(S) DOWNTO 1 DO            IF COPY(S,I,1)<>' ' THEN BREAK;      R:=COPY(S,1,I);      IF R=' ' THEN R:='';      END;   RESULT:=R; END; {-------------------------------------------------------------------------------} FUNCTION _StrSeg(S:STRING;I:INTEGER):STRING; {取出 S 字串中第 I 節字串(以逗點分)} VAR T :TstringLIST; BEGIN IF (S='') OR (I<=0) THEN      BEGIN        {RESULT:='NIL';}        RESULT:='';        EXIT;      END;    T:=TstringLIST.CREATE; _StrToStrList(S,T);    IF I>T.COUNT THEN RESULT:=''    ELSE    RESULT:=T[I-1];    T.FREE; END; {-------------------------------------------------------------------------------} PROCEDURE _StrToStrlist(S:STRING; VAR T:TStringList);          {將含有逗點之字串轉換成TstringLIST} VAR P,I : INTEGER; BEGIN T.CLEAR; P:=_STRNUM(S,',');{計算傳入字串中有幾個逗點}    IF P=0 THEN    BEGIN    T.ADD(S);    EXIT;    END;    FOR I := 1 TO (P+1) DO BEGIN   IF I=(P+1) THEN   T.ADD(S)   ELSE   T.ADD(_LEFT(S,POS(',',S)-1));      S:=_CUTLEFT(S,POS(',',S)); END;    END; {-------------------------------------------------------------------------------} FUNCTION _STRNUM(S,SUBS:STRING):INTEGER; {取字串中出現某一子字串之次數 } VAR L,N,I : INTEGER; BEGIN   IF (LENGTH(S)=0) OR (LENGTH(SUBS)=0) THEN       BEGIN         RESULT:=0;         EXIT;       END;      N:=0;   L:=LENGTH(SUBS);   {   FOR I:=1 TO (LENGTH(S)-L+1) DO       BEGIN       IF COPY(S,I,L)=SUBS THEN N:=N+1;       END;}      i:=1;  {1999/06/21 修正,如此才不會將_STRNUM('EEE','EE') 當成2,應為1才對(算過的不算) }   while i<=(length(S)-L+1) do     begin       if copy(S,I,L)=SUBS then          begin            N:=N+1;            i:=i+L;          end       else         i:=i+1;     end;    result:=N; END; {-------------------------------------------------------------------------------} FUNCTION _LEFT(S:STRING;L:INTEGER):STRING; {同 Clipper LEFT() } VAR I:INTEGER;     R:STRING; BEGIN   R:='';      IF (S<>'') AND (L0) THEN BEGIN R:=COPY(S,1,L); END; IF L>=LENGTH(S) THEN R:=S; RESULT:=R; END; {-------------------------------------------------------------------------------} FUNCTION _RIGHT(S:STRING;L:INTEGER):STRING; {同 Clipper RIGHT() } VAR I:INTEGER; R:STRING; BEGIN R:=''; IF (S<>'') AND (L0) THEN BEGIN R:=COPY(S,(LENGTH(S)-L 1),L); END; IF L>=LENGTH(S) THEN R:=S; RESULT:=R; END; {-------------------------------------------------------------------------------} FUNCTION _CUTLEFT(S:STRING;L:INTEGER):STRING; {減掉字串中左邊若干個字元} VAR I:INTEGER; {例 _CUTRIGHT('TEST',1) 即傳回'EST'} R:STRING; BEGIN {R:=S;} R:=''; IF (S<>'') AND (L<=LENGTH(S)) AND (L>0) THEN BEGIN R:=_RIGHT(S,(LENGTH(S)-L)); END; IF L=0 THEN R:=S; RESULT:=R; END; 發表人 - bruce0211 於 2002/08/20 10:32:40
bruce0211
版主


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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-08-11 01:26:22 IP:163.32.xxx.xxx 未訂閱
元件名稱:StringGrid 進階應用 文件版本:for BCB 5.0 文件作者:BCB 源碼任務 (http://home.kimo.com.tw/bruce0829) 2002/08/11    ●前言 StringGrid 的應用很廣泛 , 今天介紹其特技應用 , 也就是最重要的兩個事件應用 : OnSelectCell : 焦點移入某一格內時會觸發 , 類似 Edit 物件之 OnEnter OnDrawCell : 物件描繪時觸發 (幾乎隨時都會觸發), 可抓出當時描繪之 Cell 的基本資料 ,               如 Cell 行列位置 , Cell 的面積座標    今天就來示範兩個特效        ●StringGrid 格(Cells)內變色特效程式範例    1.首先在 Form 上放入一個 StringGrid1 2.照下列方法在 StringGrid1 之 OnDrawCell 事件中寫入程式碼 3.觀察格內變色特效結果     
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------    void __fastcall TForm1::FormActivate(TObject *Sender)
{
  StringGrid1->RowCount=10;
  StringGrid1->ColCount=10;      for (int i=0;i<10;i  )
      {
         for (int j=0;j<10;j  )
         {
           StringGrid1->Cells[i][j]="TEST";
         }
      }    }
//---------------------------------------------------------------------------    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
  if (State.Contains(gdFixed)) return;  //如果遇到固定列或行時不執行後續程式碼      if (ARow==2) StringGrid1->Canvas->Font->Color=clRed;   //字型變色
  if (ACol==4) StringGrid1->Canvas->Brush->Color=clLime        ;  // 垂直 cell 背景變色
  if (ARow==4) StringGrid1->Canvas->Brush->Color=clYellow; // 水平 cell 背景變色      // output the text 不管之前屬性如何設 , 最終的結果都是要從這裡 OutPut
  StringGrid1->Canvas->TextRect (Rect, Rect.Left, Rect.Top,StringGrid1->Cells[ACol][ARow]);    }
//---------------------------------------------------------------------------
●具編輯物件之 StringGrid 程式範例 本範例之 StringGrid 第 0 行(Col) 有圖形顯示(如國旗圖示) StringGrid 第 1 行(Col) 為性別欄 , 焦點(Focus)移入時動態出現 ComboBox 元件 StringGrid 第 2 行(Col) 為日期欄 , 焦點(Focus)移入時動態出現 DateTimePicker 元件 說明 : 當焦點移入某一格內時 , 程式先抓出該格的四個頂角位置座標 然後判斷判斷格(Cell)的行位置 , 選擇出適合的編輯物件(動態產生) 再將這個編輯物件的座標位置放在該格的四個頂角位置座標間 這樣原來的格(Cell)就被蓋住了 , User 會以為還在 格(Cell)上編輯 其實是在動態產生的編輯物件上編輯 , User 在編輯完資料後離開動態物件 動態物件再把編輯資料塞回原來的格(Cell)內
    //---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TComboBox *MyComboBox;
TDateTimePicker *MyDateTimePicker;
//Graphics::TBitmap *MyImage;  //如果是 *.bmp 檔的話
Graphics::TIcon *MyImage;      //如果是 *.ico 檔的話 (國旗圖示)
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  //動態產生 MyComboBox 元件
  MyComboBox=new TComboBox(this);
  MyComboBox->Parent=Form1;
  MyComboBox->OnExit=&MyComboBoxExit;
  MyComboBox->Items->Clear();
  MyComboBox->Items->Add("男");
  MyComboBox->Items->Add("女");
  MyComboBox->Hide();      //動態產生 MyDateTimePicker 日期元件
  MyDateTimePicker=new TDateTimePicker(this);
  MyDateTimePicker->Parent=Form1;
  MyDateTimePicker->OnExit=&MyDateTimePickerExit;
  MyDateTimePicker->Hide();      //動態產生 MyImage 圖形物件
  //MyImage=new Graphics::TBitmap();  //如果是 *.bmp 檔的話
  MyImage=new Graphics::TIcon();      //如果是 *.ico 檔的話
  MyImage->LoadFromFile(ExtractFilePath(Application->ExeName) "F4.ICO"); //載入圖形檔
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  StringGrid1->Cells[0][0]="國籍 (圖形物件)";
  StringGrid1->Cells[1][0]="性別 (ComboBox 物件)";
  StringGrid1->Cells[2][0]="生日 (Date ComboBox 物件)";
}
//---------------------------------------------------------------------------
//為動態產生的 MyComboBox 而寫的事件
//---------------------------------------------------------------------------
void __fastcall TForm1::MyComboBoxExit(TObject *Sender)
{
  // 跳出 MyComboBox 時將資料回寫至 StringGrid1 Cell 內
  StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row]=MyComboBox->Text;
  MyComboBox->Hide();
}
//---------------------------------------------------------------------------
//為動態產生的 MyDateTimePicker 而寫的事件
//---------------------------------------------------------------------------
void __fastcall TForm1::MyDateTimePickerExit(TObject *Sender)
{
  // 跳出 MyDateTimePicker 時將資料回寫至 StringGrid1 Cell 內
  StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row]=DateToStr(MyDateTimePicker->Date);
  MyDateTimePicker->Hide();
}
//---------------------------------------------------------------------------
//當進入 StringGrid1 各個 Cell 內時則秀出對應的物件
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)
{       TRect CellRect;       if (ACol==1)
      {
         CellRect=StringGrid1->CellRect(ACol,ARow);
         MyComboBox->Left=CellRect.Left StringGrid1->Left 2;
         MyComboBox->Top=CellRect.Top StringGrid1->Top 2;
         MyComboBox->Width=CellRect.Width();
         MyComboBox->Height=CellRect.Height();             MyComboBox->Text=StringGrid1->Cells[ACol][ARow];
         MyComboBox->Show();
         MyComboBox->SetFocus();
      }       if (ACol==2)
      {
         CellRect=StringGrid1->CellRect(ACol,ARow);
         MyDateTimePicker->Left=CellRect.Left StringGrid1->Left 2;
         MyDateTimePicker->Top=CellRect.Top StringGrid1->Top 2;
         MyDateTimePicker->Width=CellRect.Width();
         MyDateTimePicker->Height=CellRect.Height();             if (Trim(StringGrid1->Cells[ACol][ARow])!="")
             MyDateTimePicker->Date=StrToDate(StringGrid1->Cells[ACol][ARow]);
         MyDateTimePicker->Show();
         MyDateTimePicker->SetFocus();
      }
}
//---------------------------------------------------------------------------
// 當 OnDrawCell 時繪圖
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{      if (ACol==0 && ARow>0) StringGrid1->Canvas->Draw(Rect.Left,Rect.Top,MyImage);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    // 釋放所有動態產生的物件
    delete MyComboBox;
    delete MyDateTimePicker;
    delete MyImage;
}
//---------------------------------------------------------------------------
發表人 - bruce0211 於 2002/08/11 15:52:12
系統時間:2024-03-29 20:15:36
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!