English 中文(简体)
Coco/OSX的超时使用图像
原标题:Overlay Mouse Image in Cocoa / OSX

I need to capture the framebuffer and save it into the file,
i am using son of grab example code to capture the buffer, but it doesn t give me the mouse pointer,

So i am drawming the mouse pointer by my own, please refer the below code snippet, everything works fine, except cursor is not drawn at proper place, there is a slightly deviation in x & y coordinate, form the Cocoa framework, i am getting mouse pointer location, it seems somehow i should get mouse cursor bounds, so the same rect i can use to draw the cursor,
Any idea, how to use Mousse location pointer to draw the mouse image at correct location?

-(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-w/2;
    int org_y = y-h/2;

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

除此以外,所有工作都从停用位置上稍有偏离。

最佳回答

你们需要考虑湿法 cur热点,即“活性点”的 cur子。 您可从<代码>-hotspot方法 NSCursor中获取这一信息,该方法涉及 cur子协调系统的底层左上角。

因此,你的法典或许应当像:

NSPoint offset = [[NSCursor arrowCursor] hotSpot];

int org_x = (x - w/2) - offset.x;
int org_y = (y - h/2) - offset.y;
问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签