【问题标题】:Capture mouse cursor in screenshot在屏幕截图中捕获鼠标光标
【发布时间】:2011-11-04 11:34:42
【问题描述】:

我正在开发 Mac 桌面应用程序,我正在使用其中捕获屏幕

CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionAll, kCGNullWindowID, kCGWindowImageDefault);

问题是,我希望它也应该显示鼠标光标,但它没有显示。

我需要为此启用任何设置吗?

在调用此函数之前我尝试了以下操作:

CGDisplayShowCursor(kCGDirectMainDisplay);
    
CGAssociateMouseAndMouseCursorPosition(true);

,但是没有用。

当我使用以下方法检查时

bool bCursor = CGCursorIsDrawnInFramebuffer(); /* This returns false */

bCursor = CGCursorIsVisible();  /* This returns true */

,te值表示光标没有在帧缓冲区中绘制,但是光标是可见的。

我想我只需要在帧缓冲区中绘制光标,我该怎么做呢?

【问题讨论】:

  • 和 2016 年一样,现在我没有使用 CG 框架来捕获桌面图像流,现在我转向了 AVFoundation 框架,它工作得非常顺利,并且内置了鼠标光标。

标签: objective-c macos cocoa


【解决方案1】:

看来,framebuffer没有给我鼠标光标,所以我自己画,这是代码sn-p,可能对你们有帮助,

-(CGImageRef)appendMouseCursor:(CGImageRef)pSourceImage{
    // 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(pSourceImage);
    size_t width =  CGImageGetWidth(pSourceImage);
    int bytesPerRow = CGImageGetBytesPerRow(pSourceImage);

    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(pSourceImage), 
                                                  CGImageGetBitmapInfo(pSourceImage));

    // first draw the image 
    CGContextDrawImage(context,bgBoundingBox,pSourceImage);

    // then mouse cursor 
    CGContextDrawImage(context,CGRectMake(0, 0, width,height),pSourceImage);

    // 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 */
}

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多