#objective-c #cocoa #cursor #cursor-position #nsevent
#objective-c #cocoa #курсор #положение курсора #nsevent
Вопрос:
Мне нужно захватить фреймбуфер и сохранить его в файл,
я использую код примера son of grab для захвата буфера, но это не дает мне указатель мыши,
Итак, я рисую указатель мыши самостоятельно, пожалуйста, обратитесь к приведенному ниже фрагменту кода, все работает нормально, за исключением того, что курсор нарисован не в нужном месте, есть небольшое отклонение в координатах x amp; y, образующих структуру Cocoa, я получаю местоположение указателя мыши, кажется, каким-то образом я должен получить границы курсора мыши, поэтому тот же rect, который я могу использовать для рисования курсора,
Есть идеи, как использовать указатель местоположения Mousse, чтобы нарисовать изображение мыши в правильном месте?
-(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 */
}
Все работает нормально, за исключением небольшого отклонения положения мыши от
Ответ №1:
Необходимо учитывать горячую точку курсора мыши, которая представляет собой пиксель в курсоре, являющийся «активной точкой». Вы можете получить это из -hotspot
метода NSCursor
, который возвращает значение NSPoint
относительно нижнего левого угла системы координат курсора.
Таким образом, ваш код, вероятно, должен быть чем-то вроде:
NSPoint offset = [[NSCursor arrowCursor] hotSpot];
int org_x = (x - w/2) - offset.x;
int org_y = (y - h/2) - offset.y;