Override tooltip text for Titlebar buttons (Close, Maximize, Minimize, Help)

孤者浪人 提交于 2019-11-28 10:11:34

As a workaround, you may not use the help button, instead: add your custom button.

Although this sample not so perfect, but it shows you the idea.

This is an extremely interesting question. My first idea was to alter the system menu, using GetSysMenu. I tried both to remove and rename the "Close" item, but the tooltip of the Close button did not change. Then I tried to capture the HWND of the tooltip window, but I did not succeed. If I let the form (I work in Delphi) display a tooltip named "Test", I can get its HWND by FindWindow(nil, 'Test'), and then I can SendMessage WM_CLOSE to it.

In the following sample code, I use a timer to constantly search for the tooltip. This is bad for performance, so one would want to find out exactly when the tooltip appears. In this case, when the tooltip is associated with a client control, one can simply use the OnHint event.

procedure TForm1.Timer1Timer(Sender: TObject);
var
  h: HWND;
begin
  h := FindWindow(nil, 'Test');
  if h <> 0 then
    SendMessage(h, WM_CLOSE, 0, 0);
end;

However, there are two problems when the tooltip is associated with the title bar buttons.

  1. I was unable to get a handle of the tooltip for the Close button by using FindWindow(nil, 'Close');
  2. If we are able to get the handle, we need a smart place to write the code - we do not want it in a timer. OnHint (in Delphi - similar events exist in all native Win32 apps) will probably only work for client controls. One might use the WM_NC* messages to deduce when a title bar tooltip is to be shown.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!