可不可以改变正在运行程序的窗口标题(100分)

  • 主题发起人 主题发起人 nfy
  • 开始时间 开始时间
N

nfy

Unregistered / Unconfirmed
GUEST, unregistred user!
同一程序不能同时运行两个,能不能通过改变正在运行程序的窗口标题,来再次运行这个程序。请给出示例代码。谢谢!
 
procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; ZAppName: array[0..127] of char;<br>&nbsp; Hold: String;<br>&nbsp; Found: HWND;<br>begin<br>&nbsp; Hold := Application.Title;<br>&nbsp; Application.Title := 'OnlyOne'<br>&nbsp; &nbsp; &nbsp;+ IntToStr(HInstance); // 暂时修改窗口标题<br>&nbsp; StrPCopy(ZAppName, Hold); // 原窗口标题<br>&nbsp; Found := FindWindow(nil, ZAppName); // 查找窗口<br>&nbsp; Application.Title := Hold; // 恢复窗口标题<br>&nbsp; if Found&lt;&gt;0 then begin<br>&nbsp; &nbsp; // 若找到则激活已运行的程序并结束自身<br><br>&nbsp; &nbsp; ShowWindow(Found, SW_RESTORE);<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; end;<br>end;<br>
 
你的意思是不是你手上有一个软件,它不允许你运行两次,而你想让他运行两次?<br>这要看对方是如果设计的,如果它是通过检测窗口标题来实现的,你当然可以通过修改窗口<br>标题来让它运行两次,如果它是通过互斥来实现,那我不知道该怎么做。<br><br>修改它的窗口标题,可以使用FindWindow(ClassName,WindowName)来查找这个窗口,<br>对于参数,ClassName给空即可,WindowName即为此窗口的标题,然后通过SetWindowText来修改<br>它的标题。
 
同意hawkx的,如果只是修改标题可能不能达到你的要求,<br>除非它是通过检测窗口标题来实现不允许运行两次的。
 
这样控制程序两次运行的方法比较低级,不同的程序就不能用相同标题了。
 
hawkx理解的对,我通过试验改变窗口标题,确实不能再次运行这个程序,可能他是通过互斥来实现,真是这样,那又该怎么实现让这程序再运行呢?
 
program ContentPRJ;<br><br>uses<br>&nbsp; Dialogs,<br>&nbsp; Windows,<br>&nbsp; Forms,<br>&nbsp; UnitContent in 'UnitContent.pas' {FormContent},<br>&nbsp; UnitConst in 'UnitConst.pas',<br>&nbsp; ConnectFunc in 'ConnectFunc.pas',<br>&nbsp; UnitCBMSConst in 'UnitCBMSConst.pas',<br>&nbsp; UnitDataModule in 'UnitDataModule.pas' {DataModule1: TDataModule},<br>&nbsp; UnitAddContent in 'UnitAddContent.pas' {FormAddContent},<br>&nbsp; UnitUpload in 'UnitUpload.pas' {FormUpload},<br>&nbsp; UnitLPARTITION in 'UnitLPARTITION.pas' {FormLPARTITION},<br>&nbsp; UnitMediaPlayer in 'UnitMediaPlayer.pas' {FormMediaPlayer},<br>&nbsp; UnitDATTOMPG in 'UnitDATTOMPG.pas' {FormDATTOMPG},<br>&nbsp; UnitBase in 'UnitBase.pas' {BaseForm},<br>&nbsp; UnitModifyContent in 'UnitModifyContent.pas' {FormModifyContent},<br>&nbsp; UnitBatchUpload in 'UnitBatchUpload.pas' {FormBatchUpload},<br>&nbsp; UnitDownload in 'UnitDownload.pas' {FormDownload},<br>&nbsp; UnitChangeName in 'UnitChangeName.pas' {FormChangeName},<br>&nbsp; CBNLoginAttest in 'CBNLoginAttest.pas',<br>&nbsp; UnitDataExchange in 'UnitDataExchange.pas' {FormDataExchange},<br>&nbsp; UnitLogin1 in 'UnitLogin1.pas' {FormLogin1},<br>&nbsp; XPBarMenu in 'XPBarMenu.pas',<br>&nbsp; XPMenu in 'XPMenu.pas',<br>&nbsp; UnitLogin in 'UnitLogin.pas' {FormLogin};<br><br>{$R *.res}<br><br>var<br>&nbsp; hMutex : HWND; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; Ret : Integer;<br><br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; Application.Title := 'CSMS3.0 Program Manage Module' ;<br>&nbsp; hMutex:=CreateMutex(nil,False,'CSMS3.0 Program Manage Module');<br>&nbsp; Ret:=GetLastError;<br>&nbsp; If Ret&lt;&gt;ERROR_ALREADY_EXISTS Then<br>&nbsp; Begin<br>&nbsp; Application.CreateForm(TFormContent, FormContent);<br>&nbsp; Application.CreateForm(TDataModule1, DataModule1);<br>&nbsp; Application.CreateForm(TFormAddContent, FormAddContent);<br>&nbsp; Application.CreateForm(TFormUpload, FormUpload);<br>&nbsp; Application.CreateForm(TFormLPARTITION, FormLPARTITION);<br>&nbsp; Application.CreateForm(TFormMediaPlayer, FormMediaPlayer);<br>&nbsp; Application.CreateForm(TFormDATTOMPG, FormDATTOMPG);<br>&nbsp; Application.CreateForm(TBaseForm, BaseForm);<br>&nbsp; Application.CreateForm(TFormModifyContent, FormModifyContent);<br>&nbsp; Application.CreateForm(TFormBatchUpload, FormBatchUpload);<br>&nbsp; Application.CreateForm(TFormDownload, FormDownload);<br>&nbsp; Application.CreateForm(TFormChangeName, FormChangeName);<br>&nbsp; Application.CreateForm(TFormDataExchange, FormDataExchange);<br>&nbsp; Application.CreateForm(TFormLogin1, FormLogin1);<br>&nbsp; Application.CreateForm(TFormLogin, FormLogin);<br>&nbsp; Application.Run;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; MessageBox(application.handle,'程序已经启动,无需再次运行','提示',MB_ICONASTERISK or MB_OK);<br>&nbsp; ReleaseMutex(hMutex);<br>end.
 
我以前也想这么做,可惜没做成,当时也是一时兴趣,所以没有再研究了。
 
多人接受答案了。
 
后退
顶部