是不是MDI主窗口? Public Function GetMDIFrame() As Long Dim hwnd As Long Dim ProcessID As Long 'must be child from the desktop window hwnd = GetWindow(GetDesktopWindow, GW_CHILD) 'while theres a window... While hwnd <> 0 ProcessID = GetWindowThreadProcessId(hwnd, ProcessID) If ProcessID = App.ThreadID Then 'same thread? 'same thread --> test window class If Right(LCase(WindowClass(hwnd)), 7) = "mdiform" Then GetMDIFrame = hwnd Exit Function End If End If 'next window hwnd = GetWindow(hwnd, GW_HWNDNEXT) Wend 'no MDWindow GetMDIFrame = 0End Function
激活!! SendMessage hParent, WM_NCACTIVATE, 1, ByVal 0& 'activate form
假设你窗口的类名为mytest(可用spy++查看),则代码如下: 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 Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Private Const SW_RESTORE = 9 Private Const SW_SHOW = 5Private Sub Command1_Click() Dim mhwnd As Long mhwnd = FindWindow("mytest", vbNullString) ShowWindow mhwnd, SW_RESTORE SetForegroundWindow mhwnd End Sub
Public Function GetMDIFrame() As Long
Dim hwnd As Long
Dim ProcessID As Long
'must be child from the desktop window
hwnd = GetWindow(GetDesktopWindow, GW_CHILD)
'while theres a window...
While hwnd <> 0
ProcessID = GetWindowThreadProcessId(hwnd, ProcessID)
If ProcessID = App.ThreadID Then 'same thread?
'same thread --> test window class
If Right(LCase(WindowClass(hwnd)), 7) = "mdiform" Then
GetMDIFrame = hwnd
Exit Function
End If
End If
'next window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Wend
'no MDWindow
GetMDIFrame = 0End Function
作用是可以把DLL中的窗口设到该进程中唯一的mdi主窗口中座位child
SendMessage hParent, WM_NCACTIVATE, 1, ByVal 0& 'activate form
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 Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_RESTORE = 9
Private Const SW_SHOW = 5Private Sub Command1_Click()
Dim mhwnd As Long
mhwnd = FindWindow("mytest", vbNullString)
ShowWindow mhwnd, SW_RESTORE
SetForegroundWindow mhwnd
End Sub
后面的12345是会变的,每次启动都不一样.这怎么通过Class得到窗口句柄?