Results 1 to 18 of 18

Thread: Accessing members of a dlg class from outside of it

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241

    Accessing members of a dlg class from outside of it

    Lets say I have a Dialog called Cdlg. And one of its member is a CButton called bButton. How would I access a function thats a member of bButton such as GetWindowText from outside of the Cdlg class, like a thread.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's a bad idea manipulating MFC objects from a different thread than they were created with. Sooner or later there'll be an assertion fault.

    But anything else, simply make the sub-object public. If you have a reference or pointer to the dialog you can then access the sub-object through it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    But looking through the code (The core for the App was made by the wizard I don't know much about MFC) I dont see any instance of the actual dialog being declared, at least not a global one that I can access outside of the Applications class.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Then you'll have to declare one.

    If this is a dialog-based app the dialog class instance is a local variable of CYourApp::InitInstance(). You need to add a CYourDlg& variable in a place you can access it globally and set it to this dialog instance:
    Code:
    CYourDlg dlg;
    g_dlgref = dlg;
    dlg.DoModal();
    //...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    What is g_dlgref? And wont dlg.DoModal() make another dialog appear on the screen?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Ye you were right I got a assertion fault when I moved the mouse over the form. So this doesn't help me since I need a way to get the caption of a button and add items to a list in report view from the thread. How could I do this?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Pass the HWND of the dialog to the new thread and create a local CWnd object there (CWnd::FromHandle or better CWnd::Attach).

    Then use the standard API wrappers for GetDlgItem etc. to get the button and use SetWindowText to change its text. Same goes for the list view. Create a local CListCtrl object and use CWnd::Attach to attach the HWND of the list view control to it. Then you can use the member functions to manipulate it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Could I create a local CmyDlg object and then use CWnd::Attach to attack it to the dialog and then use it to manipulate the controls on the dialog?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm not sure. I've never tried.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    How could I get the handle for the window in an MFC app?

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    CWnd::GetSafeHwnd()
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    I tried this and suprise suprise got another one of our lovely assertion faults:

    Code:
    AfxBeginThread(ScanThread, GetSafeHwnd());
    In the thread:

    Code:
    CmyDlg dlg;
    dlg.Attach((HWND)pParam);

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It asserts in the Attach function?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Yup.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Ok, I kinda got it to work, at least when I use _beginthread to start the thread.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Should still be AfxCreateThread in an MFC app...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    How bad is it that I'm using _beginthread instead? Any ideas why AfxBeginThread wouldn't work? It seemed to be passing the hwnd parameter wrong.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    AfxBeginThread creates a CWinThread object, which does some initializing in its constructor.
    MFC 7.0 doesn't seem to rely on its usage, but you never no.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width