控制浏览器显示隐藏c++

#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <string>
#include <stdio.h>

//windows置顶窗体终极方案
LRESULT OnForceShow(HWND hWnd)
{
    HWND hForeWnd = NULL;
    DWORD dwForeID = 0;
    DWORD dwCurID = 0;

    hForeWnd = ::GetForegroundWindow();
    dwCurID = ::GetCurrentThreadId();
    dwForeID = ::GetWindowThreadProcessId(hForeWnd, NULL);
    ::AttachThreadInput(dwCurID, dwForeID, TRUE);
    ::ShowWindow(hWnd, SW_SHOWNORMAL);
    ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    ::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    ::SetForegroundWindow(hWnd);
    ::AttachThreadInput(dwCurID, dwForeID, FALSE);

    return S_OK;
}

///< 通过进程ID获取窗口句柄
HWND GetWindowHwndByPorcessID(DWORD dwProcessID)
{
    DWORD dwPID = 0;
    HWND hwndRet = NULL;
    // 取得第一个窗口句柄
    HWND hwndWindow = ::GetTopWindow(0);
    while (hwndWindow)
    {
        dwPID = 0;
        // 通过窗口句柄取得进程ID
        DWORD dwTheardID = ::GetWindowThreadProcessId(hwndWindow, &dwPID);
        if (dwTheardID != 0)
        {
            // 判断和参数传入的进程ID是否相等
            if (dwPID == dwProcessID)
            {
                // 进程ID相等,则记录窗口句柄
                hwndRet = hwndWindow;
                break;
            }
        }
        // 取得下一个窗口句柄
        hwndWindow = ::GetNextWindow(hwndWindow, GW_HWNDNEXT);
    }
    // 上面取得的窗口,不一定是最上层的窗口,需要通过GetParent获取最顶层窗口
    HWND hwndWindowParent = NULL;
    // 循环查找父窗口,以便保证返回的句柄是最顶层的窗口句柄
    while (hwndRet != NULL)
    {
        hwndWindowParent = ::GetParent(hwndRet);
        if (hwndWindowParent == NULL)
        {
            break;
        }
        hwndRet = hwndWindowParent;
    }
    // 返回窗口句柄
    return hwndRet;
}

DWORD GetSesrvProcessId(const std::wstring& se_path,bool show) {
    HANDLE hProcessSnap;
    HANDLE hProcess;
    PROCESSENTRY32 pe32;
    // Take a snapshot of all processes in the system.
    hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE) {
        return -1;
    }

    // Set the size of the structure before using it.
    pe32.dwSize = sizeof(PROCESSENTRY32);

    // Retrieve information about the first process,
    // and exit if unsuccessful
    if (!::Process32First(hProcessSnap, &pe32)) {
        
        ::CloseHandle(hProcessSnap);  // clean the snapshot object
        return -1;
    }

    // Now walk the snapshot of processes, and
    // display information about each process in turn
    do {
        hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
        if (hProcess == NULL) {
            continue;
        }
            if (_wcsnicmp(pe32.szExeFile, se_path.c_str(), se_path.length()) == 0) {
               std::wcout << "kill by Process name  PID[" << pe32.th32ProcessID
                    << "] Processname: " << pe32.szExeFile <<"\n";
               HWND hwnd = GetWindowHwndByPorcessID(pe32.th32ProcessID);
               if (hwnd) {
                   ShowWindow(hwnd, show ? SW_SHOW :SW_HIDE);
                   if (show) {
                       OnForceShow(hwnd);
                   }
               }
                //return pe32.th32ProcessID;
            }
        

    } while (::Process32Next(hProcessSnap, &pe32));

    ::CloseHandle(hProcessSnap);
    return -1;
}

int main()
{

    GetSesrvProcessId(L"msedge.exe",false); //隐藏edge
    Sleep(3000);
    GetSesrvProcessId(L"msedge.exe",true); //显示edge
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值