Reputation: 3
originally i need a screen coordinate of an NSWindow -> NSView -> NSView (subview) -> NSRect for screen capturing. I found bitmapImageRepForCachingDisplayInRect too slow for my needs (getting the rect straight out of the view), so i'm going to use CGWindowListCreateImage.
When i try to find the screen coordinate of my final rect, i face a strange thing that i can't figure out: the contentView origin screen coordinate not equals the window origin screen coordinate, even though the contentView origin is 0,0 in the window space.
Here's my simplified code:
@interface MyWindowController ()
@end
@implementation MyWindowController
- (void)windowDidLoad {
[super windowDidLoad];
NSRect rectWindowOnScreen = [[self window] convertRectToScreen:self.window.frame];
printf("rectWindowOnScreen:%.2f %.2f %.2f %.2f\n", rectWindowOnScreen.origin.x, rectWindowOnScreen.origin.y, rectWindowOnScreen.size.width, rectWindowOnScreen.size.height);
NSRect rectWindowContentArea = [[self window] contentLayoutRect];
printf("rectWindowContentArea:%.2f %.2f %.2f %.2f\n", rectWindowContentArea.origin.x, rectWindowContentArea.origin.y, rectWindowContentArea.size.width, rectWindowContentArea.size.height);
NSRect rectWindowContentAreaOnScreen = [[self window] convertRectToScreen:[[self window] contentLayoutRect]];
printf("rectWindowContentAreaOnScreen:%.2f %.2f %.2f %.2f\n", rectWindowContentAreaOnScreen.origin.x, rectWindowContentAreaOnScreen.origin.y, rectWindowContentAreaOnScreen.size.width, rectWindowContentAreaOnScreen.size.height);
NSRect rectWiew = [[[self window] contentView] frame];
printf("rectWiew:%.2f %.2f %.2f %.2f\n", rectWiew.origin.x, rectWiew.origin.y, rectWiew.size.width, rectWiew.size.height);
NSRect rectWiewOnScreen = [[self window] convertRectToScreen:[[[self window] contentView] frame]];
printf("rectWiewOnScreen:%.2f %.2f %.2f %.2f\n", rectWiewOnScreen.origin.x, rectWiewOnScreen.origin.y, rectWiewOnScreen.size.width, rectWiewOnScreen.size.height);
}
@end
The results are:
rectWindowOnScreen:470.00 578.00 480.00 292.00
rectWindowContentArea:0.00 0.00 480.00 270.00
rectWindowContentAreaOnScreen:235.00 289.00 480.00 270.00
rectWiew:0.00 0.00 480.00 270.00
rectWiewOnScreen:235.00 289.00 480.00 270.00
How is it possible? Where does 235.00 289.00 coordinates comes from?
I tried to set [[[self window] contentView] setFrameOrigin: but results were just the same.
I run into a dead end. I'm grateful for all your help, thank you.
Upvotes: 0
Views: 440
Reputation: 330
As Willeke says, you can get the window frame in screen coordinates like this:
NSRect frameRect = window.frame;
This will include the window's title bar. If you just want the content rect, you don't need to explicitly get the content view (as in your fourth printf statement). You can simply do this:
NSRect contentRect = [window contentRectForFrameRect: window.frame];
Upvotes: 1