Private Sub Form_Load()
 Me.Caption = App.Title
 Dim a As WINDOWPLACEMENT
 c = Me.Caption
 If App.PrevInstance Then
  Me.Caption = ""
  b = FindWindow(vbNullString, c)
  GetWindowPlacement b, a
  SetForegroundWindow b
  If a.showCmd = 2 Then
   ShowWindow b, SW_RESTORE
  End If
  End
 End If
End Sub以上代码在窗体Caption里有内容情况下一切正常,当Caption为空时无论如何也不能设第一个为系统的前台窗口了。菜鸟求救。

解决方案 »

  1.   

    LZ用了一堆的API.
    加外养成一个习惯吧。用的时候先声明。
    比如 a,b,c
      

  2.   

    不 第2次运行的窗口关闭防止重复运行。第一次运行的窗体置顶。
    以上代码已经实现了,但我的是无标题窗体,应为有右键菜单,所以必须把caption内容设置为空,但caption内容为空后用上面代码第一次运行的窗体不置顶了。如果caption内容不为空则一切正常所以现在很纠结
      

  3.   

    不 第2次运行的窗口关闭防止重复运行。第一次运行的窗体置顶。
    以上代码已经实现了,但我的是无标题窗体,应为有右键菜单,所以必须把caption内容设置为空,但caption内容为空后用上面代码第一次运行的窗体不置顶了。如果caption内容不为空则一切正常所以现在很纠结
      

  4.   

     
    ' 如果有错自己改吧... 
    Private Sub Form_Load()    Dim a As WINDOWPLACEMENT    Me.Caption = App.Title
        
        If App.PrevInstance Then
        
            Me.Caption = ""
            
            Do
                b = FindWindowEx(vbNullString, b, vbNullString, vbNullString)
                If b = 0 Then:
                    Exit Do
                
                Else
                    
                    If GetWindowLong(b, GWL_USERDATA) = 997755331 Then
                        GetWindowPlacement b, a
                        SetForegroundWindow b
                        If a.ShowCmd = 2 Then ShowWindow b, SW_RESTORE
                        End
                        
                    End If
                    
                End If
            
            Loop
            
        SetWindowLong Me.hWnd, GWL_USERDATA, 997755331
        
        End If
    End Sub
      

  5.   

    看样子有戏了。嘿嘿
    1 b = FindWindowEx(vbNullString, b, vbNullString, vbNullString)~~重复运行提示“运行是错误“13”:类型不匹配”
    2 997755331 我的这个32位值应该是多少的 我用spy++看了是7位的
      

  6.   

    关于那个 b 的问题.... 主要是你没做 dim b as long ... 以后用变量一定要提前声明
    那个数是个强大的数, 是启动windows高级功能的一个钥匙
      

  7.   

    dim b as long做了的 还是一样
      

  8.   

    模块:
    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
     Public Const SW_RESTORE = 9
    Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
     Type POINTAPI
      X As Long
      Y As Long
     End Type
     Type rect
      left As Long
      top As Long
      right As Long
      bottom As Long
     End Type
     Type WINDOWPLACEMENT
      Length As Long
      flags As Long
      showCmd As Long
      ptMinPosition As POINTAPI
      ptMaxPosition As POINTAPI
      rcNormalPosition As rect
     End Type窗体:因为用了菜单编辑器  窗体里的caption是空的 没设名字 设名字的话标题栏就出来了 因为我做的是无标题的窗口Private Sub Form_Load()
    Dim b As Long
       Dim a As WINDOWPLACEMENT
          Me.Caption = App.Title
        
        If App.PrevInstance Then
        
            Me.Caption = ""
            
            Do
                b = FindWindowEx(vbNullString, b, vbNullString, vbNullString)
                If b = 0 Then
                    Exit Do
                
                Else
                    
                    If GetWindowLong(b, GWL_USERDATA) = 997755331 Then
                        GetWindowPlacement b, a
                        SetForegroundWindow b
                        If a.showCmd = 2 Then ShowWindow b, SW_RESTORE
                        End
                        
                    End If
                    
                End If
            
            Loop
            
        SetWindowLong Me.hwnd, GWL_USERDATA, 997755331
        
        End IfEnd Sub
      

  9.   

      b = FindWindowEx(0, b, vbNullString, vbNullString)
      

  10.   

    b = FindWindowEx(0, b, vbNullString, vbNullString)
      

  11.   

    谢谢,不出错了。但重复运行的没有关闭。我估计因该是997755331这里吧? 我的这个32位值应该是多少的呢 我用spy++看了是7位的
      

  12.   

    第一个运行的没有关闭, 是因为他的 caption <> 空 , 后续开的对他也不产生任何反应,是因为 ...
    再开第2个, 此时 caption = 空, 但第一个没有关闭,  再开第3个,则开不了了, 因为检测到了第2个我对你题意和代码的意思都没有深读, 我认为就是说程序第一次运行后,标题就是空的把代码再改一下:Private Sub Form_Load()    Dim a As WINDOWPLACEMENT    Me.Caption = App.Title              
       ' 这里到底有用没有, 如果窗口标题不为空,为什么第2次运行的程序要查找标题为空的程序呢?
        
        If App.PrevInstance Then
        
            'Me.Caption = ""  这里要换个地方... 看你这个代码我都不知道你到底想怎么玩了...
            
            Do
                b = FindWindowEx(vbNullString, b, vbNullString, vbNullString)
                If b = 0 Then:
                    Exit Do
                
                Else
                    
                    If GetWindowLong(b, GWL_USERDATA) = 997755331 Then
                        GetWindowPlacement b, a
                        SetForegroundWindow b
                        If a.ShowCmd = 2 Then ShowWindow b, SW_RESTORE
                        End
                        
                    End If
                    
                End If
            
            Loop
            
        SetWindowLong Me.hWnd, GWL_USERDATA, 997755331
        ' 标题到底是否需要设置为空, 如果是,在这里加 Me.Caption = ""
        End If
    End Sub
      

  13.   

    http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/41.gif
    我看了实例程序有标题 副本的没有标题。但是现在副本没有end啊 
      

  14.   

    还是一样 又回到1楼了 穿越ing...我完整窗体代码如下:Private Sub Form_Load()
     Dim a As WINDOWPLACEMENT
     Dim b As Long
     Me.Caption = "" '带菜单无标题边框窗口 这里必须为空
     c = Me.Caption '记录原窗口名称
     If App.PrevInstance Then '如果重复运行
      Me.Caption = "重复运行的窗口" '则将新窗口名称改变
      b = FindWindow(vbNullString, c) '查找原窗口句柄
      GetWindowPlacement b, a '返回原窗口的显示状态
      SetForegroundWindow b '原窗口的线程设置到前台,并且激活
      If a.showCmd = 2 Then ShowWindow b, SW_RESTORE '若原窗口最大或最小化则还原
      End '关闭新窗口
     End If
    End Sub
     Me.Caption = "" '带菜单无标题边框窗口 这里必须为空~~~若Me.Caption = "任何数值"代码都可正常运行,但我的Me.Caption = ""必须为空,请问高人怎么处理?困绕我N天了。模块如下:
    Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
     Public Const SW_RESTORE = 9
    Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
     Type POINTAPI
      X As Long
      Y As Long
     End Type
     Type rect
      left As Long
      top As Long
      right As Long
      bottom As Long
     End Type
     Type WINDOWPLACEMENT
      Length As Long
      flags As Long
      showCmd As Long
      ptMinPosition As POINTAPI
      ptMaxPosition As POINTAPI
      rcNormalPosition As rect
     End Type