Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

Tip: How to Add a Button to a Toolbar

In this post I describe how to add a button to the SAP GUI for Windows toolbar with AutoIt scripting language. 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.


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:



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

    Const $WinTitle = "[CLASS:SAP_FRONTEND_SESSION]"

    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
          Case $WM_MOUSEMOVE
            ToolTip("My Button")
          Case $WM_MOUSELEAVE
          Case $WM_LBUTTONUP
            ;-Only asynchronous processes here--------------------------
      Return _WinAPI_CallWindowProc($hBtnProc, $hWind, $uMsg, _
        $wParam, $lParam)

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

        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", _
        _WinAPI_SetWindowLong($hBtn, $GWL_WNDPROC, _
      ;-Set tray--------------------------------------------------------
        $Quit = TrayCreateItem("Exit")
        While 1
          Switch TrayGetMsg()
            Case $Quit
      _WinAPI_SetWindowLong($hToolBar, $GWL_WNDPROC, $hBtnProc)


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.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.