欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

多显示器下判断ppt是否全屏播放

程序员文章站 2022-03-07 20:14:25
#include #include #include #include #include #include #include #include bool CheckSame(DWORD dwProcessID, int niX, int....
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <iostream>
#include <TlHelp32.h>
#include <vector>
#include <algorithm>

std::string GetModuleName(DWORD dwPid);

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,
	HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
	static BOOL first = TRUE;   //标志
	std::vector<RECT> * pRect = (std::vector<RECT>*)dwData;
	//保存显示器信息
	MONITORINFO monitorinfo;
	monitorinfo.cbSize = sizeof(MONITORINFO);

	//获得显示器信息,将信息保存到monitorinfo中
	GetMonitorInfo(hMonitor, &monitorinfo);
	//若检测到主屏
	if (monitorinfo.dwFlags == MONITORINFOF_PRIMARY)
	{
		if (first)  //第一次检测到主屏
		{
			first = FALSE;
			pRect->push_back(monitorinfo.rcMonitor);
		}
		else //第二次检测到主屏,说明所有的监视器都已经检测了一遍,故可以停止检测了
		{
			first = TRUE;    //标志复位
			return FALSE;    //结束检测
		}
	}
	else
	{
		pRect->push_back(monitorinfo.rcMonitor);
	}
	first = TRUE;     // 恢复主屏标记为初始状态
	return TRUE;
}


//根据屏幕上指定的坐标点,获取坐标点窗口对应的pid
DWORD GetPidByPoint(int nX, int nY)
{
	bool bRet = false;

	POINT lpt = { nX,nY };
	HWND hwnd = (HWND)WindowFromPoint(lpt);
	HWND lHdesktop = (HWND)GetDesktopWindow();
	HWND lHparent = 0;

	while (1)
	{
		// 查找窗口的主窗口  
		lHparent = ::GetParent(hwnd);
		if (lHparent == lHdesktop || lHparent == 0)
			break;
		hwnd = lHparent;
	}

	DWORD dwPid = 0;
	GetWindowThreadProcessId(hwnd, &dwPid);
	return dwPid;
}

//查找全屏的应用窗口
BOOL FindFullScreenWindow(DWORD &dwProcessID, std::string &strProcessName, HWND &hWnd)
{
	// 检测显示器数量
	std::vector<RECT> vRect;
	EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&vRect);  // 枚举所有显示器的Rect


	/*
   这个函数获取屏幕4角的窗口的进程句柄,判断与激活句柄是否相等的方式来判断是否全屏程序。
   特别的,对 IE 之类的多标签多进程程序,子窗口的进程会和主窗口不同。需要获取窗口的主窗口来对比才可以
   */
	//bool lbRet = false;
	//HWND lHforewnd = (HWND)0x430a4a;// ::GetForegroundWindow();
	//DWORD lWDProcessID;
	//GetWindowThreadProcessId(lHforewnd, &lWDProcessID);

	std::vector<RECT>::iterator itor = vRect.begin();
	for (; itor != vRect.end(); itor++)
	{
		int nLeftPoint = itor->left;
		int nTopPoint = itor->top;
		int nRightPoint = itor->right;
		int nBottomPoint = itor->bottom;

		//左上
		DWORD dwLTPid = GetPidByPoint(nLeftPoint, nTopPoint);

		//右下
		DWORD dwRBPid = GetPidByPoint(nRightPoint - 1, nBottomPoint - 1);

		//找到全屏应用
		if (dwLTPid == dwRBPid)
		{
			dwProcessID = dwLTPid;
			strProcessName = GetModuleName(dwProcessID);
			POINT lpt = { nLeftPoint, nTopPoint };
			hWnd = (HWND)WindowFromPoint(lpt);
			return true;
		}
	}

	return false;
}

bool IsPptPlaying()
{
	DWORD dwProcessID = 0;
	std::string strProcessName;
	HWND hWnd = NULL;
	BOOL bRst = FindFullScreenWindow(dwProcessID, strProcessName, hWnd);
	if (bRst)
	{
		std::string strDst;
		//转换成小写
		std::transform(strProcessName.begin(), strProcessName.end(), std::back_inserter(strDst), ::tolower);

		if (strDst == _T("wpp.exe") || strDst == _T("powerpnt.exe"))
		{
			printf("%s is playing ...\n", strDst.c_str());
			return true;
		}
	}

	printf("nothing is playing ...\n");
	return false;
}

//根据进程id获得进程名
std::string GetModuleName(DWORD dwPid)
{
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, dwPid);
	if (INVALID_HANDLE_VALUE == hSnapshot)
	{
		return "";
	}
	PROCESSENTRY32 pe = { sizeof(pe) }; //存放进程快照信息的结构体
	BOOL ret = Process32First(hSnapshot, &pe); //获得第一个进程的信息
	//遍历
	while (ret)
	{
		if (dwPid == pe.th32ProcessID)
		{
			CloseHandle(hSnapshot);
			return std::string(pe.szExeFile);
		}
		ret = Process32Next(hSnapshot, &pe); //接着往下遍历
	}
	return "";
}

int main(int argc, _TCHAR* argv[])
{
	bool bPlaying = IsPptPlaying();
	if (bPlaying)
	{
		printf("ppt is playing \n");
	}
	else
	{
		printf("ppt not playing \n");
	}

	getchar();

	return 0;
}

 

本文地址:https://blog.csdn.net/zhuxian2009/article/details/107261853