• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

Sending message to parent window

Hi,

I got the following situation. In my dialog based application, I have placed some controls that are derived from CEdit (I added some features to the CEdit class). In that derived class, I catch the keydown message and want to add functionality to the F2 key. The task should be to delete the content of ALL CEdit derived controls in the dialog. Therefore, I want to send a custom message to the dialog class to get it delete all editboxes for me.

So this is what I've done:

1. I declared my own message in resoure.h:
#define IDM_RESET 32771

2. I try to send a message to the parent window (in the OnKeyDown-method of the CEdit derived class):
if (nChar == VK_F2) {
   ::PostMessage(GetParent()->GetSafeHwnd(),IDM_RESET,(WPARAM)0, (LPARAM)0);
}

3. In my dialog class, I added a routine to handle IDM_RESET messages using the class wizard:
ON_COMMAND(IDM_RESET, OnReset)  in the cpp-file
and
afx_msg void OnReset();   in the header file

The OnReset method is never called when I press the F2 button. I suppose I'm posting the message the wrong way, but I have no idea how to address the message to the parent window.

Thanks for any help,
Martin
0
marb
Asked:
marb
  • 2
2 Solutions
 
AndyAinscowFreelance programmer / ConsultantCommented:
I'll bet the line
   ::PostMessage(GetParent()->GetSafeHwnd(),IDM_RESET,(WPARAM)0, (LPARAM)0);
is never being called.

Try trapping the WM_SYSKEYDOWN message OR put the code into a PreTranslateMessage handler

BOOL CMyclass::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message == WM_KEYDOWN)
      {
            if(pMsg->wParam == VK_F2)
                      ::PostMessage(GetParent()->GetSafeHwnd(),IDM_RESET,(WPARAM)0, (LPARAM)0);
    }
  }
.....
}
0
 
_mb_Commented:
This ist the way, to implement user messages. The posting of the message seems to be ok.

define
----------
#define IDM_RESET       WM_USER+1    // add an arbitrary nummber to WM_USER

in .h
----------
class xy
{
       :
    afx_msg LRESULT OnReset(WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP()
       :
};


in .cpp
----------
BEGIN_MESSAGE_MAP( ... )
       :
    ON_MESSAGE(IDM_RESET, OnReset)
END_MESSAGE_MAP()

afx_msg LRESULT xy::OnReset(WPARAM wParam, LPARAM lParam)
{
       :
    return 0;
}
0
 
marbAuthor Commented:
The PostMessageline is called, I tested it in the debugger.
It's maybe the way I've declared the Message Handler that's wrong. I'm using void as return type and have no parameters. I'll try it as _mb_ suggested and will post the results tomorrow.

Another point: In my dialog class, I have a context menu entry to delete all the edit boxes. So the menu as well as the edit box can send a IDM_RESET message. The message sent by the menu is handled (with no parameters), but the messages sent by the CEdit are not handled. I assume that this is no problem, but I'm not a 100% sure.

0
 
marbAuthor Commented:
Thank you _mb_!

It was the menu issue: To handle the menu commands, I need ON_COMMAND(IDM_RESET, OnReset) and for the message handling, it's ON_MESSAGE(IDM_RESET,OnReset).
I've added the ON_MESSAGE(...) line and left OnReset as it was (i.e. without parameters) - and it work's now.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now