Link to home
Start Free TrialLog in
Avatar of Oli2
Oli2

asked on

Let the app only run once

how can I check, whether my app is already run or not?
I mean: the user shall not be allowed to run my app more than one time. I have a dizzy idea of how this works, but I'm being lazy, so go ahead, get the points...  :-)

Greetings, Oli
ASKER CERTIFIED SOLUTION
Avatar of Metty
Metty

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Oli2
Oli2

ASKER

Hi, Metty!
Thanx for the answer. I'm checking it. If it works out, you'll get the points. Give me an hour or two.

Greetings, Oli
Avatar of Oli2

ASKER

Hi, Metty!

Sorry, to say, but it don't work yet.

First of all, I get the error while compiling, in this line:
-->  PostMessage(hwnd, wm_User, 0, 0);  <--
Error: wm_User is undefined.

Maybe I should have told you before, that the app is an ShellNotifyIcon - application.

Here's some code:

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMain, frmMain);
  ShowWindow ( Application.Handle, SW_HIDE );
  Application.Run;
end.

and from the .pas file:

public
  IconData : TNotifyIconData;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
     ShowWindow ( Application.Handle, SW_HIDE );
     IconData.cbSize := SizeOf ( IconData );
     IconData.szTip := 'Click here';
     IconData.Wnd := Handle;
     IconData.uID := 234;
     IconData.hIcon := Application.Icon.Handle;
     IconData.uCallbackMessage := WM_USER + 1;
     IconData.uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP;
     Shell_NotifyIcon( NIM_ADD, @IconData );
end;

procedure TfrmMain.WndProc ( var Msg: TMessage );
var       pos: TPoint;
begin
     case Msg.Msg of
     WM_User + 1 :
        case Msg.LParam of
             WM_RBUTTONDOWN:
                begin
                     SetForegroundWindow(Application.Handle);
                     Application.ProcessMessages;
                     GetCursorPos ( pos );
                     mnuMain.Popup ( pos.x, pos.y );
                end;
             WM_LBUTTONDOWN:
                begin
                    //Still to do !
                end;
        end;
     end;
     inherited;
end;

The Form is not shown at the start. Later on, it might become visible, if the user clicks on the popupmenu - "Show Form", but if he doesn't, the Form will alway be hidden.

Greetings, Oli

Sorry! My first code was totally crap (oops)!!! I've found this
somewhere and I planned to use this code too, but I've been totally busy
with the rest of my project. Well today I've implemented the code into my
file and realized that it's totally buggy! Well below is the corrected
and tested version :-)

//Place this in your *.dpr file:
Uses
  WinProcs,
  WinTypes;

Const AppName : 'My Application'; //Place your name here
var PrevInstance : hWnd;
     
begin
  PrevInstance := FindWindow('TApplication', AppName);
  if PrevInstance <> 0 then
    if IsIconic(PrevInstance) then
       ShowWindow(PrevInstance, sw_Restore)
    else
       BringWindowToTop(PrevInstance)
  else
  begin
    Application.Title := AppName;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end.

I think this should work with your ShellnotifyIcon-App too
Avatar of Oli2

ASKER

Hi, Metty !
Well, at least this code doesn't bring up any compiling errors. Still the problem is: I can start my app as many times as I want :-(
Is it possible that my overriding of the WndProc is the problem ?
I'll try it.

Greetings, Oli
Sorry again, but in my project all works fine now.
Perhaps you forgot the line:
"Application.Title := AppName;"

My app doesn't even use "Application.Initialize"!

By the way, what app do you want to program?

Ciao, Metty
Avatar of Oli2

ASKER

Sorry, it was my fault !!!
It works just fine now !
Since you had quiet some work, I'll adjust the points from 50 to 100, is that okay for you?

Thanks a lot !

Greetings, Oli
Avatar of Oli2

ASKER

You were right, I forgot Application.Title ... stupid me !

This app is kind of a trigger - I start other parts of the app from it. ( Some are parts of the same dpr, some are own dpr's ) This is why it's pretty important to have it run only once.

Greetings, Oli