发信人: fishy()
整理人: fishy(2000-03-12 14:43:25), 站内信件
|
有没有遇到过这种情况?你用VB作了一个MDI的txt编辑器,并且设置了与.txt文 件的关联,程序又写了防止被重复执行。这样,当你的程序已经在运行的时候, 如果用户又双击txt文件,就无法打开了。
听说那些商业软件(比如Media Player)都是用DDE来解决这个问题,反正我到现 在还没有搞懂DDE怎么用,不过下面的方法也可以解决这个问题:
我们假设你的启动设为Sub Main,而且主窗体叫frmMain
你的SubMain可以这样开始:
On Error Resume Next
Dim TempStr As String, Handle As Long
AppPath = App.Path
If Right$(AppPath, 1) <> "\" Then AppPath = AppPath & "\"
If App.PrevInstance Then '重复执行
'读取已经在运行的程序的句柄
Open AppPath & "Handle.dat" For Input As #1
Line Input #1, TempStr
Close #1
Handle = Val(TempStr)
'写下当前程序得到的命令行
Open AppPath & "Command.dat" For Output As #1
Print #1, Command
Close #1
If Handle <> 0 Then
SendMessage Handle, WM_USER + 1, 0, 0
End If
End '退出
End If
'写下frmMain的句柄
Kill AppPath & "Handle.dat"
Open AppPath & "Handle.dat" For Output As #1
Print #1, CStr(frmMain.hWnd)
Close #1
Hook
.... '其它语句
然后在Module中加上以下代码:
Public AppPath As String
Public Const GWL_WNDPROC = (-4)
Public Const WM_USER = &H400
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, By Val wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (By Val hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Lo ng
Public Sub Hook()
lpPrevWndProc = SetWindowLong(frmMain.hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub unHook()
Dim tmp As Long
tmp = SetWindowLong(frmMain.hWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case Is = WM_USER + 1
'接受到重复执行的程序发来的消息
'读取Command.dat文件的内容并处理
End Select
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lPara m)
End Function
在正常退出程序的地方加上以下代码:
unHook
Kill AppPath & "Handle.dat"
-- Dim fishy As Friend
回复时请打勾
------------
欢迎大家访问酷码工作室:http://comma.my163.net
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.103.37.14]
|
|