Loading ...
Sorry, an error occurred while loading the content.

What's the use of PreTranslateMessage?

Expand Messages
  • enjoy_yjh
    I ve found in Mainfrm.h: virtual BOOL PreTranslateMessage(MSG* pMsg) { if(CFrameWindowImpl ::PreTranslateMessage(pMsg)) return TRUE; return FALSE; }
    Message 1 of 9 , Jan 31, 2007
    • 0 Attachment
      I've found in Mainfrm.h:

      virtual BOOL PreTranslateMessage(MSG* pMsg)
      {
      if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
      return TRUE;

      return FALSE;
      }

      and in ChildView.h:

      BOOL PreTranslateMessage(MSG* pMsg)
      {
      pMsg;
      return FALSE;
      }

      Can I delete them? What's its use?
    • Martin Martinsson
      The PreTranslateMsg method enables the message form to pre-translate any messages received by the message pump in Messaging. BYe ... [Non-text portions of this
      Message 2 of 9 , Jan 31, 2007
      • 0 Attachment
        The PreTranslateMsg method enables the message form to pre-translate any
        messages received by the message pump in Messaging.
        BYe


        On 2/1/07, enjoy_yjh <enjoy_yjh@...> wrote:
        >
        > I've found in Mainfrm.h:
        >
        > virtual BOOL PreTranslateMessage(MSG* pMsg)
        > {
        > if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
        > return TRUE;
        >
        > return FALSE;
        > }
        >
        > and in ChildView.h:
        >
        > BOOL PreTranslateMessage(MSG* pMsg)
        > {
        > pMsg;
        > return FALSE;
        > }
        >
        > Can I delete them? What's its use?
        >
        >
        >


        [Non-text portions of this message have been removed]
      • enjoy_yjh
        ... any ... So can I remove this code generated by appwizard if I don t need it in class CChildView ? BOOL PreTranslateMessage(MSG* pMsg) { pMsg; return FALSE;
        Message 3 of 9 , Feb 1, 2007
        • 0 Attachment
          --- In wtl@yahoogroups.com, "Martin Martinsson" <maddinthegreat@...>
          wrote:
          >
          > The PreTranslateMsg method enables the message form to pre-translate
          any
          > messages received by the message pump in Messaging.
          > BYe
          >
          >
          So can I remove this code generated by appwizard if I don't need it in
          class CChildView ?

          BOOL PreTranslateMessage(MSG* pMsg)
          {
          pMsg;
          return FALSE;
          }
        • Martin Martinsson
          It depends on whether your child form mut pretranslate messages. Whene not you can safely remove this. Bye Martin ... [Non-text portions of this message have
          Message 4 of 9 , Feb 1, 2007
          • 0 Attachment
            It depends on whether your child form mut pretranslate messages.
            Whene not you can safely remove this.

            Bye
            Martin

            On 2/2/07, enjoy_yjh <enjoy_yjh@...> wrote:
            >
            > --- In wtl@yahoogroups.com <wtl%40yahoogroups.com>, "Martin Martinsson"
            > <maddinthegreat@...>
            > wrote:
            > >
            > > The PreTranslateMsg method enables the message form to pre-translate
            > any
            > > messages received by the message pump in Messaging.
            > > BYe
            > >
            > >
            > So can I remove this code generated by appwizard if I don't need it in
            > class CChildView ?
            >
            > BOOL PreTranslateMessage(MSG* pMsg)
            > {
            > pMsg;
            > return FALSE;
            > }
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Roel Vanhout
            I ve always wondered too what this is for exactly. Can you explain a bit more on what you mean by pretranslate messages ? Is it to allow a form to catch
            Message 5 of 9 , Feb 2, 2007
            • 0 Attachment
              I've always wondered too what this is for exactly. Can you explain a bit
              more on what you mean by 'pretranslate messages'? Is it to allow a form
              to catch non-char keypresses (ctrl-something, alt-something, function
              keys etc.) or are there other uses?


              cheers,

              roel


              Martin Martinsson wrote:
              >
              >
              > It depends on whether your child form mut pretranslate messages.
              > Whene not you can safely remove this.
              >
              > Bye
              > Martin
              >
              > On 2/2/07, enjoy_yjh <enjoy_yjh@...
              > <mailto:enjoy_yjh%40yahoo.com.cn>> wrote:
              > >
              > > --- In wtl@yahoogroups.com <mailto:wtl%40yahoogroups.com>
              > <wtl%40yahoogroups.com>, "Martin Martinsson"
              > > <maddinthegreat@...>
              > > wrote:
              > > >
              > > > The PreTranslateMsg method enables the message form to pre-translate
              > > any
              > > > messages received by the message pump in Messaging.
              > > > BYe
              > > >
              > > >
              > > So can I remove this code generated by appwizard if I don't need it in
              > > class CChildView ?
              > >
              > > BOOL PreTranslateMessage(MSG* pMsg)
              > > {
              > > pMsg;
              > > return FALSE;
              > > }
              > >
              > >
              > >
              >
              > [Non-text portions of this message have been removed]
              >
              >


              --
              Roel Vanhout, RIKS BV
              Abtstraat 2a, 6211 LS Maastricht
              +31 (0)43 388 3708
            • Ryan Ginstrom
              ... For example, if you are hosting an ActiveX control (like IE), you can pass it messages here so it can do its own keyboard/mouse handling. Keeping the
              Message 6 of 9 , Feb 2, 2007
              • 0 Attachment
                > Behalf Of Roel Vanhout
                > I've always wondered too what this is for exactly. Can you
                > explain a bit more on what you mean by 'pretranslate
                > messages'? Is it to allow a form to catch non-char keypresses
                > (ctrl-something, alt-something, function keys etc.) or are
                > there other uses?

                For example, if you are hosting an ActiveX control (like IE), you can pass
                it messages here so it can do its own keyboard/mouse handling.

                Keeping the do-nothing function in generic views makes it easy to swap in
                other views later. That's what supporting the interface is all about...

                In fact, I would leave off deleting such functions until I was ready to
                ship, and even then I'd want some profiling data to show it was worthwhile.

                Regards,
                Ryan

                --
                Ryan Ginstrom
              • Michael Stephenson
                I m definitely no expert, but I ll take a stab at this... Your app has a message pump which processes messages (this comes from the CMessageLoop class). A
                Message 7 of 9 , Feb 2, 2007
                • 0 Attachment
                  I'm definitely no expert, but I'll take a stab at this...

                  Your app has a message pump which processes messages (this comes from the
                  CMessageLoop class). A standard SDK message loop might look like this:

                  while(::GetMessage(&msg, NULL, 0, 0))
                  {
                  ::TranslateMessage(&msg);
                  ::DispatchMessage(&msg);
                  }

                  It just runs continuously until a WM_QUIT is received, and then the loop
                  exits and typically then so does your app.

                  In WTL (and also in MFC), the message loop is a bit spruced up so that it
                  first calls ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) to get a look at
                  the messages before they are translated and dispatched.

                  In WTL, the CMessageLoop member of your _Module maintains an array of
                  message filters from classes that are interested in getting a "peek" at
                  messages before they are translated and dispatched. The class peeking at a
                  message can return a boolean to indicate whether the message has been
                  handled by the class (TRUE) or that the message should continue on through
                  the message routing (FALSE). If a message is handled by a class object in
                  PreTranslateMessage, it is eaten at that point and not subsequently
                  dispatched through the message routing chain.

                  When a class wants to have the ability to get a peek at messages, it has to
                  call _Module.GetMessageLoop()->AddMessageFilter(this) somewhere during it's
                  initialization, typically in a constructor or in it's Create method. For
                  some WTL classes, this may already be occurring in the standard base class
                  (such as in CFrameWindowImpl), but in most classes that you create you will
                  have to derive from CMessageFilter and then make the AddMessageFilter call
                  in your derived class.

                  This adds the class to the array of classes that want to get a peek at
                  messages. The class must also implement PreTranslateMessage, because this
                  is the virtual method that the message loop calls for each entry in the
                  array of message filters until one of them returns TRUE indicating that the
                  message was handled, or until the end of the array is reached, in which case
                  normal message routing takes place (the message is dispatched).

                  Deriving a class from CMessageFilter just brings in this one virtual method,
                  PreTranslateMessage, which must be implemented by the derived class. Note
                  that this is a virtual method and is called by the "framework" as part of
                  the message pumping/filter process; PreTranslateMessage does not get called
                  in response to a particular message as a standard message handler would.
                  Note also that if a class adds itself to the message filter array during
                  initialization, it should also remove itself during deconstruction,
                  otherwise the message loop would attempt to call the class'
                  PreTranslateMessage method after the class object no longer exists, which
                  will cause a fault due to a bad pointer.

                  So, what is the use of PreTranslateMessage? Think of it, again, as getting
                  a peek at a message before it is dispatched and having the ability to
                  process the message yourself and then even decide whether the message gets
                  dispatched at all. I can't think of any cases offhand where the only way to
                  solve a problem is to use PreTranslateMessage, but in my limited experience
                  PreTranslateMessage has mostly been a convenience.

                  For example, let's say we have an edit control class that we have created,
                  and we want to not allow certain keystrokes in the control.
                  PreTranslateMessage gives us an easy way to handle the WM_CHAR message
                  ourselves, before it is dispatched, and eat it if we like, preventing the
                  underlying subclassed control from even seeing it.

                  As another example, let's say we have a frame window that has a dialog-based
                  child window based on CDialogImpl (in MFC, this might be called a
                  CFormView). Let's say we want to hide the main window if the user presses
                  the Esc key. By the default message routing, the dialog will get first
                  crack as the message and handle it in it's OnCancel handler. If one is not
                  provided, the CDialogImpl default handler will close the dialog, thus
                  closing the main frame. So, we might want to, in the frame, use
                  PreTranslateMessage to get a crack at the Esc keypress before the dialog
                  even sees it and have the frame hide itself and return TRUE so that the
                  message gets eaten then and there. This could be done in the dialog in a
                  custom OnCancel handler, which could then send a message to the parent to
                  hide itself, but PreTranslateMessage can be a lot simpler, especially if
                  there are multiple parents between the dialog and the main frame (which
                  would involve each parent forwarding the message to it's parent until the
                  frame gets the message).

                  There are certainly better examples of it's usefulness, but hopefully that
                  helps.

                  _____

                  From: wtl@yahoogroups.com [mailto:wtl@yahoogroups.com] On Behalf Of Roel
                  Vanhout
                  Sent: Friday, February 02, 2007 3:34 AM
                  To: wtl@yahoogroups.com
                  Subject: Re: [wtl] Re: What's the use of PreTranslateMessage?




                  I've always wondered too what this is for exactly. Can you explain a bit
                  more on what you mean by 'pretranslate messages'? Is it to allow a form
                  to catch non-char keypresses (ctrl-something, alt-something, function
                  keys etc.) or are there other uses?

                  cheers,

                  roel

                  Martin Martinsson wrote:
                  >
                  >
                  > It depends on whether your child form mut pretranslate messages.
                  > Whene not you can safely remove this.
                  >
                  > Bye
                  > Martin
                  >
                  > On 2/2/07, enjoy_yjh <enjoy_yjh@yahoo. <mailto:enjoy_yjh%40yahoo.com.cn>
                  com.cn
                  > <mailto:enjoy_yjh%40yahoo.com.cn>> wrote:
                  > >
                  > > --- In wtl@yahoogroups. <mailto:wtl%40yahoogroups.com> com
                  <mailto:wtl%40yahoogroups.com>
                  > <wtl%40yahoogroups.com>, "Martin Martinsson"
                  > > <maddinthegreat@...>
                  > > wrote:
                  > > >
                  > > > The PreTranslateMsg method enables the message form to pre-translate
                  > > any
                  > > > messages received by the message pump in Messaging.
                  > > > BYe
                  > > >
                  > > >
                  > > So can I remove this code generated by appwizard if I don't need it in
                  > > class CChildView ?
                  > >
                  > > BOOL PreTranslateMessage(MSG* pMsg)
                  > > {
                  > > pMsg;
                  > > return FALSE;
                  > > }
                  > >
                  > >
                  > >
                  >
                  > [Non-text portions of this message have been removed]
                  >
                  >

                  --
                  Roel Vanhout, RIKS BV
                  Abtstraat 2a, 6211 LS Maastricht
                  +31 (0)43 388 3708






                  [Non-text portions of this message have been removed]
                • Roel Vanhout
                  Great explanation, thank you Michael I understand now. cheers, roel ... -- Roel Vanhout, RIKS BV Abtstraat 2a, 6211 LS Maastricht +31 (0)43 388 3708
                  Message 8 of 9 , Feb 5, 2007
                  • 0 Attachment
                    Great explanation, thank you Michael I understand now.


                    cheers,

                    roel



                    Michael Stephenson wrote:
                    >
                    >
                    > I'm definitely no expert, but I'll take a stab at this...
                    >
                    > Your app has a message pump which processes messages (this comes from the
                    > CMessageLoop class). A standard SDK message loop might look like this:
                    >
                    > while(::GetMessage(&msg, NULL, 0, 0))
                    > {
                    > ::TranslateMessage(&msg);
                    > ::DispatchMessage(&msg);
                    > }
                    >
                    > It just runs continuously until a WM_QUIT is received, and then the loop
                    > exits and typically then so does your app.
                    >
                    > In WTL (and also in MFC), the message loop is a bit spruced up so that it
                    > first calls ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) to get a look at
                    > the messages before they are translated and dispatched.
                    >
                    > In WTL, the CMessageLoop member of your _Module maintains an array of
                    > message filters from classes that are interested in getting a "peek" at
                    > messages before they are translated and dispatched. The class peeking at a
                    > message can return a boolean to indicate whether the message has been
                    > handled by the class (TRUE) or that the message should continue on through
                    > the message routing (FALSE). If a message is handled by a class object in
                    > PreTranslateMessage, it is eaten at that point and not subsequently
                    > dispatched through the message routing chain.
                    >
                    > When a class wants to have the ability to get a peek at messages, it has to
                    > call _Module.GetMessageLoop()->AddMessageFilter(this) somewhere during it's
                    > initialization, typically in a constructor or in it's Create method. For
                    > some WTL classes, this may already be occurring in the standard base class
                    > (such as in CFrameWindowImpl), but in most classes that you create you will
                    > have to derive from CMessageFilter and then make the AddMessageFilter call
                    > in your derived class.
                    >
                    > This adds the class to the array of classes that want to get a peek at
                    > messages. The class must also implement PreTranslateMessage, because this
                    > is the virtual method that the message loop calls for each entry in the
                    > array of message filters until one of them returns TRUE indicating that the
                    > message was handled, or until the end of the array is reached, in which case
                    > normal message routing takes place (the message is dispatched).
                    >
                    > Deriving a class from CMessageFilter just brings in this one virtual method,
                    > PreTranslateMessage, which must be implemented by the derived class. Note
                    > that this is a virtual method and is called by the "framework" as part of
                    > the message pumping/filter process; PreTranslateMessage does not get called
                    > in response to a particular message as a standard message handler would.
                    > Note also that if a class adds itself to the message filter array during
                    > initialization, it should also remove itself during deconstruction,
                    > otherwise the message loop would attempt to call the class'
                    > PreTranslateMessage method after the class object no longer exists, which
                    > will cause a fault due to a bad pointer.
                    >
                    > So, what is the use of PreTranslateMessage? Think of it, again, as getting
                    > a peek at a message before it is dispatched and having the ability to
                    > process the message yourself and then even decide whether the message gets
                    > dispatched at all. I can't think of any cases offhand where the only way to
                    > solve a problem is to use PreTranslateMessage, but in my limited experience
                    > PreTranslateMessage has mostly been a convenience.
                    >
                    > For example, let's say we have an edit control class that we have created,
                    > and we want to not allow certain keystrokes in the control.
                    > PreTranslateMessage gives us an easy way to handle the WM_CHAR message
                    > ourselves, before it is dispatched, and eat it if we like, preventing the
                    > underlying subclassed control from even seeing it.
                    >
                    > As another example, let's say we have a frame window that has a dialog-based
                    > child window based on CDialogImpl (in MFC, this might be called a
                    > CFormView). Let's say we want to hide the main window if the user presses
                    > the Esc key. By the default message routing, the dialog will get first
                    > crack as the message and handle it in it's OnCancel handler. If one is not
                    > provided, the CDialogImpl default handler will close the dialog, thus
                    > closing the main frame. So, we might want to, in the frame, use
                    > PreTranslateMessage to get a crack at the Esc keypress before the dialog
                    > even sees it and have the frame hide itself and return TRUE so that the
                    > message gets eaten then and there. This could be done in the dialog in a
                    > custom OnCancel handler, which could then send a message to the parent to
                    > hide itself, but PreTranslateMessage can be a lot simpler, especially if
                    > there are multiple parents between the dialog and the main frame (which
                    > would involve each parent forwarding the message to it's parent until the
                    > frame gets the message).
                    >
                    > There are certainly better examples of it's usefulness, but hopefully that
                    > helps.
                    >
                    > _____
                    >
                    > From: wtl@yahoogroups.com <mailto:wtl%40yahoogroups.com>
                    > [mailto:wtl@yahoogroups.com <mailto:wtl%40yahoogroups.com>] On Behalf Of
                    > Roel
                    > Vanhout
                    > Sent: Friday, February 02, 2007 3:34 AM
                    > To: wtl@yahoogroups.com <mailto:wtl%40yahoogroups.com>
                    > Subject: Re: [wtl] Re: What's the use of PreTranslateMessage?
                    >
                    > I've always wondered too what this is for exactly. Can you explain a bit
                    > more on what you mean by 'pretranslate messages'? Is it to allow a form
                    > to catch non-char keypresses (ctrl-something, alt-something, function
                    > keys etc.) or are there other uses?
                    >
                    > cheers,
                    >
                    > roel
                    >
                    > Martin Martinsson wrote:
                    > >
                    > >
                    > > It depends on whether your child form mut pretranslate messages.
                    > > Whene not you can safely remove this.
                    > >
                    > > Bye
                    > > Martin
                    > >
                    > > On 2/2/07, enjoy_yjh <enjoy_yjh@yahoo. <mailto:enjoy_yjh%40yahoo.com.cn>
                    > com.cn
                    > > <mailto:enjoy_yjh%40yahoo.com.cn>> wrote:
                    > > >
                    > > > --- In wtl@yahoogroups. <mailto:wtl%40yahoogroups.com> com
                    > <mailto:wtl%40yahoogroups.com>
                    > > <wtl%40yahoogroups.com>, "Martin Martinsson"
                    > > > <maddinthegreat@...>
                    > > > wrote:
                    > > > >
                    > > > > The PreTranslateMsg method enables the message form to pre-translate
                    > > > any
                    > > > > messages received by the message pump in Messaging.
                    > > > > BYe
                    > > > >
                    > > > >
                    > > > So can I remove this code generated by appwizard if I don't need it in
                    > > > class CChildView ?
                    > > >
                    > > > BOOL PreTranslateMessage(MSG* pMsg)
                    > > > {
                    > > > pMsg;
                    > > > return FALSE;
                    > > > }
                    > > >
                    > > >
                    > > >
                    > >
                    > > [Non-text portions of this message have been removed]
                    > >
                    > >
                    >
                    > --
                    > Roel Vanhout, RIKS BV
                    > Abtstraat 2a, 6211 LS Maastricht
                    > +31 (0)43 388 3708
                    >
                    > [Non-text portions of this message have been removed]
                    >
                    >


                    --
                    Roel Vanhout, RIKS BV
                    Abtstraat 2a, 6211 LS Maastricht
                    +31 (0)43 388 3708
                  • toddmarshall2002
                    We use PreTranslateMessage to get WM_KEYUP status of any key; shift, control, or any character key also. for WM_KEYDOWN we monitor tab, shift, control, delete,
                    Message 9 of 9 , Feb 14, 2008
                    • 0 Attachment
                      We use PreTranslateMessage to get WM_KEYUP status of any key; shift,
                      control, or any character key also.

                      for WM_KEYDOWN we monitor tab, shift, control, delete, F-keys, etc.

                      Perhaps implementing WM_KEYUP and WM_KEYDOWN message handlers would've
                      been a better idea for doing this.
                      t.
                    Your message has been successfully submitted and would be delivered to recipients shortly.