VC callback function in the use of the transformation method http://tech.163.com 06:34:12 Source: vczx user comments 0 Forum
Writing callback functions is always one of the skills used to write special processing functions. The following describes how to use callback.
1. Set the callback function as a () (this is the simplest case, without parameters, but the actual situation of our application is often complicated ), the operation function using the callback function is B (), but function B requires parameters. This parameter is the address variable pointing to function A. This variable is generally a function pointer. Usage:
Int A (char * P); // callback function
Typedef int (* callback) (char * P); // declares the function pointer of the callback type.
Callback mycallback; // declare the function pointer variable
Mycallback = A; // obtain the address of function.
Function B is generally written as B (callback lpcall, char * P,...); // The parameter form after P is omitted here.
So the callback mechanism can be solved as: function B must complete certain functions, but he cannot implement all functions by himself. Function A is required, that is, the callback function. B's implementation is:
B (callback lpcall, char * pProvide)
{
............ // Self-implemented function Statement of B
Lpcall (ppprovide); // it is processed by the callback function, that is, function.
............ // Self-implemented function Statement of B
}
// -------------- Example -------------
Char * P = "Hello! ";
Callback mycallback;
Mycallback =;
B (A, P );
The above is the basic application of callback. The transformation mentioned in this article is actually the conversion between the caller class and the class where the callback function is located by passing in different function addresses.
1. Problem Description
The cuploadfile class completes data upload and displays the corresponding progress on the interface.
The main function is send (...) and the callback function getcurstate ();
Class cuploadfile: Public cdialog
{
......
Int send (maid IP, maid );
Static int getcurstate (INT ncurdone, int ninall, void * pparam );
......
}
Int cuploadfile: Send (maid IP, maid)
{
... // Export the function for data transmission
Int ret = upload (lpstr) (lpctstr) m_strdata,
Getcurstate, // process the interface in this callback function
This, // cuploadfile's own pointer, that is, the parameter accepted by pparam
(Lpstr) (lpctstr) uploadfilepath,
"",
"",
);
}
Int cuploadfile: getcurstate (INT ncurdata, int ninall, void * pparam)
{
.........
Uploadfile * pthis = (uploadfile *) pparam; // The amount of data currently transmitted by ncurdata
// Ninall total data volume
// With pthis, you can perform various operations on the interface.
.............
}
However, after careful observation, we can find that this class brings together data transmission and interface display, which is not easy to reuse. In addition, many changes are required in the reuse process.
Remember the static member functions of the class passed in by the current callback function.
Now we separate the data transfer from the display on the interface. Callback refers to the static function of the interface processing class.
Interface processing class cshowgui, data upload class cuploaddata
Class cuploaddata
{
......
Typedef int (* setuploadcaller) (INT ncurdata, int ninall, void * pparam );
Int uploadfile (lpctstr lpfilenamepath, lpvoid lparam, setuploadcaller caller );
// Accept external access parameters, mainly because the callback function address is passed through the caller parameter,
Int send (maid IP, maid );
... // Note that the getcurstate function is not required at this time.
}
Class cshowgui: Public cdialog
{
.......
Typedef int (* setuploadcaller) (INT ncurdata, int ninall, void * pparam );
Void setcallback (lpctstr strpath );
Static int getcurstate (INT ncurdata, int ninall, void * pparam );
Cuploaddata m_uploa
D; // The data upload class is a member variable of the interface display class.
.......
}
Void cshowgui: setcallback (lpctstr strpath)
{
Cuploaddata myuploaddata;
Setuploadcaller mycaller; // declare a function pointer variable
Mycaller = curstate; // get the address of the interface Handler
Myuploaddata. uploadfile (strpath, this, mycaller); // interface processing class function input, realizing the separation of data input and interface processing.
}
Through the above demonstration, the interface and data are separated, and the callback functions play different roles, so different problems should be handled flexibly, however, because the data processing class does not know the interface processing class or the type of the external call class, it cannot flexibly process different display methods of the interface. In this regard, I also hope that my friends who like to study technology will continue to study.