Çeşitli kod ipuçları

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosya değiştirme bilgileri almak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function GetFileModifyDate(FileName: string): TDateTime;
var
  h: THandle;
  Struct: TOFSTRUCT;
  lastwrite: Integer;
  t: TDateTime;
begin
  h := OpenFile(PChar(FileName), Struct, OF_SHARE_DENY_NONE);
  try
    if h <> HFILE_ERROR then
    begin
      lastwrite := FileGetDate(h);
      Result    := FileDateToDateTime(lastwrite);
    end;
  finally
    CloseHandle(h);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Opendialog1.Execute then
    label1.Caption := FormatDateTime('dddd, d. mmmm yyyy hh:mm:ss',
      GetFileModifyDate(Opendialog1.FileName));
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

TEXT dosyayı satır satır okumak ve değiştirmek

Mesaj gönderen ikutluay »

Kod: Tümünü seç

procedure TForm1.Button1Click(Sender: TObject);
var
  i, z: Integer;
  f: TextFile;
  t: string;
  Data: array of string;
begin
  if OpenDialog1.Execute then
  begin
    //Read line by line in to the array data
    AssignFile(f, OpenDialog1.FileName);
    Reset(f);
    z := 0;
    SetLength(Data, 0);
    //Repeat for each line until end of file
    repeat
      Inc(z);
      readln(f, t);
      SetLength(Data, Length(Data) + Length(t));
      Data[z] := t;
    until EOF(f);

    SetLength(Data, Length(Data) + 3 * z);
    //Add to each line the line number
    for i := 1 to z do Data[i] := IntToStr(i) + ' ' + Data[i];
    SetLength(Data, Length(Data) + 2);
    //Add a carriage return and line feed
    Data[1] := Data[1] + #13 + #10;
    i       := Length(Data[5]);
    Data[5] := '';
    SetLength(Data, Length(Data) - i);
    //create a new textfile with the new data
    AssignFile(f, OpenDialog1.FileName + '2');
    ReWrite(f);
    //write all lines
    for i := 1 to z do writeln(f, Data[i]);
    //save file and close it
    CloseFile(f);
  end;
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

uzun path göstermek

Mesaj gönderen ikutluay »

Kod: Tümünü seç

{
 Set the label autosize property to false and set the property width
 to the max. displayed length
}
 

uses
  FileCtrl;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Opendialog1.Execute then
    label1.Caption := MinimizeName(Opendialog1.FileName,
                                   label1.Canvas, label1.Width);
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosya ASCII tiptemi

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function isAscii(NomeFile: string): Boolean;
const
  SETT = 2048;
var
  i: Integer;
  F: file;
  a: Boolean;
  TotSize, IncSize, ReadSize: Integer;
  c: array[0..Sett] of Byte;
begin
  if FileExists(NomeFile) then
  begin
    {$I-}
    AssignFile(F, NomeFile);
    Reset(F, 1);
    TotSize := FileSize(F);
    IncSize := 0;
    a       := True;
    while (IncSize < TotSize) and (a = True) do
    begin
      ReadSize := SETT;
      if IncSize + ReadSize > TotSize then ReadSize := TotSize - IncSize;
      IncSize := IncSize + ReadSize;
      BlockRead(F, c, ReadSize);
      // Iterate
      for i := 0 to ReadSize - 1 do
        if (c[i] < 32) and (not (c[i] in [9, 10, 13, 26])) then a := False;
    end; { while }
    CloseFile(F);
    {$I+}
    if IOResult <> 0 then Result := False
    else 
      Result := a;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    if isAscii(OpenDialog1.FileName) then
      ShowMessage('ASCII File');
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

başka bir programı çalıştırmak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

uses
  ShellApi;

{ Start notepad }

ShellExecute(Handle, 'open', 'notepad.exe', '', nil, SW_SHOW);

WinExec('C:\Windows\notepad.exe', SW_SHOW);

{ Start notepad and load a file }

ShellExecute(Handle, 'open', 'notepad', 'c:\MyFile.txt', nil, SW_SHOW);

{ Open a txt file }

ShellExecute(Handle, 'open', 'c:\Readme.txt', nil, nil, SW_SHOW);


{ Calling "Dir" from the DOS-Prompt and redirect the output to a file }

{1. With Winexec }

procedure ExecuteShellCommand(cmdline: string; hidden: Boolean);
const
  flags: array [Boolean] of Integer = (SW_SHOWNORMAL, SW_HIDE);
var
  cmdbuffer: array [0..MAX_PATH] of Char;
begin
  GetEnvironmentVariable('COMSPEC', cmdBUffer, SizeOf(cmdBuffer));
  StrCat(cmdbuffer, ' /C ');
  StrPCopy(StrEnd(cmdbuffer), cmdline);
  WinExec(cmdbuffer, flags[hidden]);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecuteShellCommand('dir C:\ > c:\temp\dirlist.txt', True);
end;


{2. With Shellexecute }

procedure ExecuteShellCommand(cmdline: string; hidden: Boolean);
const
  flags: array[Boolean] of Integer = (SW_SHOWNORMAL, SW_HIDE);
var
  cmdbuffer: array[0..MAX_PATH] of Char;
begin
  GetEnvironmentVariable('COMSPEC', cmdBUffer, SizeOf(cmdBuffer));
  ShellExecute(0,'open',cmdbuffer, PChar('/c' + cmdline), nil, flags[hidden]);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecuteShellCommand('copy file1.txt file2.txt', True);
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

erarly trap ve programı kapat

Mesaj gönderen ikutluay »

Kod: Tümünü seç

Procedure TForm1.HandleMessage(var Msg:TMessage);
begin
 if msg.msg = WM_CLOSE //You can insert there other message constans from windows unit.
 then
  begin
   {Your code when user clicked close button}
   showmessage('You can not close this window!');
  end
 else form1.wndproc(msg); //if not closed system handle the message default
end;
procedure TForm1.Create(Sender:Tobject);
begin
 form1.Windowproc:=form1.handlemessage; //run handling
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

toplam ve boş disk alanı miktarını bulmak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

procedure TForm1.Button1Click(Sender: TObject);
var
  freeSpace, totalSpace: Double;
  s: Char;
begin
  // Drive letter
  // Laufwerksbuchstabe
  s := 'D';

  freeSpace  := DiskFree(Ord(s) - 64);
  totalSpace := DiskSize(Ord(s) - 64);

  label1.Caption := Format('Free Space: %12.0n', [freeSpace]);
  Label2.Caption := Format('Total Space: %12.0n', [totalSpace]);
  Label3.Caption := IntToStr(Round((totalSpace - freeSpace) / totalSpace * 100)) +
    ' Percent used.';
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

özel windows klasörlerin (path) yollarını bulmak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

uses
  ActiveX, ShlObj;

procedure TForm1.Button1Click(Sender: TObject);
// Replace CSIDL_HISTORY with the constants below
var
  Allocator: IMalloc;
  SpecialDir: PItemIdList;
  FBuf: array[0..MAX_PATH] of Char;
  PerDir: string;
begin
  if SHGetMalloc(Allocator) = NOERROR then
  begin
    SHGetSpecialFolderLocation(Form1.Handle, CSIDL_HISTORY, SpecialDir);
    SHGetPathFromIDList(SpecialDir, @FBuf[0]);
    Allocator.Free(SpecialDir);
    ShowMessage(string(FBuf));
  end;
end;

// With Windows Me/2000, the SHGetSpecialFolderLocation function
// is superseded by ShGetFolderLocation.


// function to get the desktop folder location:

function GetDeskTopPath : string;
var
  shellMalloc: IMalloc;
  ppidl: PItemIdList;
  PerDir: string;
begin
  ppidl := nil;
  try
    if SHGetMalloc(shellMalloc) = NOERROR then
    begin
      SHGetSpecialFolderLocation(Form1.Handle, CSIDL_DESKTOP, ppidl);
      SetLength(Result, MAX_PATH);
      if not SHGetPathFromIDList(ppidl, PChar(Result)) then
        raise exception.create('SHGetPathFromIDList failed : invalid pidl');
      SetLength(Result, lStrLen(PChar(Result)));
    end;
  finally
   if ppidl <> nil then
         shellMalloc.free(ppidl);
  end;
end;

{
  Constants:

  CSIDL_DESKTOP
  CSIDL_INTERNET
  CSIDL_PROGRAMS
  CSIDL_CONTROLS
  CSIDL_PRINTERS
  CSIDL_PERSONAL
  CSIDL_FAVORITES
  CSIDL_STARTUP
  CSIDL_RECENT
  CSIDL_SENDTO
  CSIDL_BITBUCKET
  CSIDL_STARTMENU
  CSIDL_DESKTOPDIRECTORY
  CSIDL_DRIVES
  CSIDL_NETWORK
  CSIDL_NETHOOD
  CSIDL_FONTS
  CSIDL_TEMPLATES
  CSIDL_COMMON_STARTMENU
  CSIDL_COMMON_PROGRAMS
  CSIDL_COMMON_STARTUP
  CSIDL_COMMON_DESKTOPDIRECTORY
  CSIDL_APPDATA
  CSIDL_PRINTHOOD
  CSIDL_ALTSTARTUP
  CSIDL_COMMON_ALTSTARTUP
  CSIDL_COMMON_FAVORITES
  CSIDL_INTERNET_CACHE
  CSIDL_COOKIES
  CSIDL_HISTORY
}


{***********************************************************}
{ Read paths from registry. by LENIN INC }

uses
  Windows, SysUtils, Registry;

type
  TSystemPath = (Desktop, StartMenu,
    Programs, Startup, Personal, AppData,
    Fonts, SendTo, Recent, Favorites, Cache,
    Cookies, History, NetHood, PrintHood,
    Templates, LocADat, WindRoot, WindSys,
    TempPath, RootDir, ProgFiles, ComFiles,
    ConfigPath, DevicePath, MediaPath, WallPaper);

function GetSystemPath(SystemPath: TSystemPath): string;
var
  ph: PChar;
begin
  with TRegistry.Create do
    try
      RootKey := HKEY_CURRENT_USER;
      OpenKey('\Software\Microsoft\Windows\CurrentVersion\' +
        'Explorer\Shell Folders', True);
      case SystemPath of
        Desktop: Result   := ReadString('Desktop');
        StartMenu: Result := ReadString('Start Menu');
        Programs: Result  := ReadString('Programs');
        Startup: Result   := ReadString('Startup');
        Personal: Result  := ReadString('Personal');
        AppData: Result   := ReadString('AppData');
        Fonts: Result     := ReadString('Fonts');
        SendTo: Result    := ReadString('SendTo');
        Recent: Result    := ReadString('Recent');
        Favorites: Result := ReadString('Favorites');
        Cache: Result     := ReadString('Cache');
        Cookies: Result   := ReadString('Cookies');
        History: Result   := ReadString('History');
        NetHood: Result   := ReadString('NetHood');
        PrintHood: Result := ReadString('PrintHood');
        Templates: Result := ReadString('Templates');
        LocADat: Result   := ReadString('Local AppData');
        WindRoot: 
          begin
            GetMem(ph, 255);
            GetWindowsDirectory(ph, 254);
            Result := Strpas(ph);
            Freemem(ph);
          end;
        WindSys: 
          begin
            GetMem(ph, 255);
            GetSystemDirectory(ph, 254);
            Result := Strpas(ph);
            Freemem(ph);
          end;
        TempPath: 
          begin
            GetMem(ph, 255);
            GetTempPath(254, ph);
            Result := Strpas(ph);
            Freemem(ph);
          end;
        RootDir: 
          begin
            GetMem(ph, 255);
            GetSystemDirectory(ph, 254);
            Result := (Copy(Strpas(ph), 1, 2));
            Freemem(ph);
          end;
      end;
      RootKey := HKEY_LOCAL_MACHINE;
      OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion', True);
      case SystemPath of
        ProgFiles: Result := ReadString('ProgramFilesDir');
        ComFiles: Result := ReadString('CommonFilesDir');
        ConfigPath: Result := ReadString('ConfigPath');
        DevicePath: Result := ReadString('DevicePath');
        MediaPath: Result := ReadString('MediaPath');
        WallPaper: Result := ReadString('WallPaperDir');
      end;
    finally
      CloseKey;
      Free;
    end;
  if (Result <> '') and (Result[Length(Result)] <> '\') then
    Result := Result + '\';
end;

//Use
procedure TForm1.Button1Click(Sender: TObject);
begin
  label1.Caption := GetSystemPath(DevicePath);
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosya yada klasör fiziksel olarak varmı

Mesaj gönderen ikutluay »

Kod: Tümünü seç

uses
 FileCtrl;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if FileExists('c:\boot.ini') then
    ShowMessage('File exists! Datei existiert!');

  if FileExists('c:\windows') then
    ShowMessage('Directory Exists!');
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosyayı açmak mümkünmü bakan kod

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function IsFileInUse(const fName: TFileName): Boolean;
var
  HFileRes: HFILE;
begin
  Result := False;
  HFileRes := CreateFile(PChar(fName),
                         GENERIC_READ or GENERIC_WRITE,
                         0,
                         nil,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);
  Result := (HFileRes = INVALID_HANDLE_VALUE);
  if not Result then
    CloseHandle(HFileRes);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    if IsFileInUse(Opendialog1.FileName) then
      ShowMessage('File is in use!');
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

runtime da component taşıma boyutlandırma

Mesaj gönderen ikutluay »

Kod: Tümünü seç

{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Dieser Tip enthält eine Klasse mit der man zur Laufzeit Komponenten
 in der Größe verändern bzw verschieben kann so wie man es aus der
 Entwicklungsumgebung her gewohnt ist.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 This Tip provides a tool class that implements the functionality of
 moving or resizing any component at runtime (as in the IDE)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}

//Als eigne Unit
unit Egal;

interface

uses Controls, ExtCtrls, QGraphics, Classes, SysUtils, StdCtrls;

type
  Markierungen = class
    constructor Create(Komponente: TControl);
    destructor Destroy();
  private
    panels: array[0..7] of TPanel;
    LblPos: TPanel;
    Komp: TControl;
    FDownX, FDownY: Integer;
    FDragging: Boolean;
    OrgMDown, OrgMUp: TMouseEvent;
    OrgMMove: TMouseMoveEvent;
    OrgMClick: TNotifyEvent;
    procedure panelsMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure panelsMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure panelsMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure NewPos();
  end;

implementation

type
  TMoveCracker = class(TControl);

constructor Markierungen.Create(Komponente: TControl);
var 
  i: Byte;
begin
  Komp := Komponente;
  for i := 0 to 7 do 
  begin //Die acht Markierungspunkte erstellen.
    panels[i]           := TPanel.Create(Komponente);
    panels[i].Parent    := Komponente.Parent;
    panels[i].Width     := 5;
    panels[i].Height    := 5;
    panels[i].Color     := clBlack;
    panels[i].BevelOuter := bvNone;
    panels[i].OnMouseDown := panelsMouseDown;
    panels[i].OnMouseMove := panelsMouseMove;
    panels[i].OnMouseUp := panelsMouseUp;
    panels[i].Tag       := i;
  end;
  NewPos(); //Die Markierungen an die richtige Position bringen
  OrgMDown  := TPanel(Komp).OnMouseDown; //Sicheren der orginalen Mousereignisse
  OrgMUp    := TPanel(Komp).OnMouseUp;
  OrgMMove  := TPanel(Komp).OnMouseMove;
  OrgMClick := TPanel(Komp).OnClick;
  TPanel(Komp).OnClick := nil;    //für funktionen benötige Ereignisse zuweisen
  TPanel(Komp).OnMouseDown := panelsMouseDown;
  TPanel(Komp).OnMouseUp := panelsMouseUp;
  TPanel(Komp).OnMouseMove := panelsMouseMove;
  LblPos    := TPanel.Create(Komp); //gibt beim Verschieben größe bzw Position an
  with LblPos do 
  begin
    Parent     := Komp.Parent;
    Visible    := False;
    BevelOuter := bvNone;
    Color      := clYellow;
    Height     := 16;
    Width      := 50;
  end;
end;

procedure Markierungen.NewPos();
begin
  panels[0].Left := Komp.Left - 2;
  panels[0].Top  := Komp.Top - 2;
  panels[1].Left := Komp.Left + Komp.Width div 2;
  panels[1].Top  := Komp.Top - 2;
  panels[2].Left := Komp.Left + Komp.Width - 2;
  panels[2].Top  := Komp.Top - 2;
  panels[3].Left := Komp.Left + Komp.Width - 2;
  panels[3].Top  := Komp.Top + Komp.Height - 2;
  panels[4].Left := Komp.Left + Komp.Width div 2;
  panels[4].Top  := Komp.Top + Komp.Height - 2;
  panels[5].Left := Komp.Left - 2;
  panels[5].Top  := Komp.Top + Komp.Height - 2;
  panels[6].Left := Komp.Left - 2;
  panels[6].Top  := Komp.Top + Komp.Height div 2 - 1;
  panels[7].Left := Komp.Left + Komp.Width - 2;
  panels[7].Top  := Komp.Top + Komp.Height div 2 - 1;
end;

destructor Markierungen.Destroy();
var 
  i: Byte;
begin
  TPanel(Komp).OnMouseDown := OrgMDown; //Rückgabe der Orginalen Eregnissprozeduren
  TPanel(Komp).OnMouseUp   := OrgMUp;
  TPanel(Komp).OnMouseMove := OrgMMove;
  TPanel(Komp).OnClick     := OrgMClick;
  for i := 0 to 7 do panels[i].Free;
  LblPos.Free;
end;

procedure Markierungen.panelsMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer); //Funktion aus Swissdelphicenter entnommen
begin                     //Tip: "Komponenten während der Laufzeit verschieben?"
  FDownX         := X;
  FDownY         := Y;
  FDragging      := True;
  TMoveCracker(Sender).MouseCapture := True;
  LblPos.Visible := True;
end;

procedure Markierungen.panelsMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FDragging then
    with Sender as TControl do 
    begin
      if Sender = Komp then 
      begin
        Left := X - FDownX + Left; //Es wurde direkt auf die Komponente geklickt
        Top  := Y - FDownY + Top;
        LblPos.Caption := '[' + IntToStr(Left) + ',' + IntToStr(Top) + ']';
      end 
      else 
      begin
        case TPanel(Sender).Tag of
          0: 
            begin //oben links
              Komp.Top    := Y - FDownY + TPanel(Sender).Top + 2;
              Komp.Height := Komp.Height - (Y - FDownY);
              Komp.Left   := X - FDownX + TPanel(Sender).Left + 2;
              Komp.Width  := Komp.Width - (X - FDownX);
            end;
          1: 
            begin //oben mitte
              Komp.Top    := Y - FDownY + TPanel(Sender).Top + 2;
              Komp.Height := Komp.Height - (Y - FDownY);
            end;
          2: 
            begin //oben rechts
              Komp.Width  := X - FDownX + Komp.Width - 2;
              Komp.Top    := Y - FDownY + TPanel(Sender).Top + 2;
              Komp.Height := Komp.Height - (Y - FDownY);
            end;
          3: 
            begin //unten rechts
              Komp.Width  := X - FDownX + Komp.Width - 2;
              Komp.Height := Y - FDownY + Komp.Height - 2;
            end;
          4: Komp.Height := Y - FDownY + Komp.Height - 2; //unten mitte
          5: 
            begin //unten links
              Komp.Left   := X - FDownX + TPanel(Sender).Left + 2;
              Komp.Width  := Komp.Width - (X - FDownX);
              Komp.Height := Y - FDownY + Komp.Height - 2;
            end;
          6: 
            begin //nach links
              Komp.Left  := X - FDownX + TPanel(Sender).Left + 2;
              Komp.Width := Komp.Width - (X - FDownX);
            end;
          7: Komp.Width := X - FDownX + Komp.Width - 2; //nach rechts
        end;
        LblPos.Caption := '[' + IntToStr(Komp.Width) + ',' + IntToStr(Komp.Height) + ']';
      end;
      newPos(); //zum Nachführen der Markierungspanel
      LblPos.Left := TControl(Sender).Left + X;
      LblPos.Top  := TControl(Sender).Top + Y + 20;
      LblPos.BringToFront;
      LblPos.Refresh;
    end;
end;

procedure Markierungen.panelsMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer); //Funktion aus Swissdelphicenter entnommen
begin                     //Tip: "Komponenten während der Laufzeit verschieben?"
  if FDragging then
  begin
    FDragging      := False;
    TMoveCracker(Sender).MouseCapture := False;
    LblPos.Visible := False;
  end;
end;

end.

//In eigenes Programm muss nur noch:

uses Egal;

var 
  Veraendern: Markierungen;

  //In diesem Beispiel über ein Onclickereigniss welches jedes auf dem Form befindliche
  //Komponente und das Form selbst bekommt. (Auf Komponente Klicken löst Möglichkeit
  //zum größe ändern und verschieben aus und ein Klick wo anders beendet sie wieder.

procedure TForm1.FormClick(Sender: TObject);
  begin  if Assigned(Veraendern) then 
begin
  Veraendern.Destroy;
  Veraendern := nil;
end 
else 
Veraendern := Markierungen.Create(TControl(Sender));

end;

//Will man eine PaintBox benutzen muss man diese noch sichtbar machen.
//Z.B. so:

procedure TForm1.FormShow(Sender: TObject);
  begin  PaintBox1Paint(Sender);
  end;

  procedure TForm1.PaintBox1Paint(Sender: TObject);
    begin  with PaintBox1 do 
  begin
    Canvas.Pen.Style := psDash;
    Canvas.Rectangle(0, 0, Width, Height);
  end;
  end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosyayı rekürsif (alt klasörlerle birlikte) arama

Mesaj gönderen ikutluay »

Kod: Tümünü seç

procedure GetAllFiles(mask: string);
var
  search: TSearchRec;
  directory: string;
begin
  directory := ExtractFilePath(mask);

  // find all files
  if FindFirst(mask, $23, search) = 0 then
  begin
    repeat
      // add the files to the listbox
      Form1.ListBox1.Items.Add(directory + search.Name);
      Inc(Count);
    until FindNext(search) <> 0;
  end;

  // Subdirectories/ Unterverzeichnisse
  if FindFirst(directory + '*.*', faDirectory, search) = 0 then
  begin
    repeat
      if ((search.Attr and faDirectory) = faDirectory) and (search.Name[1] <> '.') then
        GetAllFiles(directory + search.Name + '\' + ExtractFileName(mask));
    until FindNext(search) <> 0;
    FindClose(search);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  directory: string;
  mask: string;
begin
  Count := 0;
  Listbox1.Items.Clear;

  directory := 'C:\temp\';
  mask := '*.*';

  Screen.Cursor := crHourGlass;
  try
    GetAllFiles(directory + mask);
  finally
    Screen.Cursor := crDefault;
  end;
  ShowMessage(IntToStr(Count) + ' Files found');
end;


{**************************************}
{ Code from P. Below: }

// recursively scanning all drives

  { excerpt from form declaration, form has a listbox1 for the
    results, a label1 for progress, a button2 to start the scan,
    an edit1 to get the search mask from, a button3 to stop
    the scan. }
  private
    { Private declarations }
    FScanAborted: Boolean;

  public
    { Public declarations }
    
function ScanDrive(root, filemask: string; hitlist: TStrings): Boolean;

implementation

function TForm1.ScanDrive(root, filemask: string; hitlist: TStrings): Boolean;
  function ScanDirectory(var path: string): Boolean;
  var
    SRec: TSearchRec;
    pathlen: Integer;
    res: Integer;
  begin
    label1.Caption := path;
    pathlen := Length(path);
    { first pass, files }
    res := FindFirst(path + filemask, faAnyfile, SRec);
    if res = 0 then
      try
        while res = 0 do 
        begin
          hitlist.Add(path + SRec.Name);
          res := FindNext(SRec);
        end;
      finally
        FindClose(SRec)
      end;
    Application.ProcessMessages;
    Result := not (FScanAborted or Application.Terminated);
    if not Result then Exit;

    {second pass, directories}
    res := FindFirst(path + '*.*', faDirectory, SRec);
    if res = 0 then
      try
        while (res = 0) and Result do 
        begin
          if ((Srec.Attr and faDirectory) = faDirectory) and
            (Srec.Name <> '.') and
            (Srec.Name <> '..') then 
          begin
            path := path + SRec.Name + '\';
            Result := ScanDirectory(path);
            SetLength(path, pathlen);
          end;
          res := FindNext(SRec);
        end;
      finally
        FindClose(SRec)
      end;
  end;
begin
  FScanAborted := False;
  Screen.Cursor := crHourglass;
  try
    Result := ScanDirectory(root);
  finally
    Screen.Cursor := crDefault
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ch: Char;
  root: string;
begin
  root := 'C:\';
  for ch := 'A' to 'Z' do 
  begin
    root[1] := ch;
    case GetDriveType(PChar(root)) of
      DRIVE_FIXED, DRIVE_REMOTE:
        if not ScanDrive(root, edit1.Text, listbox1.Items) then
          Break;
    end;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin // aborts scan
  FScanAborted := True;
end;

 
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

klasörün toplam boyutu (içindekilerin)

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function GetDirSize(dir: string; subdir: Boolean): Longint;
var
  rec: TSearchRec;
  found: Integer;
begin
  Result := 0;
  if dir[Length(dir)] <> '\' then dir := dir + '\';
  found := FindFirst(dir + '*.*', faAnyFile, rec);
  while found = 0 do
  begin
    Inc(Result, rec.Size);
    if (rec.Attr and faDirectory > 0) and (rec.Name[1] <> '.') and (subdir = True) then
      Inc(Result, GetDirSize(dir + rec.Name, True));
    found := FindNext(rec);
  end;
  FindClose(rec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  label1.Caption := FloatToStr(GetDirSize('e:\download', False) / Sqr(1024)) + ' MBytes';
  label2.Caption := FloatToStr(GetDirSize('e:\download', True) / Sqr(1024)) + ' MBytes';
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

aktiv klasörü bul veya değiştir

Mesaj gönderen ikutluay »

Kod: Tümünü seç

procedure TForm1.Button1Click(Sender: TObject);
begin
  label1.Caption := GetCurrentDir;
end;


// The SetCurrentDir function sets the current directory:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetCurrentDir('c:\windows');
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

osya tarih saatini alma

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function GetFileDateTime(const FileName: TFileName): TDateTime;
var
  FStruct: TOFSTRUCT;
  wndFile: Integer;
begin
  wndFile := OpenFile(PChar(FileName), FStruct, OF_SHARE_DENY_NONE);
  Result  := FileDateToDateTime(FileGetDate(wndFile));
  CloseHandle(wndFile);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Opendialog1.Execute then
    label1.Caption := DateTimeToStr(GetFileDateTime(Opendialog1.FileName));
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
Cevapla