(MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView? - Programmer

This is a discussion on (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView? - Programmer ; Alright, so I tried to use AfxGetMainWnd() from inside a CScrollView-derived class to access a function inside of a CMDIFrameWnd-derived class. It worked under the "Debug" configuration, but I'm getting a nasty error under the "Release" configuration: Coil error LNK2019: ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?

  1. (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?

    Alright, so I tried to use AfxGetMainWnd() from inside a
    CScrollView-derived class to access a function inside of a
    CMDIFrameWnd-derived class. It worked under the "Debug" configuration,
    but I'm getting a nasty error under the "Release" configuration:

    Coil error LNK2019: unresolved external symbol "public: void __thiscall
    CMainFrame::setCurrRef(class ATL::CStringT StrTraitMFC > >)"
    (?setCurrRef@CMainFrame@@QAEXV?$CStringT@DV?$StrTr aitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z)
    referenced in function "protected: void __thiscall
    CCoilView::OnEditRefrigerantselection(void)"
    (?OnEditRefrigerantselection@CCoilView@@IAEXXZ)

    So I've decided to do it using another method: by sending a message to
    the application from within the CScrollView-derived class and handling
    it from within the CMDIFrameWnd-derived class. However, I've made my
    own custom messages; I've just handled the ones the application
    generates for selecting different menu options, etc. So, how do I
    make, send, receive, and act on custom messages? Thanks.


  2. Re: (MFC) How to use messages to communicate between CMDIFrameWndand CScrollView?

    Cyde Weys wrote:
    > So I've decided to do it using another method: by sending a message to
    > the application from within the CScrollView-derived class and handling
    > it from within the CMDIFrameWnd-derived class. However, I've made my
    > own custom messages; I've just handled the ones the application
    > generates for selecting different menu options, etc. So, how do I
    > make, send, receive, and act on custom messages? Thanks.
    >


    http://www.mvps.org/vcfaq/mfc/12.htm

    --
    Scott McPhillips [VC++ MVP]


  3. Re: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?



    Scott McPhillips [MVP] wrote:
    > Cyde Weys wrote:
    > > So I've decided to do it using another method: by sending a message to
    > > the application from within the CScrollView-derived class and handling
    > > it from within the CMDIFrameWnd-derived class. However, I've made my
    > > own custom messages; I've just handled the ones the application
    > > generates for selecting different menu options, etc. So, how do I
    > > make, send, receive, and act on custom messages? Thanks.
    > >

    >
    > http://www.mvps.org/vcfaq/mfc/12.htm


    Alright, I have a question. First, from the article:

    ---------------
    Your secondary thread will need an hwnd (not a CWnd*) to post to. You
    can pass the main hwnd when you create the thread. Then you are ready
    to send or post a message from the secondary thread:


    ::PostMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
    or,
    ::SendMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
    ---------------

    How do I set up this whole hwnd business? I'm working on a rather
    large program (most of it not written by me) and it doesn't really have
    any hwnd stuff going on. And I don't see how exactly to pass "the main
    hwnd" to the thread. The only place I can see where a "thread" is
    created is the following, and I don't see any way to pass parameters:

    ---------------
    CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(
    IDR_COILTYPE,
    RUNTIME_CLASS(CCoilDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CCoilView));
    AddDocTemplate(pDocTemplate);


  4. Re: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?

    This error is a link time error. You should resolve it and see why
    setCurrRef is not exported or not found.

    You can always access mainframe window (CMDIFrameWnd for MDI) using
    AfxGetMainWnd from your view. It does not appear you are dealing with
    worker threads here. You can use pMainWnd->PostMessage(WM_YOURMESSAGE,
    ...). It does not explicitly require a handle but knows to use m_hWnd
    that is part of the base class CWnd.

    ---------
    Ajay Kalra
    ajaykalra@yahoo.com


  5. Re: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?



    Ajay Kalra wrote:
    > This error is a link time error. You should resolve it and see why
    > setCurrRef is not exported or not found.


    I'm trying to resolve it :-/

    > You can always access mainframe window (CMDIFrameWnd for MDI) using
    > AfxGetMainWnd from your view. It does not appear you are dealing with
    > worker threads here. You can use pMainWnd->PostMessage(WM_YOURMESSAGE,
    > ..). It does not explicitly require a handle but knows to use m_hWnd
    > that is part of the base class CWnd.


    The only place in my program where I see anything called pMainWnd
    (actually m_pMainWnd) is here:

    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
    return FALSE;
    m_pMainWnd = pMainFrame;
    // Enable drag/drop open
    m_pMainWnd->DragAcceptFiles();

    But what is this variable? It's not defined in my App.h file, yet it
    is apparently a member variable? I guess it's one of the variables
    from the class my main App inherits from? If I use m_pMainWnd inside
    of my CScrollView-derived class will it function properly?


  6. Re: (MFC) How to use messages to communicate between CMDIFrameWndand CScrollView?

    Cyde Weys wrote:
    >>http://www.mvps.org/vcfaq/mfc/12.htm

    >
    >
    > Alright, I have a question. First, from the article:
    >
    > ---------------
    > Your secondary thread will need an hwnd (not a CWnd*) to post to. You
    > can pass the main hwnd when you create the thread. Then you are ready
    > to send or post a message from the secondary thread:
    >
    >
    > ::PostMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
    > or,
    > ::SendMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
    > ---------------
    >
    > How do I set up this whole hwnd business? I'm working on a rather
    > large program (most of it not written by me) and it doesn't really have
    > any hwnd stuff going on. And I don't see how exactly to pass "the main
    > hwnd" to the thread. The only place I can see where a "thread" is
    > created is the following, and I don't see any way to pass parameters:
    >
    > ---------------
    > CMultiDocTemplate* pDocTemplate;
    > pDocTemplate = new CMultiDocTemplate(
    > IDR_COILTYPE,
    > RUNTIME_CLASS(CCoilDoc),
    > RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    > RUNTIME_CLASS(CCoilView));
    > AddDocTemplate(pDocTemplate);


    The above code is not creating a thread. Not relevant. By the way, it
    takes years to understand what the above code does. It is the "big
    bang" of an MFC application: It creates the entire universe. Ignore it.

    As far as I know, your problem does not involve multiple threads. But
    the referenced article, although it is about interthread messaging,
    shows you how to define and handle a custom message posted to a window,
    no matter where or how the message was generated. When everything is in
    one thread you can simply use

    AfxGetMainWnd()->PostMessage(...)
    or
    AfxGetMainWnd()->SendMessage(...)

    from anywhere in your application and you don't have to mess with HWNDs.
    Note: AfxGetMainWnd() is a handy built-in function to get the
    m_pMainWnd you asked Ajay about.

    --
    Scott McPhillips [VC++ MVP]


  7. Re: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?

    m_pMainWnd is member of CWinThread, your app's base class. You can use
    AfxGetMainWnd to get to it.

    ---------
    Ajay Kalra
    ajaykalra@yahoo.com


  8. Re: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?

    ARGHHH!!!!!! Permission to kill myself? God dammit ...

    So I got the message map thing working in Debug, but not in Release.
    So I go back and look to see why in the hell it's not working, and I
    look at the code in MainFrm.cpp more carefully and ... guess what block
    I've inadvertently inserted both functions setCurrRef and
    OnMyCurrRefMessage into?

    #ifdef _DEBUG

    #endif


    AAAAARRRRRRGGGHHHHHHHHHHHHHHHHHH


  9. Re: (MFC) How to use messages to communicate between CMDIFrameWnd and CScrollView?


    Cyde Weys wrote:
    > ARGHHH!!!!!! Permission to kill myself? God dammit ...
    >
    > So I got the message map thing working in Debug, but not in Release.
    > So I go back and look to see why in the hell it's not working, and I
    > look at the code in MainFrm.cpp more carefully and ... guess what block
    > I've inadvertently inserted both functions setCurrRef and
    > OnMyCurrRefMessage into?
    >
    > #ifdef _DEBUG
    >
    > #endif


    And by the way, thanks a lot for your help. I didn't end up using the
    messages because once I figured out the real error it runs a bit
    smoother just calling ((CMainFrame*)AfxGetMainWnd())->setCurrRef([...])
    than using messages, but I did get it to work with messages too, so
    I've learned something!


+ Reply to Thread