2011-11-04 139 views
4

我开发的Mac桌面应用程序,我在哪里捕捉使用不显示鼠标光标

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

,并显示屏幕截图, 问题是,我期待它应该显示鼠标光标过于屏幕,但它没有显示, 我需要启用任何设置吗?

我试图调用此函数

CGDisplayShowCursor(kCGDirectMainDisplay); 

CGAssociateMouseAndMouseCursorPosition(true); 

之前以下,但它没有工作, 当我使用以下

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

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

该值表示选中,光标未在framebuffer的绘制( )但光标是可见的, 我想我只需要做的是,在画框缓冲区中绘制光标,但这是如何挑战的,

提前致谢。

+0

至于2016年,现在的我我没有使用CG框架来捕获桌面图像流,现在我移动AVFoundation框架,它工作非常顺利,并添加了鼠标光标内置。 – Amitg2k12

回答

6

看来,帧缓冲不给我上的光标,所以我画我自己,这是代码片段,可能会帮助全额你们,

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