0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

パソコンに入力されたマウスの位置を表示する【C#】

Posted at

動機

前回、Windows でキーロガーをプロトタイプで作成しましたが、キーロガーできたならマウスロガーもできるのでは、と思ったのがきっかけです。

そこで、今回はマウスロガーを作ってみました。マウスの位置が、作成したデスクトップアプリ上に表示されるようになります。

なお、本アプリは WindowsFormsApp で作成しました。

過去の記事(WpfApp)

参考 1:ノートパソコンの画面の明るさをボタン一つで変更する【C#】
参考 2:ノートパソコンの画面の音量をボタン一つで変更する【C#】
参考 3:ノートパソコンの画面のバッテリー状況をボタン一つで取得する【C#】
参考 4:ノートパソコンの画面の通信料状況をボタン一つで取得する【C#】
参考 5:ノートパソコンで現在使用中のアプリをボタン一つで把握する【C#】
参考 6: ノートパソコンで現在使用中のオーディオ機器情報をボタン一つで取得する【C#】
参考 7:パソコンのユーザー情報取得やアプリ起動・終了、ディスプレイ画面の切替などを、ボタンを作って動かす【C#】

過去の記事(WindowsFormsApp)

参考 1:パソコンに接続しているディスプレイ情報をボタン一つで取得する【C#】
参考 2:パソコンに入力されたキーを表示する【C#】

イメージ画像

今回も UI は特にこだわってはいません。

マウスの一座標が表示されるだけの、シンプルな UI となっております。

mouse_logger.gif

ソース

Form1.Designer.cs

Form1.Designer.cs

namespace WindowsFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("MS UI Gothic", 31.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.label1.Location = new System.Drawing.Point(204, 161);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(152, 54);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("MS UI Gothic", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.label2.Location = new System.Drawing.Point(8, 173);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(208, 40);
            this.label2.TabIndex = 1;
            this.label2.Text = "マウス座標:";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
    }
}

Form1.cs

Form1.cs
using MouseHook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private IntPtr hHook;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetHook();
        }
        private int SetHook()
        {
            IntPtr hmodule = WindowsAPI.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

            hHook = WindowsAPI.SetWindowsHookEx((int)WindowsAPI.HookType.WH_MOUSE_LL, (WindowsAPI.HOOKPROC)MyHookProc, hmodule, IntPtr.Zero);
            if (hHook == null)
            {
                MessageBox.Show("SetWindowsHookEx 失敗", "Error");
                return -1;
            }
            else
            {
                MessageBox.Show("SetWindowsHookEx 成功", "OK");
                return 0;
            }
        }

        private IntPtr MyHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (0 == WindowsAPI.HC_ACTION)
            {
                WindowsAPI.MSLLHOOKSTRUCT MouseHookStruct = (WindowsAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WindowsAPI.MSLLHOOKSTRUCT));
                label1.Text = string.Format("Mouse Position : {0:d}, {1:d}", MouseHookStruct.pt.x, MouseHookStruct.pt.y);
            }

            return WindowsAPI.CallNextHookEx(hHook, nCode, wParam, lParam);

        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            WindowsAPI.UnhookWindowsHookEx(hHook);
        }
    }
}

おわりに

今回はマウスの位置座標を取得するデスクトップを作成しました。

誰かのお役に立てればと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?