Lock the computer and turn off the monitor

Introduction

In the daily use of the computer, sometimes you want the display to be turned off, and the host needs to continue to run. For desktop hosts, you can press the power button of the monitor. There is no other way to close the screen after changing the power mode. This function can be achieved by sending a message to Windows to turn off the display, see the code for details.

source code

/*
 * 版权所有 (C) 2015 dccw (http://blog.51cto.com/13187574)
 *
 * 按照 Apache 许可 2.0 版本(称为“许可”)授予许可;
 * 要使用此文件,必须遵循“许可”中的说明。
 * 你可以从以下位置获取“许可”的副本
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 除非适用法律要求或书面同意,根据
 * “许可”分配的软件“按原样”分配,
 * 不提供任何形式(无论是明示还是默示)的担保和条件。
 * 参见“许可”了解“许可”中管理权限和
 * 限制的指定语言。
 *
 * 本代码实现了关闭显示器和锁定计算的功能
 * 编译命令:cl.exe /D "NDEBUG" 关闭显示器.c /link kernel32.lib user32.lib
 * 使用方法:
 * 命令行执行: 关闭显示器.exe [-?hdl]
 * 快捷方式执行: 新建桌面快捷方式->输入对象位置->打开该快捷方式的属性页面
 * ->在目标的后面加上相应的参数->点击快捷键输入框,按下你想使用的快捷键
 * ->点击确定->焦点位于桌面时按下你设置的快捷键即可运行
 */

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

static int flag_show_usage = 0;
static int flag_lock_workstation = 0;
static int flag_shutdown_monitor = 0;

static void show_usage(char const *argv) {
    fprintf(stderr,"用法: %s [-?hdl]\n\n"
        "选项:\n"
        "   -?,-h       : 帮助\n"
        "   -d          : 关闭显示器\n"
        "   -l          : 锁定计算机\n",
        argv
        );
}

static int get_option(int argc, char *const *argv) {
    char     *p;
    int      i;

    for (i = 1; i < argc; i++) {
        p = argv[i];

        if (*p++ != '-') {
            fprintf(stderr,"无效的选项: \"%s\"", argv[i]);
            return -1;
        }

        while (*p) {
            switch (*p++) {
                case '?':
                case 'h':
                    flag_show_usage = 1;
                    break;
                case 'd':
                    flag_shutdown_monitor = 1;
                    break;
                case 'l':
                    flag_lock_workstation = 1;
                    break;
                default:
                    fprintf(stderr,"无效的选项: \"%c\"", *(p - 1));
                    return -1;
            }
        }
    }
    return 0;
}

static void lock_workstation(void) {
    if( !LockWorkStation() ) {
        _tprintf(_T("锁定计算机失败 %d\n"), GetLastError());
    }
}

static void shutdown_monitor(void) {
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
}

int main(int argc, char **argv) {
    if (get_option(argc, argv) != 0) {
        return 1;
    }

    if (flag_show_usage) {
        show_usage(argv[0]);
    }

    if (flag_lock_workstation) {
        lock_workstation();
    }

    if (flag_shutdown_monitor) {
        shutdown_monitor();
    }

    return 0;
}

Compile method

cl.exe /D "NDEBUG" 关闭显示器.c /link kernel32.lib user32.lib

Instructions

  • Command line execution: close monitor.exe [-?hdl]
  • Shortcut execution: Create a new desktop shortcut -> enter the object location -> open the property page of the shortcut -> add the corresponding parameters after the target -> click the shortcut key input box, press the shortcut key you want to use - >Click OK->Press the shortcut key you set to run when the focus is on the desktop

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324788262&siteId=291194637