偶编一个程序,需要激活其它的窗口,如计算器,用VB中Appactivate是不行的,上一些网一查,都说用SetForegroundWindow,但我试了一下,还是不行,只看到窗口的标题栏在闪烁,却得不到焦点。我用的测试方法为:加入一个Timer,然后设为1500。在timer事件中加入SetForegroundWindow me.hwnd。然后一运行程序就点其它的窗口,可怎么也不能让自己拥有焦点。这是什么原因?别人怎么可以的?还请高手指教。

解决方案 »

  1.   

    lWnd = FindWindow(vbNullString, "Alive")
    lRet = ShowWindow(lWnd, SW_SHOWNORMAL)
    lRet = SetForegroundWindow(lWnd)
      

  2.   

    【声明】
    Public Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
    【说明】
    将窗口设为系统的前台窗口。这个函数可用于改变用户目前正在操作的应用程序
    【返回值】
    Long,非零表示成功,零表示失败。会设置GetLastError
    【参数表】
      hwnd -----------  Long,带到前台的窗口
    【其它】
    不应随便使用它,因为一旦程序突然从后台进入前台,可能会使用户产生迷惑
      

  3.   

    先调用下面的函数:
    Public Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long
      

  4.   

    偶来蹭分咯,偶系统是xp咯,下面代码完全正确,呵呵,所以无法继续探讨咯。Option Explicit
    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 LongPrivate Sub Command1_Click()
        Dim h As Long
        h = FindWindow("SciCalc", vbNullString)
        If h Then
            SetForegroundWindow h
        End If
    End Sub除了楼上大虾说的,BringWindowToTop,还有一招就是设置窗体topmost,然后把bringwindowtotop,setforegroundwindow,setactivewindow等等都加上咯,最后去掉topmost,呵呵。
      

  5.   

    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
    Call SetWindowPos(Me.hwnd, -1, 0, 0, Me.Width, Me.Height, &H40)'先置顶Call SetWindowPos(Me.hwnd, -2, 0, 0, Me.Width, Me.Height, &H40)'再取消置顶