Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
    'Ask for a Window title
    Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
    'Search the window
    WinWnd = FindWindow(vbNullString, Ret)
    If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
    'Show the window
    ShowWindow WinWnd, SW_SHOWNORMAL
    'Create a buffer
    lpClassName = Space(256)
    'retrieve the class name
    RetVal = GetClassName(WinWnd, lpClassName, 256)
    'Show the classname
    MsgBox "Classname: " + Left$(lpClassName, RetVal)
    'Post a message to the window to close itself
    PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解决方案 »

  1.   

    这是篇关闭窗口的例子啦.
    我是要用API实现和
    Form1.Show ,Form2
    这个语句相同的效果.
      

  2.   

    有点难度,在创建窗体时(CreateWindowEx)将 hwndParent 参数设为form1的句柄,能得到你要的效果,但是谁喜欢在VB里CreateWindow呢?
      

  3.   

    当然我不想用CreateWindow,因为在实际运行时我只能得到form的句柄.而且VB里的form就算已经load了,也可以实现这样的效果.
    ozw(沧浪客),请具体一点,至少要有一个可以运行的过程.如果只是说SetWindowLong,那不是等于告诉我"用API"吗.
    crystal2000(靠VB吃饭),你试试看就知道了.
      

  4.   

    '两个窗口form1,form2
    '在form1在添加两个按钮 command1,command2,然后把下列代码粘进form1Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Const SW_SHOW = 5
    Private Const SW_HIDE = 0Private Sub Command1_Click()
     ShowWindow Form2.hwnd, SW_SHOWEnd SubPrivate Sub Command2_Click()
     ShowWindow Form2.hwnd, SW_HIDE
    End Sub
      

  5.   

    cuiyxy(沧海鲨鱼),这不是在显示和隐藏窗口吗?你试试我的代码就知道我要什么效果了.