Thema: Delphi Test-Programm

Einzelnen Beitrag anzeigen

TJDD

Registriert seit: 25. Aug 2005
26 Beiträge
 
#5

Re: Test-Programm

  Alt 8. Sep 2005, 21:07
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, tlhelp32, ShellAPI, ExtCtrls, AppEvnts;

const IC_CLICK = WM_APP + 201;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Timer1: TTimer;
    ApplicationEvents1: TApplicationEvents;
    procedure ApplicationEvents1Minimize(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure Systray(var sMsg: TMessage); message IC_CLICK;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  NIM : TNotifyIconData;

implementation

{$R *.dfm}

var bclose : boolean;

function GetProcessID(Exename: string): DWORD;
var
  hProcSnap: THandle;
  pe32: TProcessEntry32;
begin
  result := 0;
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap <> INVALID_HANDLE_VALUE then
  begin
    pe32.dwSize := SizeOf(ProcessEntry32);
    if Process32First(hProcSnap, pe32) = true then
    begin
      while Process32Next(hProcSnap, pe32) = true do
      begin
        if pos(Exename, pe32.szExeFile) <> 0 then
          result := pe32.th32ProcessID;
      end;
    end;
    CloseHandle(hProcSnap);
  end;
end;

function GetProcessHandleFromID(ID: DWORD): THandle;
begin
  result := OpenProcess(SYNCHRONIZE, False, ID);
  CloseHandle(result);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  bClose := True;
end;

procedure TForm1.Systray(var sMsg: TMessage);
begin
  inherited;
  if (sMsg.LParam = WM_LBUTTONDOWN) then
    begin
      Show;
      Shell_NotifyIcon(NIM_DELETE, @NIM);
      Application.Restore;
    end;
end;

procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
  Form1.FormStyle:=fsStayOnTop;
  Hide;
  with NIM do
    begin
      cbSize := SizeOf (nIM);
      Wnd := Handle;
      uID := 0;
      uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
      uCallbackMessage := IC_CLICK;
      hIcon := Application.Icon.Handle;
      szTip := 'Network';
    end;
    Shell_NotifyIcon(NIM_ADD, @NIM);
end;


procedure TForm1.Timer1Timer(Sender: TObject);
var
  hProcess: THandle;
  wf: DWORD;
begin
  bClose := False;
  while bClose = False do
  begin
    hProcess := GetProcessHandleFromID(GetProcessID('notepad.exe'));
    if hProcess = 0 then
       winexec(pchar('notepad.exe'),sw_shownormal)
    else
      label1.caption := 'Bereits offen';
    Application.HandleMessage;
  end;
end;

end.
Erst mal danke an alle die mir Antwort gaben

Habe bei dem obigen Quellcode noch ein Problem wenn ich das Programm satrte wenn es aus war versucht der Timmer das programm immer wieder neu zu öffnen also hab ich nachher zichtausend fenster offen

kann man das irgendwie ausschalten also wenn das Programm laüft mach nix öffnen??
  Mit Zitat antworten Zitat