FindWindowEx

王朝百科·作者佚名  2010-02-20  
宽屏版  字体: |||超大  

函数功能:该函数获得一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。

函数原型:HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,LPCTSTR lpszClass,LPCTSTR lpszWindow);

参数;

hwndParent:要查找子窗口的父窗口句柄。

如果hwndParent为NULL,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。

Windows NT5.0 and later:如果hwndParent是HWND_MESSAGE,函数仅查找所有消息窗口。

hwndChildAfter :子窗口句柄。查找从在Z序中的下一个子窗口开始。子窗口必须为hwndPareRt窗口的直接子窗口而非后代窗口。如果HwndChildAfter为NULL,查找从hwndParent的第一个子窗口开始。如果hwndParent 和 hwndChildAfter同时为NULL,则函数查找所有的顶层窗口及消息窗口。

lpszClass:指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobaIAddAtom函数产生的全局成员。该成员为16位,必须位于lpClassName的低16位,高位必须为0。

lpszWindow:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为 NULL,则为所有窗口全匹配。返回值:如果函数成功,返回值为具有指定类名和窗口名的窗口句柄。如果函数失败,返回值为NULL。

若想获得更多错误信息,请调用GetLastError函数。

速查 NT:4.0对以上版本;Windows:95以上版本;Windows CE:不支持;头文件:winuser.h;库文件:user32.lib;Unicode:在Windows NT上实现为Unicode和ANSI两种版本。

在窗口列表中寻找与指定条件相符的第一个子窗口

返回值

Long,找到的窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError

参数表

参数 类型及说明

hWnd1 Long,在其中查找子的父窗口。如设为零,表示使用桌面窗口(通常说的顶级窗口都被认为是桌面的子窗口,所以也会对它们进行查找)

hWnd2 Long,从这个窗口后开始查找。这样便可利用对FindWindowEx的多次调用找到符合条件的所有子窗口。如设为零,表示从第一个子窗口开始搜索

lpsz1 String,欲搜索的类名。零表示忽略

lpsz2 String,欲搜索的类名。零表示忽略

相关例子:

'Example Name: Changing a VB Toolbar to a Rebar-Style Toolbar

'------------------------------------------------------------------------------

'

' BAS Moduel Code

'

'------------------------------------------------------------------------------

Option Explicit

Public Const WM_USER = &H400

Public Const TB_SETSTYLE = WM_USER + 56

Public Const TB_GETSTYLE = WM_USER + 57

Public Const TBSTYLE_FLAT = &H800

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 FindWindowEx Lib "user32" _

   Alias "FindWindowExA" _

  (ByVal hWnd1 As Long, _

   ByVal hWnd2 As Long, _

   ByVal lpsz1 As String, _

   ByVal lpsz2 As String) As Long

'--end block--'

'------------------------------------------------------------------------------

'

' Form Code

'

'------------------------------------------------------------------------------

 

Option Explicit

Private Sub Form_Load()

    With Combo1

        .Width = Toolbar1.Buttons("combo1").Width

        .Top = (Toolbar1.Height - Combo1.Height) 2

        .Left = Toolbar1.Buttons("combo1").Left

        .AddItem "Black" ' Add colours for text.

        .AddItem "Blue"

        .AddItem "Red"

        .ListIndex = 0

    End With

End Sub

Private Sub Command1_Click()

   Dim style As Long

   Dim hToolbar As Long

   

  'get the handle of the toolbar

   hToolbar = FindWindowEx(Toolbar1.hwnd, 0&, "ToolbarWindow32", vbNullString)

   

  'retrieve the toolbar styles

   style = SendMessage(hToolbar, TB_GETSTYLE, 0&, ByVal 0&)

   

  'Set the new style flag

   If style And TBSTYLE_FLAT Then

         style = style Xor TBSTYLE_FLAT

   Else: style = style Or TBSTYLE_FLAT

   End If

   

  'apply the new style to the toolbar

   Call SendMessage(hToolbar, TB_SETSTYLE, 0, ByVal style)

   Toolbar1.Refresh

   

End Sub

'Example Name:Disabling the Combo Edit Box  

'------------------------------------------------------------------------------

'

' BAS Moduel Code

'

'------------------------------------------------------------------------------

Option Explicit

Public Const EM_SETREADONLY = &HCF

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 SetWindowText Lib "user32" _

   Alias "SetWindowTextA" _

  (ByVal hwnd As Long, _

   ByVal lpString 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 Long

'--end block--'

 

'------------------------------------------------------------------------------

'

' Form Code

'

'------------------------------------------------------------------------------

 

Option Explicit

Sub Form_Load()

  'fill a combo with 10 of the 

  'system's screen fonts.

   Dim i As Integer

   For i = 1 To 10

      Combo1.AddItem Screen.Fonts(i)

   Next

End Sub

Private Sub Command1_Click()

   Dim hwndEdit As Long

   

  'get the handle to the edit portion

  'of the combo control

   hwndEdit = FindWindowEx(Combo1.hwnd, 0&, vbNullString, vbNullString)

   

   If hwndEdit <> 0 Then

     

     'prove you got it by changing its text (style 0 only)

      Call SetWindowText(hwndEdit, "You can copy, but not change me  :-)")

      

     'and disable the edit control's editing ability

      Call SendMessage(hwndEdit, EM_SETREADONLY, 1&, ByVal 0&)

   End If

   

End Sub

'Example Name:Fixing the IE5/MsComCtrl 5 Toolbar Problem 

'------------------------------------------------------------------------------

'

' BAS Moduel Code

'

'------------------------------------------------------------------------------

Option Explicit

Public Const WM_USER As Long = &H400

Public Const TB_SETSTYLE As Long = WM_USER + 56

Public Const TB_GETSTYLE As Long = WM_USER + 57

Public Const TBSTYLE_WRAPABLE As Long = &H200  'buttons to wrap when form resized

Public Const TBSTYLE_FLAT As Long = &H800      'flat IE3+ style toolbar

Public Const TBSTYLE_LIST As Long = &H1000     'places captions beside buttons

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 FindWindowEx Lib "user32" _

   Alias "FindWindowExA" _

  (ByVal hWnd1 As Long, _

   ByVal hWnd2 As Long, _

   ByVal lpsz1 As String, _

   ByVal lpsz2 As String) As Long

'--end block--'

'------------------------------------------------------------------------------

'

' Form Code

'

'------------------------------------------------------------------------------

Option Explicit

Private Sub Form_Load()

  'centre form

   Me.Move (Screen.Width - Me.Width) 2, (Screen.Height - Me.Height) 2

   Me.Show

   

  'for the demo, make toolbars 1 and 3 flat

  'args:             ctrl      horiz  wrapable

   CreateFlatToolbar Toolbar1, False, False

   CreateFlatToolbar Toolbar3, True, False

   

  'set toolbar 4 to horizontal. Since

  'toolbar 2 retains the default

  'appearance, no styles are specified.

  'args:                   ctrl      horiz wrapable

   CreateHorizontalToolbar Toolbar4, True, False

End Sub

Private Sub Command1_Click()

   

  '                 ctrl,    ndx, caption

  SetToolbarCaption Toolbar1, 1, "Go Backwards"

  SetToolbarCaption Toolbar2, 1, "Go Backwards"

   

  SetToolbarCaption Toolbar3, 1, "Go Backwards"

  SetToolbarCaption Toolbar4, 1, "Go Backwards"

   

  'this is the code to produce the illustration 2 results

  'Toolbar1.Buttons(1).Caption = "Go Backwards"

  'Toolbar2.Buttons(1).Caption = "Go Backwards"

  'Toolbar3.Buttons(1).Caption = "Go Backwards"

  'Toolbar4.Buttons(1).Caption = "Go Backwards"

   

End Sub

Private Sub CreateFlatToolbar(TBar As Toolbar, _

                              fHorizontal As Boolean, _

                              fWrapable As Boolean)

   Dim hTBar As Long

   Dim style As Long

       

  'to assure that the toolbar has correctly calculated

  'the button widths based on the assigned captions,

  'force a refresh

   TBar.Refresh

   

  'get the handle of the toolbar

   hTBar = FindWindowEx(TBar.hwnd, 0&, "ToolbarWindow32", vbNullString)

   

  'retrieve the current toolbar style

   style = SendMessage(hTBar, TB_GETSTYLE, 0&, ByVal 0&)

   style = style Or TBSTYLE_FLAT Or TBSTYLE_WRAPABLE

   

  'if a horizontal layout was specified, add that style

   If fHorizontal Then style = style Or TBSTYLE_LIST

   

  'apply the new style to the toolbar and refresh

   Call SendMessage(hTBar, TB_SETSTYLE, 0&, ByVal style)

   TBar.Refresh

   

  'now that the toolbar is flat, if the wrapable

  'style is not desired, it can be removed.

  'A refresh is not required.

   If fWrapable = False Then

      style = style Xor TBSTYLE_WRAPABLE

      Call SendMessage(hTBar, TB_SETSTYLE, 0&, ByVal style)

   End If

    

End Sub

Private Sub CreateHorizontalToolbar(TBar As Toolbar, _

                                    fHorizontal As Boolean, _

                                    fWrapable As Boolean)

   Dim hTBar As Long

   Dim style As Long

   

  'get the handle of the toolbar

   hTBar = FindWindowEx(TBar.hwnd, 0&, "ToolbarWindow32", vbNullString)

   

  'retrieve the current toolbar style

   style = SendMessage(hTBar, TB_GETSTYLE, 0&, ByVal 0&)

   

  'Set the new style flags

   If fHorizontal Then

      style = style Or TBSTYLE_LIST

   End If

   

   If fWrapable Then

      style = style Or TBSTYLE_WRAPABLE

   End If

   

  'apply the new style to the toolbar and refresh

   Call SendMessage(hTBar, TB_SETSTYLE, 0&, ByVal style)

   TBar.Refresh

    

End Sub

Private Sub SetToolbarCaption(TBar As Toolbar, _

                              TBIndex As Integer, _

                              newCaption As String)

   Dim hTBar As Long

   Dim inStyle As Long

   Dim tempStyle As Long

    

  'get the handle to the toolbar and its current style

   hTBar = FindWindowEx(TBar.hwnd, 0&, "ToolbarWindow32", vbNullString)

   inStyle = SendMessage(hTBar, TB_GETSTYLE, 0&, ByVal 0&)

   

  'if the toolbar has had the flat style applied

   If inStyle And TBSTYLE_FLAT Then

      tempStyle = inStyle Xor TBSTYLE_FLAT

      Call SendMessage(hTBar, TB_SETSTYLE, 0&, ByVal tempStyle)

      TBar.Buttons(TBIndex).Caption = newCaption

      TBar.Refresh

      

     'restore the previous style, and refresh once more

      Call SendMessage(hTBar, TB_SETSTYLE, 0&, ByVal inStyle)

      TBar.Refresh

      

   Else

   

     'its not flat, so just change the text

      TBar.Buttons(TBIndex).Caption = newCaption

      

   End If

End Sub

'Example Name: Obtaining the Combo Box Edit Window Handle 

'------------------------------------------------------------------------------

'

' BAS Moduel Code

'

'------------------------------------------------------------------------------

Option Explicit

Public Const CB_ADDSTRING = &H143

Public Const CB_SETITEMDATA = &H151

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 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 SetWindowText Lib "user32" _

   Alias "SetWindowTextA" _

  (ByVal hwnd As Long, _

   ByVal lpString As String) As Long

'--end block--'

'------------------------------------------------------------------------------

'

' Form Code

'

'------------------------------------------------------------------------------

Option Explicit

Private Sub Command1_Click()

   Dim hwndEdit As Long

   

  'get the handle to the edit portion

  'of the combo control

   hwndEdit = FindWindowEx(Combo1.hwnd, 0&, vbNullString, vbNullString)

   

  'prove you got it by changing its text (style 0 only)

   Call SetWindowText(hwndEdit, "FindWindowEx found it!")

   

  'add it to the list...

   Call SendMessage(Combo1.hwnd, CB_ADDSTRING, _

                       0&, ByVal "FindWindowEx: Edit handle is " & CStr(hwndEdit))

   

End Sub

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝百科 版权所有