C++实现一键关闭桌面的示例代码

来自:网络
时间:2023-07-24
阅读:

方法一:

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);
}
返回顶部
顶部