【VC开源代码栏目提醒】:网学会员在VC开源代码频道为大家收集整理了WINDOWSTREECTRL.CPP提供大家参考,希望对大家有所帮助!
// WindowsTreeCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "WindowsTreeCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWindowsTreeCtrl
CWindowsTreeCtrl::CWindowsTreeCtrl()
{
}
CWindowsTreeCtrl::~CWindowsTreeCtrl()
{
}
BEGIN_MESSAGE_MAP(CWindowsTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CWindowsTreeCtrl)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWindowsTreeCtrl message handlers
// Function name : CWindowsTreeCtrl::GetClassName
// Description :
// Return type : CString
// Argument : HWND hWnd
CString CWindowsTreeCtrl::GetItemName(HWND hWnd)
{
CString result;
::GetClassName(hWnd, result.GetBuffer(1024),1024);
result.ReleaseBuffer();
CString itemText;
itemText.Format(_T("0x%08X, %s"), hWnd, result);
return itemText;
}
// Function name : CWindowsTreeCtrl::EnumWindowProc
// Description : Call for each window
// Return type : BOOL
// Argument : HWND hWnd
// Argument : LPARAM lParam
BOOL CWindowsTreeCtrl::EnumWindowProc(HWND hWnd, LPARAM lParam)
{
CWindowsTreeCtrl* pTree = (CWindowsTreeCtrl*)lParam;
pTree->NewBranch(hWnd);
return TRUE;
}
// Function name : CWindowsTreeCtrl::Enum
// Description : Called to fill this tree.
// Return type : void
void CWindowsTreeCtrl::Enum()
{
::EnumWindows((WNDENUMPROC)EnumWindowProc, (LPARAM)this);
}
// Function name : CWindowsTreeCtrl::NewBranch
// Description :
// Return type : void
// Argument : HWND hWnd
void CWindowsTreeCtrl::NewBranch(HWND hWnd)
{
HTREEITEM hRoot = InsertItem(GetItemName(hWnd));
SetItemData(hRoot, (LPARAM)hWnd);
AddAllChilds(hRoot);
}
// Function name : CWindowsTreeCtrl::AddAllChilds
// Description : Add all windows descendent of hRoot to tree
// Return type : void
// Argument : HTREEITEM hRoot
void CWindowsTreeCtrl::AddAllChilds(HTREEITEM hRoot)
{
if (HWND hWnd = (HWND)GetItemData(hRoot))
if (IsWindow(hWnd))
{
CWnd* pWnd = CWnd::FromHandle(hWnd);
if (IsWindow(pWnd->m_hWnd))
{
CWnd* pChild = pWnd->GetWindow(GW_CHILD);
while (pChild)
{
HTREEITEM hItem = InsertItem(GetItemName(pChild->m_hWnd), hRoot);
SetItemData(hItem, (LPARAM)pChild->m_hWnd);
AddAllChilds(hItem);
pChild = pChild->GetWindow(GW_HWNDNEXT);
}
}
}
}