如何根据不同的命令行参数作不同的事?要求:此程序只能运行一个实例.(100分)

  • 主题发起人 主题发起人 ChengDi
  • 开始时间 开始时间
C

ChengDi

Unregistered / Unconfirmed
GUEST, unregistred user!
如何根据不同的命令行参数作不同的事?要求:此程序只能运行一个实例.
 
<a href="DispQ.asp?LID=142451">请问如何令所写的程序不能同时打开两个?</a>
 
嘿,问问题之前检索一下多好 ^O^
 
&nbsp; &nbsp;获得程序的命令行参数:<br>&nbsp; &nbsp; &nbsp; For iCount := 0 to ParamCount do<br>&nbsp; &nbsp; &nbsp; &nbsp; { something}<br><br>&nbsp; &nbsp; 程序只运行一个实例,已答问题中有。
 
请明确我的要求<br>1、一个实例和不同参数作不同的事两个要求要同时满足。<br>2、对“不同参数作不同的事”的解释:<br>&nbsp; &nbsp;例如:myprg.exe red &nbsp; 显示红色窗口<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myprg.exe blue &nbsp; 显示兰色窗口<br>
 
use paramstr to get parameter.<br>if you change your parameter while your program is still running,<br>you can use one file to do such things.<br>for example:<br><br>&nbsp;Your file my.dat<br>&nbsp; &nbsp; &nbsp;ColorValue=Blue.<br><br>At this time you can use TStringList to get this value and your program accord to this value to what your want<br>&nbsp;
 
看上面我的回答。
 
你想不想把第二个实例的参数传递给第一个实例?
 
请明确我的要求<br>1、一个实例和不同参数作不同的事两个要求要同时满足。<br>&gt;&gt;这两个东西本来就没有矛盾,可以分别满足嘛。<br><br>2、对“不同参数作不同的事”的解释:<br>&nbsp; &nbsp;例如:myprg.exe red &nbsp; 显示红色窗口<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myprg.exe blue &nbsp; 显示兰色窗口<br>&gt;&gt;这个自己判断一下ParamStr不就可以了?是red就显示红色,是blue就显示蓝色。<br>如果需要参数传递,看看以前的贴子,怎么把第二个实例的参数传递给第一个实例,<br>不久解决了?<br><br><br>
 
那么如果我要将这封装成控件,如何实现?
 
<br>在Form_Show 事件处理的尾部中,加上如下代码:<br><br><br>1。 如果你希望 &nbsp;运行 MyPrg &nbsp;Red 就不能再运行MyPrg Red,但能运行 MyPrg blue.则可以这样:<br>&nbsp; &nbsp; &nbsp; &nbsp; <br><br>&nbsp; &nbsp; &nbsp;hMutex := OpenMutex(nil,true,paramstr[1])); <br>&nbsp; &nbsp; &nbsp;if &nbsp;hMutex&gt;0 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hMutex:=CreateMutex(nil,false,paramstr[1])<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; 在Form_Close 事件中,执行如下代码:<br>&nbsp; &nbsp;CloseHandle(hMutex);<br><br><br><br>&nbsp; 说明:<br>&nbsp; &nbsp; &nbsp; &nbsp;hMutex为全局变量<br><br>2。 如果你希望 &nbsp;运行 MyPrg &nbsp;Red 不能再运行 MyPrg blue.则可以这样:<br>&nbsp; &nbsp; &nbsp;hMutex := OpenMutex(nil,true,'you mutex'); <br>&nbsp; &nbsp; &nbsp;if &nbsp;hMutex&gt;0 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hMutex:=CreateMutex(nil,false,'you mutex')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; 在Form_Close 事件中,执行如下代码:<br>&nbsp; &nbsp;CloseHandle(hMutex);<br><br><br>对控件同样实用。<br><br><br><br>
 
多人接受答案了。
 
后退
顶部