NSWindow 标题栏右半部分不可拖动
我有一个 NSWindow 实例,可以通过编程方式调整大小(使用 setFrame)。创建时,它的默认大小为 50x50。然后我调用 setFrame 将其大小调整为 350x450:一切都很好,我可以随意拖动窗口。然后,在程序的后面部分,我将其大小调整为 1024x768。此时,标题栏唯一可以拖动的部分是前 350 像素:左侧部分。如果我拖动正确的部分,什么也不会发生。好像标题栏仍然认为窗口具有以前的大小。
更新(添加了设置框架的代码):
void CGLWindowMac::_setSize(int width, int height)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSRect sFrame;
sFrame = [NSWindow contentRectForFrameRect:[mObjcImp frame] styleMask:[mObjcImp styleMask]];
sFrame.origin.y += sFrame.size.height;
sFrame.origin.y -= height;
sFrame.size.height = height;
sFrame.size.width = width;
sFrame = [NSWindow frameRectForContentRect:sFrame
styleMask:[mObjcImp styleMask]];
NSLog(@"Frame Before setsize: cur Size(%f, %f) ", [mObjcImp frame].size.width, [mObjcImp frame].size.height);
[mObjcImp setFrame:sFrame display:YES animate:NO];
NSLog(@"Frame After setsize: cur Size(%f, %f) new val(%d, %d)", [mObjcImp frame].size.width, [mObjcImp frame].size.height, width, height);
// Tell the application that the window size has change.
onSize(width, height);
[pool release];
}
这是输出:
2011-05-04 08:50:47.313 ClientProMac[2461:a0f] Frame Before setsize: cur Size(50.000000, 72.000000)
2011-05-04 08:50:47.340 ClientProMac[2461:a0f] Frame After setsize: cur Size(350.000000, 472.000000) new val(350, 450)
2011-05-04 08:50:49.148 ClientProMac[2461:7003] Frame Before setsize: cur Size(350.000000, 472.000000)
2011-05-04 08:50:49.160 ClientProMac[2461:7003] Frame After setsize: cur Size(1024.000000, 790.000000) new val(1024, 768)
奇怪的是,如果我然后通过拖动窗口的角来调整窗口大小,完整的标题栏将再次变得可拖动:错误消失了。
以前有人见过这种行为吗?是什么原因造成的以及如何解决它。
谢谢。
I have an instance of NSWindow that I resize programatically (with setFrame). At its creation, it has a default size of 50x50. I then call setFrame to resize it to 350x450: everything is fine, I can drag my window all I want. Then, later in the program, I resize it to 1024x768. At this point, the only part of the title bar that I can drag is the first 350 pixels: the left part. Nothing happens if I drag the right part. As if the title bar still thinks the window has its previous size.
Updated (Added the code that sets the frame):
void CGLWindowMac::_setSize(int width, int height)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSRect sFrame;
sFrame = [NSWindow contentRectForFrameRect:[mObjcImp frame] styleMask:[mObjcImp styleMask]];
sFrame.origin.y += sFrame.size.height;
sFrame.origin.y -= height;
sFrame.size.height = height;
sFrame.size.width = width;
sFrame = [NSWindow frameRectForContentRect:sFrame
styleMask:[mObjcImp styleMask]];
NSLog(@"Frame Before setsize: cur Size(%f, %f) ", [mObjcImp frame].size.width, [mObjcImp frame].size.height);
[mObjcImp setFrame:sFrame display:YES animate:NO];
NSLog(@"Frame After setsize: cur Size(%f, %f) new val(%d, %d)", [mObjcImp frame].size.width, [mObjcImp frame].size.height, width, height);
// Tell the application that the window size has change.
onSize(width, height);
[pool release];
}
And here is the output:
2011-05-04 08:50:47.313 ClientProMac[2461:a0f] Frame Before setsize: cur Size(50.000000, 72.000000)
2011-05-04 08:50:47.340 ClientProMac[2461:a0f] Frame After setsize: cur Size(350.000000, 472.000000) new val(350, 450)
2011-05-04 08:50:49.148 ClientProMac[2461:7003] Frame Before setsize: cur Size(350.000000, 472.000000)
2011-05-04 08:50:49.160 ClientProMac[2461:7003] Frame After setsize: cur Size(1024.000000, 790.000000) new val(1024, 768)
The weird thing is that if I then resize the window by dragging its corner, the full title bar becomes draggable again: the bug disappear.
Anyone saw this behavior before? What causes it and how can I fix it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSWindow(以及 cocoa 中的大多数 GUI 操作)不是线程安全的。确保在主线程上调整窗口大小。
例如:
如果需要向后兼容并且不想使用GCD,则可以使用-[NSObject PerformSelectorOnMainThread:withObject:waitUntilDone:]。
NSWindow (and most GUI operations in cocoa) are not thread safe. Make sure you're resizing the window on the main thread.
For example:
If you need backwards compatibility and don't want to use GCD, you can use -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:].