获得一个对话框上的所有控件

本文介绍了两种在Windows环境下获取窗口句柄的方法。方法一利用GetWindow函数遍历对话框的所有子窗口;方法二通过EnumChildWindows函数枚举指定父窗口的所有子窗口,并使用回调函数收集特定类型的子窗口句柄。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


方法一:
m_hWnd代表对话框window  
  HWND   hwndChild   =   GetWindow(   m_hWnd   ,   GW_CHILD   );  
  while(   hwndChild   )  
  {  
  hwndChild   =   GetWindow(   hwndChild   ,   GW_HWNDNEXT   );  
  }

GetWindow
The GetWindow function retrieves a handle to a window that has the specified relationship (Z order or owner) to the specified window.

HWND GetWindow(
  HWND hWnd,  // handle to original window
  UINT uCmd   // relationship flag
);

Parameters
hWnd
Handle to a window. The window handle retrieved is relative to this window, based on the value of the uCmd parameter.
uCmd
Specifies the relationship between the specified window and the window whose handle is to be retrieved.
This parameter can be one of the following values: Value Meaning

GW_CHILD   The retrieved handle identifies the child window at the top of the Z order,
     if the specified window is a parent window; otherwise, the retrieved handle is NULL.
     The function examines only child windows of the specified window.
     It does not examine descendant windows.
GW_HWNDNEXT The retrieved handle identifies the window below the specified window in the Z order.
     If the specified window is a topmost window, the handle identifies the topmost window below
     the specified window. If the specified window is a top-level window,
      the handle identifies the top-level window below the specified window.
      If the specified window is a child window, the handle identifies the sibling window
       below the specified window.
....
请查看msdn获得更多信息


方法二:
vector<HWND> hwv;
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
 TCHAR buf[100];

 ::GetClassName( hwnd, (LPTSTR)&buf, 100 );
 if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ) /

{
  *(HWND*)lParam = hwnd;//子窗口句柄哦
  hwv.push_back( hwnd ); //收集所有的字窗口句柄
  //return FALSE;
 }
 else
  return TRUE;
};

//You can store the interface pointer in a member variable
//for easier access
void CDlg::OnGetDocInterface()
{
  HWND hWnd = this->m_hWnd;
  if ( hWnd != NULL )
  {
   HWND hWndChild=NULL;
   // Get 1st document window
   /*
   系统会查找hWnd的字窗口,然后对每一字窗口都要调用函数EnumChildProc
   并把字窗口的句柄欻给函数EnumChildProc,另外还会把参数(LPARAM)&hWndChild 传给EnumChildProc,
   所以每一个回调EnumChildProc,传进去的参数有一个都是相同的(即 (LPARAM)&hWndChild).
   另外一个参数则总是不同(即字窗口句柄).
   只有当 对所有字窗口都进行回调函数的调用或者回调函数返回FLASE,EnumChildWindows才会返回.
   */
   ::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
   if ( hWndChild )
   {
   
   } // else document not ready
  }

}

EnumChildWindows
The EnumChildWindows function enumerates the child windows that belong to the specified parent window
by passing the handle to each child window, in turn, to an application-defined callback function.
 EnumChildWindows continues until the last child window is enumerated
 or the callback function returns FALSE.

BOOL EnumChildWindows(
  HWND hWndParent,         // handle to parent window
  WNDENUMPROC lpEnumFunc,  // pointer to callback function
  LPARAM lParam            // application-defined value
);
....
请查看msdn获得更多信息
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值