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

MFC like subclassing via class inheritance

Expand Messages
  • offcity97
    Hi, in MFC if I want to override WM_CHAR message, I would do like class CMyEdit : public CEdit. However, in WTL how can I do such thing? Thanks in advance.
    Message 1 of 3 , Jun 3, 2002
      Hi, in MFC if I want to override WM_CHAR message,
      I would do like class CMyEdit : public CEdit.

      However, in WTL how can I do such thing?

      Thanks in advance.
    • umeca74
      ... you can do this but it won t help you trapping any messages. Instead you need to create your own edit subclass like: class CMyEdit : public
      Message 2 of 3 , Jun 5, 2002
        --- In wtl@y..., "offcity97" <offcity@n...> wrote:
        > Hi, in MFC if I want to override WM_CHAR message,
        > I would do like class CMyEdit : public CEdit.
        > However, in WTL how can I do such thing?

        you can do this but it won't help you trapping any messages.
        Instead you need to create your own edit subclass like:

        class CMyEdit : public CWindowImpl<CMyEdit, CEdit>
        {
        DECLARE_WND_SUPERCLASS(_T("MyEdit"), CEdit::GetWndClassName())

        BEGIN_MSG_MAP(CMyEdit)
        MESSAGE_HANDLER(WM_CHAR, OnChar)
        END_MSG_MAP()

        LRESULT OnChar(UINT uMsg, WPARAM wp, LPARAM lp, BOOL& bHandled)
        {
        ...
        }

        };

        It's a bit more work than MFC since you'll have to do your own
        message cracking (but take a look at WTL's atlcrack.h for assistance
        on this subject). Other than that, the SDK docs on WM_CHAR and all
        the other messages come to your rescue :)

        HTH
        Nikos
      • Jim Bucciferro
        You don t have to subclass the edit control, unless that is your intention. If you just want to trap the WM_CHAR message for an edit control that is used in a
        Message 3 of 3 , Jun 19, 2002
          You don't have to subclass the edit control, unless
          that is your intention.
          If you just want to trap the WM_CHAR message for an
          edit control that is used in a dialog:

          create a member variable for the control
          CContainedWindow m_edit;

          In your dialog's constructor:
          CMyDialog::CMyDialog() : m_edit(_T("Edit"),this,1)...
          {}

          create an alternate message map for the edit control
          BEGIN_MESSAGE_MAP(CMyDialog)
          ...
          ...
          ALT_MSG_MAP(1)
          MESSAGE_HANDLER(WM_CHAR, OnCharEdit)
          END_MESSAGE_MAP()

          create the handler as described in the previous
          answer.

          LRESULT OnCharEdit(UINT uMsg, WPARAM wp, LPARAM lp,
          BOOL& bHandled)
          {
          // Force numbers only - along with backspaces and tabs
          if( isdigit(wParam) || wParam == VK_BACK || wParam
          == VK_TAB )
          m_edit.DefWindowProc(WM_CHAR, wParam, lParam);

          SetValid(); // or whatever
          return 0;
          }

          This solution is for a single instance - subclassing
          the control as explained below is a better fit for all
          edit controls.

          JB



          --- umeca74 <umeca74@...> wrote:
          > --- In wtl@y..., "offcity97" <offcity@n...> wrote:
          > > Hi, in MFC if I want to override WM_CHAR message,
          > > I would do like class CMyEdit : public CEdit.
          > > However, in WTL how can I do such thing?
          >
          > you can do this but it won't help you trapping any
          > messages.
          > Instead you need to create your own edit subclass
          > like:
          >
          > class CMyEdit : public CWindowImpl<CMyEdit, CEdit>
          > {
          > DECLARE_WND_SUPERCLASS(_T("MyEdit"),
          > CEdit::GetWndClassName())
          >
          > BEGIN_MSG_MAP(CMyEdit)
          > MESSAGE_HANDLER(WM_CHAR, OnChar)
          > END_MSG_MAP()
          >
          > LRESULT OnChar(UINT uMsg, WPARAM wp, LPARAM lp,
          > BOOL& bHandled)
          > {
          > ...
          > }
          >
          > };
          >
          > It's a bit more work than MFC since you'll have to
          > do your own
          > message cracking (but take a look at WTL's
          > atlcrack.h for assistance
          > on this subject). Other than that, the SDK docs on
          > WM_CHAR and all
          > the other messages come to your rescue :)
          >
          > HTH
          > Nikos
          >
          >


          __________________________________________________
          Do You Yahoo!?
          Yahoo! - Official partner of 2002 FIFA World Cup
          http://fifaworldcup.yahoo.com
        Your message has been successfully submitted and would be delivered to recipients shortly.