C + + Get task bar Open program window sample _c language

Source: Internet
Author: User

Copy Code code as follows:

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);
};

Copy Code code as follows:

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.