Connecting Tech Pros Worldwide Forums | Help | Site Map

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

LT
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Phlip
Guest
 
Posts: n/a
#2: Jul 22 '05

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


LT wrote:
[color=blue]
> I'm calling the CMonthCalCtrl::GetMonthRange method there is an
> assertion error inside that method: ASSERT(::IsWindow(m_hWnd));[/color]

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

If trouble persists, use http://groups.google.com to find a newsgroup
qualified to answer. This newsgroup can only accurately discuss
platform-neutral C++.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


Lt
Guest
 
Posts: n/a
#3: Jul 22 '05

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


User "Phlip" <phlip_cpp@yahoo.com> wrote :[color=blue]
> Why can't you say "if" before calling GetMonthRange?[/color]
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


Phlip
Guest
 
Posts: n/a
#4: Jul 22 '05

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


Lt wrote:
[color=blue]
> Phlip wrote:[/color]
[color=blue][color=green]
> > Why can't you say "if" before calling GetMonthRange?[/color][/color]
[color=blue]
> I need get variables timeFrom, timeUntil for the rest of the code.[/color]

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/bi...UserInterfaces


Lt
Guest
 
Posts: n/a
#5: Jul 22 '05

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


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


Phlip
Guest
 
Posts: n/a
#6: Jul 22 '05

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


Lt wrote:
[color=blue]
> 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);[/color]

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/bi...UserInterfaces


Lt
Guest
 
Posts: n/a
#7: Jul 22 '05

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


Philip wrote:[color=blue]
>GetDlgItem returns a HWND, which happens to be the first member inside[/color]
CMonthCalCtrl

CWindow::GetDlgItem (ATL) returns a HWND but I'm using CWnd::GetDlgItem
(MFC) that returns CWnd*
[color=blue]
>Try:
>CMonthCalCtrl cal(GetDlgItem(IDC_MONTHCALENDAR2));
>int nCount = cal.GetMonthRange(...);[/color]

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

Lt


Phlip
Guest
 
Posts: n/a
#8: Jul 22 '05

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


Lt wrote:
[color=blue][color=green]
> >GetDlgItem returns a HWND, which happens to be the first member inside[/color]
> CMonthCalCtrl
>
> CWindow::GetDlgItem (ATL) returns a HWND but I'm using CWnd::GetDlgItem
> (MFC) that returns CWnd*[/color]

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

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


Closed Thread