天天看點

PHP開發Windows桌面應用程式執行個體

PHP_GTK2.0 開發桌面應用程式

php gtk 官方網站

http://gtk.php.net/

官方有gtk的中文手冊 具體安裝請參見 手冊

更簡單的方法是用另外一款軟體 名字叫做gnope

下載下傳位址:http://www.gnope.org/download.php

安裝過程很簡單 安裝好之後 在桌面會有個PHP-Gtk2 Applications 的快捷方式圖示

輕按兩下該圖示 會出現一個php的桌面程式 裡面列出了所有的demo 有控件示範 還有遊戲(前提是安裝時選擇了安裝遊戲)

應用詳細講解:

http://www.16ec.com/tp15/site/index.php和http://blog.csdn.net/zhangking/archive/2010/08/20/5825416.aspx

看過這些程式 你是不迫不及待要自己萊寫一個桌面應用程式了啊

下面就給出一個簡單的登陸程式:

<?php
if (!class_exists(‘gtk’)) {
     die("lease load the php-gtk2 module in your php.ini\r\n");
}
function login(GtkWindow $wnd, GtkEntry $txtUsername, GtkEntry $txtPassword)
{
    $strUsername = $txtUsername->get_text();
    $strPassword = $txtPassword->get_text();
    $errors = null;
    if (strlen($strUsername) == 0) {
        $errors .= "Username is missing.\r\n";
    }
    if (strlen($strPassword) == 0) {
        $errors .= "No password given.\r\n";
    }
    if ($errors !== null) {
        $dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL,
            Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $errors);
        $dialog->set_markup(
            "The following errors occured:\r\n"
            . "<span foreground=’red’>" . $errors . "</span>"
        );
        $dialog->run();
        $dialog->destroy();
    }
else {
        $wnd->destroy();
    }
}
$wnd = new GtkWindow();
$wnd->set_title(‘Login’);
$wnd->connect_simple(‘destroy’, array(‘gtk’, ‘main_quit’));
$txtUsername = new GtkEntry();
$txtPassword = new GtkEntry();
$lblUsername = new GtkLabel(‘_Username’,true);
$lblPassword = new GtkLabel(‘_Password’,true);
$btnLogin = new GtkButton(‘_Login’);
$btnCancel = new GtkButton(‘_Cancel’);
$lblUsername->set_mnemonic_widget($txtUsername);
$lblPassword->set_mnemonic_widget($txtPassword);
$btnCancel->connect_simple(‘clicked’,array($wnd,’destroy’));
$btnLogin->connect_simple(‘clicked’,'login’,$wnd,$txtUsername,$txtPassword);
$tbl = new GtkTable(3,2);
$tbl->attach($lblCredit,0,2,0,1);
$tbl->attach($lblUsername,0,1,1,2);
$tbl->attach($txtUsername,1,2,1,2);
$tbl->attach($lblPassword,0,1,2,3);
$tbl->attach($txtPassword,1,2,2,3);
$bbox = new GtkHButtonBox();
$bbox->set_layout(Gtk::BUTTONBOX_EDGE);
$bbox->add($btnLogin);
$bbox->add($btnCancel);
$vbox = new GtkVBox();
$vbox->pack_start($tbl);
$vbox->pack_start($bbox);
$wnd->add($vbox);
//$lblHello = new GtkLabel("This is my first php talbe application\nhaha");
//$wnd->add($lblHello);
$wnd->show_all();
Gtk::main();
?>
           

在 Windows 上安裝使用 Gnope

使用 Gnope 在 Windows 上安裝和維護 PHP-GTK 2 已經被證明幾乎是最簡單的方法。 Gnope 是全功能的 PHP-GTK 2 安裝包,包含 PHP 5.1 、GTK 2.6 和 PEAR 。 簡單的點幾下滑鼠你就可以安裝完成!

隻要從 gnope.org 下載下傳安裝包,并跟随螢幕上的指引。 Gnope 也擁有它自己的 PEAR 自動安裝頻道, 那裡你可以下載下傳 PHP-GTK 2 程式添加到官方的 PEAR 包中。 例如從 Gtk2 category 。

手工安裝

從 PHP-GTK 2 下載下傳 頁面下載下傳 PHP-GTK 2 的針對 Windows 的 Alpha 版本二進制發行包。解壓縮檔案到指定目錄。 一個名為 php-gtk 的目錄将會建立,并包含發行版的所有内容。

你需要 php.exe (CLI 版本)和 php5ts.dll ,這兩個都可從 PHP 5 的 Windows 發行版中獲得, 并且放置到 php-gtk 目錄。如果你希望使用這個 php.exe 的其他擴充,請確定你已經存放二進制檔案于此。

之後,你必須設定環境變量 PATH 包含發行包中的 gtk+2.6.9 目錄。 你也可以讓批處理檔案 gtkpath.bat 在運作時設定适當的 PATH 變量用于目前指令行會話中。

從 CVS 編譯

這裡僅僅包含在 Windows 上安裝 PHP-GTK 2 最主要的步驟。如果你希望了解更多内容, 請從 CVS 上擷取 win32/README.win32.txt 并閱讀。

from: http://gtk.php.net/manual/zh_cn/html/tutorials/tutorials.installation.windows.html

=============

WinBinder 讓phper 可以在window 系統開發桌面軟體

WinBinder 是一種開源的 PHP 動态擴充(.dll) ,也算是腳本程式設計語言,為php 在window 下的開發提供使用者界面UI ,它負責調用window 的API 接口。其運作 PHP 程式員輕松地使用 PHP 建立 Windows 應用程式。當然,這個隻能在 Windows 下運作。本身是一個軟體,php 程式員可以通過這個軟體開發界面。官方網站說得很不錯,無須編譯php. 隻要儲存檔案擴充為.phpw ,然後用 php.exe 打開就可以運作。具體開發感受如何,要試驗一下才知道。初步感覺不會比php-gtk 差。

可以在此處下載下傳到 WinBinder 的包 , 下那個 .zip 包,一般不需要用 .exe 的。下完之後解壓縮,将 winbinder_xxx\PHP\PHP4\ext 目錄下的(如果用的是 PHP5 則複制 PHP5 目錄下的) php_winbinder.dll 複制到 PHP 擴充目錄下,一般在 C:\php\extensions ,然後修改 php.ini 檔案,加上:

extension=php_winbinder.dll

然後到打開指令行(cmd ),進入到 winbinder_xxx\Code\Samples 目錄下,運作:

C:phpcliphp.exe manytests_main.phpw

這樣就可以看到 Windows 視窗了,測試用的例子很多,可以都試試。我試了一下中文,還是支援得不錯的,想想也是,WinBinder 是通過擴充的方式,加載到 PHP 中,其調用的都是 Windows 的 APIs 。實際上可以将 WinBinder 分為兩層,一層是底層,叫 API 層,作為 PHP 擴充直接連接配接 Windows 的 API ,并為第二層(高層,叫 PHP 層)提供統一的調用接口。

Hello, world 代碼示例

<?php

include("../inc/winbinder.php");                                // 包含 WinBinder PHP 庫檔案

wb_create_window(NULL, PopupWindow, "Hello world!", 480, 320); // 建立 Windows 應用程式

wb_main_loop();                                                 // 進入消息循環

?>

winbinder 的安裝版的安裝與配置

首先去http://winbinder.org/files/WinBinder-0.46.0.exe 下載下傳winbinder ,這是一個安裝版的,下載下傳後輕按兩下安裝。

1 、next

PHP開發Windows桌面應用程式執行個體

2 、next

PHP開發Windows桌面應用程式執行個體

3 、選擇安裝路徑然後next ,這裡我選擇的是d;/winbinder

PHP開發Windows桌面應用程式執行個體

4 、選擇php 的版本,我這裡的是php5.1

PHP開發Windows桌面應用程式執行個體

5 、選擇下載下傳的伺服器,選擇台灣比較快( 又隻有台灣的選項而沒有中國的,氣憤)

PHP開發Windows桌面應用程式執行個體

6 、next

PHP開發Windows桌面應用程式執行個體

 7 、install

PHP開發Windows桌面應用程式執行個體

8 、等待

PHP開發Windows桌面應用程式執行個體

9 、安裝完成後點确認就行了。可以做第10步了。  

10 、安裝好後打開D:\WinBinder\phpcode\examples ,任找一個.phpw 擴充名的檔案,選擇打開方式為D:\WinBinder\binaries\php51\php-win.exe 。

PHP開發Windows桌面應用程式執行個體

現在已經可以運作以.phpw 為擴充名的檔案了。例如

PHP開發Windows桌面應用程式執行個體

這裡要講解幾個問題

1: 我們可以選擇的打開方式有三種,分别是php.exe php-win.exe php-cgi.exe 如果打開方式選擇的是php-cgi.exe

那麼在運作的時候,就會有一個指令提示符視窗出現,在實際應用中這個視窗也許看來很多餘,但是在調試的時候可是很有用的哦

他能夠提示你程式中有哪些錯誤。

2: 剛才我們選擇的打開方式為D:\WinBinder\binaries\php51\ 下的.exe 檔案,可能有朋友要問了,為什麼不能選擇本機中已經 安裝好的php( 假設是D:/php) 檔案夾下的.exe 檔案呢?其實是可以的,但是這裡有一個問題,如果選擇php 檔案夾下的.exe 檔案,那麼它使用 的是c:\windows 下的php.ini 配置檔案,這樣的話winbinder 運作時加載的.dll 檔案,實際上是D:/php/ext/ 檔案夾中的 檔案。是以這個時候需要把php.ini 中的extension_dir 設定為./ext 因為ext 檔案夾是和.exe 檔案在同一檔案夾中的( 即 D:\php 檔案夾) 。然後還需要把D:\WinBinder\binaries\php51\ext 中的php_winbinder.dll 檔案放到 D:\php\ext 中,然後相應的在c:\windows 下的php.ini 檔案中加上extension=php_winbinder.dll 。另外 如果需要使用相應的庫檔案的話,需要在php.ini 中對應的檔案名前的; 号去掉,這個我想大家都知道吧。

如果選擇打開方式為D:\WinBinder\binaries\php51\ 下的.exe 檔案則就沒有這麼麻煩,它使用的是D:\WinBinder \binaries\php51\ 下的php.ini 檔案,你可以不作任何的配置就可以使用,但是他隻預設支援mysql 和sqlite 這兩個庫,如果你 希望使用其他的庫例如gd 那麼就需要修改D:\WinBinder\binaries\php51\ 下的php.ini 檔案,然後把對應的庫檔案( 例如 php_gd.dll) 放到D:\WinBinder\binaries\php51\ext 下。