📄 enumwndsdlg.cpp
字号:
// EnumWndsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EnumWnds.h"
#include "EnumWndsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEnumWndsDlg dialog
CWndSet * CEnumWndsDlg::m_pLast;
CEnumWndsDlg::CEnumWndsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEnumWndsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEnumWndsDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_setRoot.m_pBrother = NULL;
m_setRoot.m_pSon = NULL;
m_lstImage.Create(16,16,ILC_COLOR,0,1);
m_lstImage.SetBkColor(RGB(255,255,255));
m_lstImage.Add(AfxGetApp()->LoadIcon(IDI_WND));
}
void CEnumWndsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEnumWndsDlg)
DDX_Control(pDX, IDC_TREE_WNDS, m_treeWnd);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEnumWndsDlg, CDialog)
//{{AFX_MSG_MAP(CEnumWndsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ENUM_WNDS, OnEnum)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEnumWndsDlg message handlers
BOOL CEnumWndsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_treeWnd.SetImageList(&m_lstImage,TVSIL_NORMAL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CEnumWndsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEnumWndsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEnumWndsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEnumWndsDlg::OnEnum()
{
// TODO: Add your control notification handler code here
Free(m_setRoot.m_pSon);
Free(m_setRoot.m_pBrother);
// 初始化根结点
m_setRoot.m_nHandle = (UINT)::GetDesktopWindow();
m_setRoot.m_pBrother = NULL;
m_setRoot.m_pSon = NULL;
m_setRoot.m_strTitle = _T("DeskTop");
EnumChildWnd(&m_setRoot);
// 显示得到的窗体树
m_treeWnd.DeleteAllItems();
AddNode(&m_setRoot,TVI_ROOT);
}
void CEnumWndsDlg::EnumChildWnd(CWndSet *pParent)
{
m_pLast = NULL;
HWND hWnd = (HWND)pParent->m_nHandle;
EnumChildWindows(hWnd,EnumWindowsProc,(LPARAM)pParent);
CWndSet *pSetBrother = pParent->m_pSon;
CWndSet *pSetSon = NULL;
while (pSetBrother)
{
// 外循环查找“兄弟窗体”
hWnd = (HWND)pSetBrother->m_nHandle;
m_pLast = NULL;
EnumChildWindows(hWnd,EnumWindowsProc,(LPARAM)pSetBrother);
pSetSon = pSetBrother->m_pSon;
while (pSetSon)
{
// 内循环递归查找下一级“子窗体集”
EnumChildWnd(pSetSon);
pSetSon = pSetSon->m_pBrother;
}
pSetBrother = pSetBrother->m_pBrother;
}
}
BOOL CALLBACK CEnumWndsDlg::EnumWindowsProc(HWND hWnd, LPARAM lpParam)
{
// lpParam 指向窗体hWnd的父窗体
CWndSet *pSet = (CWndSet *)lpParam;
// 获得窗体的名字,长度值预设为188已足够
char chTitle[188];
::GetWindowText(hWnd,(LPTSTR)chTitle,188);
if (m_pLast == NULL)
{
// 枚举到的是第一个子窗体
pSet->m_pSon = new CWndSet;
ASSERT(pSet->m_pSon);
pSet->m_pSon->m_nHandle = (UINT)hWnd;
pSet->m_pSon->m_strTitle = chTitle;
pSet->m_pSon->m_pSon = NULL;
m_pLast = pSet->m_pSon;
}
else
{
// 枚举到随后的窗体
m_pLast->m_pBrother = new CWndSet;
ASSERT(m_pLast->m_pBrother);
m_pLast->m_pBrother->m_nHandle = (UINT)hWnd;
m_pLast->m_pBrother->m_strTitle = chTitle;
m_pLast->m_pBrother->m_pSon = NULL;
m_pLast = m_pLast->m_pBrother;
}
m_pLast->m_pSon = NULL;
m_pLast->m_pBrother = NULL;
return TRUE;
}
void CEnumWndsDlg::Free(CWndSet *pNode)
{
if (pNode == NULL)
return;
Free(pNode->m_pSon);
Free(pNode->m_pBrother);
delete pNode;
}
void CEnumWndsDlg::AddNode(CWndSet *pSet,HTREEITEM hItem)
{
if (pSet == NULL)
return;
// 加入本结点
CString strShow;
strShow.Format("%s 0x%x",pSet->m_strTitle,pSet->m_nHandle);
HTREEITEM hParentItem = m_treeWnd.InsertItem(strShow,0,0,hItem);
// 加入下一级结点
CWndSet *pSon = pSet->m_pSon;
while (pSon)
{
AddNode(pSon,hParentItem);
pSon = pSon->m_pBrother;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -