求救啊,郁闷那?我想用API使一个窗口在最前台并取得焦点,怎么不行啊?
我新建一个文件名为 "New "的文本文件,在后台或者最小化时,用如下函数到前台怎么没用啊?
Option Explicit
Private Declare Function SwitchToThisWindow Lib "user32.dll " (ByVal hwnd As Long, ByVal bRestore As Boolean)
Private Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetForegroundWindow Lib "user32 " () As Long
Private Declare Function SetFocusAPI& Lib "user32 " Alias "SetFocus " (ByVal hwnd As Long)
Private Declare Function BringWindowToTop Lib "user32 " (ByVal hwnd As Long) As Long
Private Declare Function SetActiveWindow Lib "user32 " (ByVal hwnd 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 Long
Private Const EM_SETPASSWORDCHAR = &HCC
Private Const HWND_TOPMOST& = -1
Private Const SWP_NOSIZE& = &H1
Private Const SWP_NOMOVE& = &H2
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 Sub Command1_Click()
Dim hdl As Long
Dim b
hdl = FindWindow( "Notepad ", "New ") 'vbNullString #32770
'SetWindowPos hdl, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
'b = SwitchToThisWindow(hdl, True)
'SetFocusAPI (hdl)
'SetActiveWindow (hdl)
'AppActivate "New "
' BringWindowToTop (hdl)
SetForegroundWindow (hdl)
End Sub
以上函数我都试过了,为什么窗口就是不到前台呢?
[解决办法]
用showwindow试试
[解决办法]
SetForegroundWindow是可以的
问题是你的FindWindow可能没找到hwnd,debug看看hdl可能是0吧?
你是英文版系统吗,英文版Notepad标题好像也不是New呀?
换成这样试试
hdl = FindWindow( "Notepad ", vbNullString)
注意前提:1、Notepad要已打开;2、Notepad不是最小化状态,否则请用showwindow