完成自编辑地DatePicker控件

文章作者 100test 发表时间 2008:03:29 14:21:09
来源 100Test.Com百考试题网


简介

我想要这样一个控件,既包括标准date picker控件的功能,又能让用户任何时候都能自己输入日期。标准的date picker控件有其自己的实现自编辑日期的方法,但是这并没有达到我想要的。

文档

此控件基于微软的CDateTimeCtrl类,我使用了一些小技巧用我自己的编辑控件和按钮替换了标准的控件,但是处理类仍继承于CDateTimeCtrl。为了实现这个功能,我销毁了已存在的窗口,并且创建了一个与类相关联的我自己的窗口。工作原理参看CDateTimeEditCtrl::OnRecreate。

基于我的实现方法,这个控件的使用与标准的CDateTimeCtrl完全一样。在你的对话框资源中加入一个标准的Date picker控件,然后为其创建CDateTimeEditCtrl类型的成员变量。

这个控件支持通常的Date picker风格,下面几个风格不支持:DTS_UPDOWN, DTS_SHOWNONE, DTS_APPCANPARSE, DTS_LONGDATEFORMAT, and DTS_TIMEFORMAT。

消息传递与标准控件一样,但是不支持DTM_GETRANGE, DTM_SETRANGE和DTM_SETFORMAT消息。控件中事件的通知与标准控件一样,发送给父窗口。

一些额外的公有函数也被加入以提供更强的功能扩展,这些公有函数列表如下:

BOOL Create(DWORD dwStyle, const RECT&. rect, CWnd* pParentWnd, UINT nID).
与CDateTimeCtrl功能类似.


CEdit* GetEditControl() const.
返回一个指向编辑控件的指针,通过这个指针可以直接处理它的属性。


void EnableButton(BOOL bEnable = TRUE).
允许或禁止用来下拉calendar control的按钮

void RestoreFocus(BOOL bRestore = TRUE).
Determines the behaviour when clicking on the button. If TRUE then the control re-sets the focus back to the window whitch had the focus on clicking the button, else the focus is set to the edit control. If the control is not editable focus is always set back to the previously focused window. The default is for the focus to be set to the edit control.


void SetNonEditable(BOOL bNonEditable = TRUE).
Used to make the edit portion of the control non-editable. The default is editable.


BOOL GetNonEditable().
Returns whether the edit control is currently editable.


virtual BOOL IsValidDate(LPCTSTR lpszDate = NULL).
Returns whether the date string passed is valid. If lpszDate is NULL then it returns whether the text in the edit control is a valid date string.


void SetValidCharsOnly(BOOL bValidCharsOnly = TRUE).
Sets whether the user can only enter characters that are valid


BOOL GetValidCharsOnly().
Returns whether the user can only enter valid characters into the edit control


void SetValidChars(LPCTSTR lpszValidChars = NULL).
Sets the characters that are valid for the user to type into the edit control. If NULL is specified, then the default characters are used (0-9 and the current users locales date separator). If the user changes the locale settings then the control will detect this and use the new separator.


CString GetValidChars().
Returns the characters that have been set as being valid for the user to type into the edit control


void SetAllowUpDownKeys(BOOL bAllow = TRUE).
Sets whether the up/down arrow keys will increment/decrement the part of the date string that contains the cursor.


BOOL GetAllowUpDownKeys().
Returns whether the up/down keys will increment/decrement parts of the date string.

相关文章


C 中调用DLL实现数据加密
VC 与MATLAB混合编程
VC 中的伪随机数
C 智能指针应用分析
完成自编辑地DatePicker控件
专用寄存器的作用
通用寄存器的作用
汇编语言基础之CPU资源和存储器
汇编非数值数据的表示
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛