Problem passing a COLORREF variable from CDialog to CView

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Eran (anonymous_at_discussions.microsoft.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 15:36:09 -0800

I am writing some basic code, not a full program, in MFC in order to learn and understand them better. I prefer to do it step by step with patience. Besides, it is fun like a game of solutions. The current code is about CDialog, CButton, CColorDialog and that is all. The problem is how to successfully pass a COLORREF variable from the CDialog class to the CView class.
 
First, I create a menu item function in the CMyView class, that directly loads and runs a color-selection dialog box, without writing a derived CDialog class code. Then I create a public COLORREF variable in the CMyView header file and put it in the menu item function and also in the OnDraw function in that class. The reason for putting the menu item function in the CMyView class and not in the CMainFrame class is that the menu item function and the OnDraw function can share the same public variable in the same class. (Correct me if I am wrong.) The code works successfully. No problem.
 
Next, after creating the simple code above, I try to write a bit more complicated code that loads a simple small modal dialog as called CMyColorDlg with one button, OK button and CANCEL button only. The one button as called Color button is for loading and running the color-selection dialog box. I successfully create and run the simple small modal dialog and then the color-selection dialog box but fail to apply a color to the client area of the single-document window.
 
So I try three different ways. First, I create a public COLORREF variable m_ColorDlg in the CMyColorDlg header file and write the following CButton function in the CMyColorDlg implementation class below. (CMyColorDlg is a derived class of CDialog.)
 
void CMyColorDlg::OnColor()
{
    // TODO: Add your control notification handler code here
    CColorDialog dlg;
    if (dlg.DoModal() == IDOK)
    {
        m_ColorDlg = dlg.GetColor();
    }
 
}
 
Then I instantiate a protected variable myColorDlg of the CMyColorDlg class and put it in the OnDraw function in the CMyView class. Please see one of the lines in the OnDraw function as follows below.
 
brush.CreateSolidBrush(myColorDlg.m_ColorDlg);
 
It does not work.
 
I try to pass the COLORREF variable from the CMyColorDlg class to the CMyView class through two other functions instead, one in the CMyColorDlg class and another one in the CMyView class, as follows below.
 
COLORREF CMyColorDlg::ReturnColorFromDlg()
{
 return m_ColorDlg;
}
 
void CMyView::GetColorFromDlg()
{
 m_color = myColorDlg.ReturnColorFromDlg();
 InvalidateRect(NULL);
}
 
It does not work either.
 
I try to pass it through the DoDataExchange function but it is impossible. My feeling is that the CDialog class is very “stubborn” afterall, and I have no idea how to successfully pass the color variable from the CMyColorDlg class to the CMyView class.
 
So I wonder how successfully you or any other professional programmer creates and runs the code. If you know how, please give me a clear idea how to do it, so that I will successfully apply a color from the color-selection dialog box indirectly to the client area. I will appreciate your help. I wonder if it is possible to do it successfully on a property *** instead of a dialog, doesn't it? I hope once I know how to do it on the dialog, I would be able to do the same thing on the property ***. Am I right?

Thanks for any help,
Eran


Quantcast