文章目录
- 界面设置
- 初始化
- 主对话框
- 子页面初始化
- 页面1枚举窗口
- 页面2枚举进程
- 全部代码
界面设置
ui 设置
>创建CTablCtrl
> 创建页控件(子窗口),style设置成为chlid
添加类
页面中加入listCtrl 控件
添加变量
分别添加初始化函数
初始化
主对话框
BOOL CManagerDlg::OnInitDialog()
{CDialogEx::OnInitDialog();// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标//插入m_table.InsertItem(0, L"窗口");m_table.InsertItem(0, L"进程");m_page1.Create(IDD_PAGE1_DLG,&m_table);//创建页m_page2.Create(IDD_PAGE2_DLG,&m_table);//调整位置CRect rect;m_table.GetClientRect(&rect);rect.top = 22;rect.left = 2;rect.right -= 2;rect.bottom -= 3;m_page1.MoveWindow(&rect);m_page2.MoveWindow(&rect);m_page1.ShowWindow(SW_SHOW);m_page2.ShowWindow(SW_HIDE);return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
设置样式选择改变
切换窗口
void CManagerDlg::OnSelchangeTab(NMHDR *pNMHDR, LRESULT *pResult)
{int nSel=m_table.GetCurSel();//0选择成功1选择失败switch (nSel){case 0:m_page1.ShowWindow(SW_SHOW);m_page2.ShowWindow(SW_HIDE);break;case 1:m_page1.ShowWindow(SW_HIDE);m_page2.ShowWindow(SW_SHOW);break;}*pResult = 0;
}
改变页面大小
void CManagerDlg::OnSize(UINT nType, int cx, int cy)
{CDialogEx::OnSize(nType, cx, cy);if (m_table.GetSafeHwnd()){m_table.MoveWindow(15, 12, cx-30, cy-24);CRect rect;m_table.GetClientRect(&rect);rect.top = 22;rect.left = 2;rect.right -= 2;rect.bottom -= 3;m_page1.MoveWindow(&rect);m_page2.MoveWindow(&rect);}
}
子页面初始化
页面1
BOOL CPage1Dlg::OnInitDialog()
{CDialogEx::OnInitDialog();m_list.InsertColumn(0, L"窗口名", LVCFMT_LEFT, 120);m_list.InsertColumn(0, L"状态", LVCFMT_LEFT, 120);return TRUE; // return TRUE unless you set the focus to a control// 异常: OCX 属性页应返回 FALSE
}
页面2
BOOL CPage2Dlg::OnInitDialog()
{CDialogEx::OnInitDialog();m_list.InsertColumn(0, L"进程名", LVCFMT_LEFT, 120);m_list.InsertColumn(0, L"PID", LVCFMT_LEFT, 120);return TRUE; // return TRUE unless you set the focus to a control// 异常: OCX 属性页应返回 FALSE
}
改变页面大小
void CPage2Dlg::OnSize(UINT nType, int cx, int cy)
{CDialogEx::OnSize(nType, cx, cy);if (m_list.GetSafeHwnd())//判断窗口是否存在{m_list.MoveWindow(0, 0, cx, cy);}
}
页面1枚举窗口
void CPage1Dlg::EnunWindows()
{CWnd* pWnd = GetParent()->GetParent();//CWnd* pWnd=GetDesktopWindow();//获取桌面pWnd=pWnd->GetWindow(GW_HWNDFIRST);int i = 0;while (pWnd){//获取标题CString str;pWnd->GetWindowText(str);m_list.InsertItem(i,str);pWnd=pWnd->GetWindow(GW_HWNDNEXT);i++;}
}
页面2枚举进程
void CPage2Dlg::EnumProcess()
{//创建进程快照HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//返回进程列表PROCESSENTRY32 pe32{ sizeof(PROCESSENTRY32) };//枚举驻留的进程BOOL b = Process32First(hSnapshot, &pe32);int i = 0;while (b){m_list.InsertItem(i, pe32.szExeFile);CString str;str.Format(L"%d", pe32.th32ParentProcessID);m_list.SetItemText(i, 1, str);b = Process32Next(hSnapshot, &pe32);//获取下一个进程i++;}
}
全部代码
// ManagerDlg.h: 头文件
//#pragma once
#include "CPage1Dlg.h"
#include "CPage2Dlg.h"// CManagerDlg 对话框
class CManagerDlg : public CDialogEx
{
// 构造
public:CManagerDlg(CWnd* pParent = nullptr); // 标准构造函数// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_MANAGER_DIALOG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持// 实现
protected:HICON m_hIcon;// 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()
public:CTabCtrl m_table;CPage1Dlg m_page1;CPage2Dlg m_page2;afx_msg void OnSelchangeTab(NMHDR *pNMHDR, LRESULT *pResult);afx_msg void OnSize(UINT nType, int cx, int cy);
};
// ManagerDlg.cpp: 实现文件
//#include "pch.h"
#include "framework.h"
#include "Manager.h"
#include "ManagerDlg.h"
#include "afxdialogex.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// CManagerDlg 对话框CManagerDlg::CManagerDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_MANAGER_DIALOG, pParent)
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CManagerDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_TAB1, m_table);
}BEGIN_MESSAGE_MAP(CManagerDlg, CDialogEx)ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CManagerDlg::OnSelchangeTab)ON_WM_SIZE()
END_MESSAGE_MAP()// CManagerDlg 消息处理程序BOOL CManagerDlg::OnInitDialog()
{CDialogEx::OnInitDialog();// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标//插入m_table.InsertItem(0, L"窗口");m_table.InsertItem(0, L"进程");m_page1.Create(IDD_PAGE1_DLG,&m_table);//创建页m_page2.Create(IDD_PAGE2_DLG,&m_table);//调整位置CRect rect;m_table.GetClientRect(&rect);rect.top = 22;rect.left = 2;rect.right -= 2;rect.bottom -= 3;m_page1.MoveWindow(&rect);m_page2.MoveWindow(&rect);m_page1.ShowWindow(SW_SHOW);m_page2.ShowWindow(SW_HIDE);return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。void CManagerDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作区矩形中居中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;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}
}//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CManagerDlg::OnQueryDragIcon()
{return static_cast<HCURSOR>(m_hIcon);
}void CManagerDlg::OnSelchangeTab(NMHDR *pNMHDR, LRESULT *pResult)
{int nSel=m_table.GetCurSel();//0选择成功1选择失败switch (nSel){case 0:m_page1.ShowWindow(SW_SHOW);m_page2.ShowWindow(SW_HIDE);break;case 1:m_page1.ShowWindow(SW_HIDE);m_page2.ShowWindow(SW_SHOW);break;}*pResult = 0;
}void CManagerDlg::OnSize(UINT nType, int cx, int cy)
{CDialogEx::OnSize(nType, cx, cy);if (m_table.GetSafeHwnd()){m_table.MoveWindow(15, 12, cx-30, cy-24);CRect rect;m_table.GetClientRect(&rect);rect.top = 22;rect.left = 2;rect.right -= 2;rect.bottom -= 3;m_page1.MoveWindow(&rect);m_page2.MoveWindow(&rect);}
}
// CPage1Dlg 对话框
class CPage1Dlg : public CDialogEx
{DECLARE_DYNAMIC(CPage1Dlg)public:CPage1Dlg(CWnd* pParent = nullptr); // 标准构造函数virtual ~CPage1Dlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_PAGE1_DLG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CListCtrl m_list;virtual BOOL OnInitDialog();afx_msg void OnSize(UINT nType, int cx, int cy);void EnunWindows();
};
// CPage1Dlg.cpp: 实现文件
//#include "pch.h"
#include "Manager.h"
#include "CPage1Dlg.h"
#include "afxdialogex.h"// CPage1Dlg 对话框IMPLEMENT_DYNAMIC(CPage1Dlg, CDialogEx)CPage1Dlg::CPage1Dlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_PAGE1_DLG, pParent)
{}CPage1Dlg::~CPage1Dlg()
{
}void CPage1Dlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, m_list);
}BEGIN_MESSAGE_MAP(CPage1Dlg, CDialogEx)ON_WM_SIZE()
END_MESSAGE_MAP()// CPage1Dlg 消息处理程序BOOL CPage1Dlg::OnInitDialog()
{CDialogEx::OnInitDialog();m_list.InsertColumn(0, L"窗口名", LVCFMT_LEFT, 120);m_list.InsertColumn(0, L"状态", LVCFMT_LEFT, 120);EnunWindows();return TRUE; // return TRUE unless you set the focus to a control// 异常: OCX 属性页应返回 FALSE
}void CPage1Dlg::OnSize(UINT nType, int cx, int cy)
{CDialogEx::OnSize(nType, cx, cy);if (m_list.GetSafeHwnd())//判断窗口是否存在{m_list.MoveWindow(0, 0, cx, cy);}
}void CPage1Dlg::EnunWindows()
{CWnd* pWnd = GetParent()->GetParent();//CWnd* pWnd=GetDesktopWindow();//获取桌面pWnd=pWnd->GetWindow(GW_HWNDFIRST);int i = 0;while (pWnd){//获取标题CString str;pWnd->GetWindowText(str);m_list.InsertItem(i,str);pWnd=pWnd->GetWindow(GW_HWNDNEXT);i++;}
}
// CPage2Dlg 对话框
class CPage2Dlg : public CDialogEx
{DECLARE_DYNAMIC(CPage2Dlg)public:CPage2Dlg(CWnd* pParent = nullptr); // 标准构造函数virtual ~CPage2Dlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_PAGE2_DLG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CListCtrl m_list;virtual BOOL OnInitDialog();afx_msg void OnSize(UINT nType, int cx, int cy);void EnumProcess();
};
// CPage2Dlg.cpp: 实现文件
//#include "pch.h"
#include "Manager.h"
#include "CPage2Dlg.h"
#include "afxdialogex.h"
#include "tlhelp32.h"// CPage2Dlg 对话框IMPLEMENT_DYNAMIC(CPage2Dlg, CDialogEx)CPage2Dlg::CPage2Dlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_PAGE2_DLG, pParent)
{}CPage2Dlg::~CPage2Dlg()
{
}void CPage2Dlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, m_list);
}BEGIN_MESSAGE_MAP(CPage2Dlg, CDialogEx)ON_WM_SIZE()
END_MESSAGE_MAP()// CPage2Dlg 消息处理程序BOOL CPage2Dlg::OnInitDialog()
{CDialogEx::OnInitDialog();m_list.InsertColumn(0, L"进程名", LVCFMT_LEFT, 120);m_list.InsertColumn(0, L"PID", LVCFMT_LEFT, 120);EnumProcess();return TRUE; // return TRUE unless you set the focus to a control// 异常: OCX 属性页应返回 FALSE
}void CPage2Dlg::OnSize(UINT nType, int cx, int cy)
{CDialogEx::OnSize(nType, cx, cy);if (m_list.GetSafeHwnd())//判断窗口是否存在{m_list.MoveWindow(0, 0, cx, cy);}
}void CPage2Dlg::EnumProcess()
{//创建进程快照HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//返回进程列表PROCESSENTRY32 pe32{ sizeof(PROCESSENTRY32) };//枚举驻留的进程BOOL b = Process32First(hSnapshot, &pe32);int i = 0;while (b){m_list.InsertItem(i, pe32.szExeFile);CString str;str.Format(L"%d", pe32.th32ParentProcessID);m_list.SetItemText(i, 1, str);b = Process32Next(hSnapshot, &pe32);//获取下一个进程i++;}
}