怎样判断程序是首次运行?(50分)

  • 主题发起人 主题发起人 xihahi
  • 开始时间 开始时间
X

xihahi

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,谢谢!
 
给你一个范例,如第二次则不能激活form<br>/////////////<br>program Project1;<br><br>uses<br>&nbsp; Forms, Windows,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1};<br><br>var<br>&nbsp; Mutex: THandle;<br><br>{$R *.RES}<br><br>begin<br>&nbsp; Mutex := CreateMutex(NIL, True, 'Form1');<br>&nbsp; if GetLastError &lt;&gt; ERROR_ALREADY_EXISTS then<br>&nbsp; begin<br>// &nbsp; &nbsp;Application.Initialize;<br>&nbsp; &nbsp; Application.CreateHandle;<br>&nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; Application.Run;<br>&nbsp; end;<br>&nbsp; ReleaseMutex(Mutex);<br>end.<br><br>///<br>unit Unit1; &nbsp; &nbsp;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, ExtCtrls, Menus;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure WMHotKey(var Message:TMessage);message WM_HOTKEY;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.WMHotKey(var Message:TMessage);<br>var<br>&nbsp; AType: TMsgDlgType;<br>&nbsp; AButton: TMsgDlgButtons;<br>&nbsp; WResult: Word;<br>begin<br>&nbsp; AType := mtConfirmation;<br>&nbsp; AButton:= mbYesNoCancel;<br>&nbsp; WResult := MessageDlg('End of RUN?',AType,AButton,0);<br>&nbsp; if WResult= mrYes then<br>&nbsp; begin<br>&nbsp; &nbsp; Form1.Close;<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; // Alt+Control+A<br>&nbsp; if RegisterHotKey(Form1.handle,0,MOD_ALT OR MOD_CONTROL,65) then<br>&nbsp; &nbsp; ShowMessage('OK');<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Form1.Hide;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; if UnRegisterHotKey(Form1.Handle,0) then<br>&nbsp; &nbsp; ShowMessage('IIOK');<br>end;<br><br>end.<br>
 
还可以用全局原子法<br>const iAtom='SingleAPP'<br>begin<br>&nbsp; &nbsp; &nbsp; if &nbsp;GloadFindAtom(iAtom)=0 then<br>&nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GloabalAddAtom(iAtom);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.initialize;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.createFom(FForm1,form1); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.run;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GlobalDeleteAtom(BlobalFindAtom(iAtom));<br>&nbsp; &nbsp; &nbsp; End<br>&nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage('have one');<br>End;<br><br>&nbsp; &nbsp; &nbsp;
 
嘻嘻<br>其实,共有三种方法...
 
还有就是查找窗体法。呵呵
 
xixi,不知道是我问题没写清楚,还是各位没看清楚,我的意思是:<br><br>一个程序安装后,我要在第一次运行它时,要求用户输入注册信息。<br>而不是说用户不能同时运行两遍程序<br>
 
? 还有信号量等方法呢!
 
写一点东西到注册表里去不就得了?<br>下次运行时,一查注册表不就知道了?
 
算了,我还是写ini &nbsp;文件吧,谢谢各位
 
后退
顶部