光标覆盖,Mac Objective-c

发布于 2024-12-26 00:50:35 字数 2896 浏览 3 评论 0原文

好的,所以我试图做到这一点,以便当我的应用程序打开时,光标会发生变化,但要更改它,我会放置图像叠加层。所以这是我的代码...

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // get the cursor image 
        NSPoint mouseLoc; 
        mouseLoc = [NSEvent mouseLocation]; //get cur

        NSLog(@"Mouse location is x=%d,y=%d",(int)mouseLoc.x,(int)mouseLoc.y);

        // get the mouse image 
        NSImage *overlay    =   [[[NSCursor arrowCursor] image] copy];

        NSLog(@"Mouse location is x=%d,y=%d cursor width = %d, cursor height = %d",(int)mouseLoc.x,(int)mouseLoc.y,(int)[overlay size].width,(int)[overlay size].height);

        int x = (int)mouseLoc.x;
        int y = (int)mouseLoc.y;
        int w = (int)[overlay size].width;
        int h = (int)[overlay size].height;
        int org_x = x;
        int org_y = y;

        size_t height = CGImageGetHeight([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);
        size_t width =  CGImageGetWidth([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);
        int bytesPerRow = CGImageGetBytesPerRow([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        unsigned int * imgData = (unsigned int*)malloc(height*bytesPerRow);

        // have the graphics context now, 
        CGRect bgBoundingBox = CGRectMake (0, 0, width,height);

        CGContextRef context =  CGBitmapContextCreate(imgData, width, 
                                                      height, 
                                                      8, // 8 bits per component 
                                                      bytesPerRow, 
                                                      CGImageGetColorSpace([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]), 
                                                      CGImageGetBitmapInfo([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]));

        // first draw the image 
        CGContextDrawImage(context,bgBoundingBox,[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        // then mouse cursor 
        CGContextDrawImage(context,CGRectMake(0, 0, width,height),[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        // then mouse cursor 
        CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL] );


        // assuming both the image has been drawn then create an Image Ref for that 

        CGImageRef pFinalImage = CGBitmapContextCreateImage(context);

        CGContextRelease(context);

        return pFinalImage; /* to be released by the caller */
    }

所以我尝试了这个,它编译时带有有关 url 路径的警告,但没有错误。但是当我打开应用程序时,它崩溃了!那么有人可以帮我吗?

ok so im trying to make it so that when my app opens, the cursor changes, but to change it im putting an image overlay. So here is the code i have...

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // get the cursor image 
        NSPoint mouseLoc; 
        mouseLoc = [NSEvent mouseLocation]; //get cur

        NSLog(@"Mouse location is x=%d,y=%d",(int)mouseLoc.x,(int)mouseLoc.y);

        // get the mouse image 
        NSImage *overlay    =   [[[NSCursor arrowCursor] image] copy];

        NSLog(@"Mouse location is x=%d,y=%d cursor width = %d, cursor height = %d",(int)mouseLoc.x,(int)mouseLoc.y,(int)[overlay size].width,(int)[overlay size].height);

        int x = (int)mouseLoc.x;
        int y = (int)mouseLoc.y;
        int w = (int)[overlay size].width;
        int h = (int)[overlay size].height;
        int org_x = x;
        int org_y = y;

        size_t height = CGImageGetHeight([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);
        size_t width =  CGImageGetWidth([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);
        int bytesPerRow = CGImageGetBytesPerRow([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        unsigned int * imgData = (unsigned int*)malloc(height*bytesPerRow);

        // have the graphics context now, 
        CGRect bgBoundingBox = CGRectMake (0, 0, width,height);

        CGContextRef context =  CGBitmapContextCreate(imgData, width, 
                                                      height, 
                                                      8, // 8 bits per component 
                                                      bytesPerRow, 
                                                      CGImageGetColorSpace([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]), 
                                                      CGImageGetBitmapInfo([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]));

        // first draw the image 
        CGContextDrawImage(context,bgBoundingBox,[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        // then mouse cursor 
        CGContextDrawImage(context,CGRectMake(0, 0, width,height),[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]);

        // then mouse cursor 
        CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL] );


        // assuming both the image has been drawn then create an Image Ref for that 

        CGImageRef pFinalImage = CGBitmapContextCreateImage(context);

        CGContextRelease(context);

        return pFinalImage; /* to be released by the caller */
    }

So ive tried this and it compiles with warnings regarding the url path but it has no errors. But when i open the app, it crashes! So can anyone help me out?

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

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

发布评论

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

评论(2

烂柯人 2025-01-02 00:50:35

如果您想更改光标,为什么不使用您想要显示的任何图像创建一个新的 NSCursor 实例呢?

If you want to change the cursor, why aren't you just creating a new instance of NSCursor with whatever image you want to show?

も让我眼熟你 2025-01-02 00:50:35

您将获取一个 NSURL* 并将其传递给使用 CGImageRef 的函数。这根本就没有道理,所以我不知道为什么你没有收到错误。要从 URL 加载 CGImage,您需要使用 CGImageSource 函数,例如 CGImageSourceCreateWithURL

PS,不要只是说它崩溃了,而是在调试器下运行它并找出它崩溃的确切位置。

You're taking an NSURL* and passing it to functions that take a CGImageRef. That isn't even close to making sense, so I don't know why you didn't get errors. To load a CGImage from a URL, you need to use the CGImageSource functions like CGImageSourceCreateWithURL.

P.S., rather than just saying it crashes, run it under the debugger and find out exactly where it crashes.

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