Solved

Let the app only run once

Posted on 1998-12-19
8
209 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:Oli2
  • 5
  • 3
8 Comments
 

Accepted Solution

by:
Metty earned 100 total points
ID: 1352012
//the usual stuff above
var hwnd: Word;
 
begin
  if hPrevInst = 0 then
  begin
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end
  else
  begin
    hwnd := FindWindow('TForm1', nil);
    if (not IsWindowVisible(hwnd)) then
    begin
      ShowWindow(hwnd, sw_ShowNormal);
      PostMessage(hwnd, wm_User, 0, 0);
    end;
    else
      SetWindowPos(hwnd, HWND_TOP, 0,0,0,0,
        SWP_NOSIZE or SWP_NOMOVE);
  end;
end.
//====================================================

In the form's PAS file add a message response function for the wm_User
message.

//====================================================
{in the form declaration}
public
  procedure WMUser(var msg: TMessage); message wm_User;

{in the implementation section}
procedure TForm1.WMUser(var msg: TMessage);
begin
  Application.Restore;
end;

0
 
LVL 1

Author Comment

by:Oli2
ID: 1352013
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
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352014
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

0
 

Expert Comment

by:Metty
ID: 1352015
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
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:Oli2
ID: 1352016
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
0
 

Expert Comment

by:Metty
ID: 1352017
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
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352018
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
0
 
LVL 1

Author Comment

by:Oli2
ID: 1352019
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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now