Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Message Queues for MFC windows classes

Message Queues for MFC windows classes

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi We know that Windows keeps a message queue for every thread. If i am not wrong for win32 applications, only one message loop is enough. But in MFC applications, we have Message Map macros(DECLARE_MESSAGE_MAP/BEGIN_MESSAGE_MAP/END_MESSAGE_MAP) for every window class. Mainfrm, chidfrm, childview, doc etc.. Do all of the message map macros that we see in the source code indicate there is a message loop that buried in MFC source code? I couldn't make relation between win32 message handling and MFC message handling. Thanks...

    M 1 Reply Last reply
    0
    • S sawerr

      Hi We know that Windows keeps a message queue for every thread. If i am not wrong for win32 applications, only one message loop is enough. But in MFC applications, we have Message Map macros(DECLARE_MESSAGE_MAP/BEGIN_MESSAGE_MAP/END_MESSAGE_MAP) for every window class. Mainfrm, chidfrm, childview, doc etc.. Do all of the message map macros that we see in the source code indicate there is a message loop that buried in MFC source code? I couldn't make relation between win32 message handling and MFC message handling. Thanks...

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      sawerr wrote:

      We know that Windows keeps a message queue for every thread.

      Only if you call a function that causes a queue to be created on a thread.

      sawerr wrote:

      for win32 applications, only one message loop is enough.

      Yes, one is sufficient, but not necessary unless you create at least one window. For example, you don't need a message loop in a console app.

      sawerr wrote:

      Do all of the message map macros that we see in the source code indicate there is a message loop that buried in MFC source code?

      Yes, but not a separate message loop for each window/class. There's still one per UI thread. See the source code for CWinThread::Run() - the per-thread message loop is there. The CWinApp class derives from CWinThread, so for an MFC GUI app, your required CWinApp object generally provides the first/main message loop. Once messages are extracted from the queue in a message loop, then they are dispatched to their destination window via a call to the destination window's windowproc (window procedure). That's basic Windows windowing[^]. The message map table macros generate compiled code that maps a message received in the windowproc to a call to the appropriate window class method. Hope that makes a little sense. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        sawerr wrote:

        We know that Windows keeps a message queue for every thread.

        Only if you call a function that causes a queue to be created on a thread.

        sawerr wrote:

        for win32 applications, only one message loop is enough.

        Yes, one is sufficient, but not necessary unless you create at least one window. For example, you don't need a message loop in a console app.

        sawerr wrote:

        Do all of the message map macros that we see in the source code indicate there is a message loop that buried in MFC source code?

        Yes, but not a separate message loop for each window/class. There's still one per UI thread. See the source code for CWinThread::Run() - the per-thread message loop is there. The CWinApp class derives from CWinThread, so for an MFC GUI app, your required CWinApp object generally provides the first/main message loop. Once messages are extracted from the queue in a message loop, then they are dispatched to their destination window via a call to the destination window's windowproc (window procedure). That's basic Windows windowing[^]. The message map table macros generate compiled code that maps a message received in the windowproc to a call to the appropriate window class method. Hope that makes a little sense. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        sawerr
        wrote on last edited by
        #3

        For example if we want to use Edit control in win32 app, the code will be:

        hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
        0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
        //http://www.winprog.org/tutorial/app_one.html[^]

        Here we create a "control". That control has a message loop in its source code which was written by controls developer. If an event happens, it is sent to its module(for example user32.dll) not to our application. It sends to our program a notification. OK. But in MFC, We use CEditView which is derived from CView. So we create a new window. And CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",...) is in the CEditView. Has that view window got a message loop too? If no, how can we handle for example WM_CLOSE

        //class CTextView : public CEditView

        void CTextView::OnClose()
        {
        // TODO: Add your message handler code here and/or call default

        CEditView::OnClose();
        

        }

        You said:

        Mark Salsbery wrote:

        The CWinApp class derives from CWinThread, so for an MFC GUI app, your required CWinApp object generally provides the first/main message loop

        If view window has a message loop, is that mean it is created by a new thread? For example, When user close this CeditView window, is this message sent to Main Window or View Window? If it is sent to main window, does main window route it to view window? I'm sorry, i really don't solve/understand the mechanism... Thank you.

        M 1 Reply Last reply
        0
        • S sawerr

          For example if we want to use Edit control in win32 app, the code will be:

          hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
          WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
          0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
          //http://www.winprog.org/tutorial/app_one.html[^]

          Here we create a "control". That control has a message loop in its source code which was written by controls developer. If an event happens, it is sent to its module(for example user32.dll) not to our application. It sends to our program a notification. OK. But in MFC, We use CEditView which is derived from CView. So we create a new window. And CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",...) is in the CEditView. Has that view window got a message loop too? If no, how can we handle for example WM_CLOSE

          //class CTextView : public CEditView

          void CTextView::OnClose()
          {
          // TODO: Add your message handler code here and/or call default

          CEditView::OnClose();
          

          }

          You said:

          Mark Salsbery wrote:

          The CWinApp class derives from CWinThread, so for an MFC GUI app, your required CWinApp object generally provides the first/main message loop

          If view window has a message loop, is that mean it is created by a new thread? For example, When user close this CeditView window, is this message sent to Main Window or View Window? If it is sent to main window, does main window route it to view window? I'm sorry, i really don't solve/understand the mechanism... Thank you.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          sawerr wrote:

          Here we create a "control". That control has a message loop in its source code which was written by controls developer.

          No, it doesn't. It has a window procedure implemented by the control developer. You are still responsible for providing a message loop. You're confusing message loops with window procedures. Window procs are related to the window class. MFC subclasses windows so the MFC framework gets first peek at messages before forwarding unhandled (i.e. no message map entry) messages to the original window proc.

          sawerr wrote:

          If view window has a message loop, is that mean it is created by a new thread?

          That doesn't make sense. Windows don't have message loops. Threads can implement a message loop. Windows have window procedures, which are functions called by the system to send a window a message. Study the link I gave you on windowing. It's the fundamentals of Windows programming, and in my opinion, essential to know and understand completely to be an effective Windows programmer. If you need more drawn-out documentation on how it all works, then I recommend the book "Windows Programming" by Charles Petzold. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            sawerr wrote:

            Here we create a "control". That control has a message loop in its source code which was written by controls developer.

            No, it doesn't. It has a window procedure implemented by the control developer. You are still responsible for providing a message loop. You're confusing message loops with window procedures. Window procs are related to the window class. MFC subclasses windows so the MFC framework gets first peek at messages before forwarding unhandled (i.e. no message map entry) messages to the original window proc.

            sawerr wrote:

            If view window has a message loop, is that mean it is created by a new thread?

            That doesn't make sense. Windows don't have message loops. Threads can implement a message loop. Windows have window procedures, which are functions called by the system to send a window a message. Study the link I gave you on windowing. It's the fundamentals of Windows programming, and in my opinion, essential to know and understand completely to be an effective Windows programmer. If you need more drawn-out documentation on how it all works, then I recommend the book "Windows Programming" by Charles Petzold. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            S Offline
            S Offline
            sawerr
            wrote on last edited by
            #5

            Mark Salsbery wrote:

            You're confusing message loops with window procedures.

            Yes, that's right. And now i understand. -One loop -Get Message and -DispatchMessage finds the right window procedure. Thank you for help... :rose:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups