C++实现一键关闭桌面

方法一:

C++关闭桌面,explorer.exe

scss 复制代码
#include<Windows.h>
#include <TlHelp32.h>
#include"resource.h"
#pragma warning(disable:4996)
void taskkill(const char * name)
{
	HANDLE info_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //拍摄系统中所有进程的快照
	if (info_handle == INVALID_HANDLE_VALUE)
	{
		MessageBox(0, TEXT("获取进程句柄失败!"), TEXT("错误"), 0);
		return;
	}

	PROCESSENTRY32W program_info;
	program_info.dwSize = sizeof(PROCESSENTRY32W);  //设置结构体大小
	int bResult = Process32FirstW(info_handle, &program_info); //获取所有进程中第一个进程的信息
	if (!bResult)
	{
		MessageBox(0, TEXT("获取进程句柄失败!"), TEXT("错误"), 0);
		return;
	}
	char tmp[MAX_PATH];
	HANDLE hProcess;
	while (bResult)
	{
		wchar_t *pro_name = program_info.szExeFile;
		wcstombs(tmp, pro_name, MAX_PATH);
		if (!strcmp(tmp, name))
		{
			hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, program_info.th32ProcessID);
			if (hProcess) {
				TerminateProcess(hProcess, 0);
				CloseHandle(hProcess);//OpenProcess打开的也要关闭  
			}
			break;
		}
		//获得下一个进程的进程信息
		bResult = Process32Next(info_handle, &program_info);
	}
	CloseHandle(info_handle);//关闭句柄
}
int WINAPI WinMain(HINSTANCE h1, HINSTANCE h2, LPSTR cmd, int show)
{
	taskkill("explorer.exe");
}

方法二:

C++ 一键关闭屏幕

ini 复制代码
#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WindowProc( HWND hwnd,      // handle to window
                             UINT uMsg,      // message identifier
                             WPARAM wParam,  // first message parameter
                             LPARAM lParam   // second message parameter
                            );

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 延时防止双击exe关闭屏幕后,又打开屏幕。关闭屏幕的消息,先于系统的双击消息;
    Sleep(200);

    ::SendMessage(HWND_BROADCAST, WM_SYSCOMMAND,  SC_MONITORPOWER,  (LPARAM)2);

    static TCHAR szAppName[] = TEXT("HelloWin");

    WNDCLASS wndClass;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WindowProc;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hInstance = hInstance;
    wndClass.hIcon = LoadIcon(hInstance, (char*)IDI_ICON1);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = szAppName;

    if (!RegisterClass(&wndClass))
    {
        MessageBox(NULL, TEXT("注册窗口失败!"), TEXT(""), 0);
        return 0;
    }

    HWND hWnd = CreateWindow(szAppName, TEXT("Hello Program"),
        WS_OVERLAPPEDWINDOW, 0, 0, 0, 0,
        NULL, NULL, hInstance, NULL);

    ::SendMessage(hWnd, WM_DESTROY, 0, NULL);
    
    return 0;
}

LRESULT CALLBACK WindowProc( HWND hwnd,      // handle to window
                            UINT uMsg,      // message identifier
                            WPARAM wParam,  // first message parameter
                            LPARAM lParam   // second message parameter
                            )
{
    HDC  hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (uMsg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

本程序使用C语言编写,调用的Windows API,所以只能在windows上运行

本程序无界面,启动后直接隐藏/显示桌面图标,运行结束后自动退出

操作系统:windows 10

开发工具:Visual Studio 2019

下面先贴源代码:

ini 复制代码
#include <stdio.h>
#include <windows.h>
#include <WinUser.h>
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" ) // 设置入口地址


//遍历全部子窗口的子窗口 , Z序遍历
HWND findChildWindowHandleByName(HWND parent, int level, TCHAR* winNameBuf)
{
	HWND child = NULL;
	HWND hWnd = NULL;
	TCHAR buf[MAX_PATH];
	DWORD pid = 0, tid = 0;
	do {
		child = FindWindowEx(parent, child, NULL, NULL);
		int ret = GetWindowText(child, buf, MAX_PATH);
		buf[ret] = 0;
		tid = GetWindowThreadProcessId(child, &pid);
		for (int i = 0; i < level; ++i)
			printf("\t");
		printf("%s ,  pid:%d, tid:%d\n", buf, pid, tid);
		if (memcmp(winNameBuf, buf, sizeof(winNameBuf)) == 0)
		{
			hWnd = child;
			return hWnd;
		}
		if (child)
			hWnd = findChildWindowHandleByName(child, level + 1, winNameBuf);
		if (hWnd != NULL)
		{
			return hWnd;
		}
	} while (child);
	return NULL;
}

//遍历全部 explore 下的窗口 , Z序遍历
HWND findWindowHandleByName(TCHAR* winNameBuf)
{
	HWND child = NULL;
	HWND hWnd = NULL;
	TCHAR buf[MAX_PATH];
	DWORD pid = 0, tid = 0;

	do {
		//查找 Explore 下的一个窗口,若是能找到则根据 Explore 下的child 继续找
		child = FindWindowEx(NULL, child, NULL, NULL);
		int ret = GetWindowText(child, buf, MAX_PATH);
		buf[ret] = 0;
		tid = GetWindowThreadProcessId(child, &pid);
		printf("%s ,  pid:%d, tid:%d\n", buf, pid, tid);
		if (memcmp(winNameBuf, buf, sizeof(winNameBuf)) == 0)
		{
			hWnd = child;
			return hWnd;
		}
		//遍历子窗口们
		if (child)
			hWnd = findChildWindowHandleByName(child, 1, winNameBuf);
		if (hWnd != NULL)
		{
			return hWnd;
		}
	} while (child);
	return NULL;
}

int main(void)
{
	HWND hWnd = NULL;
	HWND hWnd2 = NULL;
	TCHAR desktopNameBuf[] = { L"FolderView" };		// 桌面句柄名

	// 遍历桌面句柄
	hWnd = findWindowHandleByName(desktopNameBuf);
	if (hWnd != NULL)
	{
		// 切换显示与隐藏桌面
		if (IsWindowVisible(hWnd))
		{
			ShowWindow(hWnd, SW_HIDE);
		}
		else
		{
			ShowWindow(hWnd, SW_SHOW);
		}

		// 模拟按键 Win+D
		//Sleep(100);
		//keybd_event(VK_LWIN, 0, 0, 0);
		//keybd_event('D', 0, 0, 0);
		//Sleep(100);
		//keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
		//keybd_event('D', 0, KEYEVENTF_KEYUP, 0);
	}

	// 根据任务栏类名寻找句柄
	hWnd = FindWindowEx(NULL, NULL, L"Shell_TrayWnd", NULL);
	if (hWnd != NULL)
	{
		// 寻找是否有第二个屏幕的任务栏
		hWnd2 = FindWindowEx(NULL, NULL, L"Shell_SecondaryTrayWnd", NULL);
		if (hWnd2 != NULL)
		{
			// 切换显示与隐藏任务栏
			if (IsWindowVisible(hWnd))
			{
				ShowWindow(hWnd, SW_HIDE);
				ShowWindow(hWnd2, SW_HIDE);		// 第二屏幕任务栏需单独隐藏
			}
			else
			{
				ShowWindow(hWnd, SW_SHOW);		// 显示第一屏幕任务栏后,第二屏幕任务栏会自动显示
			}
		}
	}

	return 0;
}

新程序新增了隐藏任务栏的功能。

相关推荐
FIN技术铺1 小时前
问:数据库的六种锁机制实践总结?
数据库·sql·oracle
程序员劝退师_2 小时前
优惠券秒杀的背后原理
java·数据库
Gauss松鼠会2 小时前
GaussDB全密态数据库等值查询
数据库·oracle·gaussdb
JSUITDLWXL2 小时前
在Oracle数据中更新整个对象和更新对象的某几个字段时,他们的锁是相同的吗
数据库·oracle
杏花春雨江南2 小时前
ddl/dml/dcl
数据库·oracle
Matrix702 小时前
HBase理论_HBase架构组件介绍
大数据·数据库·hbase
不太灵光的程序员2 小时前
【HBase分布式数据库】第七章 数据的导入导出 importtsv导入数据
数据库·分布式·hbase
Mephisto.java2 小时前
【大数据学习 | HBASE高级】region split机制和策略
数据库·学习·hbase
Lucifer三思而后行2 小时前
YashanDB YAC 入门指南与技术详解
数据库·后端
大气层煮月亮2 小时前
python调用MySql详细步骤
数据库·mysql