Private Sub Command1_Click()
dim flag as boolean
Dim b As Variant
if flag=true then
b = ShellExecute(Me.hWnd, vbNullString, "c:\windows\calc.exe", vbNullString, vbNullString, 1)
flag=false
else
.....
end if
End Sub
dim flag as boolean
Dim b As Variant
if flag=true then
b = ShellExecute(Me.hWnd, vbNullString, "c:\windows\calc.exe", vbNullString, vbNullString, 1)
flag=false
else
.....
end if
End Sub
Const INFINITE = &HFFFFFFFFOption ExplicitPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long' 分别声明 Process ID 及 Process Handle 变数
Dim pId As Long, pHnd As Long
Dim Flag As BooleanPrivate Sub Command1_Click()
If Flag = True Then
' TerminateProcess 所传入的是 Process Handle
Call TerminateProcess(pHnd, 0)
Call CloseHandle(pHnd)
Flag = False
Else
Flag = True
End If
' Shell传回Process ID,运行的程序是CALC.EXE
pId = Shell("CALC.EXE", 1)
' 取得 Process Handle
pHnd = OpenProcess(SYNCHRONIZE, 0, pId)
End Sub
如果该计算器未关闭时再按命令按扭,请问如何不再打开计算器,而是使它得到焦点。这个代码的思路和上面的不同而是 如果在就关闭 再打开一个
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 1740
TabIndex = 0
Top = 1350
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFFOption ExplicitPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long' 分别声明 Process ID 及 Process Handle 变数
Dim pId As Long, pHnd As Long
Dim Flag As BooleanPrivate Sub Command1_Click()
If Flag = True Then
' TerminateProcess 所传入的是 Process Handle
Call TerminateProcess(pHnd, 0)
Call CloseHandle(pHnd)
Flag = False
Else
Flag = True
End If
' Shell传回Process ID,运行的程序是CALC.EXE
pId = Shell("CALC.EXE", 1)
' 取得 Process Handle
pHnd = OpenProcess(SYNCHRONIZE, 0, pId)
End Sub'我的QQ为 26603030 如果不可以请交流(说是csdn的朋友)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
Dim b As Long
Dim WinEst As WINDOWPLACEMENT b = FindWindow("SciCalc", vbNullString)
If b = 0 Then
b = ShellExecute(Me.hwnd, vbNullString, "calc.exe", vbNullString, vbNullString, 1)
Else
b = SetForegroundWindow(b)
End If
End Sub 这才是正确的方法!不过如果计算器被最小化,还需要用到SetWindowPlacement函数来还原。
不知你想过没有,如果此时计算器中正好有计算的数据,你这么一关闭,能行么?
On Error GoTo 1
AppActivate "计算机标题栏"
Exit Sub
1: Shell "计算机路径"
End Sub
Private Sub Command1_Click()
On Error GoTo 1
AppActivate "计算器"
'在这里加个吊出窗口
'......
Exit Sub
1: Shell "CALC.EXE", 1
End Sub
呵呵,我只是说说大体思路而已。
=============================================================
'Declarations
Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongConst STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400
'Actual Code
Public ExeState As Boolean
Public hExeProc As LongPublic Sub ExeQuit(ByVal JobToDo As String)
Dim hProcess As Long
Dim RetVal As Long
'The next line launches JobToDo as icon,
'captures process ID
hExeProc =Shell(JobToDo, 1)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False,hExeProc )
ExeState = True
Do
GetExitCodeProcess hProcess, RetVal
DoEvents: Sleep 100
'Loop while the process is active
Loop While RetVal = STILL_ACTIVE
ExeQuit = False
End SubSub cmdCalc_Click()
If ExeState = False Then
Call ExeQuit("Calc.exe")
Else
SetForegroundWindow(hExeProc)
Exit Sub
End If
End Sub
Private Declare Function FindWindow% Lib "user32" Alias "FindWindowA" (ByVal lpclassname As Any, ByVal lpCaption As Any)
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongConst HWND_TOPMOST = -1
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Private Function Float_Calc()
Dim X&, hwnd%
X = Shell("CALC.EXE", 1)
hwnd% = FindWindow%("SciCalc", 0&)
Call SetWindowPos(hwnd%, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End FunctionPrivate Sub Command1_Click()
Float_Calc
End Sub
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow% Lib "user32" Alias "FindWindowA" (ByVal lpclassname As Any, ByVal lpCaption As Any)
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Const HWND_TOPMOST = -1
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Private Const SW_RESTORE = 9Private Sub Command1_Click()
b = FindWindow("SciCalc", vbNullString)
If b = 0 Then
x = Shell("CALC.EXE", 1)
b = FindWindow("SciCalc", vbNullString)
SetWindowPos b, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
If IsIconic(b) <> 0 Then
ShowWindow b, SW_RESTORE
Else
SetWindowPos b, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
End If
End If
End SubVB6+WIN98下通过
(1) 在窗体上任意位置按任意键,计算器应当关闭,再按Command1时,计算器就显示出来。
(2) 或者,在窗体上任意位置按任意键,计算器掩藏起来(不要总是处于TOP),再按Command1时,计算器就显示出来。
谢谢!
在 API VIEW 中 查查 HWND_NOTOPMOST 的值。不要 随便 去改 HWND_TOPMOST 的值, 那是 MS 约定的 常数值 。
: sxyid(吴聿) 如果我写的代码你正确运行过,一定可以达到你的要求! 我很失望!!
uguess(uguess):你好!你是哪一位啊,是不是我们彼此认识?