Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#10

AW: Single Instanz mit Extras, hier findet man ein Beispiel

  Alt 23. Nov 2018, 12:22
Das ist nicht mein Code. Der ist von stackoverflow.
Ich verstehe das mit deinem Icon nicht.
Falls Du Dir das kleine Archiv nicht geladen hast, zeige ich Anhand meiner .DPR was ich meine.

Delphi-Quellcode:
program OneApp;

uses
  Windows, Messages, Forms, ShellApi,
  uMain in 'uMain.pas{frmMain};

{$R *.res}

procedure Main;
var
  i: Integer;
  Arg: String;
  Window: HWND;
  CopyDataStruct: TCopyDataStruct;
  NotifyIconData: TNotifyIconData;
Begin
// try find our window handle by custom classname, kinda bulletproof
  Window := FindWindow( SWindowClassName, nil );
// no old instance found = create a new one
  if Window = 0 then
    begin
      Application.Initialize;
      Application.Title := 'OneApp';
// Application.MainFormOnTaskbar := True; // unsupported in Delphi 5
// Application.ShowMainForm := False; // removed, was used while testing
      Application.CreateForm( TfrmMain, frmMain );
      Application.Run;
    end
    else
// old instance found! copy any commandline(s) to it
    begin
// do we need to copy data?
      if ( ParamCount > 0 ) then
        begin
          FillChar( CopyDataStruct, SizeOf( CopyDataStruct ), 0 );
          for i := 1 to ParamCount do
            begin

// ich habe schon verstanden was Du mir sagen wolltest, Du prüfst hier an dieser Stelle nach irgendwas

              Arg := ParamStr( i );
              CopyDataStruct.cbData := ( Length( Arg ) +1 ) * SizeOf( Char );
              CopyDataStruct.lpData := PChar( Arg );
              SendMessage( Window, WM_COPYDATA, 0, LPARAM( @CopyDataStruct ) );
            end;
        end;

// in meinem Beispiel wiederum soll der User den "-visible" switch erst gar nicht kennen
// da wir uns hier in dem "der Prozess existiert" Block befinden
// addiere ich einfach mein "machs Sichtbar" und verarbeite es im Hauptsource

// setup argument to bring window back
      Arg := '-visible';
      CopyDataStruct.cbData := ( Length( Arg ) +1 ) * SizeOf( Char );
      CopyDataStruct.lpData := PChar( Arg );
      SendMessage( Window, WM_COPYDATA, 0, LPARAM( @CopyDataStruct ) );

// Alles was nötig war um ein Fenster aus egal was für einem Status Wiederherzustellen steht hier demarkiert da sich nun der Hauptsource darum kümmert
      
(*

  Switched to above handling method

// Bring old Window back to Foreground in any case

// an dieser Stellte fehlte Deinem Beispiel der ShowWindow() Befehl
// damit wird ein unsichtbares Fenster wieder sichtbar

      ShowWindow( Window, SW_SHOW );
      SetForegroundWindow( Window );

// try find notification icon by Application.Title and remove it
      Window := FindWindow( nil, PChar( Application.Title ) );
      if Window <> 0 then
        begin
          NotifyIconData.cbSize := SizeOf( TNotifyIconData );
          NotifyIconData.Wnd := Window;
          NotifyIconData.uID := 0;
          Shell_NotifyIcon( NIM_DELETE, @NotifyIconData );
        end;
*)

    end;
End;

Begin
  Main;
End.
Hier die Auszüge aus dem Hauptsource:
Delphi-Quellcode:
// ist nicht wirklich schön das ich ne Vcl Aufrufe, kann man auch als seperate methode ablegen und hier aufrufen

// this invoke our commandline copy action
procedure TfrmMain.ProcessArgument( const Arg: String );
begin
// filter out fixed parameters and do specific actions
  if Arg = '-visiblethen
    mnuShowClick( Self )
    else
// for demonstration purposes just throw given argument to Memo1
    Memo1.Lines.Add( Arg );
end;


// hier ist der "magische" Unterschied
// ich sage der Vcl Visible := True anstelle des ShowWindow() damit ich auch ein Taskbar Icon habe

// bring window back from notify status
procedure TfrmMain.mnuShowClick(Sender: TObject);
begin
// show window
// Show;
 Visible := True;
// if at all; remove old icon
 if Shell_NotifyIcon( NIM_DELETE, @NotifyIconData ) then
   Memo1.Lines.Add( 'Shell_NotifyIcon NIM_DELETE: ' + 'Success!' )
   else
   begin
     Memo1.Lines.Add( 'Shell_NotifyIcon NIM_DELETE: ' + 'Failed!' );
     Memo1.Lines.Add( 'Error: ' + SysErrorMessage( GetLastError ) );
   end;
end;
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat