如何正确找到已经运行的程序的实例的主窗口句柄?(不用FindWindow) ( 积分: 150 )

  • 主题发起人 主题发起人 smithcouple
  • 开始时间 开始时间
S

smithcouple

Unregistered / Unconfirmed
GUEST, unregistred user!
只允许程序运行一个实例并且重新激活已运行的程序的窗口很容易实现。<br><br>但像Word,它在重新激活已运行的窗口之后还能使得以前的窗口打开命令行参数中所指的文件。<br><br>我以前的做法是:每次运行程序,比如打开了某个文件的时候,标题栏发生变化,我将标题栏的字符串记录在ini文件中。<br>等我第二次运行程序实例的时候,由于已经存在一个实例,不新启动程序,发送WM_COPYDATA消息,但SendMessage必须要知道发给谁,也就是已经运行的那个程序的窗口句柄,所以我再从ini文件中获得先前保存的窗口标题。进而通过FindWindow得到已经运行的程序的窗口句柄。这个办法虽然有效,但我认为还得通过ini文件等手动转一道。应该还有更好的方法能够直接获得已经运行的程序窗口句柄。<br><br>请大家帮我看看,我对API并不是很熟,许多消息及命令都没用过,谢谢!
 
findwindow(nil,pchar(formcaption));
 
LS方法没有用,我就是想问怎么不用findwindow的方法
 
为什么不建立一个Map内存?用MapOfFile来操作,然后直接在这个里面存主窗口的Handle,后面启动的直接取就可以了。参看JVCL的SingleInstance控件的源码,非常简单。
 
SingleInstance看了之后还是一头雾水,里面看似很简单,我没照到它里面关于Map的地方<br><br>有没有详细一些的例子呢?
 
在主窗口单元中<br>const<br>&nbsp;&nbsp;MapName&nbsp;&nbsp;=&nbsp;'ExportLY&nbsp;map';<br><br>Type<br>&nbsp;&nbsp;TUserMsg&nbsp;=&nbsp;Record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serverName&nbsp;&nbsp;&nbsp;:&nbsp;string[255];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DatabaseName&nbsp;:&nbsp;string[255];<br>&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;PUserMsg&nbsp;=&nbsp;^TUserMsg;<br><br>&nbsp;&nbsp;hMap:&nbsp;THandle;<br>&nbsp;&nbsp;pInfo:&nbsp;Pointer;<br>&nbsp;&nbsp;MapSize:&nbsp;Integer&nbsp;=&nbsp;SizeOf(TUserMsg);<br><br><br>procedure&nbsp;&nbsp;CreateMap;<br>begin<br>&nbsp;&nbsp;hMap&nbsp;:=&nbsp;CreateFileMapping($FFFFFFFF,&nbsp;nil,&nbsp;PAGE_READWRITE&nbsp;or&nbsp;SEC_COMMIT,<br>&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;MapSize,&nbsp;MapName);<br>end;<br><br>procedure&nbsp;&nbsp;ReleaseMap;<br>begin<br>&nbsp;&nbsp;&nbsp;CloseHandle(hMap);<br>end;<br><br>procedure&nbsp;&nbsp;SaveServerName;<br>&nbsp;var<br>&nbsp;&nbsp;MapInfo&nbsp;:&nbsp;TUserMsg;<br>&nbsp;&nbsp;hCurMap:&nbsp;THandle;<br>begin<br><br>&nbsp;&nbsp;&nbsp;hCurMap&nbsp;&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,&nbsp;True,&nbsp;MapName);<br>&nbsp;&nbsp;&nbsp;pInfo&nbsp;:=&nbsp;MapViewOfFile(hCurMap,&nbsp;FILE_MAP_WRITE,&nbsp;0,&nbsp;0,&nbsp;MapSize);<br>&nbsp;&nbsp;&nbsp;MapInfo.serverName&nbsp;&nbsp;&nbsp;:=&nbsp;ParamStr(1);<br>&nbsp;&nbsp;&nbsp;MapInfo.DatabaseName&nbsp;:=&nbsp;ParamStr(2);<br>&nbsp;&nbsp;&nbsp;Move(MapInfo,&nbsp;pInfo^,&nbsp;MapSize);<br>&nbsp;&nbsp;&nbsp;UnmapViewOfFile(pInfo);<br>&nbsp;&nbsp;&nbsp;CloseHandle(hCurMap);<br><br>end;<br>//&nbsp;从map中获取信息<br>procedure&nbsp;LoadServerName;<br>&nbsp;var<br>&nbsp;&nbsp;&nbsp;MapInfo&nbsp;:&nbsp;TUserMsg;<br>&nbsp;&nbsp;&nbsp;hCurMap&nbsp;:&nbsp;THandle;<br>begin<br><br>&nbsp;&nbsp;//<br>&nbsp;&nbsp;hCurMap&nbsp;&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,&nbsp;True,&nbsp;MapName);<br>&nbsp;&nbsp;pInfo&nbsp;:=&nbsp;MapViewOfFile(hcurMap,&nbsp;FILE_MAP_WRITE,&nbsp;0,&nbsp;0,&nbsp;MapSize);<br><br>&nbsp;&nbsp;MapInfo&nbsp;:=&nbsp;TUserMsg(pInfo^);<br>&nbsp;&nbsp;SystemParam.ServerName&nbsp;&nbsp;&nbsp;:=&nbsp;MapInfo.serverName;<br>&nbsp;&nbsp;SystemParam.DatabaseName&nbsp;:=&nbsp;MapInfo.DataBaseName;<br>&nbsp;&nbsp;UnmapViewOfFile(pInfo);<br>&nbsp;&nbsp;CloseHandle(hCurMap);<br><br>end;<br><br>在工程单元中:<br>var<br>&nbsp;&nbsp;MapInfo:&nbsp;TUserMsg;<br>begin<br><br>&nbsp;&nbsp;Application.Initialize;<br>&nbsp;&nbsp;Application.Title&nbsp;:=&nbsp;'导出';<br>&nbsp;&nbsp;hmutex:=createmutex(nil,&nbsp;false,&nbsp;'LYExport2000');<br>&nbsp;&nbsp;ret:=getlasterror;<br>&nbsp;&nbsp;if&nbsp;ret=error_already_exists&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;releasemutex(hmutex);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMutex);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;ParamCount&nbsp;&gt;=&nbsp;2&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SystemParam.ServerName&nbsp;&nbsp;&nbsp;:=&nbsp;&nbsp;ParamStr(1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SystemParam.DataBaseName&nbsp;:=&nbsp;&nbsp;ParamStr(2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SaveServerName;&nbsp;&nbsp;//&nbsp;写入map<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application.Terminate;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Application.CreateForm(Tfm_export,&nbsp;fm_export);<br>&nbsp;&nbsp;Application.ShowMainForm&nbsp;:=&nbsp;False;<br>&nbsp;&nbsp;Application.Run;<br>end.
 
后退
顶部