开发者

Convert a given point from the window’s base coordinate system to the screen coordinate system

开发者 https://www.devze.com 2023-03-13 15:09 出处:网络
I am trying to figure out the way to convert a given point from the window’s base coordinate system to the screen coordinate system. I mean something like - (NSPoint)convertBaseToScreen:(NSPoint)poin

I am trying to figure out the way to convert a given point from the window’s base coordinate system to the screen coordinate system. I mean something like - (NSPoint)convertBaseToScreen:(NSPoint)point.

But I want it from quartz/carbon.

I have CGContextRef and its Bounds with me. But the bounds are with respect to Window to which CGContextRef belongs. For Example, if window is at location (100, 100, 50, 50) with respect to screen the contextRef for window would be (0,0, 50, 50). i.e. I am at location (0,0) but actually on screen开发者_C百科 I am at (100,100). I

Any suggestion are appreciated.

Thank you.


The window maintains its own position in global screen space and the compositor knows how to put that window's image at the correct location in screen space. The context itself, however doesn't have a location.

Quartz Compositor knows where the window is positioned on the screen, but Quartz 2D doesn't know anything more than how big the area it is supposed to draw in is. It has no idea where Quartz Compositor is going to put the drawing once it is done.

Similarly, when putting together the contents of a window, the frameworks provide the view system. The view system allows the OS to create contexts for drawing individual parts of a window and manages the placement of the results of drawing in those views, usually by manipulating the context's transform, or by creating temporary offscreen contexts. The context itself, however, doesn't know where the final graphic is going to be rendered.


I'm not sure if you can use directly CGContextRef, you need window or view reference or something like do the conversion. The code I use does the opposite convert mouse coordinates from global (screen) to view local and it goes something like this:

Point mouseLoc; // point you want to convert to global coordinates
HIPoint where; // final coordinates
PixMapHandle portPixMap;
// portpixmap is needed to get correct offset otherwise y coord off at least by menu bar height
portPixMap = portPixMap = GetPortPixMap( GetWindowPort( GetControlOwner( view ) ) );
QDGlobalToLocalPoint(GetWindowPort( GetControlOwner( view ), &mouseLoc);
where.x = mouseLoc.h - (**portPixMap).bounds.left;
where.y = mouseLoc.v - (**portPixMap).bounds.top;
HIViewConvertPoint( &where, NULL, view );

so I guess the opposite is needed for you (haven't tested if it actually works):

void convert_point_to_screen(HIView view, HIPoint *point)
{
    Point point; // used for QD calls
    PixMapHandle portPixMap = GetPortPixMap( GetWindowPort( GetControlOwner( view ) ) );

    HIViewConvertPoint( &where, view, NULL ); // view local to window local coordtinates

    point.h = where->x + (**portPixMap).bounds.left;
    point.w = where->y + (**portPixMap).bounds.top;

    QDLocalToGlobalPoint(GetWindowPort( GetControlOwner( view ), &point);

    // convert Point to HIPoint
    where->x = point.h;
    where->y = point.v;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消