"Go" Windows message Delivery Process: General window message Delivery (Wm_lbuttonclick)

Source: Internet
Author: User

Original URL: http://blog.csdn.net/hyhnoproblem/article/details/6182646

This example explains the delivery process for a generic window message by adding a Wm_lbuttonclick message handler function to the view of a single document program. Based on vs 2005

[CPP]View Plaincopy
    1. Begin_message_map (CMyView, CView)
    2. ON_WM_LBUTTONDBLCLK ()
    3. End_message_map ()
    4. ON_WM_LBUTTONDBLCLK macro Expansion
    5. #define ON_WM_LBUTTONDBLCLK ()/
    6. {wm_lbuttondblclk, 0, 0, 0, AFXSIG_VWP,/
    7. (afx_pmsg) (AFX_PMSGW)/
    8. (static_cast< void (afx_msg_call CWnd::*) (UINT, CPoint) > (&thisclass:: ONLBUTTONDBLCLK))},

As you can see from the code above, the type label for the Wm_lbuttonclick message is AFXSIG_VWP and there is an enumeration type Afxsig in Afxmsg_.h, and the message label is used primarily to differentiate the type of the message handler function. The value of AFXSIG_VWP is defined in this type:

[CPP]View Plaincopy
    1. Enum Afxsig
    2. {
    3. //...  
    4. AFXSIG_VWP = afxsig_v_w_p
    5. //...  
    6. }

In AfxWndProc, the handle in the message is mapped to a window class pointer, which points to cmyview. AfxWndProc Call Afxcallwndproc,afxcallwndproc call Cwnd::windowproc,cwnd::windowproc call Cwnd::onwndmsg,cwnd::o Nwndmsg completes the processing of the message.

[CPP]View Plaincopy
  1. Wincore.cpp 1746
  2. BOOL cwnd::onwndmsg (UINT message, WPARAM WPARAM, LPARAM LPARAM, lresult* pResult)
  3. {
  4. // ...  
  5. //Getmessagemap is a virtual function because the current pointer is pointing to CMyView, so the CMyView message map is taken
  6. const afx_msgmap* pmessagemap; pmessagemap = Getmessagemap ();
  7. UINT ihash; ihash = (LoWord ((dword_ptr) pmessagemap) ^ message) & (IHASHMAX-1);
  8. Winmsglock.lock (Crit_winmsgcache);
  9. //Global message hash cache, lookup cache
  10. Afx_msg_cache* Pmsgcache; Pmsgcache = &_afxMsgCache[iHash];
  11. Const afx_msgmap_entry* Lpentry;
  12. if (message = = Pmsgcache->nmsg && Pmessagemap = = Pmsgcache->pmessagemap)
  13. {
  14. //Cache hit
  15. Lpentry = pmsgcache->lpentry;
  16. Winmsglock.unlock ();
  17. if (lpentry = = NULL)
  18. return FALSE;
  19. //cache hit, and it needs to be handled
  20. if (Message < 0xc000)
  21. Goto Ldispatch;  //System messages?
  22. Else
  23. Goto ldispatchregistered;  //Registered message?
  24. }
  25. Else
  26. {
  27. //Not in the cache, look for it
  28. pmsgcache->nmsg = message;
  29. Pmsgcache->pmessagemap = Pmessagemap;
  30. For (/* Pmessagemap already init ' ed */; Pmessagemap->pfngetbasemap! = NULL;
  31. Pmessagemap = (*pmessagemap->pfngetbasemap) ())
  32. {
  33. //Note:catch common but fatal mistake!!
  34. //Begin_message_map (Cmywnd, Cmywnd)
  35. ASSERT (Pmessagemap! = (*pmessagemap->pfngetbasemap));
  36. if (Message < 0xc000)
  37. {
  38. //Constant window message
  39. if (lpentry = Afxfindmessageentry (pmessagemap->lpentries,
  40. Message, 0, 0))! = NULL)
  41. {
  42. Pmsgcache->lpentry = Lpentry;
  43. Winmsglock.unlock ();
  44. Goto Ldispatch;
  45. }
  46. }
  47. Else
  48. {
  49. //Registered Windows message
  50. Lpentry = pmessagemap->lpentries;
  51. While ((Lpentry = Afxfindmessageentry (lpentry, 0xc000, 0, 0)) = NULL)
  52. {
  53. uint* Pnid = (uint*) (LPENTRY->NSIG);
  54. ASSERT (*pnid >= 0xc000 | | *pnid = = 0);
  55. //Must be successfully registered
  56. if (*pnid = = message)
  57. {
  58. Pmsgcache->lpentry = Lpentry;
  59. Winmsglock.unlock ();
  60. Goto ldispatchregistered;
  61. }
  62. lpentry++; //Keep looking past this one
  63. }
  64. }
  65. }
  66. Pmsgcache->lpentry = NULL;
  67. Winmsglock.unlock ();
  68. return FALSE;
  69. }
  70. // ...  
  71. Ldispatch:
  72. MMF.PFN = lpentry->pfn;
  73. switch (LPENTRY->NSIG)
  74. {
  75. //...  
  76. Case afxsig_v_u_p: //Message label
  77. {
  78. CPoint Point (LParam);
  79. (this->*mmf.pfn_v_u_p) (static_cast<uint> (WParam), point);
  80. }
  81. Break ;
  82. //...  
  83. }
  84. //...  
  85. Ldispatchregistered: //For registered Windows messages
  86. ASSERT (Message >= 0xc000);
  87. ASSERT (sizeof (MMF) = = sizeof (MMF.PFN));
  88. MMF.PFN = lpentry->pfn;
  89. LResult = (this->*mmf.pfn_l_w_l) (WParam, LParam);
  90. }
  91. Message-handling function type Enumeration
  92. Union messagemapfunctions
  93. {
  94. Afx_pmsg PFN; //generic member function pointer
  95. // ...  
  96. LRESULT (afx_msg_call CWnd::* pfn_l_w_l) (WPARAM, LPARAM);
  97. };

"Go" Windows message Delivery Process: General window message Delivery (Wm_lbuttonclick)

Related Article

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.