|
This is because the date/time control doesn't really have the focus, as the overlying edit control has it. To remedy this, you can add code to the edit control to pass the NM_KILLFOCUS notification on to the date/time control's parent when the edit control loses focus. You'll need to add this manually, as the Class wizard doesn't work properly with nested classes. Add the following to the message map of the CDateTimeEditCtrlEditCtrl:
ON_WM_KILLFOCUS()
then add the following to the CDateTimeEditCtrlEditCtrl message map declarations in the header file:
afx_msg void OnKillFocus(CWnd* pNewWnd);
and finally, add the following function to the cpp file:
void CDateTimeEditCtrlEditCtrl::OnKillFocus(CWnd* pNewWnd)
{
CDateTimeEditCtrl* pDateTimeEditCtrl = DYNAMIC_DOWNCAST(CDateTimeEditCtrl, GetParent());
if (pDateTimeEditCtrl != NULL)
{
CWnd* pParent = pDateTimeEditCtrl->GetParent();
if (pParent != NULL)
{
NMHDR nmhdr;
nmhdr.hwndFrom = pDateTimeEditCtrl->m_hWnd;
nmhdr.idFrom = pDateTimeEditCtrl->GetDlgCtrlID();
nmhdr.code = NM_KILLFOCUS;
pParent->SendMessage(WM_NOTIFY, (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
}
}
CEdit::OnKillFocus(pNewWnd);
} What you will find though is that the parent gets a NM_KILLFOCUS when passing the focus to the control, as the control immediately passes the focus onto the edit control. To stop this, add a handler for the NM_KILLFOCUS to the CDateTimeEditCtrl, modify it to return BOOL, modify the message map entry to use ON_NOTIFY_REFLECT_EX , and add the following code to it:
BOOL CDateTimeEditCtrl::OnKillfocus(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = 0;
if (CWnd::GetFocus() == m_pEdit)
return TRUE;
return FALSE;
} This should make the notifications work as per the standard date control.
I'll add this to my list of pending updates, and post it as soon as I get some time.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Thanks for your help. I think I understand.
But I still have a problem with the OnKillfocus method : the mapping with ON_NOTIFY_REFLECT_EX doesn't accept it because of its BOOL return value (it expects a void method)...
|
|
|
|
|
Yes, it works !
In fact the problem was the name of the method : I've changed CDateTimeEditCtrl::OnKillfocus in CDateTimeEditCtrl::OnNMKillfocus and it works very well.
Thank you very much !
This KillFocus message will be useful to read the new COleDateTime only after the user has finished to write or choose it.
|
|
|
|
|
Hi, I'm not a CPP expert, but tried to add it to our solution as a separate project. It didn't compile. Do you have a compilable version under Visual Studio 2003 ?
Thx,
noy
|
|
|
|
|
Hello,
I also had an error during compilation. I have solved it this way, in the CDateTimeEditCtrlEditCtrl::OnKeyDown method :
CString sSeps(s_chDateSep);
I hope that will help you...
|
|
|
|
|
Sorry I didn't answer this one. For some reason, the notification of this, and PetitPapaNoel's reply got put in my Junk email folder.
I am hoping to go through all my articles' code soon and make sure they work in VS2003 and VS2005, and publish updates accordingly.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Can anyone tell me, how i can implement the DTS_SHOWNONE Style?
thanx in advance
Bender
|
|
|
|
|
This control was intended to be an alternative to using the standard control with the DTS_SHOWNONE style, so I hadn't thought that anyone would actually want it. Can you tell me why you need this style with this class, and what you would want to give you that you don't get already?
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
ah, i see
well, i want to use the control in a project, where Stingray's Objective Grid is
already heavily used.
I have to show the Date-Text in the picker in another color and i have to draw the background of the picker in another color too.(not the calender, that would be too easy )
The Problem is, i cant replace the CDateTimeCtrl class (removed #include afxdtctl.h and replaced it with my "CDateTimeCtrl.h"), i get Linker Errors (NAFXCWD.LIB->WINCTRL5.CPP) like hell in VC6
So your way of handling that problem seemed the most effective to gain control over that ...grmbl#* DatePicker Great work, by the way!!!
The DTS_SHOWNONE style is used with Objective Grid, and the customer wants to see that control in exactly that way, it is displayed in the grids.....
|
|
|
|
|
Not sure why the standard one causes you problems, but my class simply uses a standard DateTime picker, and super-imposes an edit control. If my class works for you, then I would have thought you should be able to create you own override of CDateTimeCtrl, which just does nothing.
Alternatively, although I don't intend to fully implement DTS_SHOWNONE in the way that the standard control does (i.e. with the check box etc), you could remove the ASSERTs which fire when trying to use the style in my class.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
well, the standard one won't let me change the colors, thats just it
I tried to overwrite the CDateTimeCtrl of course, but that wont help, because i cant use it together with that "Objective Grid"... Plus, even if in a Testproject without the Grid, i cant change the
Textcolor in the Picker.
The checkbox is just wanted from the customerside, i for sure would not care, if i see that dumb checkbox hahaha I removed the asserts for DTS_SHOWNONE, but that did not help. And time is running out, so many other stuff todo...
But thanks for your patience man! :->
|
|
|
|
|
Thank for the great work. I need to catch the SETFOUS message, when tha edit is focused. Can you tell me how ?
Tank You.
Raffaele Eugenelo
|
|
|
|
|
To catch the control getting the focus, you can add a handler for NM_SETFOCUS to the control's parent.
(I noticed when trying this that while tabbing to the control generates this notification, clicking in the control does not. However, the behaviour in my control is exactly the same as in the standard control. If you want to catch a focus change caused by the mouse clicking in the edit control, you could try adding a WindowProc or DefWindowProc override to the CDateTimeEditCtrlEditCtrl class, and sending the WM_SETFOCUS to the parent.)
Paul.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Thanks for an insightful article with great code!
Has anyone expanded on the DateTimePicker idea to include the editing of the time of day ?
Thanks, Fred
|
|
|
|
|
Thanks - it's good to know that people find it useful.
I have often thought that there should be a better way of editing time. What kind of support did you have in mind for that?
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Hi Paul,
I have seen various edit control projects that allow input according to a given format. The format "HH:MM" might show a CEdit with a fixed colon (':') sign at the 3rd char position and allows the input of two numbers before and after the colon.
Additionally features like cursor-up/down rotates numbers in the ranges 00 -> 23 or 00 -> 59 respectively. The caret jumps from the 2nd char to the 4th, thereby skipping the fixed colon sign.
Other format suggestions could be "hh/MM/SS", where hh means a 12 hour clock (as opposed to the big letter HH for a 24 hour clock), the fixed seperator chars are / for this format and seconds (00 -> 59) can also be entered.
I think classes for pattern input using a customized CEdit already exist. It would be cool to integrate such a control for time input.
Thanks,
Fred
|
|
|
|
|
Hello! If I try to type in something like that: 09.31 then an error appears.
|
|
|
|
|
Thanks for this great control.
Any plan to implement DTM_SETFORMAT for this control ?
We are waiting for that..!
Anand
|
|
|
|
|
I wasn't planning to implement DTM_SETFORMAT at all for my control, as I didn't think it would be necessary. I will have a look at the work involved, and perhaps implement it in a future update, though it probably won't be very soon.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Great control!
To make the visual feedback clearer if the ontrol is disabled, I changed CDateTimeEditCtrlEditCtrl::CtlColor :
HBRUSH CDateTimeEditCtrlEditCtrl::CtlColor
(CDC* pDC, UINT nCtlColor)
{
if (m_bNonEditable && IsWindowEnabled())
{
pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
return GetSysColorBrush(COLOR_WINDOW);
}
return NULL;
}
|
|
|
|
|
Thanks for this idea. I have added it to my latest update which I have posted and should appear here soon.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
I had to change any HTASK I found to a DWORD to get it to compile in Visual Studio .NET 2003
|
|
|
|
|
Thanks for letting me know about this. I'm only just about moving to VS.NET so I've not yet found these things. I have added #if _MFC_VER >= 0x0700 tests to my code so that my future updates will compile with all versions of VS/MFC.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Hi friends
Great control. Thanks.
Did anyone tried to put this or similar control in a cell of MFC Grid (of Chris Maunder) ?
If yes, please give some guideline/code/help.
Thanks
Ana
Ana_v123
|
|
|
|
|
Does anyone know how the color ( the white space) of a CDateTimeCtrl is changed. I have tried to handle WM_CTLCOLOR , but it seems this control does not send such a message.
|
|
|
|