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

  • 主题发起人 主题发起人 刘麻子
  • 开始时间 开始时间

刘麻子

Unregistered / Unconfirmed
GUEST, unregistred user!
DateTimePicker 控件, 在 dmUpDown 模式下, 如何能在每次得到焦点的时候, 默认编辑位置在 Day 的位置而不是 Year/Month 的位置呢?
 
DateTimePicker 控件, 在 dmUpDown 模式下, 如何能在每次得到焦点的时候, 默认编辑位置在 Day 的位置而不是 Year/Month 的位置呢?
 
阿紫路过
 
麻麻。。什么时候给我传啊?
 
专业路过接分……
 
看来好象没有办法,学习
 
麻子老弟,还认识我吗?<br>好几年没见了<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> &nbsp;// common control info stuff<br> &nbsp;//<br><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> &nbsp; &nbsp;bUnicode: Bool; // BitBool = Boolean ??<br> &nbsp; &nbsp;bInFakeCustomDraw: Bool;<br> &nbsp; &nbsp;uiCodePage: uInt;<br> &nbsp; &nbsp;dwExStyle: DWord;<br> &nbsp; &nbsp;iVersion: lResult;<br> &nbsp;end;<br><br><br> &nbsp;//<br> &nbsp;// SUBEDITCONTROL stuff<br> &nbsp;//<br><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;TCalid = DWord;<br> &nbsp;HDPA = Pointer;<br><br>type &nbsp;<br> &nbsp;PCalendarType = ^TCalendarType;<br> &nbsp;TCalendarType = record<br> &nbsp; &nbsp;Calid: TCalid; &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> &nbsp; &nbsp;fMirrorSEC: Bool; &nbsp; // Whether or not to mirror the SubEditControls<br> &nbsp; &nbsp;fSwapTimeMarker: Bool; // Whether we need to swap the AM/PM symbol around or not<br> &nbsp;end;<br><br><br> &nbsp;//<br> &nbsp;// DATEPICK stuff<br> &nbsp;//<br><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;fEnabled: Bool;<br> &nbsp; &nbsp;fUseUpDown: Bool;<br> &nbsp; &nbsp;fFocus: Bool;<br> &nbsp; &nbsp;fNoNotify: Bool;<br> &nbsp; &nbsp;fCapture: Bool;<br> &nbsp; &nbsp;fShow: Bool; &nbsp; &nbsp; &nbsp; &nbsp;// TRUE iff we should continue to show MonthCal<br><br> &nbsp; &nbsp;fCheck: Bool; &nbsp; &nbsp; &nbsp; // TRUE iff the checkbox is checked<br> &nbsp; &nbsp;fCheckFocus: Bool; &nbsp;// TRUE iff the checkbox has focus<br><br> &nbsp; &nbsp;fLocale: Bool; &nbsp; &nbsp; &nbsp;// TRUE iff the format string is LOCALE dependent<br> &nbsp; &nbsp;fHasMark: Bool; &nbsp; &nbsp; // true iff has am/pm in current format<br> &nbsp; &nbsp;fFreeEditing: Bool; // TRUE if in the middle of a free-format edit<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.
 
会不会模拟立即发个tab按键信息;<br><br><br>有请楼主路过一下这里,结贴<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=3265470
 
// 调用示例, 请将Timer1.Interval设为1, 谢谢合作, ^^<br><br>unit Unit1;<br><br>interface<br><br>uses Classes, Controls, Forms, StdCtrls, ComCtrls, ExtCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;DateTimePicker1: TDateTimePicker;<br> &nbsp; &nbsp;Timer1: TTimer;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Timer1Timer(Sender: TObject);<br> &nbsp; &nbsp;procedure DateTimePicker1Enter(Sender: TObject);<br> &nbsp;end;<br><br>var Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>uses DatePick;<br><br> &nbsp;// Timer1.OnTimer<br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br> &nbsp;Timer1.Enabled := False; // 遇到消息后设置&quot;焦点&quot;<br> &nbsp;DatePick_SetCurSelect(DateTimePicker1.Handle, 4);<br>end;<br><br> &nbsp;// DateTimePicker1.OnEnter<br>procedure TForm1.DateTimePicker1Enter(Sender: TObject);<br>begin<br> &nbsp;Timer1.Enabled := True; // Post一个消息到消息队列<br>end;<br><br>end.
 
TDateTimePicker实际上是封装了由系统提供的SysDateTimePick32控件, 位于ComCtl32.dll中..<br><br>所以一开始遇到这个问题, 首先想到的是, 是否有专用的控件消息, 来设置/读取所谓的&quot;焦点&quot;, 但是查了一通帮助, 翻了几遍CommCtrl.pas和CommCtrl.h, 都没找到..<br><br>发鼠标键盘消息也试过, 效果不是很好, 而且我觉得, 这也不是解决问题的好办法, 不是最直接的方法, 只能是不得已而为之..<br><br>我知道此控件内部肯定有变量来记录当前&quot;焦点&quot;, 否则, 在绘制的时候, 它怎么知道哪一块显亮呢?<br><br>好在我手上有部分 Windows 2000 的源码, 仔细分析了此控件的窗口过程, 主要是看对WM_PAINT的处理..<br><br>结果验证我之前的许多猜测, 并且得到了&quot;窗口变量&quot;的结构, 然后就有了上面的代码, 并且在我的 Windows 2000 (sp5) 上测试通过..<br><br>但是不能保证在其他系统(ComCtl32.dll版本不同)上也能得到正确的结果, 因为结构的定义可能变化, 所以这也不是个好方法, 慎用!!<br><br>不过, 至少我们知道了, 此控件如何工作的, 并且, 不能设置&quot;焦点&quot;, 是它没有提供消息&quot;接口&quot;而已, 所以, 这次尝试还有点意义的 ..
 
那个应该叫做内部焦点吧
 
在程序內給個賦值語句,焦點就到了。
 
有劳了谢谢呀.马上试验呵呵.<br>报告试验结果仍然不好呵呵,当点击updown按钮时候默认在年位置则年位置有变化,<br>变化后设置焦点为day,不过<br>感觉已经进步了。可惜不大。
 
to kinneng:<br> &nbsp;叫&quot;内部焦点&quot;应该更确切一些, 谢谢提示!<br><br>to bbscom: 请问是哪个赋值语句?<br><br>to 陈晨:<br> &nbsp;老大啊,不要只看表面现象啊. 现在我们已经能直接设置其&quot;内部焦点&quot;了, 还有什么不能做的呢? <br> &nbsp;我的那个调用例子, 只是随手写的, 出现您说的那个问题, 并不奇怪. <br> &nbsp;因为逻辑上WM_TIMER是Post的消息, 需要之前的消息都处理完了(比如点击UpDown按钮的消息, 注意, 此时&quot;内部焦点&quot;还在年上呢), <br> &nbsp;等下次从队列&quot;取出&quot;了WM_TIMER, 才会执行OnTimer从而DatePick_SetCurSelect的, 所以..
 
麻子厉害,好贴就要顶呀!
 
高手,,,学习ing!!!
 
麻子兄!高人!
 
高人啊,支持一下!<br>我没有灌水的意思啊!
 
后退
顶部