问题:自身窗口是通过外部程序命令行调用出来的, 上面一个按钮点击过后通过SETWINDOWPOS将自身移动到桌面外,并且是非活动状态。但我发现此后, 无论如何都无法将这个窗口做到:“激活+显示+前台(不是固定置顶)+当前操作窗口”下面是我恢复窗口的一系列函数, 怎么组合都只是得到效果:前台+显示+闪标题(好像激活但只是闪标题,并不是当前操作窗口)!!!!
ShowWindow Me.hwnd, SW_RESTORE
SetActiveWindow Me.hwnd
'SetWindowPos Me.hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOSIZE '缺陷无法激活窗口
SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3 '缺陷无法激活窗口
SetForegroundWindow Me.hwnd '单独用无法置到顶层
SetWindowPos Me.hwnd, -2, 0, 0, 0, 0, 3 '缺陷无法激活窗口
'base.SetFocus Me.Text1.hwnd '还是不行Text倒是得到焦点,但还是只闪标题
'SetForegroundWindow jdUscHwnd
'jdUscHwnd
If fx < Screen.Width + 50000 Then
Me.Left = fx: Me.Top = fy
Else
Me.Left = 0: Me.Top = 0
End If
'SetActiveWindow Me.hwnd
'下面4句全都无法将自身设置为 当前操作窗口, 只是闪标题
SendMessage Me.hwnd, WM_ACTIVATE, 0, 0
SendMessage Me.hwnd, WM_SETFOCUS, 0, 0
SendMessage Me.hwnd, WM_ACTIVATE, 0, vbNullString
SendMessage Me.hwnd, WM_SETFOCUS, 0, vbNullString过路的高人来指点指点吧, 如何才能做到那个效果呢? 感谢感谢,
ShowWindow Me.hwnd, SW_RESTORE
SetActiveWindow Me.hwnd
'SetWindowPos Me.hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOSIZE '缺陷无法激活窗口
SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3 '缺陷无法激活窗口
SetForegroundWindow Me.hwnd '单独用无法置到顶层
SetWindowPos Me.hwnd, -2, 0, 0, 0, 0, 3 '缺陷无法激活窗口
'base.SetFocus Me.Text1.hwnd '还是不行Text倒是得到焦点,但还是只闪标题
'SetForegroundWindow jdUscHwnd
'jdUscHwnd
If fx < Screen.Width + 50000 Then
Me.Left = fx: Me.Top = fy
Else
Me.Left = 0: Me.Top = 0
End If
'SetActiveWindow Me.hwnd
'下面4句全都无法将自身设置为 当前操作窗口, 只是闪标题
SendMessage Me.hwnd, WM_ACTIVATE, 0, 0
SendMessage Me.hwnd, WM_SETFOCUS, 0, 0
SendMessage Me.hwnd, WM_ACTIVATE, 0, vbNullString
SendMessage Me.hwnd, WM_SETFOCUS, 0, vbNullString过路的高人来指点指点吧, 如何才能做到那个效果呢? 感谢感谢,
ShowWindow Me.hwnd, SW_RESTOREIf fx < Screen.Width + 50000 Then
Me.Left = fx: Me.Top = fy
Else
Me.Left = 0: Me.Top = 0
End IfEnableWindow Me.hWnd,True试试上面这个程序行不行。
Public Const SW_RESTORE = 9
Public Const SW_SHOW = 5 'SW_SHOW对最小化窗口会失效,如果窗口最小化必须用SW_RESTORE才能显示
Public Const SW_HIDE = 0Public Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Const WM_DESTROY = &H2
Public Const WM_CLOSE = &H10
Public Const WM_ACTIVATE = &H6
Public Const WM_SETFOCUS = &H7'SendMessage Text1.hwnd, WM_CLOSE, &O0, &O0
'DestroyWindow Command2.hwnd
'SendMessage Text1.hwnd, WM_DESTROY, &O0, &O0
Public Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
'Public Declare Function GetActiveWindow Lib "user32" () As Long
Public 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
Public Const HWND_TOPMOST = -1 '固定在顶部
Public Const SWP_NOSIZE = &H1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_NOMOVE = &H2
Public Const SWP_SHOWWINDOW = &H40
Public Const SWP_NOZORDER = &H4
Public Const HWND_TOP = 0 '不会固定在顶部
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As LongPublic Declare Function GetForegroundWindow Lib "user32" () As Long
Public 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'以下几句用来隐藏自己用!!
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
'Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_EXSTYLE = (-20)
Private Const WS_EX_APPWINDOW As Long = &H40000
Private Const WS_EX_MDICHILD As Long = &H40&
Public OldParent As Long
'Public showFlags As BooleanPublic Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPublic Const MD5String = "a344442-9fu891-312l3-39fjrr-xxxxx"
'MD5值
'以下API是获得目标窗口程序路径的
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private 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 GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
这样看行不行:
If fx < Screen.Width + 50000 Then
Me.Left = fx: Me.Top = fy
Else
Me.Left = 0: Me.Top = 0
End IfEnableWindow Me.hWnd,TrueSetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3
ShowWindow Me.hwnd, SW_RESTORE
http://tieba.baidu.com/f?kz=959555407
DoEvents
If Timer1.Interval <> 500 Then
If IsWindow(prtHwnd) = 0 Then
Timer1.Interval = 500
Dim msg$
msg = "运行错误!"
MsgBox msg, vbInformation, "提示"
End If
Else
Timer1.Interval = 200
'Crecked处理
End If
'//////////////////////////////////////////
'检测用户隐藏窗口等一系列事件
'//////////////////////////////////////////
Dim gt$, tStr$
gt = GetText(FormHwndList)
'#为隐藏标记,检测到此标记过后,立即隐藏窗口(移动走)
If (Left(gt, 1) = "#" And Me.Left < Screen.Width + 50000) Then
If Me.Left > -Screen.Width Then '如果最小化,left会置为-480000
fx = Me.Left: fy = Me.Top
End If
If Me.WindowState <> 0 Then:ShowWindow Me.hwnd, SW_SHOW '隐藏前记录窗口位置
Me.Left = Screen.Width + 60000: Me.Top = Screen.Height + 60000
LastHideCmd = 1
End If
If Me.WindowState <> 0 Then
If Me.WindowState <> 0 Then:ShowWindow Me.hwnd, SW_RESTORE '隐藏前记录窗口位置
Me.Left = Screen.Width + 60000: Me.Top = Screen.Height + 60000
LastHideCmd = 2
End If
'@为正常标记,检测到过后立即恢复窗口
If Hotkey2(18, 192) And LastHideCmd > 0 Then
Me.WindowState = 0
gt = GetText(FormHwndList)
tStr = "@" & Mid(gt, 2)
SetWindowText FormHwndList, tStr
LastHideCmd = 0
If LastHideCmd <> 2 Then
ShowHideTaskButton Me.hwnd, True
End If
'////////////下面语句在显示窗口效果的时候, 存在致命缺陷/////////////////
'如何有效的将一个窗口显示在最前面(但不是固定置顶)同时激活窗口呢?
'这里不能用SW_SHOW(对最小化窗口会失效),但restore命令无法让窗口有效的在最顶部,用setwindowpos可以解决
'ShowWindow Me.hwnd, SW_RESTORE
'SetActiveWindow Me.hwnd
'SetWindowPos Me.hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOSIZE '缺陷无法激活窗口
EnableWindow Me.hwnd, True
SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3 '缺陷无法激活窗口
'SetForegroundWindow Me.hwnd
ShowWindow Me.hwnd, SW_RESTORE
'SetWindowPos Me.hwnd, -2, 0, 0, 0, 0, 3 '缺陷无法激活窗口
SetFocusAPI Me.hwnd
'SetForegroundWindow Me.hwnd
'base.SetFocus Text1.hwnd
'SetForegroundWindow jdUscHwnd
'jdUscHwnd
If fx < Screen.Width + 50000 Then
Me.Left = fx: Me.Top = fy
Else
Me.Left = 0: Me.Top = 0
End If
'SetActiveWindow Me.hwnd
'SendMessage Me.hwnd, WM_ACTIVATE, 0, 0
'SendMessage Me.hwnd, WM_SETFOCUS, 0, 0
'SendMessage Me.hwnd, WM_ACTIVATE, 0, vbNullString
'SendMessage Me.hwnd, WM_SETFOCUS, 0, vbNullString
'SetWindowPos Me.hwnd, -2, 0, 0, 0, 0, 3 '取消窗口最前
End If
If Me.Left > -Me.Width And Me.Left < Screen.Width And Me.Top > -Me.Height And Me.Top < Screen.Height Then '如果最小化,left会置为-480000
fx = Me.Left: fy = Me.Top
Me.Caption = "show"
Else
If Me.WindowState = 0 Then
Me.Caption = "hide"
Else
Me.Caption = "min"
End If
End If
'If Me.WindowState = 1 Then
' ShowWindow Me.hwnd, SW_SHOWNOACTIVATE
' SetWindowPos Me.hwnd, HWND_NOTOPMOST, Screen.Width + 60000, Screen.Height + 60000, 0, 0, SWP_NOACTIVATE Or SWP_NOSIZE Or SWP_SHOWWINDOW
' 'MoveWindow Me.hwnd, Screen.Width + 50000, Me.Top, Me.Width, Me.Height, True
'End If
'如果是正常状态,检测用户ClickTaskButton事件
'If Me.WindowState = 0 Then
' 'If GetActiveWindow() = Me.hwnd Then
' Dim fhwnd As Long
' fhwnd = GetForegroundWindow()
' If fhwnd = Me.hwnd Then
' If Me.Left >= -Me.Width And Me.Left <= Screen.Width And Me.Top >= -Me.Height And Me.Top <= Screen.Height Then
' fx = Me.Left: fy = Me.Top
' Else
' 'SetWindowPos Me.hwnd, HWND_NOTOPMOST, fx, fy, 0, 0, SWP_SHOWWINDOW Or SWP_NOSIZE Or SWP_NOZORDER
' Me.Left = fx: Me.Top = fy
' 'Me.Caption = Me.Left
' End If
' Else
' Me.Caption = "环境循标CAD"
' End If
' 'Me.Caption = fhwnd & ":" & Me.hwnd
'Else
' 'If Me.WindowState = 1 Then Me.Caption = "show 1"
'End If
'Private Const HWND_TOPMOST = -1
'Private Const HWND_NOTOPMOST = -2
'Private Const SWP_NOACTIVATE = &H10
'Private Const SWP_NOMOVE = &H2
'Private Const SWP_SHOWWINDOW = &H40
'Me.Enabled = False
End Sub
hForegdWnd = GetForegroundWindow()
dwCurID = GetCurrentThreadId()
dwForeID = GetWindowThreadProcessId(hForegdWnd, 0)
AttachThreadInput dwForeID, dwCurID, 1
SetForegroundWindow hwnd
AttachThreadInput dwForeID, dwCurID, 0
可以确定的说这种现象跟系统环境有关,我有三台电脑 其中有一台就是楼主说的情况 无法真正激活窗口到前台,而另外两台电脑 即使用text1.setfocus这样简单的语句都可以轻松激活窗口到前台,这个应该是系统设置的问题,在Windows 9x/2000中,当用户正在操作其他窗口时是不能强制某个窗口为前景窗口的,而是激活窗口并调用FlashWindowEx函数来通知用户。但奇怪的是我的三台电脑都是xp sp3,如果知道这是哪个设置 或许可以解决楼主的问题,但自己的电脑尚可,客户的就不好办了...