单击自定义NSWindow [英] Click through custom NSWindow

查看:127
本文介绍了单击自定义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上,要使具有自定义形状的窗口在透明区域上单击,必须满足以下条件:

  1. 必须仅使用[window setStyleMask: NSBorderlessWindowMask]设置的NSBorderlessWindowMask创建窗口 .

  2. 不得在其上致电[window setIgnoresMouseEvent: NO].由于该方法显然包含苹果方面的错误.

  3. 窗口的
  4. 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:

- (void)drawRect:(NSRect)rect {
   ...
   // Reset the window shape and shadow.
    if (shouldDisplayWindow) {
        [[self window] display];
        [[self window] setHasShadow:NO];
        [[self window] setHasShadow:YES];
    }
}

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:

  1. The window must be created with only NSBorderlessWindowMask set by [window setStyleMask: NSBorderlessWindowMask] for example.

  2. You MUST NOT call [window setIgnoresMouseEvent: NO] on it. As that method clearly contains a bug on Apple's side.

  3. contentView of the window MUST NOT use layers. So something like this [[window contentView] setWantsLayer: YES] effectively disables click through too.

Just in case: all this was about layered windows handling in Sciter on OS X

这篇关于单击自定义NSWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆