单击自定义NSWindow [英] Click through custom NSWindow
问题描述
我有一个自定义NSWindow,并将自定义NSView设置为其contentView.
该窗口初始化为:
[window setOpaque:NO];
[window setBackgroundColor: [NSColor clearColor]];
[window setHasShadow: NO];
[window setAcceptsMouseMovedEvents: YES];
[window setLevel: NSFloatingWindowLevel];
内容视图,在其drawRect
中绘制由纯色填充的简单圆圈.
所有这些都可以正常运行-窗口出现在桌面上,我看到了那个圆圈.
唯一不起作用的是:整个窗口矩形对于鼠标单击而言都不透明.如果我要在圆圈外(但在不可见的窗口框内)单击,则我的视图会收到mouseDown事件,但我希望可以激活底层窗口(或桌面).
看来,我需要在我的NSWindow类上重写hitTest方法之类的东西,但是不幸的是,这种方法并没有.
问题也是:在OS X中是否可以在NSWindow中使用自定义的点击区域.
更新:
查看 RoundTransparentWindow 示例,该示例可以正常工作-在透明区域中单击窗口.看起来像这样:
- (void)drawRect:(NSRect)rect {
...
// Reset the window shape and shadow.
if (shouldDisplayWindow) {
[[self window] display];
[[self window] setHasShadow:NO];
[[self window] setHasShadow:YES];
}
}
CustomView.m中的
与该问题有关,但是即使出现问题(以我为例),我也无法实现鼠标单击的透明性:(
回答我自己的问题:
在OS X上,要使具有自定义形状的窗口在透明区域上单击,必须满足以下条件:
-
必须仅使用
[window setStyleMask: NSBorderlessWindowMask]
设置的NSBorderlessWindowMask
创建窗口 . -
您不得在其上致电
[window setIgnoresMouseEvent: NO]
.由于该方法显然包含苹果方面的错误.
窗口的 -
contentView 不得使用图层.所以类似这样的
[[window contentView] setWantsLayer: YES]
也会有效地禁用点击.
以防万一:这一切都是关于
UPDATE: Looking on RoundTransparentWindow sample that works as expected - the window is click through in transparent areas. Seems like this piece: in CustomView.m is related to the problem but even with it (in my case) I cannot achieve transparency for mouse clicks :( Answering my own question: On OS X, in order to have windows with custom shapes with click through on transparent areas following conditions must be met: The window must be created with only You MUST NOT call contentView of the window MUST NOT use layers. So something like this Just in case: all this was about layered windows handling in Sciter on OS X 这篇关于单击自定义NSWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!- (void)drawRect:(NSRect)rect {
...
// Reset the window shape and shadow.
if (shouldDisplayWindow) {
[[self window] display];
[[self window] setHasShadow:NO];
[[self window] setHasShadow:YES];
}
}
NSBorderlessWindowMask
set by [window setStyleMask: NSBorderlessWindowMask]
for example.[window setIgnoresMouseEvent: NO]
on it. As that method clearly contains a bug on Apple's side.[[window contentView] setWantsLayer: YES]
effectively disables click through too.