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.
Printable View
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.
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.
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.
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();
//...
What is g_dlgref? And wont dlg.DoModal() make another dialog appear on the screen?
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?
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.
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?
I'm not sure. I've never tried.
How could I get the handle for the window in an MFC app?
CWnd::GetSafeHwnd()
I tried this and suprise suprise got another one of our lovely assertion faults:
In the thread:Code:
AfxBeginThread(ScanThread, GetSafeHwnd());
Code:
CmyDlg dlg;
dlg.Attach((HWND)pParam);
It asserts in the Attach function?
Yup.
Ok, I kinda got it to work, at least when I use _beginthread to start the thread.
Should still be AfxCreateThread in an MFC app...
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.
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.