メモ帳で職場のスクリーンセーバーを無効化する

職場のパソコンというものは、通常何らかのセキュリティが講じられています。例えば小職が前にいた会社では、5分おきに強制的にスクリーンセーバーがかかってしまい、ちょっと目を離した隙にPCがロックされる面倒が発生していました。理由があってのセキュリティではあるのは間違いないですが、それも度が過ぎると非効率の根源です。開発環境を自由に入れられない職場ですが、C#コンパイラWindows標準でついてくることが分かりましたので、暇つぶしにスクリーンセーバーを起動させないexeを調べながら作ってみました。

コンパイルのやりかた
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:winexe C:\temp\ScreenSaverKiller.cs


ScreenSaverKiller.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Button button1;
        private System.ComponentModel.IContainer components;

        public Form1()
        {
            InitializeComponent();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }


        #region Windows Form Designer
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();

            this.label1.Font = new System.Drawing.Font("MS UI Gothic", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
            this.label1.Location = new System.Drawing.Point(16, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(384, 64);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            this.timer1.Enabled = true;
            this.timer1.Interval = 10;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

            this.button1.Location = new System.Drawing.Point(109, 96);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(192, 32);
            this.button1.TabIndex = 1;
            this.button1.Text = "Move cursor to the center";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
            this.ClientSize = new System.Drawing.Size(410, 144);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "Form1";
            this.Text = "ScreenSaverKiller";
            this.ResumeLayout(false);
        }
        #endregion

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            Point pos = Cursor.Position;
            //Cursor.Position = new Point(pos.X + 1, pos.Y + 1);
            label1.Text = Control.MousePosition.ToString();
            PreventScreenSaverFromStarting();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            int centerX = Screen.PrimaryScreen.Bounds.Right / 2;
            int centerY = Screen.PrimaryScreen.Bounds.Bottom / 2;
            Cursor.Position = new Point(centerX, centerY);
        }

        private void PreventScreenSaverFromStarting()
        {
            INPUT input = new INPUT();
            input.type = INPUT_MOUSE;
            input.mi = new MOUSEINPUT();
            input.mi.dwExtraInfo = IntPtr.Zero;
            input.mi.dx = 0;
            input.mi.dy = 0;
            input.mi.time = 0;
            input.mi.mouseData = 0;
            input.mi.dwFlags = 0x0001; //Move (Relative)
            int cbSize = Marshal.SizeOf(typeof(INPUT));
            uint r = SendInput(1, ref input, cbSize);
        }


        #region Win32 API
        [StructLayout(LayoutKind.Sequential)]
        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        const int INPUT_MOUSE = 0;

        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            ushort wVk;
            ushort wScan;
            uint dwFlags;
            uint time;
            IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct HARDWAREINPUT
        {
            uint uMsg;
            ushort wParamL;
            ushort wParamH;
        }

        [StructLayout(LayoutKind.Explicit)]
        struct INPUT
        {
            [FieldOffset(0)]
            public int type;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_SYSTEM_REQUIRED = 0x00000001,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_CONTINUOUS = 0x80000000,
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
        #endregion
    }
}

メモ帳でソースコード(ScreenSaverKiller.cs)をコピペし、適当な場所に保存。そしてコマンドプロンプトcscを使ってコンパイルすれば、exeファイルが出来上がります。コンパイルが通らなかったらすみません。私の環境では一応動いてます。これで5分おきにマウスを動かさなくても良いですね!

f:id:tranche:20180702095622p:plain
スクリーンショット

(ご利用条件)当ブログは筆者の個人的見解を述べたものであり、筆者の所属する団体またはその公式見解とは一切関係がありません。当ブログは特定の金融商品の売買を推奨または勧誘またはあっせんするものではありません。当ブログにおいて情報提供の対価として閲覧者から金銭を徴収することはありません。当ブログの内容の正確性に関しては万全を期していますが、筆者は何らその保証を行うものではありません。投資は自己責任です。当ブログの内容をもとにして生じた損害について、筆者は一切の責任を負いません。