[python-win32] COM events

Denes L denes1951 at yahoo.ca
Mon Apr 18 21:26:01 EDT 2016


Hi

I am having trouble figuring out how do add events to a COM UI interface.
So far all the things that I have tried after reading posts on many forums either result in an error, cause the interface to hang or do nothing at all.

The UI in reference is the SAP Business One UI API Version 9.0 a.k.a. SAbouiCOM.exe
The documentation has examples in VB and C# which I have copied below in case they help.

I am not familiar with Windows programming, VB, or C# by any strectch of one's imagination so at this stage any help is appreciated and it will be promptly tried out.
First some relevant parts from the makepy output, and when I say relevant it is what I imagine to be important, I can post the whole thing somewhere if required:

# -*- coding: mbcs -*-
# Created by makepy.py version 0.5.01
# By python version 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)]
# From type library 'SAPbouiCOM.exe'
# On Sun Mar 06 18:20:27 2016

class _IApplicationEvents:
    '_IApplicationEvents Interface'
    CLSID = CLSID_Sink = IID('{81B441CB-BB83-44B2-8A62-6DFBEB7B6217}')
    coclass_clsid = IID('{49B5C554-9A6B-4530-A90D-2B51B27BCEDA}')
    _public_methods_ = [] # For COM Server support
    _dispid_to_func_ = {
              120 : "OnLayoutKeyEvent",
              103 : "OnStatusBarEvent",
              100 : "OnItemEvent",
              109 : "OnWidgetEvent",
              108 : "OnFormDataEvent",
              106 : "OnReportDataEvent",
              101 : "OnMenuEvent",
              202 : "OnServerInvokeCompletedEvent",
              201 : "OnUDOEvent",
              102 : "OnAppEvent",
              104 : "OnProgressBarEvent",
              105 : "OnPrintEvent",
              107 : "OnRightClickEvent",
        }

    def __init__(self, oobj = None):
        if oobj is None:
            self._olecp = None
        else:
            import win32com.server.util
            from win32com.server.policy import EventHandlerPolicy
            cpc=oobj._oleobj_.QueryInterface(pythoncom.IID_IConnectionPointContainer)
            cp=cpc.FindConnectionPoint(self.CLSID_Sink)
            cookie=cp.Advise(win32com.server.util.wrap(self, usePolicy=EventHandlerPolicy))
            self._olecp,self._olecp_cookie = cp,cookie
    def __del__(self):
        try:
            self.close()
        except pythoncom.com_error:
            pass
    def close(self):
        if self._olecp is not None:
            cp,cookie,self._olecp,self._olecp_cookie = self._olecp,self._olecp_cookie,None,None
            cp.Unadvise(cookie)
    def _query_interface_(self, iid):
        import win32com.server.util
        if iid==self.CLSID_Sink: return win32com.server.util.wrap(self)

    # Event Handlers
    # If you create handlers, they should have the following prototypes:
#    def OnLayoutKeyEvent(self, eventInfo=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method LayoutKeyEvent'
#    def OnStatusBarEvent(self, Text=defaultNamedNotOptArg, messageType=defaultNamedNotOptArg):
#        'method StatusBarEvent'
#    def OnItemEvent(self, FormUID=defaultNamedNotOptArg, pVal=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'ItemEvent'
#    def OnWidgetEvent(self, pWidgetData=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method WidgetEvent'
#    def OnFormDataEvent(self, BusinessObjectInfo=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method FormDataEvent'
#    def OnReportDataEvent(self, eventInfo=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method ReportDataEvent'
#    def OnMenuEvent(self, pVal=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'MenuEvent'
#    def OnServerInvokeCompletedEvent(self, b1iEventArgs=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method ServerInvokeCompletedEvent'
#    def OnUDOEvent(self, udoEventArgs=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method UDOEvent'
#    def OnAppEvent(self, EventType=defaultNamedNotOptArg):
#        'ApplicationEvent'
#    def OnProgressBarEvent(self, pVal=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method ProgressBarEvent'
#    def OnPrintEvent(self, eventInfo=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method PrintEvent'
#    def OnRightClickEvent(self, eventInfo=defaultNamedNotOptArg, BubbleEvent=pythoncom.Missing):
#        'method RightClickEvent'


# This CoClass is known by the name 'SAPbouiCOM.Application.90.0'
class Application(CoClassBaseClass): # A CoClass
    # Application Class
    CLSID = IID('{49B5C554-9A6B-4530-A90D-2B51B27BCEDA}')
    coclass_sources = [
        _IApplicationEvents,
    ]
    default_source = _IApplicationEvents
    coclass_interfaces = [
        IApplication,
    ]
    default_interface = IApplication


class IApplication(DispatchBaseClass):
    'IApplication Interface'
    CLSID = IID('{D1F75D47-137C-4335-AC2A-3FE209831B6A}')
    coclass_clsid = IID('{49B5C554-9A6B-4530-A90D-2B51B27BCEDA}')

    def ActivateMenuItem(self, MenuUID=defaultNamedNotOptArg):
        'method ActivateMeunItem'
        return self._oleobj_.InvokeTypes(52, LCID, 1, (24, 0), ((8, 1),),MenuUID
            )

    def ActivateToolbar(self, MenuUID=defaultNamedNotOptArg):
        'method ActivateToolbar'
        return self._oleobj_.InvokeTypes(86, LCID, 1, (24, 0), ((8, 1),),MenuUID
            )

    def CreateObject(self, Type=defaultNamedNotOptArg):
        'Method CreateObject'
        ret = self._oleobj_.InvokeTypes(65, LCID, 1, (9, 0), ((3, 1),),Type
            )
        if ret is not None:
            ret = Dispatch(ret, u'CreateObject', None)
        return ret

    def ExportRptAsXML(self, rptPath=defaultNamedNotOptArg, params=defaultNamedNotOptArg):
        'property ExportRptAsXML'
        # Result is a Unicode object
        return self._oleobj_.InvokeTypes(75, LCID, 1, (8, 0), ((8, 1), (8, 1)),rptPath
            , params)

    # Result is of type IEventFilters
    def GetFilter(self):
        'GetFilter'
        ret = self._oleobj_.InvokeTypes(56, LCID, 1, (9, 0), (),)
        if ret is not None:
            ret = Dispatch(ret, u'GetFilter', '{CE8A5ED3-BA2B-4CE1-932A-53F4E92994F0}')
        return ret

    def GetFormItemDefaultHeight(self, itemType=defaultNamedNotOptArg):
        'GetFormItemDefaultHeight'
        return self._oleobj_.InvokeTypes(81, LCID, 1, (3, 0), ((3, 1),),itemType
            )

    def GetFormItemDefaultWidth(self, itemType=defaultNamedNotOptArg):
        'GetFormItemDefaultWidth'
        return self._oleobj_.InvokeTypes(80, LCID, 1, (3, 0), ((3, 1),),itemType
            )

    def GetLastBatchResults(self):
        'GetLastBatchResults'
        # Result is a Unicode object
        return self._oleobj_.InvokeTypes(57, LCID, 1, (8, 0), (),)

    def LoadBatchActions(self, XmlStr=defaultNamedNotOptArg):
        'Batch Action'
        return self._ApplyTypes_(54, 1, (24, 0), ((16392, 3),), u'LoadBatchActions', None,XmlStr
            )

    def MessageBox(self, Text=defaultNamedNotOptArg, DefaultBtn=1, Btn1Caption=u'Ok', Btn2Caption=u''
            , Btn3Caption=u''):
        'method MessageBox'
        return self._ApplyTypes_(50, 1, (3, 32), ((8, 1), (3, 49), (8, 49), (8, 49), (8, 49)), u'MessageBox', None,Text
            , DefaultBtn, Btn1Caption, Btn2Caption, Btn3Caption)

    # Result is of type IForm
    def OpenForm(self, sysObjectType=defaultNamedNotOptArg, bstrUDOObjectType=defaultNamedNotOptArg, bstrObjectKey=defaultNamedNotOptArg):
        'method OpenForm'
        ret = self._oleobj_.InvokeTypes(84, LCID, 1, (9, 0), ((3, 1), (8, 1), (8, 1)),sysObjectType
            , bstrUDOObjectType, bstrObjectKey)
        if ret is not None:
            ret = Dispatch(ret, u'OpenForm', '{50BA08F8-4392-4395-B35A-50C93CA91A0C}')
        return ret

    def RemoveWindowsMessage(self, messageType=defaultNamedNotOptArg, toProcess=defaultNamedNotOptArg):
        'method RemoveWindowsMessage'
        return self._oleobj_.InvokeTypes(105, LCID, 1, (24, 0), ((3, 1), (11, 1)),messageType
            , toProcess)

    def SendKeys(self, Text=defaultNamedNotOptArg):
        'method SendKeys'
        return self._oleobj_.InvokeTypes(53, LCID, 1, (24, 0), ((8, 1),),Text
            )

    def Serialize(self, inXML=defaultNamedNotOptArg):
        ' Serialize'
        # Result is a Unicode object
        return self._oleobj_.InvokeTypes(85, LCID, 1, (8, 0), ((8, 1),),inXML
            )

    def ServerInvoke(self, RequestUri=defaultNamedNotOptArg):
        'method ServerInvoke'
        # Result is a Unicode object
        return self._oleobj_.InvokeTypes(100, LCID, 1, (8, 0), ((8, 1),),RequestUri
            )

    def SetFilter(self, Filter=0):
        'SetFilter'
        return self._oleobj_.InvokeTypes(55, LCID, 1, (24, 0), ((9, 49),),Filter
            )

    def SetStatusBarMessage(self, Text=defaultNamedNotOptArg, Seconds=20, IsError=True):
        'method SetStatusBarMessage'
        return self._oleobj_.InvokeTypes(51, LCID, 1, (24, 0), ((8, 1), (3, 49), (11, 49)),Text
            , Seconds, IsError)

    _prop_map_get_ = {
        "AppId": (20, 2, (3, 0), (), "AppId", None),
        # Method 'Cockpits' returns object of type 'ICockpits'
        "Cockpits": (70, 2, (9, 0), (), "Cockpits", '{49193AF4-562F-4E9A-82C6-9B79B6C66383}'),
        # Method 'Company' returns object of type 'ICompany'
        "Company": (11, 2, (9, 0), (), "Company", '{195BCB8E-4D2F-4950-947C-9E3523929229}'),
        # Method 'Desktop' returns object of type 'IDesktop'
        "Desktop": (10, 2, (9, 0), (), "Desktop", '{F88352F1-5027-41CF-861B-3D7FE6D40CCD}'),
        "EventLevel": (110, 2, (3, 0), (), "EventLevel", None),
        "FocusRestored": (106, 2, (11, 0), (), "FocusRestored", None),
        "FontHeight": (83, 2, (3, 0), (), "FontHeight", None),
        "FontName": (82, 2, (8, 0), (), "FontName", None),
        # Method 'Forms' returns object of type 'IForms'
        "Forms": (1, 2, (9, 0), (), "Forms", '{02B99871-8ACD-446E-A24F-7645E7DF4B70}'),
        "IsHostedEnvironment": (107, 2, (11, 0), (), "IsHostedEnvironment", None),
        "Language": (21, 2, (3, 0), (), "Language", None),
        # Method 'Menus' returns object of type 'IMenus'
        "Menus": (2, 2, (9, 0), (), "Menus", '{BD3E79E3-F688-47D6-9030-5714F3079FA0}'),
        "MetadataAutoRefresh": (66, 2, (11, 0), (), "MetadataAutoRefresh", None),
        # Method 'ResourceData' returns object of type 'IResourceData'
        "ResourceData": (67, 2, (9, 0), (), "ResourceData", '{3C771B45-1C75-4AE7-B88E-D2D102D445BE}'),
        "SkinStyle": (68, 2, (3, 0), (), "SkinStyle", None),
        # Method 'StatusBar' returns object of type 'IStatusBar'
        "StatusBar": (64, 2, (9, 0), (), "StatusBar", '{C1DC17AD-B039-4E27-87C2-84F7EC82313F}'),
        "XSEngineBaseURL": (111, 2, (8, 0), (), "XSEngineBaseURL", None),
    }
    _prop_map_put_ = {
        "EventLevel": ((110, LCID, 4, 0),()),
        "FocusRestored": ((106, LCID, 4, 0),()),
        "MetadataAutoRefresh": ((66, LCID, 4, 0),()),
    }
    def __iter__(self):
        "Return a Python iterator for this object"
        try:
            ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
        except pythoncom.error:
            raise TypeError("This object does not support enumeration")
        return win32com.client.util.Iterator(ob, None)


# This CoClass is known by the name 'SAPbouiCOM.SboGuiApi.90.0'
class SboGuiApi(CoClassBaseClass): # A CoClass
    # SboGuiApi Class
    CLSID = IID('{6CF0D1E0-470B-4684-B9B5-70F9A5ACBB06}')
    coclass_sources = [
    ]
    coclass_interfaces = [
        ISboGuiApi,
    ]
    default_interface = ISboGuiApi


To get the UI going:

from win32com.client import Dispatch
ui = Dispatch("SAPbouiCOM.SboGuiApi")
ui.AddonIdentifier = "..."
conn = ui.Connect("...")
app = ui.GetApplication()
# functions like create msg boxes, windows, grids, etc. work fine


A sample program in C#:

// ****************************************************************************
//  BEFORE STARTING:
//  1. Add reference to the "SAP Business One UI API"
//  2. Insert the development connection string to the "Command line argument"
// -----------------------------------------------------------------
//  1.
//     a. Project->Add Reference...
//     b. select the "SAP Business One UI API 2007" From the COM folder
//
//  2.
//      a. Project->Properties...
//      b. choose Configuration Properties folder (place the arrow on Debugging)
//      c. place the connection string in the 'Command line arguments' field
//
// **************************************************************************************************


using Microsoft.VisualBasic;
using System;
namespace FilterEvents {
    public class EventFilter  {
        private SAPbouiCOM.Application SBO_Application;

        //**********************************************************
        // declaring an Event filters container object and an
        // event filter object
        //**********************************************************

        public SAPbouiCOM.EventFilters oFilters;

        public SAPbouiCOM.EventFilter oFilter;


        private void SetApplication() {

            // *******************************************************************
            // Use an SboGuiApi object to establish connection
            // with the SAP Business One application and return an
            // initialized appliction object
            // *******************************************************************

            SAPbouiCOM.SboGuiApi SboGuiApi = null;
            string sConnectionString = null;

            SboGuiApi = new SAPbouiCOM.SboGuiApi();

            // by following the steped specified above the following
            // statment should be suficient for either development or run mode

            sConnectionString = Interaction.Command();

            // connect to a running SBO Application

            SboGuiApi.Connect( sConnectionString );

            // get an initialized application object

            SBO_Application = SboGuiApi.GetApplication( -1 );

        }


        private void SetFilters() {

            // Create a new EventFilters object
            oFilters = new SAPbouiCOM.EventFilters();

            // add an event type to the container
            // this method returns an EventFilter object
            oFilter = oFilters.Add( SAPbouiCOM.BoEventTypes.et_CLICK );

            // assign the form type on which the event would be processed
            oFilter.AddEx( "139" ); // Orders Form
            oFilter.AddEx( "142" ); // Purchase Form

            oFilter = oFilters.Add( SAPbouiCOM.BoEventTypes.et_KEY_DOWN );

            // assign the form type on which the event would be processed
            oFilter.Add( 139 ); // Orders Form

            // For a list of all form types see the help or use the
            // Tools -> User Tools -> Display Debug Information option
            // in the SBO application
            // then open the desired form and hover over it with the mouse
            // the form's type will apear in the lower left side of the screen


            // Setting the application with the EventFilters object
            // in this case we will process a click event for form types 142 and 139
            // and we will process a key down event for for form type 139

            SBO_Application.SetFilter( oFilters );

        }

        public EventFilter() {

            //*************************************************************
            // set SBO_Application with an initialized application object
            //*************************************************************
            SetApplication();

            //*************************************************************
            // set SBO_Application with an initialized EventFilters object
            //*************************************************************

            SetFilters();

            // events handled by SBO_Application_AppEvent
            SBO_Application.AppEvent += new SAPbouiCOM._IApplicationEvents_AppEventEventHandler( SBO_Application_AppEvent );
            // events handled by SBO_Application_MenuEvent
            SBO_Application.MenuEvent += new SAPbouiCOM._IApplicationEvents_MenuEventEventHandler( SBO_Application_MenuEvent );
            // events handled by SBO_Application_ItemEvent
            SBO_Application.ItemEvent += new SAPbouiCOM._IApplicationEvents_ItemEventEventHandler( SBO_Application_ItemEvent );
        }

        private void SBO_Application_AppEvent( SAPbouiCOM.BoAppEventTypes EventType ) {

            //********************************************************************************
            // the following are the events sent by the application
            // (Ignore aet_ServerTermination)
            // in order to implement your own code upon each of the events
            // place you code instead of the matching message box statement
            //********************************************************************************


            switch ( EventType ) {
                case SAPbouiCOM.BoAppEventTypes.aet_ShutDown:

                    SBO_Application.MessageBox( "A Shut Down Event has been caught" + Constants.vbNewLine + "Terminating Add On...", 1, "Ok", "", "" );

                    //**************************************************************
                    //
                    // Take care of terminating your AddOn application
                    //
                    //**************************************************************

                    System.Environment.Exit( 0 );



                    break;
                case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged:

                    SBO_Application.MessageBox( "A Company Change Event has been caught", 1, "Ok", "", "" );

                    //**************************************************************
                    // Check the new company name, if your add on was not meant for
                    // the new company terminate your AddOn
                    //    If SBO_Application.Company.Name Is Not "Company1" then
                    //         Close
                    //    End If
                    //**************************************************************

                    break;
                case SAPbouiCOM.BoAppEventTypes.aet_LanguageChanged:

                    SBO_Application.MessageBox( "A Languge Change Event has been caught", 1, "Ok", "", "" );

                    //**************************************************************
                    // Check the new language name, if your AddOn's items needs
                    // to be changed, take care of it at this point
                    //
                    //    Select Case SBO_Application.Language
                    //         Case ln_English:
                    //         Case ln_French:
                    //         Case ln_German:
                    //    End Select
                    //**************************************************************

                    break;
            }


        }



        private void SBO_Application_MenuEvent( ref SAPbouiCOM.MenuEvent pVal, out bool BubbleEvent ) {
            BubbleEvent = true;
            //********************************************************************************
            // in order to activate your own forms instead of SAP Business One system forms
            // process the menu event by your self
            // change BubbleEvent to True so that SAP Business One won't process it
            //********************************************************************************

            if ( pVal.BeforeAction == true ) {

                SBO_Application.SetStatusBarMessage( "Menu item: " + pVal.MenuUID + " sent an event BEFORE SAP Business One processes it.", SAPbouiCOM.BoMessageTime.bmt_Long, true );

                // to stop SAP Business One from processing this event
                // unmark the following statement

                // BubbleEvent = True

            }
            else {

                SBO_Application.SetStatusBarMessage( "Menu item: " + pVal.MenuUID + " sent an event AFTER SAP Business One processes it.", SAPbouiCOM.BoMessageTime.bmt_Long, true );

            }

        }


        private void SBO_Application_ItemEvent( string FormUID, ref SAPbouiCOM.ItemEvent pVal, out bool BubbleEvent ) {

            BubbleEvent = true;
            //**************************************************************************
            // BubbleEvent sets the behavior of SAP Business One.
            // False means that the application will not continue processing this event
            // True is the default value
            //**************************************************************************

            if ( pVal.FormType != 0 & pVal.Before_Action == true ) {

                //************************************************************
                // the message box form type is 0
                // I chose not to deal with events triggered by a message box
                //************************************************************

                switch ( pVal.EventType ) {
                    case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED:

                        // Specifies a button release (After exit).

                        SBO_Application.MessageBox( "An et_ITEM_PRESSED has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_KEY_DOWN:

                        // Specifies a key down event.

                        SBO_Application.MessageBox( "An et_KEY_DOWN has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_GOT_FOCUS:

                        // Specifies an item got focus.

                        SBO_Application.MessageBox( "An et_GOT_FOCUS has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_LOST_FOCUS:

                        // Specifies an item lost focus.

                        SBO_Application.MessageBox( "An et_LOST_FOCUS has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_COMBO_SELECT:

                        // Specifies the selection of valid value in Combo Box.

                        SBO_Application.MessageBox( "An et_COMBO_SELECT has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_CLICK:
                        // Specifies Mouse Up on editable item.

                        SBO_Application.MessageBox( "An et_CLICK has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK:
                        // Specifies Mouse Up on editable item in time interval define by
                        // SAP Business One as double-click.

                        SBO_Application.MessageBox( "An et_DOUBLE_CLICK has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_MATRIX_LINK_PRESSED:
                        // Specifies a link arrow within a matrix was pressed.

                        SBO_Application.MessageBox( "An et_MATRIX_LINK_PRESSED has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_MATRIX_COLLAPSE_PRESSED:
                        // Specifies collapsed or expanded list within a matrix.

                        SBO_Application.MessageBox( "An et_MATRIX_COLLAPSE_PRESSED has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_VALIDATE:
                        //Specifies an item validation event.

                        SBO_Application.MessageBox( "An et_VALIDATE has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_LOAD:
                        // Specifies that SAP Business One application opened a form.

                        SBO_Application.MessageBox( "An et_FORM_LOAD has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD:
                        // Specifies that SAP Business One application closed a form.

                        SBO_Application.MessageBox( "An et_FORM_UNLOAD has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_ACTIVATE:
                        // Specifies that the focus is set on the form.

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_DEACTIVATE:
                        // Specifies that the form lost focus.

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_CLOSE:
                        // Specifies that the form was closed by calling the Close event.

                        SBO_Application.MessageBox( "An et_FORM_CLOSE has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_RESIZE:
                        // Specifies resizing of the form.

                        SBO_Application.MessageBox( "An et_FORM_RESIZE has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                    case SAPbouiCOM.BoEventTypes.et_FORM_KEY_DOWN:
                        // Specifies a key press on an area which is not item of the form.

                        SBO_Application.MessageBox( "An et_FORM_KEY_DOWN has been sent by a form with the unique ID: " + FormUID, 1, "Ok", "", "" );

                        break;
                }
            }
        }
    }
}


And the same sample program in Visual Basic:

'****************************************************************************
' BEFORE STARTING:
' 1. Add reference to the "SAP Business One UI API"
' 2. Insert the development connection string to the "Command line argument"
'-----------------------------------------------------------------
' 1.
'    a. Project->Add Reference...
'    b. select the "SAP Business One UI API 2007" From the COM folder
'
' 2.
'     a. Project->Properties...
'     b. choose Configuration Properties folder (place the arrow on Debugging)
'     c. place the connection string in the 'Command line arguments' field
'
'**************************************************************************************************

Option Strict Off
Option Explicit On

Public Class EventFilter
    Private WithEvents SBO_Application As SAPbouiCOM.Application

    '//**********************************************************
    '// declaring an Event filters container object and an
    '// event filter object
    '//**********************************************************

    Public oFilters As SAPbouiCOM.EventFilters

    Public oFilter As SAPbouiCOM.EventFilter


    Private Sub SetApplication()

        '*******************************************************************
        '// Use an SboGuiApi object to establish connection
        '// with the SAP Business One application and return an
        '// initialized appliction object
        '*******************************************************************

        Dim SboGuiApi As SAPbouiCOM.SboGuiApi
        Dim sConnectionString As String

        SboGuiApi = New SAPbouiCOM.SboGuiApi()

        '// by following the steped specified above the following
        '// statment should be suficient for either development or run mode

        sConnectionString = Command()

        '// connect to a running SBO Application

        SboGuiApi.Connect(sConnectionString)

        '// get an initialized application object

        SBO_Application = SboGuiApi.GetApplication()

    End Sub

    Private Sub SetFilters()

        '// Create a new EventFilters object
        oFilters = New SAPbouiCOM.EventFilters()

        '// add an event type to the container
        '// this method returns an EventFilter object
        oFilter = oFilters.Add(SAPbouiCOM.BoEventTypes.et_CLICK)

        '// assign the form type on which the event would be processed
        oFilter.AddEx("139") 'Orders Form
        oFilter.AddEx("142") 'Purchase Form

        oFilter = oFilters.Add(SAPbouiCOM.BoEventTypes.et_KEY_DOWN)

        '// assign the form type on which the event would be processed
        oFilter.Add(139) 'Orders Form

        '// For a list of all form types see the help or use the
        '// Tools -> User Tools -> Display Debug Information option
        '// in the SBO application
        '// then open the desired form and hover over it with the mouse
        '// the form's type will apear in the lower left side of the screen


        '// Setting the application with the EventFilters object
        '// in this case we will process a click event for form types 142 and 139
        '// and we will process a key down event for for form type 139

        SBO_Application.SetFilter(oFilters)

    End Sub
    Public Sub New()

        '//*************************************************************
        '// set SBO_Application with an initialized application object
        '//*************************************************************
        SetApplication()

        '//*************************************************************
        '// set SBO_Application with an initialized EventFilters object
        '//*************************************************************

        SetFilters()

    End Sub

    Private Sub SBO_Application_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles SBO_Application.AppEvent

        '//********************************************************************************
        '// the following are the events sent by the application
        '// (Ignore aet_ServerTermination)
        '// in order to implement your own code upon each of the events
        '// place you code instead of the matching message box statement
        '//********************************************************************************


        Select Case EventType

            Case SAPbouiCOM.BoAppEventTypes.aet_ShutDown

                SBO_Application.MessageBox("A Shut Down Event has been caught" _
                    & vbNewLine & "Terminating Add On...")

                '//**************************************************************
                '//
                '// Take care of terminating your AddOn application
                '//
                '//**************************************************************

                End



            Case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged

                SBO_Application.MessageBox("A Company Change Event has been caught")

                '//**************************************************************
                '// Check the new company name, if your add on was not meant for
                '// the new company terminate your AddOn
                '//    If SBO_Application.Company.Name Is Not "Company1" then
                '//         Close
                '//    End If
                '//**************************************************************

            Case SAPbouiCOM.BoAppEventTypes.aet_LanguageChanged

                SBO_Application.MessageBox("A Languge Change Event has been caught")

                '//**************************************************************
                '// Check the new language name, if your AddOn's items needs
                '// to be changed, take care of it at this point
                '//
                '//    Select Case SBO_Application.Language
                '//         Case ln_English:
                '//         Case ln_French:
                '//         Case ln_German:
                '//    End Select
                '//**************************************************************

        End Select

    End Sub


    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent

        '//********************************************************************************
        '// in order to activate your own forms instead of SAP Business One system forms
        '// process the menu event by your self
        '// change BubbleEvent to True so that SAP Business One won't process it
        '//********************************************************************************

        If pVal.beforeAction = True Then

            SBO_Application.SetStatusBarMessage( _
            "Menu item: " + pVal.menuUID + " sent an event BEFORE SAP Business One processes it.", SAPbouiCOM.BoMessageTime.bmt_Long, True)

            '// to stop SAP Business One from processing this event
            '// unmark the following statement

            '// BubbleEvent = True

        Else

            SBO_Application.SetStatusBarMessage( _
            "Menu item: " & pVal.menuUID & " sent an event AFTER SAP Business One processes it.", SAPbouiCOM.BoMessageTime.bmt_Long, True)

        End If

    End Sub

    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent


        '//**************************************************************************
        '// BubbleEvent sets the behavior of SAP Business One.
        '// False means that the application will not continue processing this event
        '// True is the default value
        '//**************************************************************************

        If pVal.formType <> 0 And pVal.Before_Action = True Then

            '//************************************************************
            '// the message box form type is 0
            '// I chose not to deal with events triggered by a message box
            '//************************************************************

            Select Case pVal.EventType

                '//************************************************************
                '// every event will open a message box with the event
                '// name and the form UID how sent it
                '//************************************************************

            Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED

                    '// Specifies a button release (After exit).

                    SBO_Application.MessageBox( _
                    "An et_ITEM_PRESSED has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_KEY_DOWN

                    '// Specifies a key down event.

                    SBO_Application.MessageBox( _
                    "An et_KEY_DOWN has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_GOT_FOCUS

                    '// Specifies an item got focus.

                    SBO_Application.MessageBox( _
                    "An et_GOT_FOCUS has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_LOST_FOCUS

                    '// Specifies an item lost focus.

                    SBO_Application.MessageBox( _
                    "An et_LOST_FOCUS has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_COMBO_SELECT

                    '// Specifies the selection of valid value in Combo Box.

                    SBO_Application.MessageBox( _
                    "An et_COMBO_SELECT has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_CLICK
                    '// Specifies Mouse Up on editable item.

                    SBO_Application.MessageBox( _
                    "An et_CLICK has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK
                    '// Specifies Mouse Up on editable item in time interval define by
                    '// SAP Business One as double-click.

                    SBO_Application.MessageBox( _
                    "An et_DOUBLE_CLICK has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_MATRIX_LINK_PRESSED
                    '// Specifies a link arrow within a matrix was pressed.

                    SBO_Application.MessageBox( _
                    "An et_MATRIX_LINK_PRESSED has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_MATRIX_COLLAPSE_PRESSED
                    '// Specifies collapsed or expanded list within a matrix.

                    SBO_Application.MessageBox( _
                    "An et_MATRIX_COLLAPSE_PRESSED has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_VALIDATE
                    '//Specifies an item validation event.

                    SBO_Application.MessageBox( _
                    "An et_VALIDATE has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_FORM_LOAD
                    '// Specifies that SAP Business One application opened a form.

                    SBO_Application.MessageBox( _
                    "An et_FORM_LOAD has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD
                    '// Specifies that SAP Business One application closed a form.

                    SBO_Application.MessageBox( _
                    "An et_FORM_UNLOAD has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_FORM_ACTIVATE
                    '// Specifies that the focus is set on the form.

                Case SAPbouiCOM.BoEventTypes.et_FORM_DEACTIVATE
                    '// Specifies that the form lost focus.

                Case SAPbouiCOM.BoEventTypes.et_FORM_CLOSE
                    '// Specifies that the form was closed by calling the Close event.

                    SBO_Application.MessageBox( _
                    "An et_FORM_CLOSE has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_FORM_RESIZE
                    '// Specifies resizing of the form.

                    SBO_Application.MessageBox( _
                    "An et_FORM_RESIZE has been sent by a form with the unique ID: " + FormUID)

                Case SAPbouiCOM.BoEventTypes.et_FORM_KEY_DOWN
                    '// Specifies a key press on an area which is not item of the form.

                    SBO_Application.MessageBox( _
                    "An et_FORM_KEY_DOWN has been sent by a form with the unique ID: " + FormUID)

            End Select
        End If
    End Sub
End Class


Thanks,
Denes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20160419/d38463bf/attachment-0001.html>


More information about the python-win32 mailing list