Modeless Dialog Communication (MFC)

This is a discussion on Modeless Dialog Communication (MFC) within the Windows Programming forums, part of the Platform Specific Boards category; Iím trying to figure out how to pass variables back and forth between a modeless dialog and my main program. ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    Modeless Dialog Communication (MFC)

    Iím trying to figure out how to pass variables back and forth between a modeless dialog and my main program. The program is a dialog based application and Iím trying to write a live trade window to view and edit the list of stocks being used. In
    Code:
    OnInitDialog()
    of the main window I created the modeless by

    Code:
    CTraderDlg *m_pTdlg = new CTraderDlg(this);
    	m_pTdlg->Create(IDD_TRADER_DIALOG);
    	m_pTdlg->ShowWindow(TRUE);
    But, how would I pass variables to this window or changes made to the list back to the main application?

    Someone in IRC said something about SendMessage or SetWindowLongPtr to a variableÖ if someone could give me an example I should be able to run with itÖ please.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    SendMessage is a good method. Basically, you should define a new message

    #define WM_MYDATAMESSAGETHINGY (WM_USERMESSAGE + 1)

    then, from the dialog just Send the message with whatever parameters you feel like sending. The main window will have to handle the WM_MYDATAMESSAGETHINGY message.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    In the modeless dialog's class I added the line
    Code:
    #define WM_LOADFILE WM_USER+0x100
    in the class header and am using sendmessage as so.
    Code:
    m_pMain->SendMessage(WM_CLOSE);
    Where m_pMain is a pointer to the main window I got using..
    Code:
    m_pMain = CWnd::FindWindow(NULL, _T("New Deal Options"));
    in the class's constructor.

    In the main window I'm trying to send a message to all I changed was
    Code:
    BEGIN_MESSAGE_MAP(CSGOptionsBlackBoxDlg, CResizeDialog)
            .....
            .....
            .....
    	ON_MESSAGE (WM_LOADFILE, OnBnClickedImport)
    END_MESSAGE_MAP()
    But, when I compile I get this error....
    c:\Documents and Settings\office3\My Documents\Visual Studio Projects\SGOptionsBlackBox\SGOptionsBlackBoxDlg.cp p(124) : error C2440: 'static_cast' : cannot convert from 'void (__thiscall CSGOptionsBlackBoxDlg::* )(void)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
    None of the functions with this name in scope match the target type

    What exactly does this mean? How do I fix it?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try reinterpret_cast instead.

    For future reference, if you highlight the error message in the error window of the ide and press F1 you'll get useful information regarding the specific error message.

    c2440
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Erm, no, it's not a good idea to cast function pointers under any circumstances. Make the signature of your target function match what ON_MESSAGE requires. (Note: it requires the signature (LRESULT (WPARAM, LPARAM)).

    LRESULT OnBnClickedImport(WPARAM, LPARAM)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    You can also use from the modless dlg

    GetParent()

    or

    AfxGetApp()

    and then call these classes methods directly.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I, too, recommend windows messages.

    Be very careful with SendMessage() if you're using it across a thread boundary. It's easy to create deadlock conditions with it. PostMessage() doesn't have that problem.

    If there's not a multithreading issue to deal with, then either should be fine.

    I tend to use PostMessage() in general unless I really want the calling code to wait for the message to finish being processed.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    Thank you, using SendMessage I was able to send messages w/o a problem and use caller functions to get @ the functions I wanted. The one thing I donít get is how to pass variables, pointers, arraysÖ whatever using SendMessage.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Just cast one of the parameters. E.g.:

    Code:
    wind->SendMessage(UWM_MYMESSAGENAME, (WPARAM) &buffer, (LPARAM) count);
    
    ...
    
    LRESULT OnMyMessage(WPARAM w, LPARAM l){
         char * buffer = (char *) w;
         int count = (int) l;
         ...
    }
    The only thing to know about passing pointers:
    1. Pointers can't be passed over a process boundary (but they can be passed over a thread boundary).
    2. If you pass pointers over a thread boundary, and you use PostMessage(), your pointer must be guaranteed to still be valid when the other thread accesses it (no local variables!)
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 01:59 PM
  2. Replies: 6
    Last Post: 04-27-2004, 08:02 PM
  3. Dialog Box & Property Sheet :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 01:33 PM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21