读书人

ShellExecute 来下多谢!最先解决的

发布时间: 2012-01-19 00:22:28 作者: rapoo

ShellExecute 高手进来下,谢谢!急,最先解决的可以得大部份分
ShellExecute Me.hwnd, "open ", "calc.exe ", " ", " ", 1

怎么指定打开计算器显示的位置?

若计算器已打开,再次单击又会再打开一个,怎么才可以在已打开情况下,不打开另一个?

[解决办法]
Option Explicit

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function ShellExecute Lib "shell32.dll " Alias "ShellExecuteA " (ByVal hWnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function MoveWindow Lib "user32 " (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32 " (ByVal hWnd As Long, lpRect As RECT) As Long

Private Sub MoveWindowTo(ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long)
Dim Rec As RECT
If hWnd <> 0 Then
GetWindowRect hWnd, Rec
MoveWindow hWnd, X, Y, Rec.Right - Rec.Left, Rec.Bottom - Rec.Top, 1
End If
End Sub

Private Sub tmrAfterLoadCalc_Timer()
Dim WinWnd As Long
WinWnd = FindWindow(vbNullString, "计算器 ")
If WinWnd <> 0 Then
tmrAfterLoadCalc.Enabled = False
MoveWindowTo WinWnd, 200, 200
End If
End Sub

Private Sub Command1_Click()
Dim WinWnd As Long
WinWnd = FindWindow(vbNullString, "计算器 ")
If WinWnd <> 0 Then
MoveWindowTo WinWnd, 200, 200
Else
ShellExecute Me.hWnd, "open ", "calc.exe ", " ", " ", 1
tmrAfterLoadCalc.Interval = 50
tmrAfterLoadCalc.Enabled = True
End If
End Sub


[解决办法]
tmrAfterLoadCalc是个timer控件

思路就是 Command1 按下时
判断是否有标题为“计算器”的程序
有的话用MoveWindowTo 移动“计算器”到指定位置
没有就用shellexcute打开,同时让tmrAfterLoadCalc控件运行
tmrAfterLoadCalc的功能就是把“计算器”移动到指定位置.....

^_^

最近都不想写代码了,好累啊

读书人网 >VB

热点推荐