0
votes

I'm coding a custom button derived from tExCustomControl wich, in turn, is derived from tCustomControl. The tExCustomControl component, takes care of painting and showing the caption. I managed to display the caption with the accel char underlined, using WinAPI. How I can tell Windows that the accel key is associated with the tExButton, so it can handle the event?

1

1 Answers

2
votes

You don't tell Windows anything. When the user types an accelerator, Windows sends your app a WM_SYSCHAR message, which the VCL handles automatically. As the VCL searches for which control handles the accelerator, your component will receive a CM_DIALOGCHAR message, which you need to respond to, eg:

type
  TMyCustomButton = class(tExCustomControl)
  private
    procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
  end;

procedure TMyCustomButton.CMDialogChar(var Message: TCMDialogChar);
begin
  if IsAccel(Message.CharCode, Caption) and Enabled and Visible and
    (Parent <> nil) and Parent.Showing then
  begin
    Click;
    Result := 1;
  end else
    inherited;
end;

IsAccel() is a public function in the Vcl.Forms unit:

function IsAccel(VK: Word; const Str: string): Boolean;

It parses an accelerator from the provided Str value and compares it to the provided VK value.

The code above is exactly how TSpeedButton implements accelerators, for instance.