Question : GetDlgItem and handles

My main class in inherited from CPropertySheet.  I make four tabs by using:
CPropertyPage::Construct(int ResourceId);
then
CPropertySheet::AddPage(CPropertyPage *page);

On these page I have CButton class(check boxes) created using the resource editor.  This is what I am trying to do to get the handle of the box so I can use SendMessage()
to change their states.

CWnd *tempCWnd;
HWND *checkOne;

// this always returns NULL, why?
tempCWnd = GetDlgItem(IDC_CHECK_ONE);
checkOne = tempCWnd->m_hWnd;

Answer : GetDlgItem and handles

>>All I am needing to do is set the values of 'check boxes' that reside on each CPropertyPage

All of the above-describe methods can fail because the dialog windows are not created until needed, so any scheme that relies on GetDlgItem() will tend to fail unless you employ special precautions and ugly kludges.

--=-==---=-=-=-=-=-=-=-=-=-=-=-=

THis is a clear case where you are working outside of the "system"  MFC makes this real easy if you conform to the "right" way to do these things.

1) For each property page, create a CPropertyPage-derived class (in the dialog editor, double-click in the titlebar to bring up the ClassWizard.  It will prompt "Do you want to make a class?" and select Yes.  

2) At the next prompt, give the class a name (I use the convention CDlgPgXxxx).  Then Scroll down in the list of classes and choose CPropertyPage.

3) Having done that, you can now have the ClassWizard create variables that will correspond to each control and/or the setting of each control.  For instance, while looking at a property page, press Ctrl+W, pick the Member Variables tab, select the ID of the checkbox (call it IDC_Zzzz) and click the [Add Variable] button.  I use names like m_fZzzz or m_bZzzz to tell myself that these are Boolean flags.

4) Now convert you code that looks like this:

m_VideoPage = new CPropertyPage(IDD_VIDEO_SHEET);
m_SoundPage = new CPropertyPage(IDD_SOUND_SHEET);

AddPage(m_VideoPage);
AddPage(m_SoundPage);

to code like this:

  #include "DlgPgVideo.h"
  #include "DlgPgSound.h"
...
  CDlgPgVideo dlgVideo;
  CDlgPgSound dlgSound;

  AddPage( &dlgVideo );
  AddPage( &dlgSound );

5) To preset the checkboxes (or pre-fill edit text boxes, etc, use code like this:

  dlgVideo.m_fXxxx=  TRUE;
  dlgSound.m_fYyyyy= TRUE;
  dlgSound.m_fZzzz=  FALSE;
  // bonus code: Obtain the setting from last time:
  dlgSound.m_fZzzz= AfxGetApp()->GetProfileInt("Sound","Zzzz" FALSE);

  int nRet= DoModal();
  if (nRet== IDOK ) {
    if ( dlgSound.m_fZzzz ) {
      MessageBox("So, you are a Zzzzz sort of person" );
    }
    // bonus code: Save the setting for next time
    GetApp()->WriteProfileInt("Sound","Zzzz", dlgSound.m_fZzzz);
  }

If, you need to set or obtain any setting while the page is active, you can use UpdateData() to set the valriables to the values that match the on-screen settings.
(you can also use GetDlgItem() and then call SetCheck() or GetCheck(), but why bother?)

-- Dan
Random Solutions  
 
programming4us programming4us