Sending message to parent window

Posted on 2005-04-21
Last Modified: 2013-11-20

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
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,
Question by:marb
    LVL 43

    Assisted Solution

    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);
    LVL 3

    Accepted Solution

    This ist the way, to implement user messages. The posting of the message seems to be ok.

    #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);

    in .cpp
        ON_MESSAGE(IDM_RESET, OnReset)

    afx_msg LRESULT xy::OnReset(WPARAM wParam, LPARAM lParam)
        return 0;

    Author Comment

    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.


    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    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.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now