在对话框中直接拖入一个日历控件,然后添加一个CMonthCalCtrl类的变量(m_p),然后给这个控件添加一个MCN_SELECT消息,然后消息响应函数如下:         CTime a;

m_t.GetCurSel(a);
CString str;
str.Format("%d,%d,%d",a.GetYear(),a.GetMonth(),a.GetDay());
MessageBox(str);
可是奇怪的是显示的时间并不是我选中的时间,并且也没什么规律好像?为什么?新手,详细一点,谢谢!

解决方案 »

  1.   

    不知道你为什么有这样的问题,我新建了一个工程,用你的代码拷进去试了试,好用的很void CMyDlg::OnSelectMonthcalendar1(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    // TODO: Add your control notification handler code here


    CTime a;

    m_a.GetCurSel(a);
    CString str;
    str.Format("%d,%d,%d",a.GetYear(),a.GetMonth(),a.GetDay());
    MessageBox(str);
              
              *pResult = 0;
    }
      

  2.   

    然后添加一个CMonthCalCtrl类的变量(m_p),
    m_t.GetCurSel(a);??
    m_p.GetCurSel(a);??老兄,难道是你的笔误??
      

  3.   

    我是说这里笔误,程序那儿不可能笔误的,我又发现了,用debug编译,得到的时间是错的,但是用release编译,就能得到正确的结果,不知道为什么?有没有高人指点一下!谢谢了
      

  4.   

    “在对话框中直接拖入一个日历控件,然后添加一个CMonthCalCtrl类的变量(m_p)”在对话框构函数里这个变量系统初始化为0;(值为:1970。01。01)改一下就OK了。
      

  5.   

    改变一下day states属性就行了.
    把false 改为true
      

  6.   

    ”在对话框构函数里这个变量系统初始化为0;(值为:1970。01。01)改一下就OK了。“,这个类的变量怎么初始化为0 ,不是CTime类型的数据。“改变一下day states属性就行了.把false 改为true”,我改了,但是在debug模式下编译还是不对??期待解答!
      

  7.   

    PRB: CMonthCalCtl::GetCurSel(CTime) Returns Wrong Value Q235355RESOLUTION
    The workaround is to use the CMonthCalCtrl::GetCurSel(SYSTEMTIME*) version of GetCurSel for this class to get the correct date. The code will look like the following example: 
       SYSTEMTIME sysTime;   m_MonthCal.GetCurSel(&sysTime);   sysTime.wHour = sysTime.wMinute = sysTime.wSecond =    sysTime.wMilliseconds = 0;
      
       CTime l_time(sysTime); 
      

  8.   

    按楼上的确实可以,可是为什么呢,有没有人能解释我前面的做法只能在release下等到正确结果??
      

  9.   

    CAUSE
    MFC's implementation of CMonthCalCtrl::GetCurSel() for the CTime class calls SendMessage(MCM_GETCURSEL, &sysTime) where sysTime is a SYSTEMTIME structure. The MCM_GETCURSEL message does not fill in the hours, minutes, and seconds part of the sysTime Structure with valid values. However, the constructor for CTime class takes these values into consideration, so the date in CTime class is incorrect. 原因,没有修改小时,分和秒,用的还是构造初始化的值
    Release中一般为0,Debug中取内存中的随机值