看似简单的问题, 关于TDateTimePicker的"内部焦点".. ( 积分: 300 )

  • 主题发起人 主题发起人 刘麻子
  • 开始时间 开始时间
此乃修正后的DatePick单元, 原来那个, 某些结构定义有问题, 感谢lichengbin兄的指点.. ^^<br><br>unit DatePick;<br><br>interface<br><br>uses Windows;<br><br>function DatePick_GetCurSelect(hDatePick: HWND): Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 读取当前选中区域<br>function DatePick_SetCurSelect(hDatePick: HWND; nCur: Integer): Bool; // 设置当前选中区域<br><br>implementation<br><br> &nbsp;// *************************<br>type<br> &nbsp;BitBool = Byte;<br> &nbsp;Calid = DWord;<br> &nbsp;HDPA = Pointer;<br><br> &nbsp;//<br> &nbsp;// common control info stuff<br> &nbsp;//<br>type<br> &nbsp;PControlInfo = ^TControlInfo;<br> &nbsp;TControlInfo = record<br> &nbsp; &nbsp;hWnd: DWord; // HWND<br> &nbsp; &nbsp;hWndParent: HWND;<br> &nbsp; &nbsp;Style: DWord;<br> &nbsp; &nbsp;dwCustom: DWord;<br><br> &nbsp; &nbsp;// BITBOOL &nbsp; &nbsp; bUnicode: 1;<br> &nbsp; &nbsp;// BITBOOL &nbsp; &nbsp; bInFakeCustomDraw: 1;<br> &nbsp; &nbsp;_BitBool: BitBool;<br><br> &nbsp; &nbsp;uiCodePage: uInt;<br> &nbsp; &nbsp;dwExStyle: DWord;<br> &nbsp; &nbsp;iVersion: lResult;<br> &nbsp; &nbsp;wUIState: Word;<br> &nbsp;end;<br><br><br> &nbsp;//<br> &nbsp;// SUBEDITCONTROL stuff<br> &nbsp;//<br>type<br> &nbsp;PSubEdit = ^TSubEdit;<br> &nbsp;TSubEdit = record<br> &nbsp; &nbsp;Id: Integer; &nbsp; &nbsp; &nbsp; &nbsp; // SE_ value above<br> &nbsp; &nbsp;Rc: TRect; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // bounding box for display<br><br> &nbsp; &nbsp;pVal: PWord; &nbsp; &nbsp; &nbsp; &nbsp; // current value (in a SYSTEMTIME struct)<br> &nbsp; &nbsp;Min: uInt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // min value<br> &nbsp; &nbsp;Max: uInt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // max value<br> &nbsp; &nbsp;cIncrement: Integer; // increment value<br><br> &nbsp; &nbsp;cchMax: Integer; &nbsp; &nbsp; // max allowed chars<br> &nbsp; &nbsp;cchEdit: Integer; &nbsp; &nbsp;// current number chars entered so far<br> &nbsp; &nbsp;ValEdit: uInt; &nbsp; &nbsp; &nbsp; // value entered so far<br> &nbsp; &nbsp;flDrawText: uInt; &nbsp; &nbsp;// flags for DrawText<br><br> &nbsp; &nbsp;pv: PChar; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // formatting string<br> &nbsp; &nbsp;fReadOnly: Bool; &nbsp; &nbsp; // can this subedit be edited (receive focus)?<br> &nbsp;end;<br><br>type<br> &nbsp;PCalendarType = ^TCalendarType;<br> &nbsp;TCalendarType = record<br> &nbsp; &nbsp;_Calid: Calid; &nbsp; &nbsp; &nbsp; // Calendar id number (CAL_GREGORIAN, etc.)<br> &nbsp; &nbsp;_Lcid: LCID; &nbsp; &nbsp; &nbsp; &nbsp; // Usually LOCALE_USER_DEFAULT, but forced to US for unsupported calendars<br> &nbsp; &nbsp;dyrOffset: Integer; &nbsp;// The calendar offset (0 for Gregorian and Era)<br> &nbsp; &nbsp;HdpaYears: HDPA; &nbsp; &nbsp; // If fEra, then array of year info<br> &nbsp; &nbsp;HdpaEras: HDPA; &nbsp; &nbsp; &nbsp;// If fEra, then array of era names<br> &nbsp;end;<br><br>type<br> &nbsp;PSubEditControl = ^TSubEditControl;<br> &nbsp;TSubEditControl = record<br> &nbsp; &nbsp;pCI: PControlInfo; &nbsp;// looks like this guy needs access to the hwnd<br> &nbsp; &nbsp;fNone: Bool; &nbsp; &nbsp; &nbsp; &nbsp;// allow scrolling into SUBEDIT_NONE<br> &nbsp; &nbsp;hFont: DWord; &nbsp; &nbsp; &nbsp; // font to draw text with, HFONT<br> &nbsp; &nbsp;Rc: TRect; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// rect for subedits<br> &nbsp; &nbsp;xScroll: Integer; &nbsp; // amount pse array is scrolled<br> &nbsp; &nbsp;iSeCur: Integer; &nbsp; &nbsp;// subedit with current selection (SUBEDIT_NONE for no selection)<br> &nbsp; &nbsp;cse: Integer; &nbsp; &nbsp; &nbsp; // count of subedits in pse array<br> &nbsp; &nbsp;St: TSystemTime; &nbsp; &nbsp;// current time pse represents (pse points into this)<br> &nbsp; &nbsp;szFormat: PChar; &nbsp; &nbsp;// format string as parsed (pse points into this)<br> &nbsp; &nbsp;pSE: PSubEdit; &nbsp; &nbsp; &nbsp;// subedit array<br> &nbsp; &nbsp;cDelimeter: Char; &nbsp; // delimiter between subedits (parsed from fmt string)<br> &nbsp; &nbsp;szDelimeters: array[0..14] of Char; // delimiters between date/time fields (from resfile)<br> &nbsp; &nbsp;Ct: TCalendarType; &nbsp;// information about the calendar, **<br><br> &nbsp; &nbsp;// BITBOOL fMirrorSEC: 1; // Whether or not to mirror the SubEditControls<br> &nbsp; &nbsp;// BITBOOL fSwapTimeMarker: 1; // Whether we need to swap the AM/PM symbol around or not<br> &nbsp; &nbsp;_BitBool: BitBool;<br> &nbsp;end;<br><br><br> &nbsp;//<br> &nbsp;// DATEPICK stuff<br> &nbsp;//<br>const<br> &nbsp;MCSC_COLORCOUNT = 6;<br><br>type<br> &nbsp;PDatePick = ^TDatePick;<br> &nbsp;TDatePick = record<br> &nbsp; &nbsp;CI: TControlInfo; &nbsp; &nbsp; // all controls start with this<br><br> &nbsp; &nbsp;hWndUD: HWND;<br> &nbsp; &nbsp;hWndMC: HWND;<br> &nbsp; &nbsp;hFontMC: HFONT; &nbsp; &nbsp; &nbsp; // font for drop down cal<br><br> &nbsp; &nbsp;Clr: array[0..MCSC_COLORCOUNT-1] of ColorRef;<br><br> &nbsp; &nbsp;// HACK! stMin and stMax must remain in order and adjacent<br> &nbsp; &nbsp;StMin: TSystemTime; &nbsp; // minimum date we allow<br> &nbsp; &nbsp;StMax: TSystemTime; &nbsp; // maximum date we allow<br> &nbsp; &nbsp;StPrev: TSystemTime; &nbsp;// most recent date notified<br> &nbsp; &nbsp;Sec: TSubEditControl; // current date<br><br> &nbsp; &nbsp;RcCheck: TRect; &nbsp; &nbsp; &nbsp; // location of checkbox iff fShowNone<br> &nbsp; &nbsp;Rc: TRect; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// size of SEC space<br> &nbsp; &nbsp;RcBtn: TRect; &nbsp; &nbsp; &nbsp; &nbsp; // location of dropdown or updown<br> &nbsp; &nbsp;iseLastActive: Integer; // which subedit was active when we were last active?<br> &nbsp; &nbsp;gdtr: wParam; &nbsp; &nbsp; &nbsp; &nbsp; // Did app set min and/or max? (GDTR_MIN|GDTR_MAX)<br><br> &nbsp; &nbsp;{<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fEnabled: 1;<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fUseUpDown: 1;<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fFocus: 1;<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fNoNotify: 1;<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fCapture: 1;<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fShow: 1; &nbsp; &nbsp; &nbsp; &nbsp;// TRUE iff we should continue to show MonthCal<br><br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fCheck: 1; &nbsp; &nbsp; &nbsp; // TRUE iff the checkbox is checked<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fCheckFocus: 1; &nbsp;// TRUE iff the checkbox has focus<br><br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fLocale: 1; &nbsp; &nbsp; &nbsp;// TRUE iff the format string is LOCALE dependent<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fHasMark: 1; &nbsp; &nbsp; // true iff has am/pm in current format<br> &nbsp; &nbsp;BITBOOL &nbsp; &nbsp; &nbsp; &nbsp; fFreeEditing: 1; // TRUE if in the middle of a free-format edit<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;_BitBool1: BitBool;<br> &nbsp; &nbsp;_BitBool2: BitBool;<br> &nbsp;end;<br><br> &nbsp;// 读取当前选中区域<br>function DatePick_GetCurSelect(hDatePick: HWND): Integer;<br>var<br> &nbsp;x: PDatePick;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;x := PDatePick(GetWindowLong(hDatePick, 0));<br> &nbsp; &nbsp;Result := x.Sec.iSeCur;<br> &nbsp;except<br> &nbsp; &nbsp;Result := -1;<br> &nbsp;end;<br>end;<br><br> &nbsp;// 设置当前选中区域<br>function DatePick_SetCurSelect(hDatePick: HWND; nCur: Integer): Bool;<br>var<br> &nbsp;x: PDatePick;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;x := PDatePick(GetWindowLong(hDatePick, 0));<br> &nbsp; &nbsp;x.Sec.iSeCur := nCur;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;InvalidateRect(hDatePick, nil, True); // 重绘<br><br> &nbsp; &nbsp;Result := True;<br> &nbsp;except<br> &nbsp; &nbsp;Result := False;<br> &nbsp;end;<br>end;<br><br>end.
 
如果当前焦点不在DateTimePicker上,通过对其他控件的操作在DateTimePicker上设置焦点,该怎样设置选中区域。
 
通用控件这部分的源码, 我上传了, 有兴趣的话, 可以自己研究研究 http://www.delphibbs.com/keylife/images/u174039/comctl32.rar
 
to 悟空兄: 我注册DFW才2年呀, 而且经常上来的, 怎么会&quot;几年没见&quot;了? 呵呵.. 我的QQ 71892967, MSN: Liu_mazi@126.com, 有空多联系啊 [:)]
 
代码我在盒子上发了一份, 注意替换DatePick单元, http://www.2ccc.com/article.asp?articleid=2762
 
平分……
 
后退
顶部