Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control)

Discussion in 'C++' started by LT, Jul 25, 2004.

  1. LT

    LT Guest

    Hello,
    I have the following problem. I'm using the CMonthCalCtrl calendar
    control. I need to emphasize some of days, so I'm handling the
    MCN_GETDAYSTATE event. The problem is when my handler function is
    called for the first time during initializing of the application. When
    I'm calling the CMonthCalCtrl::GetMonthRange method there is an
    assertion error inside that method: ASSERT:):IsWindow(m_hWnd));
    It seems that my event handler is called before creating a window
    handle of the calendar control (when after initializing, there is no
    error).
    To solve this problem I would call my event handler
    OnMcnGetdaystateMonthcalendar2(NMHDR *pNMHDR, LRESULT *pResult) by
    hand after the initialization but I don't know what to write as
    parameters pNMHDR, pResult. Besides, I don't know if that idea is
    appropriate or not.


    Has anybody any idea? Maybe although something about how MFC controls
    are initialized (I'm newbie in VC++)

    There is a bit of code:

    void CqweDlg::OnMcnGetdaystateMonthcalendar2(NMHDR *pNMHDR, LRESULT
    *pResult)
    {
    LPNMDAYSTATE pDayState = reinterpret_cast<LPNMDAYSTATE>(pNMHDR);

    SYSTEMTIME timeFrom;
    SYSTEMTIME timeUntil;
    SYSTEMTIME timeDay;
    timeDay.wDay = 28;
    timeDay.wMonth = 7;
    timeDay.wYear = 2004;

    nCount = Calendar.GetMonthRange(&timeFrom, &timeUntil,
    GMR_DAYSTATE);//inside this line there is an assertion error!!!
    memset(states,0x00, sizeof(MONTHDAYSTATE)*nCount);
    int iDay, iFrom, iUntil;
    iDay = timeDay.wYear * 10000 + timeDay.wMonth * 100 + timeDay.wDay;
    iFrom = timeFrom.wYear * 10000 + timeFrom.wMonth * 100 +
    timeFrom.wDay;
    iUntil = timeUntil.wYear * 10000 + timeUntil.wMonth * 100 +
    timeUntil.wDay;
    if ( iDay >= iFrom && iDay <= iUntil )
    {
    int index = timeDay.wMonth - timeFrom.wMonth;
    if (index<0) index+=12;
    states[index] |= 1<< (timeDay.wDay - 1);//emphasize
    }

    memcpy(pDayState->prgDayState, states,
    nCount*sizeof(MONTHDAYSTATE));//copy

    *pResult = 0;
    }

    Lt
     
    LT, Jul 25, 2004
    #1
    1. Advertising

  2. LT

    Phlip Guest

    Phlip, Jul 25, 2004
    #2
    1. Advertising

  3. LT

    Lt Guest

    User "Phlip" <> wrote :
    > Why can't you say "if" before calling GetMonthRange?

    I need get variables timeFrom, timeUntil for the rest of the code. And that
    rest should be runned as well after the initialization as before a dialog
    window is visible.

    Lt
     
    Lt, Jul 25, 2004
    #3
  4. LT

    Phlip Guest

    Lt wrote:

    > Phlip wrote:


    > > Why can't you say "if" before calling GetMonthRange?


    > I need get variables timeFrom, timeUntil for the rest of the code.


    Can you concoct these values from functions in the <time.h> header file? Or
    the platform-specific equivalents? Because the user has not yet had the
    opportunity to change a value in your calendar control, you must be
    collecting values that you could generate by alternate means.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 25, 2004
    #4
  5. LT

    Lt Guest

    I works now. I've changed the line:
    nCount = Calendar.GetMonthRange(&timeFrom, &timeUntil, GMR_DAYSTATE);
    to:
    nCount =
    ((CMonthCalCtrl*)GetDlgItem(IDC_MONTHCALENDAR2))->GetMonthRange(&timeFrom,
    &timeUntil, GMR_DAYSTATE);
    I've just used GetDlgItem instead of the Calendar variable created by select
    in IDE the "Add variable..." command.

    Lt
     
    Lt, Jul 25, 2004
    #5
  6. LT

    Phlip Guest

    Lt wrote:

    > I works now. I've changed the line:
    > nCount = Calendar.GetMonthRange(&timeFrom, &timeUntil, GMR_DAYSTATE);
    > to:
    > nCount =
    > ((CMonthCalCtrl*)GetDlgItem(IDC_MONTHCALENDAR2))->GetMonthRange(&timeFrom,
    > &timeUntil, GMR_DAYSTATE);


    That line only works by accident. GetDlgItem returns a HWND, which happens
    to be the first member inside CMonthCalCtrl. Then GetMonthRange only uses
    that first member.

    Typecasts are not magic that turns anything into anything else. Avoid as
    many typecasts as possible. When the compiler tells you that your code is
    not well-formed, fix the code. Typecasts only throw away compiler
    diagnostics.

    Try:

    CMonthCalCtrl cal(GetDlgItem(IDC_MONTHCALENDAR2));
    int nCount = cal.GetMonthRange(...);

    Note that not cramming things all on one line makes them easier to fix, and
    note that nCount should not exist before it is assigned.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 25, 2004
    #6
  7. LT

    Lt Guest

    Philip wrote:
    >GetDlgItem returns a HWND, which happens to be the first member inside

    CMonthCalCtrl

    CWindow::GetDlgItem (ATL) returns a HWND but I'm using CWnd::GetDlgItem
    (MFC) that returns CWnd*

    >Try:
    >CMonthCalCtrl cal(GetDlgItem(IDC_MONTHCALENDAR2));
    >int nCount = cal.GetMonthRange(...);


    That dosen't work because the constructor CMonthCalCtrl::CMonthCalCtrl ()
    doesn't take any parameters.

    Lt
     
    Lt, Jul 25, 2004
    #7
  8. LT

    Phlip Guest

    Lt wrote:

    > >GetDlgItem returns a HWND, which happens to be the first member inside

    > CMonthCalCtrl
    >
    > CWindow::GetDlgItem (ATL) returns a HWND but I'm using CWnd::GetDlgItem
    > (MFC) that returns CWnd*


    MFC sucks. It has lead both of us astray. Switch to WTL to be happier, and
    typecast less.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 25, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,137
    Smokey Grindel
    Dec 2, 2006
  2. Joseph
    Replies:
    3
    Views:
    1,252
    Howard
    Jul 20, 2005
  3. thunk
    Replies:
    1
    Views:
    348
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    524
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    655
    thunk
    Apr 3, 2010
Loading...

Share This Page