【问题标题】:How do I create a custom NSCursor with alpha mask?如何使用 alpha 掩码创建自定义 NSCursor?
【发布时间】:2019-09-08 03:14:23
【问题描述】:

当我在Objective-C 中创建自定义NSCursor 时,Alpha 通道蒙版出现在XOR 下方的屏幕上。我期望 alpha 通道值为零是透明的,而不是 XOR 下图。我确定我的掩码数据是正确的ARGB,其中A=0 表示透明,A=255 表示不透明。我做错了什么?

static void maincustomcursor(bigmap *thedata, point hotspot)
{
    NSPoint thepoint;
    NSImage *newimage;
    NSCursor *thecursor;
    CGImageRef theimage;
    CGBitmapInfo theinfo;
    CGContextRef thecont;
    CGColorSpaceRef thecolor;
    int width, height, across;

    width = (*thedata).width;
    height = (*thedata).height;
    if (width != smalliconsize || height != smalliconsize) return;
    if (hotspot.h < 0) hotspot.h = 0;
    if (hotspot.h >= smalliconsize) hotspot.h = smalliconsize - 1;
    if (hotspot.v < 0) hotspot.v = 0;
    if (hotspot.v >= smalliconsize) hotspot.v = smalliconsize - 1;
    thepoint = NSMakePoint(hotspot.h, hotspot.v);
    across = (*thedata).rowbytes;
    thecolor = CGColorSpaceCreateDeviceRGB();
    theinfo = (CGBitmapInfo)kCGImageAlphaPremultipliedFirst;
    thecont = CGBitmapContextCreate((*thedata).baseaddr, width, height, 8, across, thecolor, theinfo);
    theimage = CGBitmapContextCreateImage(thecont);
    newimage = [[NSImage alloc] initWithCGImage:theimage size:NSZeroSize];
    thecursor = [[NSCursor alloc] initWithImage:newimage hotSpot:thepoint];
    [thecursor set];
    [thecursor release];
    [newimage release];
    CGImageRelease(theimage);
    CGContextRelease(thecont);
    CGColorSpaceRelease(thecolor);
}

【问题讨论】:

    标签: nscursor


    【解决方案1】:

    好的,我想通了。当 alpha 通道 = 0 时,您需要确保红色、绿色和蓝色也为零。如果 alpha 通道为零并且红色、绿色和蓝色为 255,那么我看到的是像素的异或。这可能与实现游标的古老方式有关,根据位掩码使用 clear、white、black 和 xor。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-29
      • 2021-07-05
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多