NSWindow setFrame | NSWindow设置屏幕上的位置

发布于 2024-10-19 15:46:32 字数 1281 浏览 1 评论 0原文

在我的应用程序中,我需要一次显示同一 NIB 的多个窗口,并且它将根据计时器关闭/释放,

除了窗口位置之外,一切都工作正常。我的要求是,它应该将窗口显示在前一个窗口的正下方,如果存在相同 NIB 的任何窗口,我能够计算新窗口的原点。

我正在使用这个函数来获取原点:

-(void)awakeFromNib{  

    NSString *eventMsg = [NSString stringWithFormat:@"%s is set “,eventName];  
    [pTextField setStringValue:eventMsg];

    [pWindow setFrameOrigin:[self pointstoDisplayScreen]];

    /* On timer callback , i will show the fadeout and fadein effects*/
    [self startTimer];
}     

/* This method returns the coordinate where to 
   draw the window 
 */
-(NSPoint)pointstoDisplayScreen{
    NSRect frame = [[NSScreen mainScreen] visibleFrame];
    NSRect windowRect = [pWindow frame];

    CGFloat yCoordinate = frame.size.height-windowRect.size.height;

    NSPoint point = NSMakePoint(frame.size.width - windowRect.size.width, frame.size.height-windowRect.size.height);

    /* Let there be some gap, if any window present and let it draw below to 
       the existing window 
     */
    point.y -=  (windowRect.size.height + windowOffset)*noOfWindow;
    NSLog([NSString stringWithFormat:@"PositionToDisplayScreen x = [%f] y = [%f]",point.x,point.y ]);

    return point;
}

问题是,如果存在前一个窗口,它会绘制新点,略低于现有窗口的右侧,

是否有我需要设置的任何属性,问题是,如果之前的位置正好在右上角,然后它在对角绘制新窗口。

In my application, I need to display a multiple window of same NIB, at a time, and it will be closed/released on the timer basis,

everything is working fine, except the window position. My requirement is, it should display the window exactly below the previous window, I am able to calculate origin for the new window if any window of same NIB is present.

I am using this function to get the origin:

-(void)awakeFromNib{  

    NSString *eventMsg = [NSString stringWithFormat:@"%s is set “,eventName];  
    [pTextField setStringValue:eventMsg];

    [pWindow setFrameOrigin:[self pointstoDisplayScreen]];

    /* On timer callback , i will show the fadeout and fadein effects*/
    [self startTimer];
}     

/* This method returns the coordinate where to 
   draw the window 
 */
-(NSPoint)pointstoDisplayScreen{
    NSRect frame = [[NSScreen mainScreen] visibleFrame];
    NSRect windowRect = [pWindow frame];

    CGFloat yCoordinate = frame.size.height-windowRect.size.height;

    NSPoint point = NSMakePoint(frame.size.width - windowRect.size.width, frame.size.height-windowRect.size.height);

    /* Let there be some gap, if any window present and let it draw below to 
       the existing window 
     */
    point.y -=  (windowRect.size.height + windowOffset)*noOfWindow;
    NSLog([NSString stringWithFormat:@"PositionToDisplayScreen x = [%f] y = [%f]",point.x,point.y ]);

    return point;
}

The problem is if if previous window is present it draws the new point, slightly below and toward right side of the existing window,

Is there any property i need set, the problem is, if the previous position is exactly on the top right, then it draw the new window on the opposite corner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似最初 2024-10-26 15:46:32
[NSWindowController setShouldCascadeWindows:NO]; 

通过设置,它可以工作,现在所有通知都绘制在屏幕的右上角。
谢谢。

[NSWindowController setShouldCascadeWindows:NO]; 

By setting this, its working, Now all the notification drawing at the top right corner of the screen.
Thanks.

甜心小果奶 2024-10-26 15:46:32

您的窗口位置正在“跳跃”,因为您没有进行任何边界检查来确定计算出的新原点是否在屏幕边界内。

NSScreen 类将为您提供屏幕的详细信息,对于每个屏幕,您无法获取其frame

现在您需要决定窗口原点如何移动;例如,到屏幕边缘(仅此而已)、到相邻屏幕(如果有)、环绕(从右到左)等。选择将取决于您的应用。

有了屏幕框架和移动算法,您就可以计算新的原点。

Your window location is "jumping" because you are not doing any bounds checking to determine that the calculated new origin is within the screen bounds.

The class NSScreen will give you details of the screen(s), for each on you cant get its frame.

Now you need to decide how you want a window origin to move; e.g. to the screen edge and no further, onto an adjacent screen if there is one, wrapping (off right means come on left), etc. The choice will depend on your application.

Armed with the screen frame(s) and the movement algorithm you can calculate your new origin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文