LoginSignup
1
3

「C++,C#,Go,Rust」の「Win32のGetCursorPos」機能を使って、パソコン画面のマウス・カーソル座標を取得する比較文(WindowsTerminal環境)

Last updated at Posted at 2023-11-04

はじめに

  • 前回の「WIn32のGetAsyncKeyState」によるキーボード入力編の続きです。
  • 今回の座標は「パソコン画面のマウス・カーソル」の座標です。「WindowsTerminal」内の座標ではないので注意です。
  • ↓前回

各言語「C++,C#,Go,Rust」のプログラムの作成条件

  • Visual studio codeや秀丸でプログラミング。
  • プログラミング中、マウスは適当に動かします。
  • 300ミリ秒ごとにループ(繰り返し)する。
  • ループを一周する毎に、変数「t」の値を「1」増やす。
  • GetCursorPosの座標情報の出力は、変数「pt」に格納する。
  • 絵文字を使いたいのでファイルはutf-8で保存する。
     (C++は日本語や絵文字が文字化けした!)

パソコン環境

  • Windows10 : 22H2
  • WindowsTerminal(プレビュー版) : 1.19.2831.0
  • c++のコンパイラ g++ : 10.3.0
  • C#のコンパイラ csc : 4.8.9037.0
  • Go : 1.21.3
  • Rust : 1. 72.0

プログラミング結果

C++

↓15行・251文字

#include <iostream>
#include <windows.h>

POINT pt;

int main(){
    long t = 0;
	while(1){
		GetCursorPos(&pt);
		std::cout << "time : " << t << " , x:y = " << pt.x <<" : "<< pt.y << std::endl;				
		Sleep(300);
    t++;
	}
	return 0;
}

C++出力結果

image.png

C#

↓22行・569文字

using System;
using System.Threading;
public struct POINT{
	public int x;
	public int y;
}
class hello_GetCursorPos {
	[System.Runtime.InteropServices.DllImport("user32.dll")]
	public static extern bool GetCursorPos(out POINT lpPoint);

	public static void Main() {
		Console.OutputEncoding = System.Text.Encoding.GetEncoding("utf-8");//🐓の文字化け対策
 		int t = 0;
		while(true){
			POINT pt;
            GetCursorPos(out pt);
			Console.WriteLine("C# 時刻:{0} マウスのカーソル座標🐓 x:y= {1}:{2}",t,pt.x,pt.y);
			Thread.Sleep(300);
			t++;
		}
	}
}

C#出力結果

image.png

Go言語

↓30行・483文字

package main

import (
	"fmt"
	"time"
	"unsafe"

	"golang.org/x/sys/windows"
)

var (
	moduser32        = windows.NewLazyDLL("user32.dll")
	procGetCursorPos = moduser32.NewProc("GetCursorPos")
)

type POINT struct {
	X int32
	Y int32
}

func main() {
	t := 0
	pt := POINT{}
	for {
		procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
		fmt.Println("Go 時刻 : ", t, "🐶マウスのカーソル座標 x:y = ", pt.X, " : ", pt.Y)
		time.Sleep(300 * time.Millisecond)
		t++
	}
}

Go出力結果
image.png

Rust

↓7行・187文字

Cargo.toml
[package]
name = "mouse_position_in_gamen"
version = "0.1.0"
edition = "2021"

[dependencies]
windows = { version = "0.51.1",features=["Win32_UI_WindowsAndMessaging","Win32_Foundation",]}

↓17行・約430文字

main.rs
use std::mem;
use std::thread;
use std::time::Duration;
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;

fn main() {
    let mut t = 0;
    loop {
        unsafe {
            let mut pt = mem::zeroed();
            let _ = GetCursorPos(&mut pt);
            println!("Rust GetCursorPos🐵  時刻:{:?} 座標 x:y= {:?}:{:?}", t, pt.x,pt.y);
        };
        thread::sleep(Duration::from_millis(300));
        t = t + 1
    }
}

Rust出力結果
image.png

感想

 以下は「C」「JavaScript」「VBA」「Go」で趣味で遊んだ経験を踏まえての感想です。

「C++」

  • 相変わらず最もシンプルに書ける。
  • 「POINT po;」は知らんなー。

「C#」

  • 「public」の記述いらんわー。
  • GetCursorPosの括弧内に「out」が必要なのが、しっくりこない。
  • GetCursorPosの前が「bool」なのは、調べなきゃ知らない。

「Go」

  • 「uintptr(unsafe.Pointer(&pt)」が難しい。

「Rust」

  • マイクロソフトの「Windowsのwin32クレート」を使ってます。
  • 「mem::zeroed();」を使う理由は、全然わかってないですが、動けばOK。
  • 「unsafe」って所有権がなくなる意味と私は理解しました。

参考にしたサイト

C++

C#

Go

Rust

1
3
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
1
3