办法一:
C++封闭桌面,explorer.exe
#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++ 一键封闭屏幕
#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
下面先贴源代码:
#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;
}
新程序新增了躲藏任务栏的功能。