Cocoa学习之路 屏幕坏点检查程序开发实例
Cocoa学习之路 屏幕坏点检查程序开发实例是本文要介绍的内容,不多说,直接进入话题。这个屏幕坏点检查程序的原理是:用 red,green,blue,black,white 五种颜色以全屏模式显示,从而检测屏幕是否存在坏点、暗点、亮点。在全屏模式下单击切换到下一个颜色,双击退出。主要学习Cocoa创建一个全屏窗口和事件处理,主要参考资料有:
RoundTransparentWindow、NSWindow 、NSEvent 。

在cocoa中全屏窗口需要继承NSWindow重写- (id)initWithContentRect,设置windowStyle为NSBorderlessWindowMask的无边界窗口
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
// Using NSBorderlessWindowMask results in a window without a title bar.
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self != nil) {
[self setLevel:NSStatusWindowLevel];
[self setBackgroundColor:[NSColor redColor]];
NSRect screenFrame = [[NSScreen mainScreen] frame];
[self setFrame:NSMakeRect(0, 0, screenFrame.size.width, screenFrame.size.height) display:YES animate:YES];
}
return self;
} 如果想要全屏窗口中响应鼠标事件,必须重写一下- (BOOL)canBecomeKeyWindow,使其总是返回YES:
- (BOOL)canBecomeKeyWindow
{
return YES;
} 在 InterfaceBuilder 中为窗口绑定Class为重写的全屏窗口Class就可以了。这样一个全屏窗口就建立了,还需要创建两个事件来处理颜色切换或关闭全屏窗口。需要在全屏窗口中单击时切换到下一个颜色,如果是双击时就退出全屏窗口。mouseUp事件处理颜色切换,mouseDown事件点击两次的时候退出全屏窗口。
- (void)mouseUp:(NSEvent *)theEvent {
NSColor *wColor = [self backgroundColor];
if(wColor == [NSColor redColor]){
[self setBackgroundColor:[NSColor greenColor]];
}
else if(wColor == [NSColor greenColor]){
[self setBackgroundColor:[NSColor blueColor]];
}
else if(wColor == [NSColor blueColor]){
[self setBackgroundColor:[NSColor blackColor]];
}
else if(wColor == [NSColor blackColor]){
[self setBackgroundColor:[NSColor whiteColor]];
}
else {
[self orderOut:nil]; // 隐藏窗口
}
}
- (void)mouseDown:(NSEvent *)theEvent {
// 判断双击
if ( [theEvent clickCount] == 2 )
{
[self orderOut:nil]; // 隐藏窗口
}
}