星期五, 12月 02, 2011

取得檔案的建立時間、修改時間及訪問時間

procdeure GetFileTime(const Tf:string);
{ 獲取文件時間,Tf表示目標文件路徑和名稱 }
const
Model='yyyy/mm/dd,hh:mm:ss'; { 設定時間格式 }
var
Tp:TSearchRec; { 申明Tp為一個查找記錄 }
T1,T2,T3:string;

begin
FindFirst(Tf,faAnyFile,Tp); { 查找目標文件} T1:=FormatDateTime(Model,
CovFileDate(Tp.FindData.ftCreationTime)));
{ 返回文件的創建時間 }
T2:=FormatDateTime(Model,
CovFileDate(Tp.FindData.ftLastWriteTime)));
{ 返回文件的修改時間 }
T3:=FormatDateTime(Model,Now));
{ 返回文件的當前訪問時間 }
FindClose(Tp);
end;

星期四, 11月 24, 2011

設定元件位置在點擊按鈕的正上方

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  point1: TPoint;
begin
  point1:=Button1.Parent.ClientToScreen(Button1.BoundsRect.TopLeft);
  point1:=ClientToScreen(point1);
  caption := 'X:'+IntToStr(point1.x)+' '+
    'Y:'+IntToStr(point1.y);
  Form2:= TForm2.Create(self);
  try
    Form2.SetPosition(point1.x, point1.y); //x右邊到元件 y上邊到元件
    Form2.ShowModal;
  finally
    Form2.Free;
  end;
end;

end.
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure SetPosition(x, y: Integer);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.SetPosition(x, y: Integer);
begin
  self.left:=x-(self.Width div 2);
  self.Top:=y-self.Height;
end;

end.

星期三, 11月 23, 2011

ms sql 檢查資料表及資料表中的資料欄是否存在


在"資料表"的"欄位"存在,傳回1;"欄位"不存在,傳回0
select count(name) from syscolumns
where id=(
select id from sysobjects
where name='資料表名稱')
and name='欄位名稱'

判斷資料表存不存在
select * from doctor..sysobjects where name='病人' and type='U')
doctor is 資料庫名稱
病人是資料表的名稱

星期四, 11月 17, 2011

combobox自動展開及關閉

新增cxcombobox,並加上item值,再加入下列事件:
procedure TForm1.cxComboBox1MouseEnter(Sender: TObject);
begin
  cxComboBox1.DroppedDown:=true;
end;

procedure TForm1.FormMouseEnter(Sender: TObject);
begin
  cxComboBox1.DroppedDown:=false;
end;

星期一, 11月 14, 2011

StrToDate要注意短日期時間格式

var
t:TDateTime;
fs:TFormatSettings;
begin
fs.ShortDateFormat:='yyyy-mm-dd';
fs.DateSeparator:='-'; //這一行一定要有
t := StrToDate('2010-01-01',fs);

取得當日開始及最後時間

//******************************************************************************
//* GetDayStartDT: 取得當日開始時間AM 00:00:01:00                              *
//******************************************************************************
function mDayStartDT(tmpDT: TDateTime): TDateTime;
var
  wYear, wMonth, wDay: WORD;
begin
  DecodeDate(tmpDT, wYear, wMonth, wDay);
  Result := EncodeDate(wYear, wMonth, wDay)+
            EncodeTime(0, 0, 0, 0);
end;
//******************************************************************************
//* GetDayEndDT: 取得當日結束時間PM 23:59:59:00                                *
//******************************************************************************
function mDayEndDT(tmpDT: TDateTime): TDateTime;
var
  wYear, wMonth, wDay: WORD;
begin
  DecodeDate(tmpDT, wYear, wMonth, wDay);
  Result := EncodeDate(wYear, wMonth, wDay)+
            EncodeTime(23, 59, 59, 0);
end;

星期三, 10月 05, 2011

設定短日期時間格式及日曆格式

  var
    m_pcDateFormat: array[0..127] of Char;  // for locale setting
    m_pcCalendarFormat: array[0..127] of Char;
  begin
    FillChar(m_pcDateFormat, 127*SizeOf(Char), 0);
    FillChar(m_pcCalendarFormat, 127*SizeOf(Char), 0);
    //取得日期格式資訊,並存放置 m_pcDateFormat 中
    GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, m_pcDateFormat, 127) ;

    //取得日曆格式,並存放置 m_pcCalendarFormat 中
    GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_ICALENDARTYPE, m_pcCalendarFormat, 127) ;

    //設定日期格式為 yyyy/MM/dd
    SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, 'yyyy/MM/dd');

    //設定日曆格式為1型式  (1="西曆 (中文)" 4="中華民國曆")
    SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_ICALENDARTYPE, '1');

    //還原
    SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, m_pcDateFormat);
    SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_ICALENDARTYPE, m_pcCalendarFormat);
  end;

星期一, 9月 05, 2011

將數字轉成容量大小或貨幣的字串(3位數一個逗點)

FormatFloat('###,###,###,##0',iFileSize);

星期三, 8月 31, 2011

不錯的delphi相關程式範例及下載網址

http://www.example-code.com/delphi/csv_update.asp

MemoryStream To String

The code you have is unnecessarily complex, even for older Delphi versions. Why should fetching the string version of a stream force the stream's memory to be reallocated, after all?
function MemoryStreamToString(M: TMemoryStream): string;

begin
SetString(Result, M.Memory, M.Size div SizeOf(Char));
end;

That works in all Delphi versions, not just Delphi 2009. It works when the stream is empty without any special case. SetString is an under-appreciated function.

If the contents of your stream aren't changing to Unicode with your switch to Delphi 2009, then you should use this function instead:
function MemoryStreamToString(M: TMemoryStream): AnsiString;

begin
SetString(Result, M.Memory, M.Size);
end;

That's equivalent to your original code, but skips the special cases.

星期一, 5月 23, 2011

取得本地時間及utc時間

DECLARE @LocalDate DATETIME
SET @LocalDate = GETDATE()
-- convert local date to utc date
DECLARE @UTCDate DATETIME
SET @UTCDate = DATEADD(Hour, DATEDIFF(Hour, GETUTCDATE(), GETDATE()), @LocalDate)
-- convert utc date to local date
DECLARE @LocalDate2 DATETIME
SET @LocalDate2 = DATEADD(Hour, DATEDIFF(Hour, GETDATE(), GETUTCDATE()), @UTCDate)
SELECT @LocalDate, @UTCDate, @LocalDate2

星期一, 5月 09, 2011

TStringList儲存及載入Objects內容


unit Unit2;

interface

uses Windows, Classes, SysUtils;

type
SelectPositionRec = Record
io : string;
broker : string;
End;
PSelectPositionRec = ^SelectPositionRec;

TMyStringList = class(TStringList)
private
public
procedure LoadFromFile(const sFileName: string); override;
procedure SaveToFile(const sFileName: string); override;
procedure Delete(Index: Integer); override;
procedure Clear; override;
end;

implementation

{ TMyStringList }

procedure TMyStringList.Clear;
var
i : integer;
begin
for i := 0 to Count - 1 do
Dispose(PSelectPositionRec(Objects[i]));
inherited;
end;

procedure TMyStringList.Delete(Index: Integer);
begin
Dispose(PSelectPositionRec(Objects[Index]));
inherited;
end;

function ReadFSLn(FS: TFileStream): String;
var
Buffer : Char;
LastChr : Char;
begin
//Init!
Result := '';
Buffer := #0;
// LastChr := #0;
//Loop till you find the end of a line
while FS.Position < FS.Size do //Make sure never to pass the EOF
begin
FS.Read(Buffer,1);
if (Buffer=#0) then //(Buffer = #10) or ((LastChr = #13) and (Buffer=#10)) or
exit; //Line found exit!
Result := Result + Buffer;
// LastChr := Buffer;
end;
end;

procedure TMyStringList.LoadFromFile(const sFileName: string);
var
fs: TFileStream;
i : integer;
sStr : string;
ipSelectPositionRec : PSelectPositionRec;
begin
Clear();
if (not FileExists(sFileName)) then
Exit;
fs:=TFileStream.Create(sFileName, fmOpenRead);
try
i:=1;
while FS.Position < FS.Size do
begin
case i of
1: begin
sStr := ReadFSLn(FS);
Inc(i);
end;
2: begin
New(ipSelectPositionRec);
ipSelectPositionRec.io:=ReadFSLn(FS);
Inc(i);
end;
3: begin
ipSelectPositionRec.broker:=ReadFSLn(FS);
AddObject(sStr, TObject(ipSelectPositionRec));
i:=1;
end;
end;
end;
finally
fs.Free;
end;
end;

procedure TMyStringList.SaveToFile(const sFileName: string);
const
CRLF = #0;
var
fs: TFileStream;
i : integer;
sStr : AnsiString;
ipSelectPositionRec : PSelectPositionRec;
begin
fs:=TFileStream.Create(sFileName, fmCreate);
try
for i := 0 to Count - 1 do
begin
ipSelectPositionRec := PSelectPositionRec(Objects[i]);
sStr := strings[i]+CRLF+ipSelectPositionRec^.io+CRLF+ipSelectPositionRec^.broker+CRLF;
fs.WriteBuffer(sStr[1], Length(sStr));
end;
finally
fs.Free;
end;
end;

end.