home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0175_Only One Instance Component For Delphi.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  3.3 KB  |  113 lines

  1. unit OnlyOne;
  2.  
  3. { * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4.  
  5.     OnlyOne, version 1.00, Freeware
  6.      A Delphi 2.0 component
  7.     composed by Gary Nielsen, 3/26/96
  8.      70323.2610@compuserve.com
  9.     
  10.     Drop the OnlyOne component onto a form and only one
  11.     instance of that window will occur.  Any attempt to make
  12.     a second instance will restore the previous window.
  13.     
  14.     caveat artifex:
  15.      Use this component at your own risk.  OnlyOne may not
  16.     work with applications that change their title bar, or
  17.     with applications that have names longer than 20 chars.
  18.     I have only tested this component on a limited number
  19.     of programs, so treat it as 'alpha'-ware.
  20.     
  21.     Acknowledgements:
  22.      To make this into a component, I used Steven L. Keyser's
  23.     JustOne component as a template.  I also derived some code
  24.     from PC Mag's Michael J. Mefford's PicAlbum utility,
  25.     in which hPrevInst is used, but, according to the
  26.     documentation, hPrevInst always equals NULL    with Delphi 2
  27.     and Win95.
  28.     
  29.     Please, if you modify or enhance this code,    drop me a
  30.     note so that I can learn from your work. 
  31.  
  32.   * * * * * * * * * * * * * * * * * * * * * * * * * * * * }
  33.  
  34. interface
  35.  
  36. uses
  37.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  38.   Forms, Dialogs;
  39.  
  40. type
  41.   TOnlyOne = class(TComponent)
  42.     private
  43.     public
  44.       constructor Create(AOwner:TComponent); override;
  45.       destructor Destroy; override;
  46.     published
  47.   end;
  48.  
  49. procedure Register;
  50. procedure LookForPreviousInstance;
  51.  
  52. var
  53.   AtomText: array[0..31] of Char;
  54.   
  55. implementation
  56.  
  57. procedure Register;
  58. begin
  59.   RegisterComponents('Win95', [TOnlyOne]);
  60. end;
  61.  
  62. procedure LookForPreviousInstance;
  63. var
  64.   PreviousInstanceWindow : hWnd;
  65.   AppName : array[0..30] of char;
  66.   FoundAtom : TAtom;
  67. begin
  68.     {put the app name into AtomText}
  69.   StrFmt(AtomText, 'OnlyOne%s', [Copy(Application.Title,1,20)]);
  70.     {check to see if there's a global atom based on the app name }
  71.   FoundAtom := GlobalFindAtom(AtomText);
  72.   if FoundAtom <> 0 then      {another instance exists}
  73.     begin
  74.         {get the app name into a pointer string }
  75.       StrFmt(AppName,'%s', [Application.Title]);
  76.         {change current title so that FindWindow doesn't see it }
  77.       Application.Title := 'destroy me';
  78.         {locate the previous instance of the app }
  79.       PreviousInstanceWindow := FindWindow(nil,AppName);
  80.         {give focus to the previous instance of the app }
  81.       if PreviousInstanceWindow <> 0 then
  82.         if IsIconic(PreviousInstanceWindow) then
  83.           ShowWindow(PreviousInstanceWindow,SW_RESTORE)
  84.         else
  85.           BringWindowToTop(PreviousInstanceWindow);
  86.         {stop the current instance of the application }
  87.       Application.Terminate;
  88.     end;
  89.   { make the global atom so no other instances can occur }
  90.   FoundAtom := GlobalAddAtom(AtomText);
  91. end;      
  92.  
  93. constructor TOnlyOne.Create(AOwner:TComponent);
  94. begin
  95.   inherited Create(AOwner);
  96.   LookForPreviousInstance;
  97. end;
  98.  
  99. destructor TOnlyOne.Destroy;
  100. var
  101.   FoundAtom : TAtom;
  102.   ValueReturned : word;
  103. begin
  104.   { must not forget to remove the global atom, so first
  105.     check to see if there's a global atom already }
  106.   FoundAtom := GlobalFindAtom(AtomText);
  107.   if FoundAtom <> 0 then          {and now remove that atom}
  108.     ValueReturned := GlobalDeleteAtom(FoundAtom);
  109.   inherited Destroy;
  110. end;
  111.  
  112. end.
  113.