unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ExtCtrls, StdCtrls;type TForm1 = class(TForm) dtp1: TDateTimePicker; procedure dtp1DropDown(Sender: TObject); procedure dtp1CloseUp(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses CommCtrl;{$R *.dfm}var g_hHook: HHOOK;function CallWndProc(code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT stdcall; function IsYearEdit(h: hwnd): Boolean; var b: array[0..255] of Char; begin Result := False; GetClassName(h, b, 256); if b <> 'Edit' then Exit; Result := GetParent(h) = DateTime_GetMonthCal(Form1.dtp1.Handle); end; begin if code = HC_ACTION then begin Result := 0; if PCWPStruct(lParam)^.message = WM_SETFOCUS then begin if IsYearEdit(PCWPStruct(lParam)^.hwnd) then begin Form1.Text := '获得焦点' end end else if PCWPStruct(lParam)^.message = WM_KILLFOCUS then if IsYearEdit(PCWPStruct(lParam)^.hwnd) then begin Form1.Text := '失去焦点' end end else Result := CallNextHookEx(g_hHook, code, wParam, lParam) end;procedure TForm1.dtp1DropDown(Sender: TObject); begin//dtp1的OnDropDown事件 g_hHook := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, hInstance, GetCurrentThreadId) end;procedure TForm1.dtp1CloseUp(Sender: TObject); begin//dtp1的OnCloseUp事件 UnhookWindowsHookEx(g_hHook) end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
dtp1: TDateTimePicker;
procedure dtp1DropDown(Sender: TObject);
procedure dtp1CloseUp(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses CommCtrl;{$R *.dfm}var
g_hHook: HHOOK;function CallWndProc(code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT stdcall;
function IsYearEdit(h: hwnd): Boolean;
var
b: array[0..255] of Char;
begin
Result := False;
GetClassName(h, b, 256);
if b <> 'Edit' then Exit;
Result := GetParent(h) = DateTime_GetMonthCal(Form1.dtp1.Handle);
end;
begin
if code = HC_ACTION then
begin
Result := 0;
if PCWPStruct(lParam)^.message = WM_SETFOCUS then
begin
if IsYearEdit(PCWPStruct(lParam)^.hwnd) then
begin
Form1.Text := '获得焦点'
end
end
else if PCWPStruct(lParam)^.message = WM_KILLFOCUS then
if IsYearEdit(PCWPStruct(lParam)^.hwnd) then
begin
Form1.Text := '失去焦点'
end
end else
Result := CallNextHookEx(g_hHook, code, wParam, lParam)
end;procedure TForm1.dtp1DropDown(Sender: TObject);
begin//dtp1的OnDropDown事件
g_hHook := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, hInstance, GetCurrentThreadId)
end;procedure TForm1.dtp1CloseUp(Sender: TObject);
begin//dtp1的OnCloseUp事件
UnhookWindowsHookEx(g_hHook)
end;end.
但是,目前好像没有api能 取得前一个焦点控件是谁!