| 发表于:2007-06-06 19:09:048楼 得分:10 |
if app.previnstance then msgbox "程序已经运行,不能再次装载。 ", vbexclamation + vbokonly, "系统消息 " unload me end ' end if 以上确实可以,不过如果同一个exe文件在两个目录下都有,以上方法就不行了。 更彻底的方法:用api函数遍历当然所有窗体,然后判断是否已经打开: '在模块里添加如下代码 public declare function enumwindows& lib "user32 " (byval lpenumfunc as long, byval lparam as long) public declare function getwindowtext lib "user32 " alias "getwindowtexta " (byval hwnd as long, byval lpstring as string, byval cch as long) as long public function enumrunonce(byval app_hwnd as long, byval lparam as long) as boolean '遍查主窗口,使用程序只运行一次 dim buf as string * 1024 dim length as long dim title as string length = getwindowtext(app_hwnd, buf, len(buf)) title = left$(buf, length) if instr(title, form1.caption) then '判断是否为执行你想要的操作,form1是主窗体,标题起的尽可能不会重复 if app_hwnd <> form1.hwnd then '程序同时只能打开一次 end end if end if enumrunonce = 1 end function '在主窗体中中添加如下代码: private sub form_load() enumwindows addressof enumrunonce, 0& '限定只能运行一次 end sub | | |
|