Hello community,

with the knowledge from here it is not very difficult to manipulate the UI object of the SAP GUI for Windows. In this case I want to demonstrate how to add a button to the first toolbar.

001.JPG

The example is in AutoIt script langauge. To add this button, the program detects the first window with SAP_FRONTEND_SESSION class. It detects the handle of the toolbar and adds a normal button. It registers for the button a callback routine. Then it runs in an endless loop, which can be only interrupted by an exit via the AutoIt button in the traybar. The callback routine contains a tooltip and the action if the button is pressed with the left mouse button. In our example case the button only opens notepad. It is very important to run only ansynchronous processes with the $WM_LBUTTONUP. otherwise AutoIt or SAPLogon crashes.

Here the code:

;-Begin-----------------------------------------------------------------

  ;-Directives----------------------------------------------------------
    AutoItSetOption("MustDeclareVars")

  ;-Include files-------------------------------------------------------
    #Include "Include\Constants.au3"
    #Include "Include\WinAPI.au3"
    #Include "Include\WindowsConstants.au3"

  ;-Constants-----------------------------------------------------------
    Const $WinTitle = "[CLASS:SAP_FRONTEND_SESSION]"

  ;-Variables-----------------------------------------------------------
    Global $ToolBar1 = "[CLASS:Afx:"
    Global $ToolBar2 = ":0:00010003:00000010:00000000]"
    Global $ToolBar
    Global $hBtnProc

  ;-Function btnSCAP_CallBack-------------------------------------------
    Func btnCallBack($hWind, $uMsg, $wParam, $lParam)
      Switch $uMsg
        ;-ToolTip-------------------------------------------------------
          Case $WM_MOUSEMOVE
            ToolTip("My Button")
          Case $WM_MOUSELEAVE
            ToolTip("")
        ;-ButtonClick---------------------------------------------------   
          Case $WM_LBUTTONUP
            ;-Only asynchronous processes here--------------------------
              Run("notepad.exe")
      EndSwitch
      Return _WinAPI_CallWindowProc($hBtnProc, $hWind, $uMsg, _
        $wParam, $lParam)
    EndFunc

  ;-Procedure AddButton-------------------------------------------------
    Func AddButton()

      ;-Variables-------------------------------------------------------
        Local $hWin, $hToolBar, $Quit, $hBtn, $ptrCallBack

      $hWin = WinGetHandle($WinTitle)
      $ToolBar = $ToolBar1 & _
        Hex(_WinAPI_GetWindowLong($hWin, $GWL_HINSTANCE)) & $ToolBar2
      $hToolBar = ControlGetHandle($WinTitle, "", $ToolBar)
      ;-My button-------------------------------------------------------
        Dim $hBtn = _WinAPI_CreateWindowEx(0, "BUTTON", "MyButton", _
          $WS_VISIBLE + $WS_CHILD, 600, 6, 75, 20, $hToolBar)
        $hbtnProc = _WinAPI_GetWindowLong($hBtn, $GWL_WNDPROC)
        Dim $ptrCallBack = DLLCallBackRegister("btnCallBack", "ptr", _
          "hwnd;uint;long;ptr")
        _WinAPI_SetWindowLong($hBtn, $GWL_WNDPROC, _
          DLLCallBackGetPtr($ptrCallBack))
      ;-Set tray--------------------------------------------------------
        Opt("TrayMenuMode",1)
        $Quit = TrayCreateItem("Exit")
        TraySetState()
      ;-MainLoop--------------------------------------------------------
        While 1
          Sleep(64)
          Switch TrayGetMsg()
            Case $Quit
              ExitLoop
          EndSwitch
        Wend
      _WinAPI_SetWindowLong($hToolBar, $GWL_WNDPROC, $hBtnProc)
      DLLCallBackFree($ptrCallBack)
    EndFunc
   
  ;-Main----------------------------------------------------------------
    AddButton()   

;-End-------------------------------------------------------------------

To do:

  • In this example the first SAP GUI window found gets this button, it should be customizable.
  • If the parent window of the button doesn’t exists anymore, it is necessary to close AutoIt in the traybar explicit. It should be detect the existence of the SAP GUI window and interrupt the endless loop automatically.

Maybe someone can integrate these functions.

Anyway, this example shows a way, how easy it is to expand specific SAP GUI for objects with own UI elements and activities.

Enjoy it.

Cheers
Stefan

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply