如何阻止href的title属性提示框显示在透明Form上方?
Delphi透明Form阻止网页title提示框置顶问题
我有两段实现Form透明的Delphi代码:
实现方式一
Form1.AlphaBlend := True; SetWindowLong(Form1.Handle, GWL_EXSTYLE, GetWindowLong(Form1.Handle,GWL_EXSTYLE) or WS_EX_TRANSPARENT or WS_EX_LAYERED);
实现方式二
type TForm1 = class(TForm) private { Private declarations } public { Public declarations } protected procedure CreateParams(var Params: TCreateParams); override; end; // AlphaBlend := True; procedure TForm1.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); Params.ExStyle := Params.ExStyle or WS_EX_TOPMOST or WS_EX_TRANSPARENT; Params.ExStyle := WS_EX_TRANSPARENT or WS_EX_TOPMOST; end;
当鼠标悬停在带有title属性的网页链接上时,浏览器弹出的title提示框会显示在我的透明Form上方,请问如何阻止这种行为?

解决方法
网页的title提示框属于系统级tooltip窗口,默认会抢占部分置顶窗口的层级。可以通过以下两种可靠方式解决:
方法1:通过消息拦截强制维护窗口层级
在Form类中添加WM_WINDOWPOSCHANGING消息处理,确保窗口始终保持置顶状态,不会被tooltip挤到下方:
首先在Form的类声明中添加消息处理函数声明:
type TForm1 = class(TForm) // ... 已有组件声明 private procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING; protected procedure CreateParams(var Params: TCreateParams); override; public { Public declarations } end;
然后实现消息处理函数:
procedure TForm1.WMWindowPosChanging(var Message: TWMWindowPosChanging); begin inherited; // 禁止窗口改变Z顺序,同时强制设置为顶级窗口 Message.WindowPos.flags := Message.WindowPos.flags or SWP_NOZORDER; SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE); end;
方法2:定时刷新窗口层级
如果不想处理消息,可以添加一个Timer组件(设置Interval为100-200ms),在Timer事件中强制将Form置顶:
procedure TForm1.Timer1Timer(Sender: TObject); begin // 保持窗口置顶,不改变位置和大小 SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE); end;
额外提示
- 第一段代码中同时使用
WS_EX_TRANSPARENT和WS_EX_LAYERED可能存在冗余,WS_EX_LAYERED配合AlphaBlend已经可以实现透明效果,可根据实际需求调整。 - 若使用
WS_EX_TOPMOST,需注意它会让窗口始终在所有非置顶窗口上方,但系统tooltip可能会突破这个层级,所以需要上述方法强制维护。
内容的提问来源于stack exchange,提问作者FLASHCODER
相关产品推荐
相关产品推荐

