# PhpGUI桌面应用开发
**Repository Path**: xbmt/php-web-view-basic-template
## Basic Information
- **Project Name**: PhpGUI桌面应用开发
- **Description**: php基于webview开发win桌面,js和php直接交互
- **Primary Language**: PHP
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 4
- **Created**: 2024-05-31
- **Last Updated**: 2024-05-31
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# php-webview
> php版本目录文件可能会报毒,请放心使用(因为经过压缩实现打包后打底仅仅 7M 的体积)
```
目录结构
.
├── library\ 库目录(制作PHP可调用拓展)
|
├── os\ 系统拓展目录
|
├── php\ php环境目录
|
├── src\ 应用目录
| └── index.php 入口文件(文件名别修改)
|
├── favicon.ico 文件图标
|
└── windows.bat windows运行文件
```
## 更新
**进入src目录**
windows用户
运行命令 `..\php\php.exe ..\php\composer.phar update` 更新
自己php环境 `php composer update` 更新
### 运行
**进入根目录**
windows用户
双击 `windows.bat` 或者运行命令 `.\php\php.exe src/index.php` 启动
自己php环境 `php src/index.php` 启动 (必须开启ffi拓展和phar拓展)
### 打包
**进入根目录**
运行命令 `.\php\php.exe build.php` 或者自己php环境运行 `php build.php`

编译后仅仅7M打底
### 效果

#### 文件图标
**提示** 文件图标必须在启动目录下,不然不显示
## 构建
有关先决条件,请阅读 [The link](https://github.com/webview/webview#prerequisites)
要构建库,请运行 **library/build.sh** 在unix系统上, **library/build.bat** 在 Windows
# 教程
配置
```php
use KingBes\PhpWebview\WebView;
use KingBes\PhpWebview\WindowSizeHint;
/**
* @param string $title 窗口标题
* @param int $width 窗口宽度
* @param int $height 窗口高度
* @param bool $debug debug模式 默认:false
* __DIR__ 入口位置
*/
$webview = new WebView('Php WebView', 640, 480, true, __DIR__);
```
获取与设置
```php
// 获取ffi 返回:FFI
$webview->getFFI();
// 获取webview 返回:mixed
$webview->getWebview();
// 获取窗口标题 返回:string
$webview->getTitle();
// 设置窗口标题 参数:title=string
$webview->setTitle(title:"新的标题");
// 获取窗口宽度 返回:int
$webview->getWidth();
// 设置窗口宽度 参数:width=int
$webview->setWidth(width:100);
// 获取窗口高度 返回:int
$webview->getHeight();
// 设置窗口高度 参数:height=int
$webview->setHeight(height:100);
// 获取窗口大小提示 返回:int
$webview->getHint();
// 设置窗口大小提示 参数:hint=WindowSizeHint::HINT_MIN
$webview->setHint(hint:WindowSizeHint::HINT_MIN);
// 修改窗口大小 参数 width=int height=int hint=WindowSizeHint :HINT_NONE 自由缩放 HINT_MIN 固定最小 HINT_MAX 固定最大 HINT_FIXED 禁止缩放
$webview->size(int $width, int $height, WindowSizeHint $hint);
// 判断是否debug 返回:bool
$webview->isDebug();
// 设置html内容 参数:html=string
$webview->setHTML(html:"html的内容");
// 绑定交互的操作 参数:name=string ,闭包函数:$req 是接收到的参数,$seq 是触发次数
$webview->bind(name:"bindName",function($seq, $req, $context){
return ["返回内容","返回数组"];
});
// 解除绑定 参数:name=你绑定过的name名
$webview->unbind(name:"bindName");
// 设置窗口url内容 参数:url=string
$webview->navigate(url:"http://www.baidu.com");
// 任务栏图标标题
$webview->icon_title('php WeView');
// 任务栏图标菜单 参数:arr = array ,具体看 示例四
$webview->icon_menu(arr:$arr);
// 显示窗口
$webview->show_win();
// 退出窗口
$webview->destroy_win();
// 运行
$webview->run();
// 销毁
$webview->destroy();
```
## 示例一 `js和php交互`
```php
require_once "vendor/autoload.php";
use KingBes\PhpWebview\WebView;
// 实例
$webview = new WebView('Php WebView', 640, 480, true, __DIR__);
$html = <<点击
EOF;
// 设置HTML
$webview->setHTML($html);
// 绑定
$webview->bind('btn', function ($seq, $req, $context) {
return $req;
});
// 运行
$webview->run();
// 销毁
$webview->destroy();
```
## 示例二 `设置本地静态文件`
```php
require_once "vendor/autoload.php";
use KingBes\PhpWebview\WebView;
// 实例
$webview = new WebView('Php WebView', 640, 480, true, __DIR__);
// 本地文件`index.html`
$navigate = "file://D:\xxx\index.html";
// 设置url
$webview->navigate($navigate);
// 运行
$webview->run();
// 销毁
$webview->destroy();
```
## 示例三 `弹出对话框`
```php
require_once "vendor/autoload.php";
use KingBes\PhpWebview\WebView;
use KingBes\PhpWebview\Dialog;
// 对话实例
$dialog = new Dialog(__DIR__);
// webview实例
$webview = new WebView('Php WebView', 640, 480, true, __DIR__);
// 获取html
$html = <<弹出
EOF;
// 设置HTML
$webview->setHTML($html);
// 绑定
$webview->bind('openMsg', function ($seq, $req, $context) use ($dialog) {
// 弹出消息窗口
$msg = $dialog->msg($req[0], $req[1]);
return ["code" => 0, "msg" => $msg];
});
// 运行
$webview->run();
// 销毁
$webview->destroy();
```
对话框功能
配置
```php
require_once "vendor/autoload.php";
use KingBes\PhpWebview\Dialog;
// __DIR__ 入口位置
$dialog = new Dialog(__DIR__);
```
功能
```php
// 消息框 参数:str=>string , type=>int (0~2) 返回: bool
$dialog->msg(str:"hello php",type:0);
// 输入框 返回:string
$dialog->prompt();
// 文件框 返回:string 说明:返回选择的文件路径字符串,不选择返回空字符串
$dialog->file();
// 文件夹框 参数: dirs=>string (初始化文件夹路径,为空则默认路径) 返回:string 说明:返回选择的文件夹路径字符串,不选择返回空字符串
$dialog->dir(dirs:"D:/");
// 保存文件 参数:content=>string (保存的内容) filename=>string (保存的文件名) path=>string (初始化文件夹路径,默认为空) 返回:bool
$dialog->save(content:"你好",filename:"test.txt",path:"D:/");
```
效果

## 示例四 `任务栏图标菜单`
```php
// 任务栏菜单 字段name是菜单名称 字段fn是点击后触发的函数
$arr = [
["name" => "显示", "fn" => function () use ($webview) {
// 显示窗口
$webview->show_win();
}],
["name" => "退出", "fn" => function () use ($webview) {
// 退出窗口
$webview->destroy_win();
}]
];
$webview->icon_menu($arr);
```
效果

# webman-webview 套壳桌面版
[码云](https://gitee.com/kllxs_admin/webview-jacketing)
# php-webview Linux桌面
拓展 [webview.so](https://github.com/KingBes/php-webview-expand) 鸡肋版
# 相关
[webview](https://github.com/webview/webview) [php-webview](https://github.com/0hr/php-webview) [dialog](https://github.com/ttytm/dialog) [static-php-cli](https://github.com/crazywhalecc/static-php-cli)
[打包为安装包](https://jrsoftware.org/isinfo.php)