确定聚焦窗口是否有活动插入符号



以下_isEdit功能检测输入是否可以应用于当前聚焦的控件:

class function TSpeedInput._getFocusedControlClassName(): WideString;
var
lpClassName: array[0..1000] of WideChar;
begin
FillChar(lpClassName, SizeOf(lpClassName), 0);
Windows.GetClassNameW(GetFocus(), PWideChar(@lpClassName), 999);
Result := lpClassName;
end;
class function TSpeedInput._isEdit(): Boolean;
const
CNAMES: array[0..3] of string = ('TEdit', 'TMemo', 'TTntMemo.UnicodeClass',
'TTntEdit.UnicodeClass');
var
cn: WideString;
i: Integer;
begin
Result := False;
cn := _getFocusedControlClassName();
for i := Low(CNAMES) to High(CNAMES) do
if cn = CNAMES[i] then begin
Result := True;
Exit;
end;
//MessageBoxW(0, PWideChar(cn), nil, 0);
end;

我不喜欢的是类名列表的硬编码。是否可以检测到当前关注的窗口属于编辑器家族,或者更好地说,它有一个活动的插入符号?(以便_isEdit为处于只读模式的WhateverItIsControl返回False(。

如果控件的Handle已分配,则可以使用以下破解:

function IsEdit(AControl: TWinControl): boolean;
begin
if AControl.HandleAllocated then
begin
Result := SendMessage(AControl.Handle, EM_SETREADONLY,
WPARAM(Ord(AControl.Enabled)), 0) <> 0;
end
else
begin
Result := AControl is TCustomEdit;
end;
end;

如果您感兴趣的控件位于特定窗体上,并且由该窗体拥有(并且是标准的Delphi控件(,则可以使用以下控件:

function TFormML2.FocusIsEdit: boolean;
var
i : integer;
begin
Result := FALSE;
for i := 0 to ComponentCount - 1 do
begin
if Components[ i ] is TCustomEdit then
begin
if (Components[ i ] as TCustomEdit).Focused and not (Components[ i ] as TCustomEdit).ReadOnly  then
begin
Result := TRUE;
break;
end;
end;
end;
end;

如果你知道这个表单,并且可以把它作为参数传递,你也可以做类似的事情。

TCustomEdit是所有编辑框、备忘录等的祖先。

最新更新