首先,我必须通知您,我是Objective-C和Cocoa的新手。
我已经阅读了一些有关此的书,并且现在能够构建非常简单的程序。
停留了15天的时间里,我一直在尝试构建一个程序,但我真的不知道该去哪里找。
我想构建一个程序,可以使用DDC/CI更改显示器的亮度,并且要显示/隐藏与Leopard或Snow Leopard的Apple亮度挡板完全相同的窗口(在其中写入亮度水平)。完全一样的风格。
使用RegisterEventHotKey
和各种IOservices功能,我可以在按F2时增加亮度,而在按F1时降低亮度。
使用自定义NSWindow(TransparentWindow)和自定义NSView(RoundedView),我已经能够获得一个与Apple亮度挡板完全一样的窗口。我把它放在awakeFromNib上,它正确显示并停留在那里。
我无法实现的(我对此感到非常疯狂)是仅在按F1或F2时才显示窗口。 (隐藏起来,我已经实现了一个NSTimer
,但是现在这已经不合时宜了)
我尝试了不同的方法:
1)从我实现RegisterEventHotKey
的NSobject类中,我创建了TransparentWindow的实例,然后将orderOut
发送到该实例。
2)我已经使用NSNotificationCenter将通知直接发送到TransparentWindow类,并从那里调用orderOut
。
3)以及其他我现在不记得的其他内容。
我现在想做的是通过在awakeFromNib
中创建窗口来使该窗口出现(这可行),然后用orderOut
隐藏该窗口(这永远都行不通)。
这些是涉及的类:
TransparentWindow.h:
#import <Cocoa/Cocoa.h>
@interface TransparentWindow : NSWindow
{
IBOutlet NSWindow *window;
}
@property (retain) IBOutlet NSWindow *window;
@end
TransparentWindow.m:
#import "TransparentWindow.h"
@implementation TransparentWindow
@synthesize window;
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag {
window = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
if (window != nil) {
[window setLevel: NSStatusWindowLevel];
[window setBackgroundColor: [NSColor clearColor]];
[window setAlphaValue:1.0];
[window setOpaque:NO];
[window setHasShadow:NO];
[window setIgnoresMouseEvents: YES];
NSLog(@"%p", window);
}
return window;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (void)notify {
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
}
- (void)handleNotification:(NSNotification*)note {
[window orderOut:self];
}
@end
RoundedView.h:
#import <Cocoa/Cocoa.h>
@interface RoundedView : NSView
{
}
@end
RoundedView.m:
#import "RoundedView.h"
@implementation RoundedView
- (void)drawRect:(NSRect)rect
{
NSColor *bgColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.25];
NSRect bgRect = rect;
int minX = NSMinX(bgRect);
int midX = NSMidX(bgRect);
int maxX = NSMaxX(bgRect);
int minY = NSMinY(bgRect);
int midY = NSMidY(bgRect);
int maxY = NSMaxY(bgRect);
float radius = 20.0; // correct value to duplicate Panther's App Switcher
NSBezierPath *bgPath = [NSBezierPath bezierPath];
// Bottom edge and bottom-right curve
[bgPath moveToPoint:NSMakePoint(midX, minY)];
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY)
toPoint:NSMakePoint(maxX, midY)
radius:radius];
// Right edge and top-right curve
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY)
toPoint:NSMakePoint(midX, maxY)
radius:radius];
// Top edge and top-left curve
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY)
toPoint:NSMakePoint(minX, midY)
radius:radius];
// Left edge and bottom-left curve
[bgPath appendBezierPathWithArcFromPoint:bgRect.origin
toPoint:NSMakePoint(midX, minY)
radius:radius];
[bgPath closePath];
[bgColor set];
[bgPath fill];
}
MainProgram.h:
#import <Cocoa/Cocoa.h>
#import <carbon/carbon.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <IOKit/IOKitLib.h>
#include <ApplicationServices/ApplicationServices.h>
#include <IOKit/i2c/IOI2CInterface.h>
@interface MainProgram : NSObject {
}
@end
MainProgram.m:
#import "MainProgram.h"
#import <Carbon/Carbon.h>
#import "TransparentWindow.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <IOKit/IOKitLib.h>
#include <ApplicationServices/ApplicationServices.h>
#include <IOKit/i2c/IOI2CInterface.h>
static OSStatus OnHotKeyEvent(EventHandlerCallRef nextHandler,EventRef theEvent,void *userData);
extern int level;
@implementation MainProgram
- (void)awakeFromNib
{
EventHotKeyRef gMyHotKeyRef;
EventHotKeyID gMyHotKeyID;
EventTypeSpec eventType;
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
InstallApplicationEventHandler(&OnHotKeyEvent, 1, &eventType, NULL, NULL);
gMyHotKeyID.signature='htk1';
gMyHotKeyID.id=1;
//RegisterEventHotKey(122, controlKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
RegisterEventHotKey(122, NULL, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
gMyHotKeyID.signature='htk2';
gMyHotKeyID.id=2;
RegisterEventHotKey(120, NULL, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
}
OSStatus OnHotKeyEvent(EventHandlerCallRef nextHandler,EventRef theEvent,
void *userData)
{
EventHotKeyID hkCom;
GetEventParameter(theEvent,kEventParamDirectObject,typeEventHotKeyID,NULL,
sizeof(hkCom),NULL,&hkCom);
int l = hkCom.id;
switch (l) {
case 1:
//NSLog(@"Hotkey 1");
level = level - 5;
if (level<0) {
level = 0;
}
else {
BrightnessMain(level);
}
break;
case 2:
//NSLog(@"Hotkey 2");
level = level + 5;
if (level>100) {
level = 100;
}
else {
BrightnessMain(level);
TransparentWindow *object3 = [[TransparentWindow alloc] init];
//[object3 orderOut:nil];
[[NSNotificationCenter defaultCenter] addObserver:object3 selector:@selector(handleNotification:) name:@"MyNotification" object:nil];
[object3 notify];
}
break;
}
return noErr;
}
@end
错误在哪里?
我不是专业程序员,但我认为这可能是一个愚蠢的简单错误,但我无法弄清楚。
MainProgram.m的更新:
我已经取代了这个
TransparentWindow *object3 = [[TransparentWindow alloc] init]
;有了这个:
NSRect contentRect;
NSSize contentSize;
contentSize.width = 210;
contentSize.height = 205;
contentRect.origin = NSMakePoint(855, 140);
contentRect.size = contentSize;
TransparentWindow *object3 = [[TransparentWindow alloc] initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
我不确定这是否是您的意思。
更正了TransparentWindow.h:
#import <Cocoa/Cocoa.h>
@interface TransparentWindow : NSWindow
{
}
@end
更正了TransparentWindow.m:
#import "TransparentWindow.h"
@implementation TransparentWindow
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag {
self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
if (self != nil) {
[self setLevel: NSStatusWindowLevel];
[self setBackgroundColor: [NSColor clearColor]];
[self setAlphaValue:1.0];
[self setOpaque:NO];
[self setHasShadow:NO];
[self setIgnoresMouseEvents: YES];
NSLog(@"%p", self);
}
return self;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (void)notify {
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
}
- (void)handleNotification:(NSNotification*)note {
[self orderOut:self];
}
@end
更新2:
如果我使用makeKeyAndOrderFront而不是orderFront,它将发生一个非常奇怪的事实。
在按F1或F2按钮的前3或4后,什么都没有发生,但是当我再次按F1或F2时,仍然没有窗口出现,但是在控制台中出现了这些奇怪的错误:
RoundedFloatingPanel[2346] <Warning>: CGSResolveShmemReference : window.RO.dirtyRegion : Reference offset (38464) exceeds bounds (32768) on shmem obj 0x229
RoundedFloatingPanel[2346] <Warning>: CGSResolveShmemReference : window.RO : Reference offset (38144) exceeds bounds (32768) on shmem obj 0x229
RoundedFloatingPanel[2346] <Error>: kCGErrorFailure: CGSNewWindowWithOpaqueShape: Cannot map window information shmem
RoundedFloatingPanel[2346] <Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
2011-02-17 16:01:42.895 RoundedFloatingPanel[2346:a0f] HIToolbox: ignoring exception 'Error (1000) creating CGSWindow' that raised inside Carbon event dispatch
(
0 CoreFoundation 0x91f186ba __raiseError + 410
1 libobjc.A.dylib 0x999e3509 objc_exception_throw + 56
2 CoreFoundation 0x91f183e8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x91f1835a +[NSException raise:format:] + 58
4 AppKit 0x93408915 _NXCreateWindow + 316
5 AppKit 0x93408720 _NSCreateWindow + 59
6 AppKit 0x93407946 -[NSWindow _commonAwake] + 1784
7 AppKit 0x9340456e -[NSWindow _commonInitFrame:styleMask:backing:defer:] + 1524
8 AppKit 0x934031c1 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1568
9 AppKit 0x93402b9b -[NSWindow initWithContentRect:styleMask:backing:defer:] + 71
10 RoundedFloatingPanel 0x000029a6 -[TransparentWindow initWithContentRect:styleMask:backing:defer:] + 119
11 RoundedFloatingPanel 0x000031ec OnHotKeyEvent + 447
12 HIToolbox 0x95ff0ecf _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1567
13 HIToolbox 0x95ff0196 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 411
14 HIToolbox 0x95fefff5 SendEventToEventTargetWithOptions + 58
15 HIToolbox 0x96024c18 _ZL29ToolboxEventDispatcherHandlerP25OpaqueEventHandlerCallRefP14OpaqueEventRefPv + 3006
16 HIToolbox 0x95ff1320 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 2672
17 HIToolbox 0x95ff0196 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 411
18 HIToolbox 0x96012a07 SendEventToEventTarget + 52
19 AppKit 0x93460c3e -[NSApplication sendEvent:] + 7494
20 AppKit 0x933f42a7 -[NSApplication run] + 917
21 AppKit 0x933ec2d9 NSApplicationMain + 574
22 RoundedFloatingPanel 0x00002368 main + 30
23 RoundedFloatingPanel 0x0000231e start + 54
24 ??? 0x00000001 0x0 + 1
最佳答案
第一的,
TransparentWindow *object3 = [[TransparentWindow alloc] init];
没有调用您在中定义的初始化程序
@implementation TransparentWindow
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)
defer:(BOOL)flag { ...
相反,请致电
TransparentWindow *object3 = [[TransparentWindow alloc] initWithContentRect: ...
styleMask: ...
backing: ... defer: ...];
带有适当的参数,以便您调用定义的内容!
接下来,不要合成属性
window
。您的TransparentWindow
是,是NSWindow
,因为它是子类,因此您不必在其中创建NSWindow*window
。看来您在window
中混合了self
和TransparentWindow
,这是非常糟糕的。删除属性和ivar
window
,并仅将window
替换为TransparentWindow
中所有出现的self
。例如,
init...
方法应类似于 self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
if (self != nil) { ...
现在,请告诉我这些变化之后的情况!我不确定它们是否足够,但是这些是最明显的问题。
关于objective-c - cocoa :以编程方式显示/隐藏具有自定义NSView的自定义NSWindow,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5028822/