MFC's callback function

Source: Internet
Author: User

There should be two types of callback functions in MFC: One is the traditional callback function from C, which is not defined as a global function, but defined in a class, to add a static constraint, a common enumxxx (), and a message response function that implements a callback through a member function pointer.

Imagine a situation where the base class A triggers an event E and callbacks a defined function F for event handling (represented in MFC as a message response function). Subclasses B and C, which inherit from Class A, may have different ways of handling e, and therefore need to overwrite the function F of base class A. Naturally, we want to define f as a virtual function that is modified with virtual.

——————————————————————————————————————————————————————

A simple definition of a callback function is what you define as called by Windows. The following two functions are excerpted from "Programming Windows with MFC", in this case, regardless of function, there is an API function in Filllistbox, it calls the callback function is Enumfontfamproc, The declarative form of a callback function is generally relatively fixed and can be referred to MSDN.

[CPP]View PlainCopy print?
  1. static int CALLBACK enumfontfamproc (enumlogfont* lpelf,newtextmetric* lpntm, int nfonttype, LPARAM LPARAM);
  2. void Cmainwindow::filllistbox ()
  3. {
  4. M_wndlistbox.resetcontent ();
  5. CCLIENTDC DC (this);
  6. :: EnumFontFamilies (HDC) DC, NULL, (Fontenumproc) Enumfontfamproc, (LPARAM) this );
  7. }
  8. int CALLBACK Cmainwindow::enumfontfamproc (enumlogfont* lpelf,newtextmetric* lpntm, int nfonttype, LPARAM LParam)
  9. {
  10. cmainwindow* pWnd = (cmainwindow*) LParam;
  11. if ((pwnd->m_wndcheckbox.getcheck () = = bst_unchecked) | |  (nFontType & Truetype_fonttype))
  12. Pwnd->m_wndlistbox.addstring (Lpelf->elflogfont.lffacename);
  13. return 1;
  14. }

Note that the last argument in the function enumfontfamilies here is a pointer to this CMainWindow object, so why do you see that the Enumfontfamproc declaration is static, in C + + The static function cannot call a non-static member, so it is not very strange to pass a this here. But why declare the function as static, which is to blame for the particularity of C + +, it is well known that the C + + compiler will add a this pointer to the object when compiling, and in the member function call will append a parameter to save the this pointer, However, Windows ' callback functions are strictly defined as parameters that must be passed in the parameter list, and the parameter list will be inconsistent with the list of parameters that Windows expects when the this pointer is added, so it is declared static (the static member function does not pass the this pointer, This is always known, but the real time is always forgotten, alas).

It is also common to use the callback function in Windows, where many API functions that support callback functions support custom lparam parameters just like the enumfontfamilies here, which can pass this If you use an API function that does not support such a custom lparam parameter, you need other methods, and a simpler approach is to copy this as a global variable so that the callback function can be used.

MFC's callback function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.