Programlama yapalım ve Öğrenelim. - Delphi Eğitim183
  Ana Sayfa
  .NET Eğitim Notları
  Visual C# .NET Örnek Kodları
  VisualBasic.NET Örnek Kodları
  J# Örnekleri
  ASP.NET Örnek Kodları
  Delphi Eğitim
  => Delphi Eğitim1
  => Delphi Eğitim2
  => Delphi Eğitim3
  => Delphi Eğitim4
  => Delphi Eğitim5
  => Delphi Eğitim6
  => Delphi Eğitim7
  => Delphi Eğitim8
  => Delphi Eğitim9
  => Delphi Eğitim10
  => Delphi Eğitim11
  => Delphi Eğitim13
  => Delphi Eğitim14
  => Delphi Eğitim15
  => Delphi Eğitim16
  => Delphi Eğitim17
  => Delphi Eğitim18
  => Delphi Eğitim19
  => Delphi Eğitim20
  => Delphi Eğitim21
  => Delphi Eğitim22
  => Delphi Eğitim23
  => Delphi Eğitim24
  => Delphi Eğitim25
  => Delphi Eğitim26
  => Delphi Eğitim27
  => Delphi Eğitim28
  => Delphi Eğitim29
  => Delphi Eğitim30
  => Delphi Eğtim31
  => Delphi Eğitim32
  => Delphi Eğitim33
  => Delphi Eğitim34
  => Delphi Eğitim35
  => Delphi Eğitim36
  => Delphi Eğitim37
  => Delphi Eğitim38
  => Delphi Eğitim39
  => Delphi Eğitim40
  => Delphi Eğitim41
  => Delphi Eğitim42
  => Delphi Eğitim43
  => Delphi Eğitim44
  => Delphi Eğitim45
  => Delphi Eğitim46
  => Delphi Eğitim47
  => Delphi Eğitim48
  => Delphi Eğitim49
  => Delphi Eğitim50
  => Delphi Eğitim51
  => Delphi Eğitim52
  => Delphi Eğitim53
  => Delphi Eğitim54
  => Delphi Eğitim55
  => Delphi Eğitim56
  => Delphi Eğitim57
  => Delphi Eğitim58
  => Delphi Eğitim59
  => Delphi Eğitim60
  => Delphi Eğitim61
  => Delphi Eğitim62
  => Delphi Eğitim63
  => Delphi Eğitim64
  => Delphi Eğitim65
  => Delphi Eğitim66
  => Delphi Eğitim67
  => Delphi Eğitim68
  => Delphi Eğitim69
  => Delphi Eğitim70
  => Delphi Eğitim71
  => Delphi Eğitim72
  => Delphi Eğitim73
  => Delphi Eğitim74
  => Delphi Eğitim75
  => Delphi Eğitim76
  => Delphi Eğitim77
  => Delphi Eğitim78
  => Delphi Eğitim79
  => Delphi Eğitim80
  => Delphi Eğitim81
  => Delphi Eğitim82
  => Delphi Eğitim83
  => Delphi Eğitim84
  => Delphi Eğitim85
  => Delphi Eğitim86
  => Delphi Eğitim87
  => Delphi Eğitim88
  => Delphi Eğitim89
  => Delphi Eğitim90
  => Delphi Eğitim91
  => Delphi Eğitim92
  => Delphi Eğitim93
  => Delphi Eğitim94
  => Delphi Eğitim95
  => Delphi Eğitim96
  => Delphi Eğitim97
  => Delphi Eğitim98
  => Delphi Eğitim99
  => Delphi Eğitim100
  => Delphi Eğitim101
  => Delphi Eğitim102
  => Delphi Eğitim103
  => Delphi Eğitim104
  => Delphi Eğitim105
  => Delphi Eğitim106
  => Delphi Eğitim107
  => Delphi Eğitim108
  => Delphi Eğitim109
  => Delphi Eğitim110
  => Delphi Eğitim111
  => Delphi Eğitim112
  => Delphi Eğitim113
  => Delphi Eğitim114
  => Delphi Eğitim115
  => Delphi Eğitim116
  => Delphi Eğitim117
  => Delphi Eğitim118
  => Delphi Eğitim119
  => Delphi Eğitim120
  => Delphi Eğitim121
  => Delphi Eğitim122
  => Delphi Eğitim123
  => Delphi Eğitim124
  => Delphi Eğitim125
  => Delphi Eğitim126
  => Delphi Eğitim127
  => Delphi Eğitim128
  => Delphi Eğitim129
  => Delphi Eğitim130
  => Delphi Eğitim131
  => Delphi Eğitim132
  => Delphi Eğitim133
  => Delphi Eğitim134
  => Delphi Eğitim135
  => Delphi Eğitim136
  => Delphi Eğitim137
  => Delphi Eğitim138
  => Delphi Eğitim139
  => Delphi Eğitim140
  => Delphi Eğitim141
  => Delphi Eğitim142
  => Delphi Eğitim143
  => Delphi Eğitim144
  => Delphi Eğitim145
  => Delphi Eğitim146
  => Delphi eğitim147
  => Delphi Eğitim148
  => Delphi Eğitim149
  => Delphi Eğitim150
  => Delphi Eğitim151
  => Delphi Eğitim152
  => Delphi Eğitim153
  => Delphi Eğitim154
  => Delphi Eğitim155
  => Delphi Eğitim156
  => Delphi Eğitim157
  => Delphi Eğitim158
  => Delphi Eğitim159
  => Delphi Eğitim160
  => Delphi Eğitim161
  => Delphi Eğitim162
  => Delphi Eğitim164
  => Delphi Eğitim165
  => Delphi Eğitim166
  => Delphi Eğitim167
  => Delphi Eğitim168
  => Delphi Eğitim169
  => Delphi Eğitim170
  => Delphi Eğitim171
  => Delphi Eğitim172
  => Delphi Eğitim173
  => Delphi Eğitim174
  => Delphi Eğitim175
  => Delphi Eğitim176
  => Delphi Eğitim177
  => Delphi Eğitim178
  => Delphi Eğitim179
  => Delphi Eğitim180
  => Delphi Eğitim181
  => Delphi Eğitim182
  => Delphi Eğitim183
  => Delphi Eğitim184
  => Delphi Eğitim185
  => Delphi Eğitim186
  => Delphi Eğitim187
  => Delphi Eğitim188
  => Delphi Eğitim189
  => Delphi Eğitim190
  => Delphi Eğitim191
  => Delphi Eğitim192
  => Delphi Eğitim193
  => Delphi Eğitim194
  => Delphi Eğitim195
  => Delphi Eğitim196
  => Delphi Eğitim197
  => Delphi Eğitim198
  => Delphi Eğitim199
  => Delphi Eğitim200
  => Delphi Eğitim201
  => Delphi Eğitim202
  => Delphi Eğitim203
  => Delphi Eğitim204
  => Delphi Eğitim205
  => Delphi Eğitim206
  => Delphi Eğitim207
  => Delphi Eğitim208
  => Delphi Eğitim209
  => Delphi Eğitim210
  => Delphi Eğitim211
  => Delphi Eğitim212
  => Delphi Eğitim213
  => Delphi Eğitim214
  => Delphi Eğitim215
  => Delphi Eğitim216
  => Delphi Eğitim217
  => Delphi Eğitim218
  => Delphi Eğitim219
  => Delphi Eğitim220
  => Delphi Eğitim221
  => Delphi Eğitim222
  => Delphi Eğitim223
  => Delphi Eğitim224
  => Delphi Eğitim225
  => Delphi Eğitim226
  => Delphi Eğitim227
  => Delphi Eğitim228
  => Delphi Eğitim229
  => Delphi Eğitim230
  => Delphi Eğitim231
  => Delphi Eğitim232
  => Delphi Eğitim233
  => Delphi Eğitim234
  => Delphi Eğitim235
  => Delphi Eğitim236
  => Delphi Eğitim237
  => Delphi Eğitim238
  => Delphi Eğitim239
  => Delphi Eğitim240
  => Delphi Eğitim241
  => Delphi Eğitim242
  İletişim

neoturk: ...Start a program and wait for its termination [2] ?...

// First create the special unit and put this code into this new unit:

 

uses

  Windows, Registry, Dialogs, Classes, SysUtils;

 

var

  aProcessInfo: PROCESS_INFORMATION;

  aProcessSecAttr: SECURITY_ATTRIBUTES;

  aThreadSecAttr: SECURITY_ATTRIBUTES;

  aStartupInfo: STARTUPINFO;

 

function RunApp(commandLine: String): integer;

var

  n: Boolean;

begin

  with aProcessSecAttr do

  begin

    bInheritHandle := False;

    lpSecurityDescriptor := nil;

    nLength := sizeOf(aProcessSecAttr);

  end;

 

  with aThreadSecAttr do

  begin

    bInheritHandle := False;

    lpSecurityDescriptor := nil;

    nLength := sizeOf(aThreadSecAttr);

  end;

 

  with aStartupInfo do

  begin

    cb := sizeOf(aStartupInfo);

    lpReserved := nil;

    lpDesktop := nil;

    lpTitle := nil;

    dwX := 0;

    dwY := 0;

    dwXSize := 300;

    dwYSize := 300;

    dwXCountChars := 80;

    dwYCountChars := 25;

    dwFillAttribute := 0;

    dwFlags := STARTF_USESTDHANDLES;

    wShowWindow := SW_SHOWDEFAULT;

    cbReserved2 := 0;

    lpReserved2 := nil;

    hStdInput := 0;

    hStdError := 0;

    hStdOutput := 0;

  end;

 

  n := CreateProcess(nil, @commandLine[1], nil, nil,

                     True, CREATE_NO_WINDOW + NORMAL_PRIORITY_CLASS,

                     nil, nil, aStartupInfo, aProcessInfo);

 

  If n Then

    RunApp := aProcessInfo.hProcess

  Else

    RunApp := -1;

end;

 

procedure CloseProcessHandles;

begin

  CloseHandle(aProcessInfo.hThread);

  CloseHandle(aProcessInfo.hProcess);

end;

 

 

// Then you may call the RunApp function:

 

  hProcessHandle := RunApp(sPrintCmd);

  if hProcessHandle > 0 then

  begin

    WaitForSingleObject(hProcessHandle, _YOUR_WAIT_TIME_);

    CloseProcessHandles;;

  end;

 

Delphi - .....................................

 

neoturk: ...Start a program and wait for its termination [2] ?...

// First create the special unit and put this code into this new unit:

 

uses

  Windows, Registry, Dialogs, Classes, SysUtils;

 

var

  aProcessInfo: PROCESS_INFORMATION;

  aProcessSecAttr: SECURITY_ATTRIBUTES;

  aThreadSecAttr: SECURITY_ATTRIBUTES;

  aStartupInfo: STARTUPINFO;

 

function RunApp(commandLine: String): integer;

var

  n: Boolean;

begin

  with aProcessSecAttr do

  begin

    bInheritHandle := False;

    lpSecurityDescriptor := nil;

    nLength := sizeOf(aProcessSecAttr);

  end;

 

  with aThreadSecAttr do

  begin

    bInheritHandle := False;

    lpSecurityDescriptor := nil;

    nLength := sizeOf(aThreadSecAttr);

  end;

 

  with aStartupInfo do

  begin

    cb := sizeOf(aStartupInfo);

    lpReserved := nil;

    lpDesktop := nil;

    lpTitle := nil;

    dwX := 0;

    dwY := 0;

    dwXSize := 300;

    dwYSize := 300;

    dwXCountChars := 80;

    dwYCountChars := 25;

    dwFillAttribute := 0;

    dwFlags := STARTF_USESTDHANDLES;

    wShowWindow := SW_SHOWDEFAULT;

    cbReserved2 := 0;

    lpReserved2 := nil;

    hStdInput := 0;

    hStdError := 0;

    hStdOutput := 0;

  end;

 

  n := CreateProcess(nil, @commandLine[1], nil, nil,

                     True, CREATE_NO_WINDOW + NORMAL_PRIORITY_CLASS,

                     nil, nil, aStartupInfo, aProcessInfo);

 

  If n Then

    RunApp := aProcessInfo.hProcess

  Else

    RunApp := -1;

end;

 

procedure CloseProcessHandles;

begin

  CloseHandle(aProcessInfo.hThread);

  CloseHandle(aProcessInfo.hProcess);

end;

 

 

// Then you may call the RunApp function:

 

  hProcessHandle := RunApp(sPrintCmd);

  if hProcessHandle > 0 then

  begin

    WaitForSingleObject(hProcessHandle, _YOUR_WAIT_TIME_);

    CloseProcessHandles;;

  end;

 

Delphi - .....................................

 

neoturk: ...Access listbox items with api ?...

{

  This code might be useful for nonVCL applications or to read Listbox

  items from another application.

}

 

{

  Die folgenden Funktionen können z.B für nonVCL Projekte verwendet werden

  oder auch zum Auslesen von Listboxen aus anderen Applikationen.

}

 

 

// Retrieve the number of items in a ListBox

// Anzahl Einträge einer ListBox ermitteln

 

function LB_GetItemCount(hListBox: THandle): Integer;

begin

  Result := SendMessage(hListBox, LB_GETCOUNT, 0, 0);

end;

 

// Delete a string in a ListBox

// Einen String in einer ListBox löschen

 

procedure LB_DeleteItem(hListBox: THandle; Index: Integer);

begin

  SendMessage(hListBox, LB_DELETESTRING, Index, 0);

end;

 

// Retrieve the selected item from a ListBox

// Gibt den Text des markiertes Items einer ListBox zurück

 

function LB_GetSelectedItem(hListBox: THandle): string;

var

  Index, len: Integer;

  s: string;

  buffer: PChar;

begin

  Index := SendMessage(hListBox, LB_GETCURSEL, 0, 0);

  len := SendMessage(hListBox, LB_GETTEXTLEN, wParam(Index), 0);

  GetMem(buffer, len + 1);

  SendMessage(hListBox, LB_GETTEXT, wParam(Index), lParam(buffer));

  SetString(s, buffer, len);

  FreeMem(buffer);

  Result := IntToStr(Index) + ' : ' + s;

end;

 

// Example, Beispiel:

 

procedure TForm1.Button1Click(Sender: TObject);

var

  hListBox: THandle;

begin

  hListBox := {...}; // listbox handle

  ListBox1.Items.Text := LB_GetSelectedItem(hListBox);

end;

 

// Retrieve a string from a ListBox

// Gibt den Text eines bestimmten Items einer ListBox zurück

 

function LB_GetListBoxItem(hWnd: Hwnd; LbItem: Integer): string;

var

  l: Integer;

  buffer: PChar;

begin

  l := SendMessage(hWnd, LB_GETTEXTLEN, LbItem, 0);

  GetMem(buffer, l + 1);

  SendMessage(hWnd, LB_GETTEXT, LbItem, Integer(buffer));

  Result := StrPas(buffer);

  FreeMem(buffer);

end;

 

// Example, Beispiel:

 

procedure TForm1.Button2Click(Sender: TObject);

var

  hListBox: THandle;

begin

  hListBox := {...}; // listbox handle

  ListBox1.Items.Text := LB_GetListBoxItem(hListBox, 2);

end;

 

// Gibt den gesamten Text einer ListBox zurück

// Retrieve all listbox items

 

function LB_GetAllItems(hWnd: Hwnd; sl: TStrings): string;

var

  RetBuffer: string;

  i, x, y: Integer;

begin

  x := SendMessage(hWnd, LB_GETCOUNT, 0, 0);

  for i := 0 to x - 1 do

  begin

    y := SendMessage(hWnd, LB_GETTEXTLEN, i, 0);

    SetLength(RetBuffer, y);

    SendMessage(hWnd, LB_GETTEXT, i, lParam(PChar(RetBuffer)));

    sl.Add(RetBuffer);

  end;

end;

 

// Example, Beispiel:

 

procedure TForm1.Button3Click(Sender: TObject);

var

  sl: TStringList;

  ListBox_Handle: THandle;

begin

  hListBox := {...}; // listbox handle

  sl := TStringList.Create;

  try

    LB_GetAllItems(ListBox_Handle, sl);

  finally

    ListBox1.Items.Text := sl.Text;

    sl.Free;

  end;

end;

 

Delphi - .....................................

 

neoturk: ...Access listbox items with api ?...

{

  This code might be useful for nonVCL applications or to read Listbox

  items from another application.

}

 

{

  Die folgenden Funktionen können z.B für nonVCL Projekte verwendet werden

  oder auch zum Auslesen von Listboxen aus anderen Applikationen.

}

 

 

// Retrieve the number of items in a ListBox

// Anzahl Einträge einer ListBox ermitteln

 

function LB_GetItemCount(hListBox: THandle): Integer;

begin

  Result := SendMessage(hListBox, LB_GETCOUNT, 0, 0);

end;

 

// Delete a string in a ListBox

// Einen String in einer ListBox löschen

 

procedure LB_DeleteItem(hListBox: THandle; Index: Integer);

begin

  SendMessage(hListBox, LB_DELETESTRING, Index, 0);

end;

 

// Retrieve the selected item from a ListBox

// Gibt den Text des markiertes Items einer ListBox zurück

 

function LB_GetSelectedItem(hListBox: THandle): string;

var

  Index, len: Integer;

  s: string;

  buffer: PChar;

begin

  Index := SendMessage(hListBox, LB_GETCURSEL, 0, 0);

  len := SendMessage(hListBox, LB_GETTEXTLEN, wParam(Index), 0);

  GetMem(buffer, len + 1);

  SendMessage(hListBox, LB_GETTEXT, wParam(Index), lParam(buffer));

  SetString(s, buffer, len);

  FreeMem(buffer);

  Result := IntToStr(Index) + ' : ' + s;

end;

 

// Example, Beispiel:

 

procedure TForm1.Button1Click(Sender: TObject);

var

  hListBox: THandle;

begin

  hListBox := {...}; // listbox handle

  ListBox1.Items.Text := LB_GetSelectedItem(hListBox);

end;

 

// Retrieve a string from a ListBox

// Gibt den Text eines bestimmten Items einer ListBox zurück

 

function LB_GetListBoxItem(hWnd: Hwnd; LbItem: Integer): string;

var

  l: Integer;

  buffer: PChar;

begin

  l := SendMessage(hWnd, LB_GETTEXTLEN, LbItem, 0);

  GetMem(buffer, l + 1);

  SendMessage(hWnd, LB_GETTEXT, LbItem, Integer(buffer));

  Result := StrPas(buffer);

  FreeMem(buffer);

end;

 

// Example, Beispiel:

 

procedure TForm1.Button2Click(Sender: TObject);

var

  hListBox: THandle;

begin

  hListBox := {...}; // listbox handle

  ListBox1.Items.Text := LB_GetListBoxItem(hListBox, 2);

end;

 

// Gibt den gesamten Text einer ListBox zurück

// Retrieve all listbox items

 

function LB_GetAllItems(hWnd: Hwnd; sl: TStrings): string;

var

  RetBuffer: string;

  i, x, y: Integer;

begin

  x := SendMessage(hWnd, LB_GETCOUNT, 0, 0);

  for i := 0 to x - 1 do

  begin

    y := SendMessage(hWnd, LB_GETTEXTLEN, i, 0);

    SetLength(RetBuffer, y);

    SendMessage(hWnd, LB_GETTEXT, i, lParam(PChar(RetBuffer)));

    sl.Add(RetBuffer);

  end;

end;

 

// Example, Beispiel:

 

procedure TForm1.Button3Click(Sender: TObject);

var

  sl: TStringList;

  ListBox_Handle: THandle;

begin

  hListBox := {...}; // listbox handle

  sl := TStringList.Create;

  try

    LB_GetAllItems(ListBox_Handle, sl);

  finally

    ListBox1.Items.Text := sl.Text;

    sl.Free;

  end;

end;

 

Delphi - .....................................

 

neoturk: ...Removing the todays date display from a tdatetimepicker ?...

uses

  CommCtrl;

 

procedure TForm1.DateTimePicker1DropDown(Sender: TObject);

var

  wnd: HWND;

  Style: Integer;

begin

  wnd := DateTime_GetMonthCal(DateTimePicker1.Handle);

  if wnd <> 0 then

  begin

    Style := GetWindowLong(wnd, GWL_STYLE);

    SetWindowLong(wnd, GWL_STYLE, Style or MCS_NOTODAY or MCS_NOTODAYCIRCLE);

  end;

end;

 

{

  The calendar will still highlite the current day but the circle and the

  today display at the bottom are gone.

}

 

Delphi - .....................................

 

neoturk: ...Removing the todays date display from a tdatetimepicker ?...

uses

  CommCtrl;

 

procedure TForm1.DateTimePicker1DropDown(Sender: TObject);

var

  wnd: HWND;

  Style: Integer;

begin

  wnd := DateTime_GetMonthCal(DateTimePicker1.Handle);

  if wnd <> 0 then

  begin

    Style := GetWindowLong(wnd, GWL_STYLE);

    SetWindowLong(wnd, GWL_STYLE, Style or MCS_NOTODAY or MCS_NOTODAYCIRCLE);

  end;

end;

 

{

  The calendar will still highlite the current day but the circle and the

  today display at the bottom are gone.

}

 

Delphi - .....................................

 

neoturk: ...Check, if a file is on a local drive ?...

function IsOnLocalDrive(aFileName: string): Boolean;

var

  aDrive: string;

begin

  aDrive := ExtractFileDrive(aFileName);

  if (GetDriveType(PChar(aDrive)) = DRIVE_REMOVABLE) or

     (GetDriveType(PChar(aDrive)) = DRIVE_FIXED) then

    Result := True

  else

    Result := False;

end;

 

 

// Example, Beispiel:

procedure TForm1.Button1Click(Sender: TObject);

begin

  if OpenDialog1.Execute then

    if IsOnLocalDrive(OpenDialog1.FileName) then

      ShowMessage(OpenDialog1.FileName + ' is on a local drive.');

end;

 

Delphi - .....................................

 

neoturk: ...Check, if a file is on a local drive ?...

function IsOnLocalDrive(aFileName: string): Boolean;

var

  aDrive: string;

begin

  aDrive := ExtractFileDrive(aFileName);

  if (GetDriveType(PChar(aDrive)) = DRIVE_REMOVABLE) or

     (GetDriveType(PChar(aDrive)) = DRIVE_FIXED) then

    Result := True

  else

    Result := False;

end;

 

 

// Example, Beispiel:

procedure TForm1.Button1Click(Sender: TObject);

begin

  if OpenDialog1.Execute then

    if IsOnLocalDrive(OpenDialog1.FileName) then

      ShowMessage(OpenDialog1.FileName + ' is on a local drive.');

end;

 

Delphi - .....................................

 

neoturk: ...Start a program and wait for its termination ?...

// With CreateProcess:

//*****************************************************

 

{1}

 

function WinExecAndWait32(FileName: string; Visibility: Integer): Longword;

var { by Pat Ritchey }

  zAppName: array[0..512] of Char;

  zCurDir: array[0..255] of Char;

  WorkDir: string;

  StartupInfo: TStartupInfo;

  ProcessInfo: TProcessInformation;

begin

  StrPCopy(zAppName, FileName);

  GetDir(0, WorkDir);

  StrPCopy(zCurDir, WorkDir);

  FillChar(StartupInfo, SizeOf(StartupInfo), #0);

  StartupInfo.cb          := SizeOf(StartupInfo);

  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW;

  StartupInfo.wShowWindow := Visibility;

  if not CreateProcess(nil,

    zAppName, // pointer to command line string

    nil, // pointer to process security attributes

    nil, // pointer to thread security attributes

    False, // handle inheritance flag

    CREATE_NEW_CONSOLE or // creation flags

    NORMAL_PRIORITY_CLASS,

    nil, //pointer to new environment block

    nil, // pointer to current directory name

    StartupInfo, // pointer to STARTUPINFO

    ProcessInfo) // pointer to PROCESS_INF

    then Result := WAIT_FAILED

  else

  begin

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);

    GetExitCodeProcess(ProcessInfo.hProcess, Result);

    CloseHandle(ProcessInfo.hProcess);

    CloseHandle(ProcessInfo.hThread);

  end;

end; { WinExecAndWait32 }

 

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  WinExecAndWait32('notepad.exe', False, True);

end;

 

{*******************************}

 

{2} "Anti-Freezing":

 

function ExecAndWait(const FileName: string; const CmdShow: Integer): Longword;

var { by Pat Ritchey }

  zAppName: array[0..512] of Char;

  zCurDir: array[0..255] of Char;

  WorkDir: string;

  StartupInfo: TStartupInfo;

  ProcessInfo: TProcessInformation;

  AppIsRunning: DWORD;

begin

  StrPCopy(zAppName, FileName);

  GetDir(0, WorkDir);

  StrPCopy(zCurDir, WorkDir);

  FillChar(StartupInfo, SizeOf(StartupInfo), #0);

  StartupInfo.cb          := SizeOf(StartupInfo);

  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW;

  StartupInfo.wShowWindow := CmdShow;

  if not CreateProcess(nil,

    zAppName, // pointer to command line string

    nil, // pointer to process security attributes

    nil, // pointer to thread security attributes

    False, // handle inheritance flag

    CREATE_NEW_CONSOLE or // creation flags

    NORMAL_PRIORITY_CLASS,

    nil, //pointer to new environment block

    nil, // pointer to current directory name

    StartupInfo, // pointer to STARTUPINFO

    ProcessInfo) // pointer to PROCESS_INF

    then Result := WAIT_FAILED

  else

  begin

    while WaitForSingleObject(ProcessInfo.hProcess, 0) = WAIT_TIMEOUT do

    begin

      Application.ProcessMessages;

      Sleep(50);

    end;

    {

    // or:

    repeat

      AppIsRunning := WaitForSingleObject(ProcessInfo.hProcess, 100);

      Application.ProcessMessages;

      Sleep(50);

    until (AppIsRunning <> WAIT_TIMEOUT);

    }

 

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);

    GetExitCodeProcess(ProcessInfo.hProcess, Result);

    CloseHandle(ProcessInfo.hProcess);

    CloseHandle(ProcessInfo.hThread);

  end;

end; { WinExecAndWait32 }

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  ExecAndWait('C:ProgrammeWinZipWINZIP32.EXE', SW_SHOW);

end;

 

 

{3}

 

{--WinExecAndWait32V2 ------------------------------------------------}

{: Executes a program and waits for it to terminate

@Param FileName contains executable + any parameters

@Param Visibility is one of the ShowWindow options, e.g. SW_SHOWNORMAL

@Returns -1 in case of error, otherwise the programs exit code

@Desc In case of error SysErrorMessage( GetlastError ) will return an

  error message. The routine will process paint messages and messages

  send from other threads while it waits.

}{ Created 27.10.2000 by P. Below

-----------------------------------------------------------------------}

 

function WinExecAndWait32V2(FileName: string; Visibility: Integer): DWORD;

  procedure WaitFor(processHandle: THandle);

  var

    Msg: TMsg;

    ret: DWORD;

  begin

    repeat

      ret := MsgWaitForMultipleObjects(1, { 1 handle to wait on }

        processHandle, { the handle }

        False, { wake on any event }

        INFINITE, { wait without timeout }

        QS_PAINT or { wake on paint messages }

        QS_SENDMESSAGE { or messages from other threads }

        );

      if ret = WAIT_FAILED then Exit; { can do little here }

      if ret = (WAIT_OBJECT_0 + 1) then

      begin

          { Woke on a message, process paint messages only. Calling

            PeekMessage gets messages send from other threads processed. }

        while PeekMessage(Msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE) do

          DispatchMessage(Msg);

      end;

    until ret = WAIT_OBJECT_0;

  end; { Waitfor }

var { V1 by Pat Ritchey, V2 by P.Below }

  zAppName: array[0..512] of char;

  StartupInfo: TStartupInfo;

  ProcessInfo: TProcessInformation;

begin { WinExecAndWait32V2 }

  StrPCopy(zAppName, FileName);

  FillChar(StartupInfo, SizeOf(StartupInfo), #0);

  StartupInfo.cb          := SizeOf(StartupInfo);

  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW;

  StartupInfo.wShowWindow := Visibility;

  if not CreateProcess(nil,

    zAppName, { pointer to command line string }

    nil, { pointer to process security attributes }

    nil, { pointer to thread security attributes }

    False, { handle inheritance flag }

    CREATE_NEW_CONSOLE or { creation flags }

    NORMAL_PRIORITY_CLASS,

    nil, { pointer to new environment block }

    nil, { pointer to current directory name }

    StartupInfo, { pointer to STARTUPINFO }

    ProcessInfo) { pointer to PROCESS_INF } then

    Result := DWORD(-1) { failed, GetLastError has error code }

  else

  begin

    Waitfor(ProcessInfo.hProcess);

    GetExitCodeProcess(ProcessInfo.hProcess, Result);

    CloseHandle(ProcessInfo.hProcess);

    CloseHandle(ProcessInfo.hThread);

  end; { Else }

end; { WinExecAndWait32V2 }

 

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  WinExecAndWait32V2('notepad.exe', SW_SHOWNORMAL);

end;

 

 

// With ShellExecuteEx:

//*****************************************************

 

{1}

 

uses

  ShellApi;

 

procedure ShellExecute_AndWait(FileName: string; Params: string);

var

  exInfo: TShellExecuteInfo;

  Ph: DWORD;

begin

  FillChar(exInfo, SizeOf(exInfo), 0);

  with exInfo do

  begin

    cbSize := SizeOf(exInfo);

    fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;

    Wnd := GetActiveWindow();

    ExInfo.lpVerb := 'open';

    ExInfo.lpParameters := PChar(Params);

    lpFile := PChar(FileName);

    nShow := SW_SHOWNORMAL;

  end;

  if ShellExecuteEx(@exInfo) then

    Ph := exInfo.HProcess

  else

  begin

    ShowMessage(SysErrorMessage(GetLastError));

    Exit;

  end;

  while WaitForSingleObject(ExInfo.hProcess, 50) <> WAIT_OBJECT_0 do

    Application.ProcessMessages;

  CloseHandle(Ph);

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  ShellExecute_AndWait('FileName', 'Parameter');

end;

 

 

 

{*******************************}

 

{2}

 

function ShellExecute_AndWait(Operation, FileName, Parameter, Directory: string;

  Show: Word; bWait: Boolean): Longint;

var

  bOK: Boolean;

  Info: TShellExecuteInfo;

{

  ****** Parameters ******

  Operation:

 

  edit  Launches an editor and opens the document for editing.

  explore Explores the folder specified by lpFile.

  find Initiates a search starting from the specified directory.

  open Opens the file, folder specified by the lpFile parameter.

  print Prints the document file specified by lpFile.

  properties Displays the file or folder's properties.

 

  FileName:

 

  Specifies the name of the file or object on which

  ShellExecuteEx will perform the action specified by the lpVerb parameter.

 

  Parameter:

 

  String that contains the application parameters.

  The parameters must be separated by spaces.

 

  Directory:

 

  specifies the name of the working directory.

  If this member is not specified, the current directory is used as the working directory.

 

  Show:

 

  Flags that specify how an application is to be shown when it is opened.

  It can be one of the SW_ values

 

  bWait:

 

  If true, the function waits for the process to terminate

}

begin

  FillChar(Info, SizeOf(Info), Chr(0));

  Info.cbSize := SizeOf(Info);

  Info.fMask := SEE_MASK_NOCLOSEPROCESS;

  Info.lpVerb := PChar(Operation);

  Info.lpFile := PChar(FileName);

  Info.lpParameters := PChar(Parameter);

  Info.lpDirectory := PChar(Directory);

  Info.nShow := Show;

  bOK := Boolean(ShellExecuteEx(@Info));

  if bOK then

  begin

    if bWait then

    begin

      while

        WaitForSingleObject(Info.hProcess, 100) = WAIT_TIMEOUT

        do Application.ProcessMessages;

      bOK := GetExitCodeProcess(Info.hProcess, DWORD(Result));

    end

    else

      Result := 0;

  end;

  if not bOK then Result := -1;

end;

Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol