Prevent button from receiving focus in Inno Setup

谁说我不能喝 提交于 2020-01-14 03:29:28

问题


In my script, when the audio button is clicked, it receives a focus (That blue thick border)


I want it to be, even when you click the audio button, the focused button is still the "Next/Install".


回答1:


This is difficult to implement due to a lack of OnEnter event in Inno Setup API.

First, you want to set TabStop property of the button to False to prevent the button from receiving focus using the Tab key.

Button.TabStop := False;

(In your case, it's the SoundCtrlButton).

If you are happy with focus always going back to the "Next" button, when it's mouse-clicked, it's easy. Just set the focus explicitly to the "Next" button at the end of the button's OnClick handler:

procedure ButtonClick(Sender: TObject);
begin
  { Some actual code }

  { If the button is focused (it won't be, when access key was used to "click" it) ... }
  if TButton(Sender).Focused then
    { ... focus the "Next" button }
    WizardForm.ActiveControl := WizardForm.NextButton;
end;

(In your case, the OnClick handler is SoundCtrlButtonClick).


Though, if you want to implement this nicely, by returning the focus back to the control that actually had the focus previously, it's more difficult.

I cannot think of better solution than scheduling a frequent timer to monitor to the focused control.

[Code]

function SetTimer(hWnd: LongWord; nIDEvent, uElapse: LongWord; 
  lpTimerFunc: LongWord): LongWord; external 'SetTimer@user32.dll stdcall';

var
  LastFocusedControl: TWinControl;

procedure FocusMonitorProc(
  H: LongWord; Msg: LongWord; IdEvent: LongWord; Time: LongWord);
begin
  { Remember focused control, unless the currently focused control is already the one, }
  { we do not want to get focused }
  if (WizardForm.ActiveControl = nil) or
     WizardForm.ActiveControl.TabStop then
  begin
    LastFocusedControl := WizardForm.ActiveControl;
  end;
end;

procedure ButtonClick(Sender: TObject);
begin
  { Some actual code }

  { If the button is focused (it won't be, when access key was used to "click" it) ... }
  if TButton(Sender).Focused and (LastFocusedControl <> nil) then
    { ... focus the previously focused control }
    WizardForm.ActiveControl := LastFocusedControl;
end;

procedure InitializeWizard();
begin
  { Set up 50ms timer to monitor the focus }
  SetTimer(0, 0, 50, CreateCallback(@FocusMonitorProc));  

  { Create the "unfocusable" button }
  SomeButton := TNewButton.Create(WizardForm);
  { Initialize button }
  SomeButton.TabStop := False;  
end;

For CreateCallback function, you need Inno Setup 6. If you are stuck with Inno Setup 5, you can use WrapCallback function from InnoTools InnoCallback library.


Alternative solution is to use a button-like image (the TBitmapImage control), instead of the actual TButton. The TBitmapImage control (not being the TWinControl) cannot receive focus at all.

And it can actually get you a nice "mute" image instead of the plain "Mute" caption.



来源:https://stackoverflow.com/questions/40494465/prevent-button-from-receiving-focus-in-inno-setup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!