// TODO: Add your message handler code here
DestroyWindow();
}
int CPopupWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
RECT rc;
GetClientRect(&rc);
//m_wndMonthCtrl is a member variable of type CMonthCalCtrl
m_wndMonthCtrl.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, rc, this, 2008);
return 0;
}
and use like this:
CRect rcBtn;
m_btnPopup.GetClientRect(rcBtn);
m_btnPopup.ClientToScreen(rcBtn);
new CPopupWnd(rcBtn.left, rcBtn.bottom, this);
when I click the year of CMonthCalCtrl, it causes this access violation, why?
Last edited by skyskeleton; December 12th, 2007 at 08:57 AM.
CMonthCalCtrl::Create
BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL Create( DWORD dwStyle, const POINT& pt, CWnd* pParentWnd, UINT nID );
Return Value
Nonzero if initialization was successful; otherwise 0.
Parameters
dwStyle
Specifies the combination of Windows styles applied to the month calendar control. SeeMonth Calendar Control Styles in the Platform SDK for more information about the styles.
rect
A reference to aRECT structure. Contains the position and size of the month calendar control.
pt
A reference to aPOINT structure that identifies the location of the month calendar control.
pParentWnd
A pointer to a CWnd object that is the parent window of the month calendar control. It must not be NULL.
nID
Specifies the month calendar control’s control ID.
Of course it is, since it was created with this ID (2008).
It would be better to get into a habit of using #define to define ID and pass it instead using value.
In case ID has to be changed and is referenced in multiple places, changing it may prove error prone. Defining macro requires change only in one place.
skyskeleton:
Firstly, I would strongly suggest changing your constructor.
Calling Create in the constructor of the MFC object is not a good idea.
You do not check if CreateEx fails. However, even if you did and it fails you are not able to prevent object from continuing its instantiation, resulting in possible memory leaks and/or other unexpected behavior.
Instantiation of the object may fail also for other reason and you can have a runaway window.
After successful instantiation of the object, you can call member function that in turn calls CreateEx.
Anyway, looking at your code I do not see any reason for having problem you described.
Could you post your project?
There are only 10 types of people in the world: Those who understand binary and those who do not.
skyskeleton:
Firstly, I would strongly suggest changing your constructor.
Calling Create in the constructor of the MFC object is not a good idea.
You do not check if CreateEx fails. However, even if you did and it fails you are not able to prevent object from continuing its instantiation, resulting in possible memory leaks and/or other unexpected behavior.
Instantiation of the object may fail also for other reason and you can have a runaway window.
After successful instantiation of the object, you can call member function that in turn calls CreateEx.
Anyway, looking at your code I do not see any reason for having problem you described.
Could you post your project?
Firstly,thank you for your reply.I create a new project according to your suggestion,but the exception already exist.The new project is in attachment.
I tried your application, noticed the bug, applied this change and it worked well.
I noticed one problem, the control's placement in the window was changed. Instead of being centered it was in the right bottom cormer of the dialog. Maybe, because of a different resolution of your video card in your dialog is placed completely outside the dialog box and that makes it invisible. Try to change the coordinates of the control. (For the begining try 0,0.)
Last edited by srelu; December 13th, 2007 at 04:27 AM.
I tried your application, noticed the bug, applied this change and it worked well.
I noticed one problem, the control's placement in the window was changed. Instead of being centered it was in the right bottom cormer of the dialog. Maybe, because of a different resolution of your video card in your dialog is placed completely outside the dialog box and that makes it invisible. Try to change the coordinates of the control. (For the begining try 0,0.)
Sure enough as you said , thank you very much!
According to your suggestion,I changed my code like this:
void CPopupWindowDlg::OnPopupwnd()
{
// TODO: Add your control notification handler code here
RECT rcBtn;
m_btnPopup.GetWindowRect(&rcBtn);
ScreenToClient(&rcBtn);
m_pWndPopup = new CPopupWnd();
BOOL bRet = m_pWndPopup->CreateWnd(rcBtn.left, rcBtn.bottom, this);
}
then it is visiable and works well.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.