我想知道如何获取 OS X / macOS 鼠标指针当前所在像素的颜色。
我编写了一个控制台应用程序,所以我没有可以覆盖的窗口或其他东西。
当我构建并运行该程序时,它应该为我提供鼠标指针当前所在颜色的控制台日志。那可能吗?
Using 这个问题和答案首先,这是一个功能齐全的命令行程序。
// To build and run, save this file as main.m, then:
//
// clang -framework Foundation -framework Cocoa main.m
// ./a.out
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
int main(int argc, const char *argv[]) {
CGDirectDisplayID mainDisplayID = CGMainDisplayID();
// NSLog(@"Main display id=%d", mainDisplayID);
while (true) {
@autoreleasepool {
CGPoint cursor = CGEventGetLocation(CGEventCreate(NULL));
// NSLog(@"Mouse pos: (%f, %f)", cursor.x, cursor.y);
CGRect rect = CGRectMake(cursor.x, cursor.y, 1, 1);
CGImageRef image = CGDisplayCreateImageForRect(mainDisplayID, rect);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
CGImageRelease(image);
NSColor *color = [bitmap colorAtX:0 y:0];
NSLog(@"%@", color);
[bitmap release];
}
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)