+62 812-1171-5379 Fast Respond

Tips dan Trik Delphi

Memeriksa isi direktori kosong / Files / Windows / Delphi 7 - XE


function DirectoryIsEmpty(Directory: string): Boolean;
var
  SR: TSearchRec;
  i: Integer;
begin
  Result := False;
  FindFirst(IncludeTrailingPathDelimiter(Directory) + '*', faAnyFile, SR);
  for i := 1 to 2 do
    if (SR.Name = '.') or (SR.Name = '..') then
      Result := FindNext(SR) <> 0;
  FindClose(SR);
end;


// Contoh penerapannya:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if DirectoryIsEmpty('C:\test') then
    Label1.Caption := 'empty'
  else
    Label1.Caption := 'not empty';
end;

Select all pada DBGrid / Database / Windows / Delphi 7 - XE


function GridSelectAll(Grid: TDBGrid): Longint;
begin
  Result := 0;
  Grid.SelectedRows.Clear;
  with Grid.DataSource.DataSet do
  begin
    First;
    DisableControls;
    try
      while not EOF do
      begin
        Grid.SelectedRows.CurrentRowSelected := True;
        Inc(Result);
        Next;
      end;
    finally
      EnableControls;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GridSelectAll(DBGrid1);
end;


Mengetahui jenis processor intel atau amd / Tool / Windows / Delphi 7 - XE


function GetCPUVendor: string;
var
  aVendor: array [0 .. 2] of LongWord;
  iI, iJ: Integer;
begin
  asm
    push  ebx
    xor   eax, eax
    dw    $A20F // CPUID instruction
    mov   LongWord ptr aVendor, ebx
    mov   LongWord ptr aVendor[+4], edx
    mov   LongWord ptr aVendor[+8], ecx
    pop   ebx
  end;
  for iI := 0 to 2 do
    for iJ := 0 to 3 do
      Result := Result +  
        Chr((aVendor[iI] and ($000000ff shl(iJ * 8))) shr(iJ * 8));
end;

Merubah font pada hint / Tool / Windows / Delphi 7 - XE


type
  TExHint = class(THintWindow)
  public
    constructor Create(AOwner: TComponent); override;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
constructor TExHint.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  with Canvas.Font do
  begin
    Name  := 'Verdana';
    Size  := Size + 15;
    Style := [fsBold, fsItalic];
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  HintWindowClass  := TExHint;
end;

Memberi warna baris tertentu pada DBGrid / Database / Windows / Delphi 7 - XE


procedure TGridForm.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  grid : TDBGrid;
  row : integer;
begin
  grid := sender as TDBGrid;
 
  row := grid.DataSource.DataSet.RecNo;
 
  if Odd(row) then
    grid.Canvas.Brush.Color := clSilver
  else
    grid.Canvas.Brush.Color := clDkGray;
 
  grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
 
end; (* DBGrid OnDrawColumnCell *)

Memanggil file txt masuk kedalam Stringgrid / Files / Windows / Delphi 7 - XE


procedure LoadStringGrid(AStringGrid: TStringGrid; const AFileName: TFileName);
var
  F: TextFile;
  Tmp, I, K: Integer;
  StrTemp: String;
begin
    AssignFile(F, AFileName);
    Reset(F);
    with AStringGrid do
    begin
      // Get number of columns
      Readln(F, Tmp);
      ColCount := Tmp;
      // Get number of rows
      Readln(F, Tmp);
      RowCount := Tmp;
      // loop through cells & fill in values
      for I := 0 to ColCount - 1 do
        for K := 0 to RowCount - 1 do
        begin
          Readln(F, StrTemp);
          Cells[I, K] := StrTemp;
        end;
    end;
    CloseFile(F);
end;

Menyimpan stringgrid kedalam bentuk file txt / Files / Windows / Delphi 7 - XE


procedure SaveStringGrid(AStringGrid: TStringGrid; const AFileName: TFileName);
var
  F: TextFile;
  I, K: Integer;
begin
    AssignFile(F, AFileName);
    Rewrite(F);
    with AStringGrid do
    begin
        // Write number of Columns/Rows
        Writeln(F, ColCount);
        Writeln(F, RowCount);
        // loop through cells
        for I := 0 to ColCount - 1 do
          for K := 0 to RowCount - 1 do
            Writeln(F, Cells[I, K]);
    end;
    CloseFile(F);
end;


Mengganti nama komputer / Tool / Windows / Delphi - XE


function SetPCName(AName: string): Boolean;
var
  PCName: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
  Size: Cardinal;
begin
  StrPCopy(PCName, AName);
  Result := Windows.SetComputerName(PCName);
end;

Mengirimkan perintah control seperti hotkey / Form / Windows / Delphi 7 - XE


procedure PostKeyEx32(key: Word; const shift: TShiftState; specialkey: Boolean);
type
  TShiftKeyInfo = record
    shift: Byte;
    vkey: Byte;
  end;
  ByteSet = set of 0..7;
const
  shiftkeys: array [1..3] of TShiftKeyInfo = (
    (shift: Ord(ssCtrl) ; vkey: VK_CONTROL),
    (shift: Ord(ssShift) ; vkey: VK_SHIFT),
    (shift: Ord(ssAlt) ; vkey: VK_MENU)
  );
var
  flag: DWORD;
  bShift: ByteSet absolute shift;
  j: Integer;
begin
  for j := 1 to 3 do
  begin
    if shiftkeys[j].shift in bShift then
      keybd_event(
        shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0
    );
  end;
  if specialkey then
    flag := KEYEVENTF_EXTENDEDKEY
  else
    flag := 0;

  keybd_event(key, MapvirtualKey(key, 0), flag, 0);
  flag := flag or KEYEVENTF_KEYUP;
  keybd_event(key, MapvirtualKey(key, 0), flag, 0);

  for j := 3 downto 1 do
  begin
    if shiftkeys[j].shift in bShift then
      keybd_event(shiftkeys[j].vkey,MapVirtualKey(shiftkeys[j].vkey, 0),KEYEVENTF_KEYUP,0);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
       // Simulate PRINTSCREEN - snapshot of the full screen
  PostKeyEx32(VK_SNAPSHOT, [], False) ;

  // Simulate PRINTSCREEN - snapshot of the active window
  PostKeyEx32(VK_SNAPSHOT, [ssAlt], False) ;

  // Simulate Spacebar key
  PostKeyEx32(VK_space, [], False) ;// ( I changed this)

  // Simulate Alt+F4 - close active window
   PostKeyEx32(VK_F4, [ssAlt], False) ;
end;


                                        
Membuat Log Off User Windows / Tool / Windows / Delphi 7 - XE


procedure LogOff;
begin
     ExitWindowsEx(EWX_LOGOFF, 0);
end;