| 发表于:2007-09-02 00:40:2812楼 得分:0 |
我晕倒........... 算了,送你个模块. 以下代码保存为 "modgetrun.bas ": '************************************************************************* '**模 块 名:modgetrun '**说 明:禁止运行多个实例,并激活已存在的实例(使用互斥体) '**创 建 人:马大哈 '**日 期:2006年7月5日 '**描 述:摘于网络 '**版 本:v1.0 '************************************************************************* option explicit private declare function openmutex lib "kernel32 " alias "openmutexa " (byval dwdesiredaccess as long, byval binherithandle as long, byval lpname as string) as long private const standard_rights_required = &hf0000 private const synchronize = &h100000 private const mutant_query_state = &h1 private const mutant_all_access = (standard_rights_required or synchronize or mutant_query_state) private const mutex_all_access = mutant_all_access private const bsf_ignorecurrenttask = &h2 private const bsf_postmessage = &h10 private const bsm_applications = &h8 private type security_attributes nlength as long lpsecuritydescriptor as long binherithandle as long end type private declare function createmutex lib "kernel32 " alias "createmutexa " (lpmutexattributes as security_attributes, byval binitialowner as long, byval lpname as string) as long private declare function registerwindowmessage lib "user32 " alias "registerwindowmessagea " (byval lpstring as string) as long private declare function broadcastsystemmessage lib "user32 " (byval dw as long, pdw as long, byval un as long, byval wparam as long, byval lparam as long) as long private declare function closehandle lib "kernel32 " (byval hobject as long) as long private declare function showwindow lib "user32 " (byval hwnd as long, byval ncmdshow as long) as long private const sw_hide = 0 private const sw_shownormal = 1 private declare function setforegroundwindow lib "user32 " (byval hwnd as long) as long private declare function getforegroundwindow lib "user32 " () as long private declare function callwindowproc lib "user32 " alias "callwindowproca " (byval lpprevwndfunc as long, byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long private declare function setwindowlong lib "user32 " alias "setwindowlonga " (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long private declare function getwindowlong lib "user32 " alias "getwindowlonga " (byval hwnd as long, byval nindex as long) as long private const gwl_wndproc = (-4) private lprevwndproc as long '前一窗体过程 private hmutex as long '互斥事件句柄 private windowmsg as long '自定义消息 private sa as security_attributes '安全属性 private const unique = "hisofty " '自定义消息名 public function newwindowproc(byval hwnd as long, byval umsg as long, _ byval wparam as long, byval lparam as long) as long '新窗体过程 select case umsg case windowmsg '自定义窗体消息处理 msgbox "我已经运行了! ", vbinformation, "呵呵,hi_softy " showwindow hwnd, sw_shownormal if getforegroundwindow() <> hwnd then setforegroundwindow hwnd end if case else newwindowproc = callwindowproc(lprevwndproc, hwnd, umsg, wparam, lparam) end select end function public sub windowhook(byval hwnd as long) '设置窗体钩子 lprevwndproc = setwindowlong(hwnd, gwl_wndproc, addressof newwindowproc) end sub public sub unwindowhook(byval hwnd as long) '卸载窗体钩子 if lprevwndproc <> getwindowlong(hwnd, gwl_wndproc) then setwindowlong hwnd, gwl_wndproc, lprevwndproc closehandle hmutex end if end sub public function initializefunction(byref theform as form) windowmsg = registerwindowmessage(unique) hmutex = openmutex(mutex_all_access, false, unique) if hmutex = 0 then hmutex = createmutex(sa, false, unique) else broadcastsystemmessage bsf_ignorecurrenttask or bsf_postmessage, bsm_applications, windowmsg, 0, 0 set theform = nothing end end if end function *************************************** 使用: 窗体load事件里面,作如下调用: private sub form_load() windowhook me.hwnd end sub 窗体unload事件里面: private sub form_unload(cancel as integer) unwindowhook me.hwnd end sub 新添加一个模块,写一个sub main函数,让工程由此过程启动: sub main() initializefunction form1 '你的主窗口 end sub | | |
|