顯示具有 DELPHI 標籤的文章。 顯示所有文章
顯示具有 DELPHI 標籤的文章。 顯示所有文章

2013年9月1日 星期日

Delphi 學習筆記 Listbox 著色

Delphi 學習筆記 Listbox 著色


=============================================================
ListBox  property 中的 style lbOwnerDrawFixed

程式中加這一段

procedure TfrmPhotoDispatch2.ListBox1DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var c:Tcanvas;
    l:integer;
    sr:string;
    cr:string;
begin

    c:=Listbox1.Canvas;
    if not (odSelected in state) then
    begin
        sr:=ListBox1.Items[Index];
        l:=Pos(',',sr);
        cr:=copy(sr,l+1,length(s)-l);

        if cr='PFS' then
        begin
              c.Brush.Color:=clgreen;
              c.Font.Color:=clWhite;
              c.FillRect(Rect);
              with Rect do
                c.textout(Left,Top,copy(sr,1,l-1));
        end;

        if cr='CD_PFS' then
        begin
              c.Brush.Color:=clyellow;
              c.Font.Color:=clblue;
              c.FillRect(Rect);
              with Rect do
                c.textout(Left,Top,copy(sr,1,l-1));
        end;

        if cr='OSI_PFS' then
        begin
              c.Brush.Color:=clPurple;
              c.Font.Color:=clWhite;
              c.FillRect(Rect);
              with Rect do
                c.textout(Left,Top,copy(sr,1,l-1));
        end;
    end;

end;

2013年1月28日 星期一

Delphi 學習筆記偵測檔案夾的檔案個數


如何偵測檔案夾的檔案個數

function FileCount(filePath:string):integer;
var pathstr:string;
    sr: TSearchRec;
    FileAttrs: Integer;
begin
    pathstr:=GetCurrentDir;
    pathstr:=pathstr+'\sqlFile\*.*';
    Form1.Edit1.Text:=pathstr;
    FileAttrs := FileAttrs + faAnyFile;
    FileCount:=0;
    // FindFirst returns 0 if a file was successfully located, otherwise, it returns an //error code.

    if SysUtils.FindFirst('K:\torch\sqlFile\*.*', FileAttrs, sr) = 0 then
    begin
            with Form1.stringgrid1 do
            begin
                rowCount:=1;
                repeat
                if (sr.Attr and FileAttrs) = sr.Attr then
                begin
                   RowCount := RowCount + 1;
                   Cells[1,RowCount-1] := sr.Name;
                   Cells[2,RowCount-1] := IntToStr(sr.Size);
                   inc(FileCount);
                end;
//FindNext returns the next entry that matches the name and attributes specified in a //previous call to FindFirst. The search record must be one that was passed to FindFirst. //The return value is zero if the function was successful. Otherwise the return value is an //error code.            
    until FindNext(sr) <> 0;
            end;
    end;
end;

Delphi 學習筆記 指標


@』是一個運算子,放在變數前面,傳回該變數再記憶體所在之位置。@也可以傳回程序或函數的記憶體位置。『^』有兩種用法,放在型態前面表示要宣告一個指標變數。放在指標變數後面,則為一個運算子,會傳回指標變數所紀錄的記憶體位置的資料值。


ex:

var x,y:Integer;
    p:^Integer;
begin
    x:=17 ;
    p:=@x;
    y:=p^;    showmessage(inttostr(y));
end;

type PInteger=^Integer; //宣告指標形態
var R:Real;
    I:Integer;
    P:Pointer;          //宣告通用型指標變數
    PI:PInteger;
begin

    P:=@R;             //任何型態變數均可指派記憶體位置給p
    PI=Pinteger(P);    //轉換通用型態指標P為整數指標
    I:=PI^;
end;


Pointer型態為通用型態指標,因為指定任何型態資料的記憶體位址,可以利用多用途的指標型態進行強制的資料型態轉換


程序型態允許開發人員以處理變數數值的方式來處理程序和函數

function Max(x,y:Integer):Integer;
begin
    if x>y then result:=x
    else result:=y;

procedure TForm1.Button3Click(Sender: TObject);
var F:function(x,y:Integer):Integer;
begin
   F:=Max; //可以將函數Max 指定給F
   showmessage(inttostr(F(3,5)));
end;end;