Skip to content

Instantly share code, notes, and snippets.

View kusa-mochi's full-sized avatar
🏠
Working from home

Kusa Mochi kusa-mochi

🏠
Working from home
View GitHub Profile
using System;
using System.Runtime.InteropServices;
namespace KusaMochiAutoLibrary.NativeFunctions
{
[StructLayout(LayoutKind.Sequential)]
public struct Win32Point
{
public Int32 X;
public Int32 Y;
// アプリのメインウィドウ
System.Windows.Window MainWindow = System.Windows.Application.Current.MainWindow;
// DIPを物理的なピクセルに変換するための係数をもとめる。
PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
double dpiWidthFactor = m.M11;
double dpiHeightFactor = m.M22;
// DIPのスクリーンサイズに係数を掛け算して、スクリーンの解像度をもとめる。
// これでは正確に取得できない場合がある。
double ScreenHeight = SystemParameters.PrimaryScreenHeight;
double ScreenWidth = SystemParameters.PrimaryScreenWidth;
Keys winFormsKey = Keys.A;
short key = (short)winFormsKey;
// 以下同様
// キーコード
short key = 65;
INPUT[] inputs = new INPUT[] {
new INPUT {
type = NativeMethods.INPUT_KEYBOARD,
ui = new INPUT_UNION {
keyboard = new KEYBDINPUT {
wVk = key,
wScan = (short) NativeMethods.MapVirtualKey (key, 0),
using System;
namespace MyNamespace {
///// APIの利用に必要な構造体・共用体の定義 ここから /////
[StructLayout(LayoutKind.Sequential)]
public struct Win32Point
{
public Int32 X;
public Int32 Y;
};
// マウスホイールの回転量(マイナスで下回転、プラスで上回転)
int amount = -3; // 下に3段階回転させる。
// マウスポインタの左右位置(左端が0)
int x = 100;
// マウスポインタの上下位置(上端が0)
int y = 200;
INPUT input = new INPUT
Win32Point mousePosition = new Win32Point {
X = 0,
Y = 0
};
// マウスポインタの現在位置を取得する。
NativeMethods.GetCursorPos (ref mousePosition);
// マウスポインタの現在位置でマウスの左ボタンの押し下げ・押し上げを連続で行うためのパラメータを設定する。
INPUT[] inputs = new INPUT[] {
using System;
namespace MyNamespace {
///// APIの利用に必要な構造体・共用体の定義 ここから /////
[StructLayout(LayoutKind.Sequential)]
public struct Win32Point
{
public Int32 X;
public Int32 Y;
};
// スクリーンの左上隅を基準(0,0)とする座標を指定する。
NativeMethods.SetCursorPos(123, 234);