tools
読み取り中…
検索中…
一致する文字列を見つけられません
winMacro.h
[詳解]
1#pragma once
2#include <windows.h>
3
4/**
5 * @file winMacro.h
6 * @brief Windows マウス・画面操作ユーティリティ
7 *
8 * Windows API を使用した画面上の任意座標のピクセル色取得と
9 * マウスクリック操作を提供します。
10 */
11
12/**
13 * @namespace wmc
14 * @brief Windows マクロ操作用名前空間
15 */
16namespace wmc {
17 /**
18 * @struct clRGB
19 * @brief RGB カラー値を保持する構造体
20 */
21 struct clRGB {
22 /**
23 * @brief コンストラクタ
24 * @param red 赤成分 (0〜255)
25 * @param green 緑成分 (0〜255)
26 * @param blue 青成分 (0〜255)
27 */
28 clRGB(int red, int green, int blue) : r(red), g(green), b(blue) {}
29 int r; ///< 赤成分
30 int g; ///< 緑成分
31 int b; ///< 青成分
32
33 /**
34 * @brief 等値比較演算子
35 * @param other 比較対象の clRGB 構造体
36 * @return RGB 全成分が等しい場合 true
37 */
38 bool operator==(const wmc::clRGB& other) const {
39 return r == other.r && g == other.g && b == other.b;
40 }
41 };
42
43 /**
44 * @brief 指定した画面座標のピクセル色を取得する
45 * @param x 取得したいピクセルの X 座標(スクリーン座標)
46 * @param y 取得したいピクセルの Y 座標(スクリーン座標)
47 * @return 取得した RGB 値。取得失敗時は {-1, -1, -1}
48 */
49 wmc::clRGB getColor(int x, int y) {
50 wmc::clRGB color = { -1, -1, -1 }; // 初期値: エラー時の値
51
52 // デスクトップ全体のデバイスコンテキストを取得
53 HDC hdc = GetDC(NULL); // NULLで画面全体を取得
54 if (!hdc) {
55 std::cerr << "Failed to get device context!" << std::endl;
56 return color;
57 }
58
59 // 指定座標のピクセルの色を取得
60 COLORREF pixelColor = GetPixel(hdc, x, y);
61 if (pixelColor == CLR_INVALID) { // エラーの場合
62 std::cerr << "Failed to get pixel color at (" << x << ", " << y << ")!" << std::endl;
63 ReleaseDC(NULL, hdc);
64 return color;
65 }
66
67 // 赤、緑、青の成分を構造体に格納
68 color.r = GetRValue(pixelColor);
69 color.g = GetGValue(pixelColor);
70 color.b = GetBValue(pixelColor);
71
72 // デバイスコンテキストを解放
73 ReleaseDC(NULL, hdc);
74
75 return color;
76 }
77
78 /**
79 * @brief 指定した画面座標でマウス左ボタンのクリックをシミュレートする
80 * @param x クリックする X 座標(スクリーン座標)
81 * @param y クリックする Y 座標(スクリーン座標)
82 */
83 void click(int x, int y) {
84 SetCursorPos(x,y);
85 INPUT inputs[2] = {0};
86 inputs[0].type = INPUT_MOUSE;
87 inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
88 inputs[1].type = INPUT_MOUSE;
89 inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
90 SendInput(2, inputs, sizeof(INPUT));
91 }
92}
Windows マクロ操作用名前空間
void click(int x, int y)
指定した画面座標でマウス左ボタンのクリックをシミュレートする
Definition winMacro.h:83
wmc::clRGB getColor(int x, int y)
指定した画面座標のピクセル色を取得する
Definition winMacro.h:49
RGB カラー値を保持する構造体
Definition winMacro.h:21
clRGB(int red, int green, int blue)
コンストラクタ
Definition winMacro.h:28
bool operator==(const wmc::clRGB &other) const
等値比較演算子
Definition winMacro.h:38
int b
青成分
Definition winMacro.h:31
int r
赤成分
Definition winMacro.h:29
int g
緑成分
Definition winMacro.h:30
@ y
Definition time.h:33