2014年3月3日 星期一

delphi dbgrid 多選實現

procedure TForm3.dbgrd1CellClick(Column: TColumn);
//shift 多選
begin
     if (dgmultiselect in dbgrd1.options) then
        if getkeystate(vk_shift) < short(0) then
        begin
           getcursorpos(thispoint);
           while not dbgrd1.DataSource.DataSet.Eof and not dbgrd1.DataSource.DataSet.bof and
                 not (dbgrd1.datasource.dataset.fields[3].asstring =
                       thisfield) do
           begin
                 if thispoint.y > firstpoint.y then
                    dbgrd1.DataSource.DataSet.prior
                 else
                 dbgrd1.DataSource.DataSet.next;
                 dbgrd1.selectedrows.currentrowselected := true;
           end;
        end;
     getcursorpos(firstpoint);
     thisfield := dbgrd1.datasource.dataset.fields[3].asstring;
end;

2014年1月29日 星期三

字串轉數字的小技巧

最常用的 strtoint() 或是 strtofloat() 或是 strtocurr()
不過這會有個問題
如果使用者輸入的不是數字的話,就會出現錯誤

一般習慣是會用 try except ..來處理
但是Delphi裡面有function可以處理這個問題,不用再使用try except來處理

strtointDef('字串',0);
strtofloatDef('字串',0);
strtocurrDef('字串',0);
後面的0就是, 轉不過去時, 就回傳的數字,可以自訂

exsample:

strtointDef('123',0)  回傳 123
strtointDef('xxx',0)  回傳 0

2014年1月27日 星期一

字串-format的用法

var
  text : string;
begin
  // Just 1 data item
  ShowMessage(Format('%s', ['Hello']));

  // A mix of literal text and a data item
  ShowMessage(Format('String = %s', ['Hello']));
  ShowMessage('');

  // Examples of each of the data types
  ShowMessage(Format('Decimal          = %d', [-123]));
  ShowMessage(Format('Exponent         = %e', [12345.678]));
  ShowMessage(Format('Fixed            = %f', [12345.678]));
  ShowMessage(Format('General          = %g', [12345.678]));
  ShowMessage(Format('Number           = %n', [12345.678]));
  ShowMessage(Format('Money            = %m', [12345.678]));
  ShowMessage(Format('Pointer          = %p', [addr(text)]));
  ShowMessage(Format('String           = %s', ['Hello']));
  ShowMessage(Format('Unsigned decimal = %u', [123]));
  ShowMessage(Format('Hexadecimal      = %x', [140]));
end;


Hello
   String = Hello
  
   Decimal          = -123
   Exponent         = 1.23456780000000E+004
   Fixed            = 12345.68
   General          = 12345.678
   Number           = 12,345,68
   Money            = ?12,345.68
   Pointer          = 0069FC90
   String           = Hello
   Unsigned decimal = 123
   Hexadecimal      = 8C

2014年1月24日 星期五

檢查是否為正確的Mac Address格式

function isMacAdrFormat(str: String): boolean;
begin
  Result := False;
  if Length(str) <> 17 then
    Exit;
  Result := ((str[1] in ['0'..'9', 'A'..'F']) and
             (str[2] in ['0'..'9', 'A'..'F']) and
             (str[3] = '-') and
             (str[4] in ['0'..'9', 'A'..'F']) and
             (str[5] in ['0'..'9', 'A'..'F']) and
             (str[6] = '-') and
             (str[7] in ['0'..'9', 'A'..'F']) and
             (str[8] in ['0'..'9', 'A'..'F']) and
             (str[9] = '-') and
             (str[10] in ['0'..'9', 'A'..'F']) and
             (str[11] in ['0'..'9', 'A'..'F']) and
             (str[12] = '-') and
             (str[13] in ['0'..'9', 'A'..'F']) and
             (str[14] in ['0'..'9', 'A'..'F']) and
             (str[15] = '-') and
             (str[16] in ['0'..'9', 'A'..'F']) and
             (str[17] in ['0'..'9', 'A'..'F']));

end;

讓程式不能重覆開啟

//專案檔最前面加上,1.檢查Mutex,若XMonitor已存在,則離開。  
if ProgramAlreadyExists() then exit;  
  
function ProgramAlreadyExists():boolean;  
var  
  hRunningForm: Thandle;  
begin  
  //g_hMutex:=CreateMutex(nil, False, 'X-Console');  
  g_hMutex:=CreateEvent(nil, False, TRUE, 'X-Monitor');  
  if (g_hMutex=0) OR (GetLastError()=ERROR_ALREADY_EXISTS) then //已存在  
  begin  
    hRunningForm := FindWindow(_XMONITOR_CLASS_NAME,nil);  
    if hRunningForm<>0 then  
    begin  
      SetForegroundWindow(hRunningForm);  
//      PostMessage(hRunningForm,WM_SYSCOMMAND,SC_MAXIMIZE,0);  
    end;  
  
    Result:=true;  
  end  
  else  
    Result:=false;  
end; 

判斷磁碟機是否有效

function ValidDrive( driveletter: Char ): Boolean;  
var  
  mask: String[6];  
  sRec: TSearchRec;  
  oldMode: Cardinal;  
  retcode: Integer;  
begin  
   oldMode :=SetErrorMode( SEM_FAILCRITICALERRORS );  
   mask:= '?:\*.*';  
   mask[1] := driveletter;  
   {$I-}  { don't raise exceptions if we fail }  
   retcode := FindFirst( mask, faAnyfile, SRec );  
   if retcode = 0 then  
     FindClose( SRec );  
   {$I+}  
   Result := Abs(retcode) in  
     [ERROR_SUCCESS,ERROR_FILE_NOT_FOUND,ERROR_NO_MORE_FILES];  
   SetErrorMode( oldMode );  
end; { ValidDrive } 

日期相差多少年、月、日、時、分、秒

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  a,b: Tdatetime;
  c: string;
begin
  a:=2007/3/13;
  b:=2006/3/2;
  c := '';
  if (daysbetween(a,b) div 30) <> 0 then
    c := inttostr(daysbetween(a,b) div 30)+'個月';

  if ((daysbetween(a,b) mod 30) <> 0)and ((daysbetween(a,b) div 30) <> 0)then
    c := c + '又' + inttostr(daysbetween(a,b) mod 30)+'天'
  else if (daysbetween(a,b) mod 30) <> 0 then
    c := inttostr(daysbetween(a,b) mod 30)+'天';

  showmessage(c);
  showmessage(inttostr(yearsbetween(a,b))+'年');
  showmessage(inttostr(monthsbetween(a,b))+'月');
  showmessage(inttostr(daysbetween(a,b))+'天');
  showmessage(inttostr(hoursbetween(a,b))+'小時');
  showmessage(inttostr(minutesbetween(a,b))+'分鐘');
  showmessage(inttostr(secondsbetween(a,b))+'秒');  
end;