Environment: W2k + php4.3.1 + php/gtk0.5.2
A simple notepad (only files can be opened for modification)
<? Php
Set_time_limit (0); // set the running time
If (! Class_exists ("gtk") // determines whether a GTK module exists.
If (strtoupper (substr ($ _ SERVER ["OS"], 0, 3) = "WIN ")
Dl ("php_gtk.dll ");
Else
Dl ("php_gtk.so ");
$ Window = & new GtkWindow (); // create a window
$ Window-> set_uposition (100,100); // the position where the window appears.
$ Window-> set_usize (gdk: screen_width ()-200), (gdk: screen_height ()-150); // window size
$ Window-> set_title ("WINDOWS"); // you can specify the window title.
$ Window-> connect_object ('deststroy', array ('gtk ', 'main _ quit'); // registers window events
$ Vbox = & new GtkVBox ();
$ Hbox = & new GtkHBox ();
$ Window-> add ($ vbox );
$ MenuBar = & new GtkMenuBar (); // create a menu
$ Vbox-> pack_start ($ menuBar, false, false, 0 );
$ File = & new GtkMenuItem ("File ");
$ MenuBar-> append ($ file );
$ FileMenu = & new GtkMenu ();
$ Open = & new GtkMenuItem ("Open ");
$ Save = & new GtkMenuItem ("Save ");
$ Line = & new GtkMenuItem ();
$ Line-> set_sensitive (true );
$ Exit = & new GtkMenuItem ("Exit ");
$ FileMenu-> append ($ open );
$ Open-> connect_object ('activate', 'showfileselection ');
$ FileMenu-> append ($ save );
$ Save-> connect_object ('activate', 'savefile ');
$ FileMenu-> append ($ line );
$ FileMenu-> append ($ exit );
$ Exit-> connect_object ('activate', array ('gtk ', 'main _ quit '));
$ File-> set_submenu ($ fileMenu );
$ Scroll = & new GtkScrolledWindow ();
$ Scroll-> set_border_width (8 );
$ Scroll-> set_policy (GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
$ Hbox-> pack_start ($ scroll, true, true, 0 );
$ Vbox-> pack_start ($ hbox, true, true, 1 );
$ Text = & new GtkText ();
$ Text-> set_editable (true );
$ Text-> set_word_wrap (true );
$ Scroll-> add ($ text );
Function showFileSelection () // file selection function
{
$ File = & new GtkFileSelection ("File Selection ");
$ OK _button = $ file-> OK _button;
$ OK _button-> connect ('clicked', 'openfile', $ file );
$ OK _button-> connect_object ('clicked', array ($ file, 'deststroy '));
$ Cancel_button = $ file-> cancel_button;
$ Cancel_button-> connect_object ('clicked', array ($ file, 'deststroy '));
$ File-> show ();
}
$ FilePath = null;
Function openFile ($ button, $ f) // function for opening a file
{
GLOBAL $ text, $ save, $ filePath;
$ FilePath = $ f-> get_filename ();
If (is_file ($ filePath ))
{
$ Fp = fopen ($ filePath, 'R ');
While (! Feof ($ fp ))
$ Str. = fgets ($ fp, 1024 );
$ Text-> insert (null, $ str );
Fclose ($ fp );
Return $ filePath;
}
}
Function saveFile () // function for saving files
{
GLOBAL $ filePath, $ text;
If (is_file ($ filePath ))
{
$ Str = $ text-> get_chars (0,-1 );
$ Fp = fopen ($ filePath, 'w ');
Fwrite ($ fp, $ str );
Fclose ($ fp );
}
Return;
}
$ Window-> show_all (); // display all controls in the window
Gtk: main (); // The most important sentence.
?>
Appendix: php/gtk Configuration
PHP/GTK (reprinted)
Published on at PM in PHP programming Forum
In the past, PHP was considered to be only used to write CGI programs on the server. If PHP can develop GUI (graphical user interface) programs in Windows, do you believe this? Recently, the PHP development team has successfully developed a PHP program bound with GTK +, so you can develop GUI programs in Windows.
1. Create a PHP/GTK runtime environment:
In fact, the GUI program is no different from the common PHPCGI program, but the PHP/GTK program is generated by the GTK class to generate the GUI. They are also open source code and rely on PHP to parse and create a window. If you have already set up the PHP debugging environment, it is relatively simple to install the PHP/GTK environment:
1. Download php_gtk.dll (this dll file is used to parse the GTK code in the PHP source program) and decompress it to the extension directory of PHP;
2. download other PHP/GTK dll files (6 in total) and decompress them to the system32 directory of Windows;
3. Open php. ini: add the "extension = php_gtk.dll" statement at the bottom of "Windows Extensions" in the extended Settings section. We recommend that you back up php first. ini to avoid modification failure and invalidate the established PHP runtime environment;
Now the PHP/GTK runtime environment has been established. Of course, you can add "dl ('php _ GTK. dll ') to dynamically load GTK support.
If you have not created a PHP runtime environment, it is easier to install it:
1. Download the entire PHP/GTK bundle support package and decompress it to the PHP4 directory of drive C;
2. Copy the dll file under the winnt directory to the system32 directory of Windows, and copy the php. ini file to the directory of Windows;
After PHP/GTK is set up, you can run a PHP/GTK program in command line mode: Enter "c: \ php4 \ PHP-q gtkprogrampath" in the running state ", change "c: \ php4 \ php" to PHP. EXE path. "gtkprogrampath" is the path of the PHP/GTK program. For example, "c: \ php4 \ php-q c: \ php4 \ samples \ hello. php" will run the example "hello world" program included in the PHP/GTK running package.
2. Compile the PHP/GTK program:
If you create a PHP/GTK application, and you need to build a PHP running environment on the other computer and use the PHP Command Line Mode for parsing to execute the application, it is too complicated. So how can we compile a PHP/GTK program? After some searches, I found the PHPCompiler software.
PHPCompiler is developed by www.javascode.com (http://www.deskcode.com/phpcompiler), which can compile PHP into an EXE executable file with built-in PHP support. If you want to compile a PHP/GTK program, you must create a runtime environment for PHP/GTK (refer to the previous steps ). To compile a PHP/GTK program, follow these steps:
1. Open PHPCompiler (), select the source code of the PHP program to be compiled in "Script to", and select the path of the EXE file after compilation in "Destination;
2. Click the "Compile" button, and a dialog box will pop up asking about the compilation mode. (if it is a PHP/GTK program, select no. If it is a pure PHPCGI program, select yes );
3. Then, a dialog box is displayed asking whether to copy the php4ts. dll file to the directory of the compiled EXE file. Select yes.
After the above steps, a PHP/GTK program is successfully compiled, but there are several notes for compiling the PHP/GTK program:
1. If you need to execute the compiled executable files, the PC must have several dll files required for the GTK runtime environment (the ones downloaded earlier). If you need to build an application, several dll files can be copied to the system32 directory during installation (however, the subroutine is not "green software" ^_^ ).
2. PHPCompiler supports PHP very well, but some people have tried to use some functions that need to be extended, such as "gzopen". It should be well prepared before compilation, it cannot be used after compilation. In fact, after compilation, the executable file is equivalent to only the default PHP support (even GTK support is not available). Therefore, if you have applied any function that requires extension support in the program, all dll files supporting functions must be dynamically loaded, such as "dl ('php _ gtk. dll.
3. When running a compiled executable file, a DOS window is displayed and closed automatically, because each GUI window is "drawn" by GTK, so there must be that window.
4. In the past, PHP may not be the real OOP language. However, when PHP/GTK is reached, any window is "drawn" by The GTK object ", without excellent OOP skills, it is difficult to write GUI programs.
Iii. PHP/GTK resources:
1. http://gtk.php.net: PHP/GTK official website; although said it is the official website, but it is very simple, out of a FAQ and mail list, almost can not find any useful thing,
2, http://www.phpgtk.com: a better interface PHP/GTK website, with the latest version information.
3. Renewal.
4. http://www.phpuk.org/gtk/:non-official gtkmanual website, which is easy to understand.
If you think E is ugly, you can also download the latest PHP/GTK runtime environment and support package from zphp.com.