C++ gets an example of the taskbar opening a program window
- 2020-04-02 02:01:35
- OfStack
// File: OpenProgramOnTaskbar.h
#pragma once
#include <vector>
struct TaskInfo
{
//The program name
CString strProgramName;
//Handle to the window
HWND hWnd;
};
class TaskbarInfo
{
public:
//The callback function
static BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam);
public:
//Record the programs that open in the task bar
std::vector<TaskInfo> m_TaskbarInfoArr;
//Top window handle
HWND m_hWnd;
public:
TaskbarInfo();
~TaskbarInfo();
//Gets program information to open in the taskbar
void GetTaskbarInof (void);
//Gets the top window handle, returns NULL on failure
HWND GetTopWnd (void);
};
// File: OpenProgramOnTaskbar.cpp
#include "stdafx.h" //Delete this sentence if there is a compilation error
#include "OpenProgramOnTaskbar.h"
#include "Psapi.h"
#pragma comment(lib, "Psapi.lib")
TaskbarInfo::TaskbarInfo()
{
}
TaskbarInfo::~TaskbarInfo()
{
}
void TaskbarInfo::GetTaskbarInof (void)
{
EnumWindows(EnumWindowProc, (LPARAM)this);
}
BOOL CALLBACK TaskbarInfo::EnumWindowProc(HWND hWnd, LPARAM lParam)
{
TaskbarInfo * pTaskbarInfo = (TaskbarInfo*)lParam;
TCHAR szWindow[256]={0};
TaskInfo taskInfo;
::GetWindowText(hWnd, szWindow, 255); //Get window title
if ( ::IsWindow(hWnd) &&
::IsWindowVisible(hWnd) &&
(::GetWindowLong(hWnd, GWL_EXSTYLE)&WS_EX_TOOLWINDOW)!=WS_EX_TOOLWINDOW &&
::GetWindowLong(hWnd, GWL_HWNDPARENT)==0 )
{
DWORD dwPID; //Save the process identifier
GetWindowThreadProcessId(hWnd, &dwPID); //Accepts a window handle. DwPID saves the process identifier for the creator of the window, and the GetWindowThreadProcessId return value is the thread identifier for that creator
HANDLE hBrowser = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID); //Open an existing process object and return a handle to the process. This is the process handle
WCHAR exePath[256];
memset(exePath, 0, 256);
//Gets the program's path and saves it to exePath
GetModuleFileNameEx(hBrowser, NULL, exePath, MAX_PATH);
CString strTemp = exePath;
strTemp.MakeLower();
//Filter folders open on the desktop and so on
if (!strTemp.IsEmpty() && strTemp.Find(_T("explorer.exe")) == -1)
{
taskInfo.strProgramName = szWindow;
taskInfo.hWnd = hWnd;
pTaskbarInfo->m_TaskbarInfoArr.push_back(taskInfo);
memset(szWindow, 0, 256);
}
}
return TRUE;
}
HWND TaskbarInfo::GetTopWnd (void)
{
DWORD dwPID; //Save the process identifier
HWND hWnd = NULL;
hWnd = ::GetForegroundWindow();
GetWindowThreadProcessId(hWnd, &dwPID); //Accepts a window handle. DwPID saves the process identifier for the creator of the window, and the GetWindowThreadProcessId return value is the thread identifier for that creator
HANDLE hBrowser = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID); //Open an existing process object and return a handle to the process. This is the process handle
WCHAR exePath[256];
memset(exePath, 0, 256);
//Gets the program's path and saves it to exePath
GetModuleFileNameEx(hBrowser, NULL, exePath, MAX_PATH);
CString strTemp = exePath;
strTemp.MakeLower();
if (!strTemp.IsEmpty() && strTemp.Find(_T("explorer.exe")) == -1)
{
m_hWnd = hWnd;
if (m_hWnd != NULL)
{
return m_hWnd;
}
}
return NULL;
}