File:openprogramontaskbar.h
#pragma once
#include <vector>
struct TaskInfo
{
Program Name
CString Strprogramname;
Window handle
HWND hwnd;
};
Class Taskbarinfo
{
Public
callback function
Static BOOL CALLBACK Enumwindowproc (HWND hwnd, LPARAM LPARAM);
Public
Log a program that opens in the taskbar
Std::vector<taskinfo> M_taskbarinfoarr;
Top-level window handle
HWND m_hwnd;
Public
Taskbarinfo ();
~taskbarinfo ();
Get open program information in the taskbar
void gettaskbarinof (void);
Gets the top-level window handle, failure returns null
HWND gettopwnd (void);
};
File:OpenProgramOnTaskbar.cpp
#include "stdafx.h"///If compile error, please delete this sentence
#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 Process Identifier
GetWindowThreadProcessId (HWnd, &dwpid); accepts a window handle. Dwpid the process identifier of the creator who saved the window, GetWindowThreadProcessId the return value is the thread identifier of the creator
HANDLE Hbrowser = OpenProcess (process_all_access, FALSE, dwpid); Opens an existing process object and returns a handle to the process, which is the process handle we want
WCHAR exepath[256];
memset (exepath, 0, 256);
Gets the path of the program and saves it to the ExePath
Getmodulefilenameex (Hbrowser, NULL, ExePath, MAX_PATH);
CString strtemp = ExePath;
Strtemp.makelower ();
Filtering folders open on the desktop, etc.
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 Process Identifier
HWND hwnd = NULL;
HWnd =:: GetForegroundWindow ();
GetWindowThreadProcessId (HWnd, &dwpid); accepts a window handle. Dwpid the process identifier of the creator who saved the window, GetWindowThreadProcessId the return value is the thread identifier of the creator
HANDLE Hbrowser = OpenProcess (process_all_access, FALSE, dwpid); Opens an existing process object and returns a handle to the process, which is the process handle we want
WCHAR exepath[256];
memset (exepath, 0, 256);
Gets the path of the program and saves it to the 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;
}