MDI环境下枚举所有打开的窗口
2016-10-12 14:44
429 查看
In MFC, each CMDIChildWnd created by the framework is managed as a child window of the MDIClient window. This MDIClient window is a child of the mainframe window and fills its client area. For MDI applications, the mainframe window is encapsulated by the CMDIFrameWnd
class. This class has a public embedded HWND member (m_hWndMDIClient), which is the handle to the MDIClient window. For MDI applications, AppWizard derives the CMainFrame class from CMDIFrameWnd.
The MDIClient maintains an internal list of child windows. In an MFC application, these child windows are either a CMDIChildWnd object or an internal window used to display the title of an iconized window. Note that this is an internal list controlled by Windows;
don't make assumptions about the ordering of children in the list after an API function is called.
//**mainfrm.h****************
class CMainFrame : public CMDIFrameWnd
{
...
public :
CWnd m_wndMDIClient;
CWnd* m_pWndCurrentChild;
CMDIChildWnd* GetNextMDIChildWnd();
int GetCountCMDIChildWnds();
...
}
//**mainfrm.cpp**************
CMainFrame::CMainFrame(): m_pWndCurrentChild(NULL)
{
//.................
}
CMainFrame::~CMainFrame()
{
m_wndMDIClient.Detach();
//.................
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (m_wndMDIClient.Attach(m_hWndMDIClient) == 0)
{
TRACE0("Failed to attach MDIClient./n" );
return -1; // fail to create
}
//.................
}
//----------------------------------------------------------------
// This function finds the CMDIChildWnd in the list of windows
// maintained by the application's MDIClient window following the
// one pointed to by the member variable m_pWndCurrentChild. If no
// further CMDIChildWnds are in the list, NULL is returned.
//----------------------------------------------------------------
CMDIChildWnd* CMainFrame::GetNextMDIChildWnd()
{
if (!m_pWndCurrentChild)
{
// Get the first child window.
m_pWndCurrentChild = m_wndMDIClient.GetWindow(GW_CHILD);
}
else
{
// Get the next child window in the list.
m_pWndCurrentChild =
(CMDIChildWnd*)m_pWndCurrentChild->GetWindow(GW_HWNDNEXT);
}
if (!m_pWndCurrentChild)
{
// No child windows exist in the MDIClient,
// or you are at the end of the list. This check
// will terminate any recursion.
return NULL;
}
// Check the kind of window
if (!m_pWndCurrentChild->GetWindow(GW_OWNER))
{
if (m_pWndCurrentChild->
IsKindOf(RUNTIME_CLASS(CMDIChildWnd)))
{
// CMDIChildWnd or a derived class.
return (CMDIChildWnd*)m_pWndCurrentChild;
}
else
{
// Window is foreign to the MFC framework.
// Check the next window in the list recursively.
return GetNextMDIChildWnd();
}
}
else
{
// Title window associated with an iconized child window.
// Recurse over the window manager's list of windows.
return GetNextMDIChildWnd();
}
}
//-----------------------------------------------------------------
// This function counts the number of CMDIChildWnd objects
// currently maintained by the MDIClient.
//-----------------------------------------------------------------
int CMainFrame::GetCountCMDIChildWnds()
{
int count = 0;
CMDIChildWnd* pChild = GetNextMDIChildWnd();
while (pChild)
{
count++;
pChild = GetNextMDIChildWnd();
}
return count;
}
class. This class has a public embedded HWND member (m_hWndMDIClient), which is the handle to the MDIClient window. For MDI applications, AppWizard derives the CMainFrame class from CMDIFrameWnd.
The MDIClient maintains an internal list of child windows. In an MFC application, these child windows are either a CMDIChildWnd object or an internal window used to display the title of an iconized window. Note that this is an internal list controlled by Windows;
don't make assumptions about the ordering of children in the list after an API function is called.
//**mainfrm.h****************
class CMainFrame : public CMDIFrameWnd
{
...
public :
CWnd m_wndMDIClient;
CWnd* m_pWndCurrentChild;
CMDIChildWnd* GetNextMDIChildWnd();
int GetCountCMDIChildWnds();
...
}
//**mainfrm.cpp**************
CMainFrame::CMainFrame(): m_pWndCurrentChild(NULL)
{
//.................
}
CMainFrame::~CMainFrame()
{
m_wndMDIClient.Detach();
//.................
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (m_wndMDIClient.Attach(m_hWndMDIClient) == 0)
{
TRACE0("Failed to attach MDIClient./n" );
return -1; // fail to create
}
//.................
}
//----------------------------------------------------------------
// This function finds the CMDIChildWnd in the list of windows
// maintained by the application's MDIClient window following the
// one pointed to by the member variable m_pWndCurrentChild. If no
// further CMDIChildWnds are in the list, NULL is returned.
//----------------------------------------------------------------
CMDIChildWnd* CMainFrame::GetNextMDIChildWnd()
{
if (!m_pWndCurrentChild)
{
// Get the first child window.
m_pWndCurrentChild = m_wndMDIClient.GetWindow(GW_CHILD);
}
else
{
// Get the next child window in the list.
m_pWndCurrentChild =
(CMDIChildWnd*)m_pWndCurrentChild->GetWindow(GW_HWNDNEXT);
}
if (!m_pWndCurrentChild)
{
// No child windows exist in the MDIClient,
// or you are at the end of the list. This check
// will terminate any recursion.
return NULL;
}
// Check the kind of window
if (!m_pWndCurrentChild->GetWindow(GW_OWNER))
{
if (m_pWndCurrentChild->
IsKindOf(RUNTIME_CLASS(CMDIChildWnd)))
{
// CMDIChildWnd or a derived class.
return (CMDIChildWnd*)m_pWndCurrentChild;
}
else
{
// Window is foreign to the MFC framework.
// Check the next window in the list recursively.
return GetNextMDIChildWnd();
}
}
else
{
// Title window associated with an iconized child window.
// Recurse over the window manager's list of windows.
return GetNextMDIChildWnd();
}
}
//-----------------------------------------------------------------
// This function counts the number of CMDIChildWnd objects
// currently maintained by the MDIClient.
//-----------------------------------------------------------------
int CMainFrame::GetCountCMDIChildWnds()
{
int count = 0;
CMDIChildWnd* pChild = GetNextMDIChildWnd();
while (pChild)
{
count++;
pChild = GetNextMDIChildWnd();
}
return count;
}
相关文章推荐
- 枚举当前环境中打开的所有IE
- 枚举当前环境中打开的所有IE
- VC++MFC 枚举当前打开的所有窗口
- 枚举当前打开的所有窗口
- 枚举当前打开的所有窗口
- 枚举当前环境中打开的所有IE
- 枚举当前打开的所有窗口
- 枚举当前打开的所有窗口
- MFC枚举所有窗体,并向打开的窗体发送关闭消息
- C++ 枚举所有窗口
- dede上怎么让所有链接在新窗口打开
- 如何使用 base 标签使页面中的所有标签在新窗口中打开
- "不是所有工作区中的窗口无法打开"错误消息(Not all of the Windows in the workspace could be open)
- 用javascript实现始终保持打开同一个子窗口以及关闭父窗口同时自动关闭所有子窗口
- MFC枚举所有窗体,并向打开的窗体发送关闭消息
- 在 CTreeCtrl 中枚举系统中的所有窗口!(I)
- 枚举当前所有窗口
- 获得所有已打开的IE浏览器窗口中的访问地址
- QT 应用程序关闭某个窗口时,关闭打开的所有其他窗口并退出程序 【转】
- 在 CTreeCtrl 中枚举系统中的所有窗口!(II)