gpt4 book ai didi

delphi - 如何避免表单在拖动时获得焦点

转载 作者:行者123 更新时间:2023-12-03 14:53:34 27 4
gpt4 key购买 nike

我有一个简单的表单,仅包含一个TTouchKeyboard。表单 BorderStyle 设置为 bsToolWindow。为了避免单击触摸键盘时表单获得焦点,我使用以下实现处理 WM_MOUSEACTIVATE 消息:

procedure TKeyboardForm.WMMouseActivate(var Message: TWMMouseActivate);
begin
Message.Result := MA_NOACTIVATE;
end;

BorderStyle 设置允许表单随标题栏一起拖动,但在这种情况下,表单仍然获得焦点。有办法避免这种情况吗?

更新:我尝试将 WS_EX_NOACTIVATE 添加到 CreateParams 中的 ExStyle,但不幸的是,这并不妨碍表单在拖动时接收焦点。

procedure TKeyboardForm.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_NOACTIVATE;
end;

最佳答案

因为我对需要我手动更新键盘表单中的焦点表单变量的方法不太满意,所以我寻找了一个更透明的解决方案并提出了这种方法。

更新:以前的方法在 VCL 样式方面存在一些问题。此外,并非所有消息处理程序都是真正必要的,尽管其他消息处理程序也被证明很有帮助。此版本与 VCL 样式配合良好,尽可能避免任何闪烁:

type
TKeyboardForm = class(TForm)
TouchKeyboard1: TTouchKeyboard;
private
FLastFocusedForm: TCustomForm;
procedure SetLastFocusedForm(const Value: TCustomForm);
protected
procedure WMActivate(var Message: TWMActivate); message WM_ACTIVATE;
procedure WMMouseActivate(var Message: TWMMouseActivate); message WM_MOUSEACTIVATE;
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
property LastFocusedForm: TCustomForm read FLastFocusedForm write SetLastFocusedForm;
public
class constructor Create;
destructor Destroy; override;
function SetFocusedControl(Control: TWinControl): Boolean; override;
end;

type
TKeyboardFormStyleHook = class(TFormStyleHook)
protected
procedure WMNCActivate(var Message: TWMNCActivate); message WM_NCACTIVATE;
end;

procedure TKeyboardFormStyleHook.WMNCActivate(var Message: TWMNCActivate);
begin
{ avoids the title bar being drawn active for blink }
Message.Active := False;
inherited;
end;

class constructor TKeyboardForm.Create;
begin
TCustomStyleEngine.RegisterStyleHook(TKeyboardForm, TKeyboardFormStyleHook);
end;

destructor TKeyboardForm.Destroy;
begin
LastFocusedForm := nil;
inherited;
end;

procedure TKeyboardForm.Notification(AComponent: TComponent; Operation: TOperation);
begin
if (Operation = opRemove) and (AComponent = FLastFocusedForm) then begin
FLastFocusedForm := nil;
end;
inherited;
end;

function TKeyboardForm.SetFocusedControl(Control: TWinControl): Boolean;
begin
LastFocusedForm := Screen.FocusedForm;
result := inherited;
end;

procedure TKeyboardForm.SetLastFocusedForm(const Value: TCustomForm);
begin
if FLastFocusedForm <> Value then
begin
if FLastFocusedForm <> nil then begin
FLastFocusedForm.RemoveFreeNotification(Self);
end;
FLastFocusedForm := Value;
if FLastFocusedForm <> nil then begin
FLastFocusedForm.FreeNotification(Self);
end;
end;
end;

procedure TKeyboardForm.WMActivate(var Message: TWMActivate);
begin
Message.Active := WA_INACTIVE;
inherited;
end;

procedure TKeyboardForm.WMMouseActivate(var Message: TWMMouseActivate);
begin
inherited;
Message.Result := MA_NOACTIVATE;
end;

procedure TKeyboardForm.WMSetFocus(var Message: TWMSetFocus);
begin
inherited;
if (FLastFocusedForm <> nil) and (message.FocusedWnd <> FLastFocusedForm.Handle) then begin
SendMessage(FLastFocusedForm.Handle, WM_SETFOCUS, 0, 0);
Message.FocusedWnd := FLastFocusedForm.Handle;
end;
end;

关于delphi - 如何避免表单在拖动时获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com