gpt4 book ai didi

macos - 当windows是关键还是非关键时绘制NSControl

转载 作者:行者123 更新时间:2023-12-03 16:17:41 27 4
gpt4 key购买 nike

我有一个 NSControl subview ,我想在控件不在 keyWindow 上时更改绘图。问题是我没有看到任何反射(reflect)该状态的属性(尝试了 enabled 属性,但事实并非如此)。

简单来说,我可以区分这两种状态吗?

disabled enabled

最佳答案

您可以使用 NSWindow 的 keyWindow 属性,如果您想检查您的控件是否是键盘事件的第一响应者,还可以测试 [[self window]firstResponder] == self 。我不相信 keyWindow 支持 KVO,但是您可以监听 NSWindowDidBecomeKeyNotification 和 NSWindowDidResignKeyNotification 。例如,

- (id)initWithFrame:(NSRect)frameRect;
{
if ( self = [super initWithFrame:frameRect] )
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(display) name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(display) name:NSWindowDidBecomeKeyNotification object:nil];
}

return self;
}

- (void)drawRect:(NSRect)aRect;
{
if ( [[self window] isKeyWindow] )
{
// one way...
}
else
{
// another way!
}
}

- (void)dealloc;
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
[super dealloc];
}

关于macos - 当windows是关键还是非关键时绘制NSControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6129238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com