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

别来无恙 提交于 2019-11-27 03:27:49

问题


I have been trying without luck to change the text of the tooltip that appears for the buttons on the main title bar of a form.

In a nutshell, we have harnessed the 'Help' button for Windows Forms to have some other purpose. This is working fine. The issue is that when hovering the mouse over that button, a 'Help' tooltip appears, which doesn't make any sense for the application.

Ideally, there would be some way to change the text of that tooltip for my application; however, at this point I would be satisfied just finding a way to disable the tooltips altogether.

I know that you can disable the tooltips for the entire OS by modifying the 'UserPreferencesMask' key in regedit, but I would really like a way to have this only affect my application.

Again, ideally there would be some way to do this with managed code, but I would not be opposed to linking into the Windows API or the like.

Thanks for any suggestions for resolving this issue!


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/2548987/override-tooltip-text-for-titlebar-buttons-close-maximize-minimize-help

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