how do i get OnClick event in CDhtmlDialog derived class

Posted on 2008-11-17
Last Modified: 2013-11-20
i have created a class that inherits from CDHtmlDialog,
i added DECLARE_DHTML_EVENT_MAP to my header file
and the following event map to the cpp file

      DHTML_EVENT_ONCLICK(L"btn1", OnBtn1)
      DHTML_EVENT_ONCLICK(L"btn2", OnBtn2)

my HTML file is loaded from a url and not from a resource.
both the btn1 and btn2 elements are included in the html file.
when clicking those elements, nothing happens,
i cant get the onclick event to get into my cpp code.

any idea what did I do wrong?

Question by:bundy9764
    LVL 19

    Accepted Solution

    The code above itself appears to be good, I ran a simple test with the following code and an HTML file located on a web server:

          DHTML_EVENT_ONCLICK(_T("btn1"), OnBtn1)

    HRESULT CDlgEmail::OnBtn1(IHTMLElement * pElement)
          AfxMessageBox(_T("OnBtn1 called."));
          return S_OK;

    Do the input elements of type button, in the remove HTML file, have an id attribute with values btn1 and btn2, i.e. id="btn1" and id="btn2" respectively?
    <input type="button" id="btn1" value="Button 1" />

    Open in new window


    Author Comment

    thats what i was trying to do...
    the page was loaded using the navigate function.
    the Ids are set on the html but still no call made to the c++ function.
    the only diffrence from your code is the use of _T macro, i have made all calls as wide strings.
    LVL 19

    Expert Comment

    The wide strings are fine if your application is compiled in Unicode mode.

    Have you used the wizard to create the class or was it added to an existing project manually? Can you post the code for your class perhaps?

    Author Comment

    attached the two files ( i have removed some of my irrelevant code)
    thanks again.
    LVL 19

    Expert Comment

    At the moment I still do not see the problem in the code however one question that arises is how do you know that OnBtn1 is not called? The code in the file for OnBtn1 would not indicate that it executed:

    HRESULT CDlgEmail::OnBtn1(IHTMLElement* pElement)
          CComBSTR szId;
          return S_OK;

    Try adding AfxMessageBox(_T("OnBtn1 called.")); to the function body to see if it gets called.

    I basically added a new MFC class derived from CDHtmlDialog to a sample project, then verified that it matches the code you have i.e. same DHTML Event Map, same approach of navigating to the web page on a server, and then created an instance of the class during a button click in the main app:

    CDlgEmail dlgE;

    The dialog opened and displayed the web page and a message box was shown when the button was clicked.

    Author Comment

    thanks for your help,
    i knew that the code doesnt work because i have a breakpoint on this function :)

    i have found the problem which was not related to the message map or anything, i was listenning to the OnNavigateComplete event, and didnt call CDhtmlDialog::OnNavigateComplete which initialize the document pointer.

    thanks again for your help.


    Author Closing Comment

    i had a mistake not accepting it earlier :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    iSeries DB2 Query 2 66
    notReplace  challenge 53 93
    VBA "SendKeys" Syntax for Multiple Keystrokes 7 87
    Window placement 17 55
    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now