MFC 任务管理器设计

在学习界面设计,因为对一些控件不熟悉,所以也参考了别人的。核心代码如下。

 

 

 

void CTasklistDlg::Expand()
{
    m_list.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
    m_list.InsertColumn(0,L"Process Name",0,100);
    m_list.InsertColumn(1,L"Pid",0,100);
    m_list.InsertColumn(2,L"Path",0,380);
}

void CTasklistDlg::OnNMRClickList(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    // TODO: Add your control notification handler code here
    CMenu    popup;
    popup.LoadMenu(IDR_MENU1);
    CMenu*    pM = popup.GetSubMenu(0);
    CPoint    p;
    GetCursorPos(&p);
    int    count = pM->GetMenuItemCount();
    if (m_list.GetSelectedCount() == 0)       //如果没有选中
    { 
        for (int i = 0; i < count; i++)
        {
            pM->EnableMenuItem(i, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);          //菜单全部变灰
        }

    }
    pM->TrackPopupMenu(TPM_LEFTALIGN, p.x, p.y, this);

    *pResult = 0;
}

#include<Tlhelp32.h>
#include<Psapi.h>
#pragma comment(lib,"Psapi.lib")

BOOL CTasklistDlg::GetProcessList()
{
    BOOL result = FALSE;
    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnap == INVALID_HANDLE_VALUE)
        return FALSE;

    int idx = 0;
    CString strID;
    HANDLE hProcess = NULL;
    CString strPath, strSys(_T("SystemRoot"));

    PROCESSENTRY32 info = { 0 };
    info.dwSize = sizeof(PROCESSENTRY32);

    BOOL bRet = Process32First(hSnap,&info);

    while (bRet)
    {
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, info.th32ProcessID);
        if (hProcess)
        {
            strPath.Empty();
            DWORD dwRet = GetModuleFileNameEx(hProcess, NULL, strPath.GetBufferSetLength(MAX_PATH), MAX_PATH);
            strPath.ReleaseBuffer();
            strPath.TrimLeft(_T("\\?")); 
            if (strPath.Left(strSys.GetLength()).CompareNoCase(strSys) == 0)
            {
                TCHAR szWin[MAX_PATH] = { 0 };
                GetWindowsDirectory(szWin,MAX_PATH);
                strPath.Replace(strSys,szWin);

            }
            SHFILEINFO shInfo = { 0 };
            SHGetFileInfo(strPath, 0, &shInfo, sizeof(SHFILEINFO), SHGFI_SMALLICON | SHGFI_ICON);
        }

        idx = m_list.InsertItem(m_list.GetItemCount(), info.szExeFile);
        strID.Empty();
        strID.Format(_T("%d"), info.th32ProcessID);
        m_list.SetItemText(idx, 1, strID);
        m_list.SetItemText(idx, 2, strPath);

        bRet = Process32Next(hSnap, &info);
    }

    return TRUE;
}

BOOL CTasklistDlg::AdjustPrivileges()
{
    HANDLE hToken = NULL;
    TOKEN_PRIVILEGES tp = { 0 };
    TOKEN_PRIVILEGES oldtp = { 0 };
    DWORD dwSize = sizeof(TOKEN_PRIVILEGES);
    LUID luid = { 0 };

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
        if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
            return TRUE;
        else
            return FALSE;
    }
    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
        CloseHandle(hToken);
        return FALSE;
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    /* Adjust Token Privileges */
    if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &oldtp, &dwSize)) {
        CloseHandle(hToken);
        return FALSE;
    }

    // close handles
    CloseHandle(hToken);
    return TRUE;
}
void CTasklistDlg::OnKillprocess()
{
    // TODO: Add your command handler code here
    //DWORD  ProcessID = 0;
    DWORD dwProcess;
    wchar_t szBuf[MAX_PATH] = {0};
    int index = m_list.GetSelectionMark(); //获取光标所在的位置
    m_list.GetItemText(index, 1, szBuf, MAX_PATH); //获取多少行光标,然后取得数据
    dwProcess = _wtol(szBuf);
    HANDLE hProcess =  OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcess);
    if (NULL == hProcess)
    {
        MessageBox(L"打开那进程句柄失败!");
    }

    if (TerminateProcess(hProcess, 0))
    {
        PROCESSITEM* pProcItem;
        pProcItem = (PROCESSITEM*)m_list.GetItemData(index);
        delete pProcItem;
        m_list.DeleteItem(index);
        MessageBox(L"进程结束成功");
    }
    else
    {
        MessageBox(L"结束进程失败!");
    }
}


void CTasklistDlg::OnInfoExit()
{
    exit(0);
    // TODO: Add your command handler code here
}


void CTasklistDlg::OnInfoRefresh()
{
    // TODO: Add your command handler code here
    m_list.SetRedraw(FALSE);
    m_list.DeleteAllItems();
    GetProcessList();
    m_list.SetRedraw(TRUE);
    m_list.Invalidate();
    m_list.UpdateWindow();
}

 

posted @ 2016-01-09 21:24  杀死比特  阅读(1645)  评论(0编辑  收藏  举报