Re: Display message on main dialog GUI
On Aug 24, 5:05 pm, Me <m...@right.her> wrote:
I normally display messages in the following way on the main Dialog:
c_Info.SetWindowText("Processing, Please wait..");
How do I display a message on the main Dialog from within another class?
Thanks.
c_Info is a child window (e.g. CStatic) of your main dialog, right?
If so, AfxGetMainWnd() should return a pointer to your main dialog.
Presuming your main dialog class is called CMainDialog,
static_cast<CMainDialog&>(*AfxGetMainWnd()).c_Info.SetWindowText (...)
should do it for you.
But! But... Design-wise, the above is crap design. And sending windows
message is equally bad (IMHO). Your "another class" needs a way to
pass some status to another part of the code. So give it a way to do
it. Try this:
class IStatusDisplay
{
public:
virtual void SetStatusText(const CString& text) = 0;
};
class CYourClass
{
public:
CYourClass(IStatusDisplay& status) : m_status(status) {}
IStatusDisplay& m_status;
void Work()
{
m_status.SetStatusText("Processing, blahblah");
DoOtherWork();
}
};
class CMainDialog : public CDialog, IStatusDisplay
{
....
virtual void SetStatusText(const CString& text)
{
c_Info.SetWindowText(text);
}
};
void Work(CMainDialog& dlg)
{
CYourClass worker(dlg);
worker.Work();
}
The above gives you a good degree of separation (dialog knows not of
CYourClass and vice-versa), while keeping code +/- simple and type
safe (forget type safety with message-based solutions). It is a simple
application of a design principle called "interface segregation
principle" (http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod).
Goran.
P.S. You almost certainly need background processing. I don't know
whether you do have it, but if you do, note that accessing CWnd-
derived objects from thread they weren't created in, leads to
trouble. If you think you can do it, you were just lucky so far.