以编程方式创建的中心窗口 [英] center programmatically created window

查看:73
本文介绍了以编程方式创建的中心窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这里使用示例来创建一个自定义的无标题栏的窗口:

I've been using the example from here to create a custom titlebar-less window:

在Mac OS X上绘制自定义窗口

我发现这是我可以在Leopard,Snow Leopard和Lion中创建无标题栏的窗口的唯一方法,其他方法在Leopard或Lion上均无法正常工作.(如果我尝试通过普通的NSWindow和IB调用无标题栏的窗口,它将不再在Leopard中启动)

I've found this is the only way i can create a titlebar-less window in Leopard, Snow Leopard and Lion, other methods don't work right either on Leopard or Lion. (If i try to invoke a titlebar-less window via normal NSWindow and IB, it won't start up in Leopard anymore)

到目前为止,这个无标题栏的自定义窗口在任何地方都可以使用,但是我无法居中,仅在Interface Builder中处于固定位置.

So far this custom titlebar-less window works great everywhere, but i can't center it, only a hard fixed position in Interface Builder.

使用[window center]将普通的NSWindow * window实现居中很容易,但是我发现在此自定义窗口子类上没有任何工作,该子类不是通过Interface Builder从nib创建的.

It's fairly easy to center a normal NSWindow *window implementation with [window center], but i've found nothing that works on this custom window subclass, a window that isn't created from nib via Interface Builder.

我已经尝试了 NSWindow 中的一些操作,但是似乎没有任何效果.

I've tried a few things from NSWindow, but nothing seems to work.

有什么想法吗?

推荐答案

CGFloat xPos = NSWidth([[window screen] frame])/2 - NSWidth([window frame])/2;
CGFloat yPos = NSHeight([[window screen] frame])/2 - NSHeight([window frame])/2;
[window setFrame:NSMakeRect(xPos, yPos, NSWidth([window frame]), NSHeight([window frame])) display:YES];

这会将它放在屏幕的文字中心,而不考虑扩展坞和菜单栏所占用的空间.如果要执行此操作,请将 [[[window screen]] frame] 更改为 [[[window screen] visibleFrame] .

This puts it at the literal center of the screen, not taking into account the space occupied by the dock and menu bar. If you want to do that, change [[window screen] frame] to [[window screen] visibleFrame].

这篇关于以编程方式创建的中心窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆