大致就是这样: Option ExplicitConst HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_NOSIZE = &H1 Const SWP_NOMOVE = &H2 Const SWP_NOACTIVATE = &H10 Const SWP_SHOWWINDOW = &H40 Private Declare Sub 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) Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click() Dim a As New Excel.Application a.Visible = True a.Workbooks.Add Dim mhwnd As Long mhwnd = FindWindow("XLMAIN", vbNullString) SetWindowPos mhwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE End Sub
用SetForegroundWindow代替SetWindowPos ,看看符合你的要求吗修改一下rainstormmaster大哥的代码:Option Explicit Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click() Dim a As New Excel.Application a.Visible = True a.Workbooks.Add Dim mhwnd As Long mhwnd = FindWindow("XLMAIN", vbNullString) SetForegroundWindow mhwnd End Sub
Option ExplicitConst HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub 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)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click()
Dim a As New Excel.Application
a.Visible = True
a.Workbooks.Add
Dim mhwnd As Long
mhwnd = FindWindow("XLMAIN", vbNullString)
SetWindowPos mhwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
如果没有好的方法,我明天就结贴了.
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click()
Dim a As New Excel.Application
a.Visible = True
a.Workbooks.Add
Dim mhwnd As Long
mhwnd = FindWindow("XLMAIN", vbNullString)
SetForegroundWindow mhwnd
End Sub