Cocoa:使用自定义 NSView 以编程方式显示/隐藏自定义 NSWindow

发布于 2024-10-17 17:57:09 字数 12371 浏览 3 评论 0原文

首先我必须告诉你我是 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 类中我创建了一个实例的透明窗口,然后我已将 orderOut 发送到该实例。

2)我使用 NSNotificationCenter 直接向透明窗口类发送通知,并从那里调用 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

First of all I have to inform you that I am new to Objective-C and Cocoa.

I have read some books about that and I am able to build quite simple programmes now.

it's been 15 days that I'm stuck with a program that I'm trying to build and I really don't know more where to look..

I want to build a program that can change the brightness of my monitor using DDC/CI and I want to show/hide a window (where it is written the level of brightness) exactly like the Apple brightness bezel of Leopard or Snow Leopard with the exact same style.

Using RegisterEventHotKey and various IOservices functions I have been able to increase brightness when I press F2 and to decrease it wheh I press F1.

Using custom NSWindow (TransparentWindow) and custom NSView (RoundedView) I have been able to obtain a window which appears exactly as the Apple brightness bezel. I have put it on awakeFromNib and it appears correctly and stays there.

What I am not able to achieve (and I'm really becoming crazy on that) is to show the window only when I press F1 or F2. (to hide it I have alredy implemented an NSTimer but this is offtopic now)

I tried different ways:

1) From the NSobject class where I implement RegisterEventHotKey I have created an instance of TransparentWindow and then I have sent orderOut to that instance.

2) I have used NSNotificationCenter to send a notification directly to TransparentWindow class and call orderOut from there.

3) and many other that I don't remember now.

What I'm trying to do now is to make the window appearing by creating it inside awakeFromNib (and this works) and then to hide it with orderOut (and this never works).

These are the involved classes:

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

Where is the mistake?
I'm not an expert programmer but I think it could be a stupid simple mistake but I'm not able to figure it out..

Update of MainProgram.m:

I have replaced this

TransparentWindow *object3 = [[TransparentWindow alloc] init];

with this:

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];

I'm not sure if this is what you meant..

Corrected TransparentWindow.h:

#import <Cocoa/Cocoa.h>

@interface TransparentWindow : NSWindow
{

}


@end

Corrected 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

Update 2:

If I use makeKeyAndOrderFront instead of orderFront it happens a really strange fact.

After the first 3 or 4 pressing of the F1 or F2 button, nothing happens but when I press F1 or F2 another time, still no window appears but I get these strange errors in console:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2024-10-24 17:57:09

首先,

  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它。看来您在 TransparentWindow 中混合了 windowself,这是非常糟糕的。

删除属性和 ivar window,并将 TransparentWindow 中所有出现的 window 替换为 self
例如,init... 方法应如下所示

 self = [super initWithContentRect:contentRect 
                                    styleMask:NSBorderlessWindowMask 
                                      backing:NSBackingStoreBuffered 
                            defer:NO];
 if (self != nil) { ...

现在,请告诉我这些更改后情况如何!我不确定它们是否足够,但这些是最明显的问题。

First,

  TransparentWindow *object3 = [[TransparentWindow alloc] init];

is not calling the initializer you defined in

  @implementation TransparentWindow


  - (id)initWithContentRect:(NSRect)contentRect 
                  styleMask:(unsigned int)aStyle 
                    backing:(NSBackingStoreType)
                      defer:(BOOL)flag { ...

Instead, call

  TransparentWindow *object3 = [[TransparentWindow alloc] initWithContentRect: ...
                                                                    styleMask: ... 
                                                                      backing: ... defer: ...];

with appropriate parameters, so that you call what you defined!

Next, don't synthesize the property window. Your TransparentWindow is a NSWindow because it's a subclass, so you don't have to create NSWindow*window in it. It seems you're mixing window and self inside TransparentWindow, which is very bad.

Remove the property and ivar window, and replace all occurrences of window inside TransparentWindow by just self.
For example, the init... method should look like

 self = [super initWithContentRect:contentRect 
                                    styleMask:NSBorderlessWindowMask 
                                      backing:NSBackingStoreBuffered 
                            defer:NO];
 if (self != nil) { ...

Now, please tell me how things go after these changes! I'm not sure it they are enough, but these are most obvious problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文