he426100/php-windows

There is no license information available for the latest version (dev-main) of this package.

dev-main 2024-01-27 07:57 UTC

This package is auto-updated.

Last update: 2024-04-27 08:28:40 UTC


README

简介

基于php ffi,移植pyautoguipymsgboxpygetwindow

示例

  • phpautogui notepad
<?php

require __DIR__ . '/../vendor/autoload.php';

use He426100\phpautogui\phpautogui;
use He426100\phpautogui\platforms\windows\windows;

$auto = new phpautogui(new windows);
$auto->press('win');
usleep(0.2 * 1000_000);

$auto->typewrite('notepad.exe', 0.1);
usleep(0.2 * 1000_000);

$auto->press('enter');
usleep(2 * 1000_000);

$auto->typewrite('PHP is the best language.', 0.1);
  • phpautogui mouse
<?php

require __DIR__ . '/../vendor/autoload.php';

use He426100\phpautogui\phpautogui;
use He426100\phpautogui\platforms\windows\windows;

$auto = new phpautogui(new windows);
$size = $auto->size();
$position = $auto->position();
echo 'currentX: ' . $position[0], ', currentY: ' . $position[1], ', width: ' . $size[0], ', height: ' . $size[1], PHP_EOL;

$s = microtime(true);
$central = [$size[0] / 2, $size[1] / 2];
$auto->moveTo($central[0], $central[1], 0.5);
assert($auto->position() == $central);
echo 'moved to (', $central[0], ', ', $central[1], '), used ' . (microtime(true) - $s) . ' s', PHP_EOL;
  • phpmsgbox
<?php

require __DIR__ . '/../vendor/autoload.php';

use He426100\phpmsgbox\phpmsgbox;
use He426100\phpmsgbox\platforms\windows\windows;

$msg = new phpmsgbox(new windows);
$msg->alert('php是最好的语言。', '温馨提示');
echo $msg->confirm('php是最好的语言。', '温馨提示'), PHP_EOL;
  • phpgetwindow
<?php

require __DIR__ . '/../vendor/autoload.php';

use He426100\phpgetwindow\phpgetwindow;
use He426100\phpgetwindow\platforms\windows\windows;

$native = new windows;
$window = new phpgetwindow($native);
echo "活动窗口标题: " . $window->getActiveWindowTitle() . "\n";
echo "所有窗口标题:\n";
print_r($window->getAllTitles());

如何写cdef

  1. 数据类型
  • windows-data-types
  • rect
  • point
  • vs_BuildTools下载windows sdk后在C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\um中找到对应的头文件,比如winint.h
  1. 函数接口
  1. 回调函数
  • EnumWindows.WNDENUMPROC
    原版定义,见 nf-winuser-EnumWindows
    BOOL EnumWindows(
    [in] WNDENUMPROC lpEnumFunc,
    [in] LPARAM      lParam
    );
    
    BOOL CALLBACK EnumWindowsProc(
        _In_ HWND   hwnd,
        _In_ LPARAM lParam
    );
    在ffi中写法是
    BOOL EnumWindows(void (*)(HWND, LPARAM), LPARAM);

    在PHP FFI中,由于不能直接声明一个函数指针类型,因此通常使用void (*)(HWND, LPARAM)来表示这样一个接受特定参数类型的函数指针。这里的void表示回调函数没有返回值(在C中通常是BOOL或者WNDENUMPROC定义的返回类型),圆括号内的(HWND, LPARAM)则是指明回调函数需要接受的参数类型。(来自千问)