2011-11-04 228 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 */ 
}