Core Function GUILink

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 39: Line 39:
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
// Create the GUI
 
// Create the GUI
$GUI = GUICreate("Window", $Title, 200, 200);
+
$GUI = GUICreate("Window", "Hello", 200, 200);
 
// Show the GUI
 
// Show the GUI
 
GUILoad( $GUI );
 
GUILoad( $GUI );

Revision as of 18:16, 13 December 2011

GUILink( <gui object>, <linktype>, <action>, <param var> )

Contents

Description

Add a link on a Click event to a GUI object.

Parameters

gui object

The GUI object to link with.

linktype

The link type to create.

action

The action to perform on when the link happens this is basically same as Eval() and it will execute as string as if it was actual Sputnik code.

param var

Optional; A variable to pass inside the link this variable will become $Param inside the link this is most useful for attaching a class to the link so you can access its variables, functions natively.

Return Value

Success: Returns 1.

Failure: Returns 0.

Remarks

Example

Heres a very simple example of using links this shows direct code execution links and links that call functions

// Create the GUI
$GUI = GUICreate("Window", "Hello", 200, 200);
// Show the GUI
GUILoad( $GUI );
// Create a button -- This button will simply display a message
$Button1 = GUICreate("Button", $GUI, "Press Me!", 8, 8);
// Create a button -- This button will call a function
$Button2 = GUICreate("Button", $GUI, "Press Me!", 8, 35);
// Add a link to the buttons
GUILink($Button1, "Click", 'MsgBox("Hello World!");'); // Display message
GUILink($Button2, "Click", 'Moo();'); // Call function
// Keep the GUI running as long as long as the window is open
While ( GUIStatus( $GUI ) ) DoEvents( );
 
Function Moo()
{
	MsgBox("Moo moo farm");
}

Heres an example of usnig the "param var" with the link "click"

// Use our custom made Inputbox class
$value = InputBox2::Show(); // Show the window
if($value) // Capture input
{
	MsgBox( "Value is $value" ); // Show what user typed
}
else
{
	MsgBox( "User did not enter a value" ); // Inform user did not type
}
 
// Out inputbox class here we will create an inputbox that works just like
// the Inputbox() actual core command.
Class InputBox2
{
	// Define some variables
	my $Input;
	my $Accept;
	my $Cancel;
	my $GUI;
	my $returnValue;
	my $hWnd;
	// Make a static function that will handle the showing of the window and return values
	// This allows 0, 1, 2 or 3 params
	Static Function Show($Title = "Inputbox 2", $Prompt = "Enter a value", $Default = "")
	{
		// Spawn a new class
		$myClass = new InputBox2();
		$myClass->$returnValue = ""; // Nothing to return yet
 
		// Create the GUI
		$myClass->$GUI = GUICreate("Window", $Title, 345, 140, -1, -1, 1);
		$myClass->$hWnd = GUIGetProp($myClass->$GUI, "Handle"); // GUIs hWnd for use with Win__() functions
		GUILoad( $myClass->$GUI ); // Show the GUI
 
		// Create the prompt
		GUICreate("Label", $myClass->$GUI, $Prompt, 20, 20, 300);
		// Create input control
		$myClass->$Input = GUICreate("TextBox", $myClass->$GUI, $Prompt, 20, 45, 300);		
		// Focus the input control
		GUIPropInvoke($myClass->$Input, "Focus");
		// Create the accept button
		$myClass->$Accept = GUICreate("Button", $myClass->$GUI, "&Accept", 20, 70, 110);
		// Create the cancel button
		$myClass->$Cancel = GUICreate("Button", $myClass->$GUI, "&Cancel", 209, 70);
 
		// Create links
		// Heres the important bit we are going to include the $myClass inside the GUILink
		// so that we can use it inside the link as if the link was really part of this
		// function/class
 
		// This button gets the text and closes the GUI
		// Here im using @"" instead of "" since @"" lets us make a multiline string
		// much easier and it doesnt resolve $variables inside it which is what we need
		// for a link
		GUILink($myClass->$Accept, "Click",
				@"
				$param->$returnValue = GUIGetProp($param->$Input, 'Text');
				GUIUnload($param->$GUI);
				",
				$myClass); 	// The link to the class goes here without this we will be unable
						// to call functions within the class
 
		// This button closes the GUI				
		GUILink($myClass->$Cancel, "Click", 'GUIUnload($param->$GUI);', $myClass);
 
		// Use WinActivate to force the window to be activated and focus
		// This is a good example of using Win__() functions on Sputnik GUI stuff
		// instead of other windows
		WinActivate($myClass->$hWnd);
 
		// Keep the GUI running as long as long as the window is open
		While ( GUIStatus( $myClass->$GUI ) ) DoEvents( );
 
		// Get return value
		$returnValue = $myClass->$returnValue;
 
		// Clean up the GUI ie delete it
		unset($myClass->$GUI); // This will clean up all controls attached to the GUI as well
 
		// Clean up the Class ie delete it
		unset($myClass);
 
		// Return
		return $returnValue;
	}
};
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox