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

L

LT

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
 
P

Phlip

LT said:
I'm calling the CMonthCalCtrl::GetMonthRange method there is an
assertion error inside that method: ASSERT:):IsWindow(m_hWnd));

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++.
 
L

Lt

User "Phlip said:
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
 
P

Phlip

Lt said:
Phlip wrote:
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.
 
L

Lt

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
 
P

Phlip

Lt said:
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.
 
L

Lt

Philip said:
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
 
P

Phlip

Lt said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top