Solved

How Get Events ?

Posted on 2001-06-27
15
603 Views
Last Modified: 2013-11-20
Hi Experts,
In VB we get the KeyPress , MouseMove Events directly to Edit (Text Box) ctrls. How this will get in MFC programming (VC++) ?
0
Comment
Question by:anij
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
That is also for VC. Using ClassWizard, View->ClassWizard->MFC ClassWizard dialog box appears, and you will find what you need here. Object IDs, Message...
0
 

Author Comment

by:anij
Comment Utility
But for Edit ctrl there is no KeyPress events
0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
Use EN_CHANGE for KeyPress event
0
 
LVL 7

Expert Comment

by:peterchen092700
Comment Utility
...and don't expect MFC to be like VB ;)
0
 
LVL 2

Expert Comment

by:Slordak
Comment Utility
The premise for Visual C++/MFC event handling is that you define certain handler functions which will handle various events, either at the control level or at the parent level.  The relationship between the event code and the function which is executed can either be established by use of one of MFC's automated wizards, or manually by inserting the correct code.  In some cases, the handler function has a fixed name (determined ahead of time) which you do not customize, and in fact all you have to do is inform the system that you will be providing a handler, rather than having the system use the appropriate default handler (which is called for all events for which you do not provide a handler).

For example, consider if you have a class named CSomeEdit derived from a CEdit control, which is declared as follows in the header file:

class CSomeEdit : public CEdit
{
public:

protected:
  // Generated message map functions
  //{{AFX_MSG(CSomeEdit)
        afx_msg void OnChar(UINT nChar, UINT nRepCnt,
          UINT nFlags);
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};

This is one way of indicating to the system that your class will have a handler function named OnChar().  In
this case, this is the reserved name for handling the
event ON_WM_CHAR.  One knows this because if one looks at the MFC reference, it shows:

ON_WM_CHAR()   afx_msg void OnChar(UINT, UINT, UINT);

  In any case, much of this can be handled automatically for you by the MFC wizard if you prefer.  That's the header section, then in the code file:

BEGIN_MESSAGE_MAP(CSomeEdit, CEdit)
  //{{AFX_MSG_MAP(CSomeEdit)
       ON_WM_CHAR()
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

Thus, now you've indicated that the CSomeEdit class
will handle the ON_WM_CHAR() event (which is posted
when someone enters a character into your edit box).  All you have to do now is implement the OnChar() function to do whatever you want to do when someone enters a character into your derived edit control.

Consult the documentation for further information on the types of events which different windows and controls can generate and which can be handled by program code.
0
 

Author Comment

by:anij
Comment Utility
Hi Slordak,
You are talking about the derived class of CEdit. How can the same thing is possible for CEdit class, or How to impliment this from the parent class CDialog
plz, help me.
0
 
LVL 7

Expert Comment

by:peterchen092700
Comment Utility
>> How to impliment this from the parent class CDialog
The Edit control sends certain notifications (like EN_CHANGE when contents changes) to the parent, where you can add a message handler for this. (usual way)

>> How can the same thing is possible for CEdit class
If you want to add additional message handling for a MFC class (e.g. the notifications are not enough), you need to dereive a class from CEdit and use this instead.

Note that MFC is only a small wrapper around Windows API here, this is roughly the way how windows works.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:anij
Comment Utility
Hi Slordak
After creating the derived class. How to set this class to the resource ctrl ?
I tried to create a variable with this class.
0
 
LVL 2

Accepted Solution

by:
Slordak earned 20 total points
Comment Utility
You may not want to derive a new class, as Peter Chen has indicated.  In many cases, the default class behavior might be sufficient.  In my case, since I usually wind up heavily customizing the behavior of the control, this is what I do.

To answer your question, though, create the parent dialog as usual from the dialog resource template (IDD_WHATEVER), and have an EDITTEXT control inside that dialog.  In your parent dialog class, declare a protected member variable of the derived control type and override the OnInitDialog function:

class CSomeDialogClass : CDialog
{
public:
  CSomeDialogClass() { Create(IDD_WHATEVER); }
  ~CSomeDialogClass() { return; }

...
  //{AFX_VIRTUAL(CSomeDialogClass)
  virtual BOOL OnInitDialog();
  //}}AFX_VIRTUAL
...

protected:
  CSomeEdit m_myEdit;
...
}

Then in your OnInitDialog routine, subclass the dialog edit control to be of your specific derived type:

BOOL CSomeDialogClass::OnInitDialog()
{
  CDialog::OnInitDialog();

  m_myEdit.SubclassDlgItem(IDC_SOMEEDIT, this);
}

This informs MFC that the generic edit control should actually be of the derived type that you've specified.  Now when someone presses a key while this edit control has focus, your handler function OnChar() will be called.  This may be overkill for what you're doing, though.  I can post a simpler example without using a new derived edit class and using DDX, perhaps.
0
 
LVL 2

Expert Comment

by:Slordak
Comment Utility
How about a simpler example?  By the way, much of this can be done through the ClassWizard.  I may be insane in that I often do it by hand.

Let's take the example where you just have a normal CEdit control inside a dialog class which you have created, and you're interested in having the parent dialog be notified when someone changes the contents of the CEdit control.

In the header file:

class CSomeOtherDialog : public CDialog
{
public:
  CSomeOtherDialog() { Create(IDD_MYDIALOG); }
  ~CSomeOtherDialog() { return; }

  //{{AFX_VIRTUAL(CSomeOtherDialog)
  virtual void DoDataExchange(CDataExchange *pDX);
  //}}AFX_VIRTUAL

protected:
  //{{AFX_MSG(CSomeOtherDialog)
  afx_msg void OnEnChange();
  //}}AFX_MSG
 
  CString m_editData;
};

In the code file:

BEGIN_MESSAGE_MAP(CSomeOtherDialog, CDialog)
  //{{AFX_MSG_MAP(CSomeOtherDialog)
  ON_EN_CHANGE(IDC_MYEDITCONTROL, OnEnChange)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CSomeOtherDialog::DoDataExchange(CDataExchange *pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CSomeOtherDialog)
  DDX_Text(pDX, IDC_MYEDITCONTROL, m_editData);
  //}}AFX_DATA_MAP
}

void OnEnChange()
{
  UpdateData(TRUE);

  /* Do whatever you want here... */
}


I think this is probably a better example for what you want to do, as this way you do not have to derive your own edit control class, but instead simply notify MFC that you wish to provide a handler for when the parent dialog receives an ON_EN_CHANGE event from the IDC_MYEDITCONTROL edit control.  In your handler function, you then use DDX to grab the updated data from the edit control and store it in m_editData, and you can then do any other processing which you like at this point.  Hopefully this makes sense.  Again, the AFX message map entries can be generated by using the ClassWizard if you prefer.
0
 

Author Comment

by:anij
Comment Utility
Hi Slodark,
I am not checked ur code. I will try it.

0
 

Author Comment

by:anij
Comment Utility
Hi Slodark,
But ur second method doesn't give the current key pressed.
which i need very much.
0
 
LVL 2

Expert Comment

by:Slordak
Comment Utility
In the second method (where one doesn't derive a new CEdit class), simply check the contents of the CString variable "m_editData" when inside the OnEnChange() function.  The DDX should have loaded the contents of the edit control into this variable in response to the UpdateData() function call.
0
 

Author Comment

by:anij
Comment Utility
Hi Slodak,

I want to know it is possible to recieve messaged from CEdit ctrl other than EN_ERRSPACE,EN_HSCROLL, EN_... (which is available in class wizards Message List for IDC_EDIT1 or ingeanaral for CEdit ctrl)
I want to get Messages like WM_CHAR,WM_MOUSEOVER etc... of CEdit ctrl.
I know it possible with a dirived class of CEdit. I don't want a dirived class I want it from CEdit itself any way ???
0
 
LVL 2

Expert Comment

by:Slordak
Comment Utility
In order to do this, you have to overload the behavior of the CEdit class, as far as I know.  As such, you would need to derive a new class, as in my first example, because it's at a slightly lower level than simply having the parent respond to edit-type messages from the child control.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now