Screen Capture from Visual FoxPro
Use
One use of SAP Automation GUI is to be able to run R/3 transactions with redesigned user interfaces. Tools like Microsoft’s Visual FoxPro and Visual Basic provide Windows 95 user interface development environments.
A starting point for such redesigns is to capture the original R/3 screen in a representation that can be used by the user interface development tool.
Example
The following example shows how to capture a particular screen as a Visual FoxPro form. For simplicity we have coded in the system name, the login ID, and the transaction code.
* Read a transaction and create a Visual FoxPro form.
* Values are hardcoded for a simple example of
* programmatic form construction.
* Define control types that we will be using - taken from guilib.h
#DEFINE CTRL_STATIC 1
#DEFINE CTRL_EDIT 2
#DEFINE CTRL_PASSWORD 3
#DEFINE CTRL_PUSHBUTTON 4
#DEFINE CTRL_RADIOBUTTON 5
#DEFINE CTRL_CHECKBOX 6
#DEFINE CTRL_FRAMEBOX 7
#DEFINE CTRL_MATCH 9
#DEFINE CTRL_LISTSTATIC 10
#DEFINE CTRL_MATCHFIX 12
* Define vertical and horizontal scales and margins.
#DEFINE SAPVSCALE 1.25
#DEFINE SAPHSCALE 1.25
#DEFINE SAPVMARG 0.5
#DEFINE SAPHMARG 0.5
#DEFINE SAPTB 1.5
CLEAR
* Start the GUI Component server.
Sap = CREATEOBJECT ("SapAutoGui.Event")
* Connect to the SAP system and logon. Change the arguments
* to those of a real system to have this work.
Sap.Connect ("mysys.sfo.sap-ag.de", "00", 0)
Sap.Logon ("000", "myuser", "mypass", "e")
* For this example, call the FBL1 transaction.
Sap.Transaction ("FBL1")
* Now build the form.
SapForm = CREATEOBJECT ("Form") && Create the form
SapForm.ScaleMode = 0 && Set units to Foxels
SapForm.Caption = Sap.Title && Set form title
* Set width and height using dimensions of the current event. Scale
* for Visual FoxPro and include room for margins and the FoxPro menu.
SapForm.Height = Sap.RowDimension * SAPVSCALE + (2 * SAPVMARG) + SAPTB
SapForm.Width = Sap.ColumnDimension * SAPHSCALE + (2 * SAPHMARG)
* Comment the next line out if you do not want to see the form as it
* is being built.
SapForm.Visible =.T.
* Loop through each control in the screen and add it to the form if
* we recognize the control type. Skip any unrecognized controls.
CtrlLast = Sap.Controls.Count - 1
For i = 0 to CtrlLast
&& Cache values so we avoid redundant OLE Automation calls.
Ctrl = Sap.Controls(i)
CtrlType = Ctrl.Type
CtrlTop = Ctrl.Top
CtrlLeft = Ctrl.Left
CtrlHeight = Ctrl.Height
CtrlWidth = Ctrl.Width
CtrlFieldName = ""
&& Provide unique names for the objects in the form. We need
&& them to identify the controls as we build the form.
SapName = "SapObj" + LTRIM(STR(i))
SapLabel = "" && Used as part of simulating frame boxes
SapIgnore =.F.
Do Case
Case CtrlType = CTRL_STATIC
SapForm.AddObject ((SapName), "Label")
SapForm.&SapName..Caption = Ctrl.Value
SapForm.&SapName..AutoSize =.T. && Size to fit the label
Case CtrlType = CTRL_EDIT
SapForm.AddObject ((SapName), "TextBox")
SapForm.&SapName..Value = LTRIM(Ctrl.Value)
SapForm.&SapName..SpecialEffect = 0 && 3D
&& For edit and some other fields, set the tooltip text
&& to the table and field names.
If Ctrl.HasFieldNames Then
CtrlFieldName = RTRIM(Ctrl.Name)
SapForm.&SapName..ToolTipText = CtrlFieldName
EndIf
Case CtrlType = CTRL_PASSWORD
SapForm.AddObject ((SapName), "TextBox")
SapForm.&SapName..Value = LTRIM(Ctrl.Value)
SapForm.&SapName..PasswordChar = "*" && For password text
SapForm.&SapName..SpecialEffect = 0 && 3D
If Ctrl.HasFieldNames Then
CtrlFieldName = RTRIM(Ctrl.Name)
SapForm.&SapName..ToolTipText = CtrlFieldName
EndIf
Case CtrlType = CTRL_PUSHBUTTON
SapForm.AddObject ((SapName), "CommandButton")
SapForm.&SapName..Caption = Ctrl.Value
Case CtrlType = CTRL_RADIOBUTTON
SapForm.AddObject ((SapName), "OptionButton")
SapForm.&SapName..Caption = Ctrl.Value
SapForm.&SapName..SpecialEffect = 0 && 3D
SapForm.&SapName..Value = IIF (Ctrl.Selected,.T.,.F.)
Case CtrlType = CTRL_CHECKBOX
SapForm.AddObject ((SapName), "CheckBox")
SapForm.&SapName..Caption = Ctrl.Value
SapForm.&SapName..SpecialEffect = 0 && 3D
SapForm.&SapName..Value = IIF (Ctrl.Selected,.T.,.F.)
Case CtrlType = CTRL_FRAMEBOX
&& Visual FoxPro does not seem to have a frame box, so combine
&& a box and label to simulate it.
SapForm.AddObject ((SapName), "Shape")
SapForm.&SapName..Curvature = 0 && Rectangular
SapForm.&SapName..SpecialEffect = 0 && 3D
SapLabel = "SapLabel" + LTRIM(STR(i))
SapForm.AddObject ((SapLabel), "Label")
SapForm.&SapLabel..Caption = RTRIM(Ctrl.Value)
SapForm.&SapLabel..AutoSize =.T.
Case CtrlType = CTRL_MATCH
SapForm.AddObject ((SapName), "ComboBox")
SapForm.&SapName..AddItem (LTRIM(Ctrl.Value))
SapForm.&SapName..ListIndex = 1
SapForm.&SapName..Style = 0 && Dropdown combo allows text entry
SapForm.&SapName..SpecialEffect = 0 && 3D
CtrlWidth = CtrlWidth + 2 && Allow room for the arrow
If Ctrl.HasFieldNames Then
CtrlFieldName = RTRIM(Ctrl.Name)
SapForm.&SapName..ToolTipText = CtrlFieldName
EndIf
Case CtrlType = CTRL_MATCHFIX
SapForm.AddObject ((SapName), "ComboBox")
SapForm.&SapName..AddItem (LTRIM(Ctrl.Value))
SapForm.&SapName..ListIndex = 1
SapForm.&SapName..Style = 2 && Dropdown list - no text entry
SapForm.&SapName..SpecialEffect = 0 && 3D
CtrlWidth = CtrlWidth + 2 && Allow room for the arrow
If Ctrl.HasFieldNames Then
CtrlFieldName = RTRIM(Ctrl.Name)
SapForm.&SapName..ToolTipText = CtrlFieldName
EndIf
Case CtrlType = CTRL_LISTSTATIC
SapForm.AddObject ((SapName), "Label")
SapForm.&SapName..Caption = Ctrl.Value
Otherwise
SapIgnore =.T. && ignore for now
EndCase
&& Set location, size, and visibility for all control types.
&& Controls use scales and margins in the same way we used them
&& to set the overall size of the form.
If !SapIgnore
&& When simulating frame boxes, shift the box down and the label
&& to the right.
If LEN(SapLabel) > 0
SapForm.&SapName..Top = (CtrlTop + 0.33) * SAPVSCALE + SAPVMARG
SapForm.&SapName..Left = CtrlLeft * SAPHSCALE + SAPHMARG
SapForm.&SapName..Height = CtrlHeight * SAPVSCALE
SapForm.&SapName..Width = CtrlWidth * SAPHSCALE
SapForm.&SapName..Visible =.T.
SapForm.&SapLabel..Top = CtrlTop * SAPVSCALE + SAPVMARG
SapForm.&SapLabel..Left = (CtrlLeft + 1) * SAPHSCALE + SAPHMARG
SapForm.&SapLabel..Height = SAPVSCALE && Just 1 unit
SapForm.&SapLabel..Visible =.T.
Else
SapForm.&SapName..Top = CtrlTop * SAPVSCALE + SAPVMARG
SapForm.&SapName..Left = CtrlLeft * SAPHSCALE + SAPHMARG
SapForm.&SapName..Height = CtrlHeight * SAPVSCALE
SapForm.&SapName..Width = CtrlWidth * SAPHSCALE
SapForm.&SapName..Visible =.T.
EndIf
EndIf
EndFor
SapForm.Visible =.T. && Make form visible when done
* Wait for a click to log off from the SAP System.
Wait Window "Click to logoff from SAP"
Sap.Logoff
Sap.Quit
* Wait for another click to finish the program, saving
* to a predefined file name.
Wait Window "Click to finish program"
SapForm.SaveAs ("SAP.SCX")