使用回调函数

来源:互联网 发布:拼多多源码 编辑:程序博客网 时间:2024/04/29 17:18

    一个回调函数本质上就是一个事件处理程序即由应用程序实现并且被系统调用。Windows应用程序代表性地实现了许多回调函数,每一个回调函数都是为了事件的纤细处理而设计。当一个事件被触发,系统通过调用适当的回调函数来通知应用程序。像其他函数一样回调函数有一个系统可以用来给应用程序传递更多的有关事件的详细消息的参数列表。一个常见的回调函数的例子就是窗口消息处理函数,这个函数被系统用来向拥有这个窗口的应用程序传递窗口消息。

    DirectX为了多种目的使用回调函数,例如,你的系统支持多种设备,DirectInput用一些设备对象来描述这每一个设备,这些对象中包含了设备的性能的详细信息,你的应用程序需要去枚举可用的设备并且检查这些设备对象良好地处理用户的所有输入。要做这样的枚举你必须实现DIEnumDeviceObjectCallback回调函数。

    你通过调用IDirectInputDevice8::EnumObjects来开始枚举并且传递给它一个指向你的IDEnumDeviceObjectCallback回调函数的指针,系统会为每一个设备调用这个回调函数,并且将传递一个包含有设备的性能信息的DIDEVICEOBJECTINSTANCE结构给它,之后你的回调函数去处理这些信息,它能返回DIENUM_CONTINUE去请求下一个设备对象,或者返回IDENUM_STOP停止枚举。

    DirectX为了多种目的使用许多其他的回调函数,为了得到详细信息,请看专门的DirectX组件的文档。

 

实现一个回调函数

    因为回调函数有不同的用途和使用方法,他们被一种与普通函数类似的方法写在合适的参考部分,无论如何,一个回调函数参考实际上是一个定义如何实现函数的模板,没有一个统一的API参考。回调函数的参考提供下列信息:

    。如何使用函数

    。函数的语法

    。每一个参数的解释

    。可能的返回值的解释

    你用该用CALLBACK或者WINAPI类型去声明回调函数,这两种方法任意一种都是合法的,你可以使用任意的函数名,在文件中使用这个名字仅仅是为了方便对不同的回调函数加以区分。

    请对照声明里的描述去实现回调函数,具体的实现将取决于特殊的函数和你的应用程序的需求,请看如何实现不同的回调函数的示例代码。

    传递一个回调函数的指针给合适的DirectX组件,DirectX组件就能使用这个函数指针去调用这个回调函数,你用这种方式传递一个函数的指针给另外的函数,因此你应该看特殊函数参考的详细文档。

    下列代码片段摘自DirectInput游戏手柄示例,它粗略揭示了被用来枚举游戏杆的DIEnumDeviceObjectsCallback回调函数的本质原理。

    // The function declaration
BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,
                                VOID* pContext );
...
// Pass the function pointer to DirectInput by calling the
// IDirectInputDevice9::EnumObjects method

if ( FAILED( hr = g_pJoystick->EnumObjects(EnumAxesCallback,
                                            (VOID*)hDlg,
                                            DIDFT_AXIS )));

...

// The function implementation
BOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,
                                VOID* pContext )
{
// Process the information passed in through the two parameters
// Return DIENUM_CONTINUE to request the next device object
// Return DIENUM_STOP to stop the enumeration
}

 

 

 

 

 

原文

 

 

 

Using Callback Functions

A callback function is essentially an event handler that is implemented by an application and called by the system. Windows applications typically implement multiple callback functions, each one designed for a particular set of events. When an event occurs, the system notifies the application by calling the appropriate callback function. The callback function also usually has a parameter list that the system can use to pass the application more detailed information about the event. The most common example of a callback function is the window procedure. This function is used by the system to pass Windows messages to the applications that owns the window.

DirectX uses callback functions for a variety of purposes. For example, your system supports multiple devices. DirectInput represents each device by a device object, that contains the details of the device's capabilities. Your application will typically need to enumerate the available devices and to examine the device objects in order to handle user input properly. To do this enumeration, you must implement a DIEnumDeviceObjectsCallback callback function.

You start the enumeration process by calling IDirectInputDevice8::EnumObjects and passing the method a pointer to your DIEnumDeviceObjectsCallback callback function. The system will call this function once for each device, and it will pass in a DIDEVICEOBJECTINSTANCE structure containing information about the device's capabilities. After your callback function has processed the information, it can return DIENUM_CONTINUE to request the next device object, or DIENUM_STOP to stop the enumeration.

DirectX uses a number of other callback functions for a variety of purposes. For details, see the documentation for the particular DirectX component.

Implementing a Callback Function

Because callback functions have different purposes and usage, they are documented in the appropriate reference section in much the same way as a regular function. However, a callback function reference is essentially a template that describes how to implement the function, not a normal API reference. The callback function reference provides the following information:

  • How the function is used
  • The function's syntax
  • An explanation of the information that will be contained by each parameter
  • An explanation of the possible return values

You should declare the function as a CALLBACK or WINAPI type. Either type is acceptable. You can use any function name. The name used in the documentation is just a convenient label for a particular callback function.

Implement the function according to the description in the reference. The implementation details will depend on the particular function and the requirements of your application. See the sample code for some examples of how to implement various callback functions.

Pass a pointer to the function to the appropriate DirectX component. The DirectX component can then use the function pointer to call the function. The way in which you pass this pointer varies from function to function, so you should see the particular function's reference for details.

The following code fragment is borrowed from the DirectInput Joystick sample. It sketches out the essential elements of a DIEnumDeviceObjectsCallback implementation that is used to enumerate the axes of a joystick.

// The function declarationBOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,                                VOID* pContext );...// Pass the function pointer to DirectInput by calling the // IDirectInputDevice9::EnumObjects methodif ( FAILED( hr = g_pJoystick->EnumObjects(EnumAxesCallback,                                             (VOID*)hDlg,                                            DIDFT_AXIS )));...// The function implementationBOOL CALLBACK EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,                                VOID* pContext ){// Process the information passed in through the two parameters// Return DIENUM_CONTINUE to request the next device object// Return DIENUM_STOP to stop the enumeration}

 

 

 

原创粉丝点击