【发布时间】:2016-01-30 20:52:54
【问题描述】:
我使用 C++ 代码开发 Qt 应用程序,而不与 Objective-C 混合。需要实现原生窗口移动(解决闪烁问题)。 现在我可以移动窗口了
struct NSPoint
{
double x;
double y;
} point;
point.x = (pos() + (mouseEvent->pos() - m_prevMousePos)).x();
point.y = (pos() + (mouseEvent->pos() - m_prevMousePos)).y();
id nsView = reinterpret_cast<id>(winId());
objc_msgSend(objc_msgSend((id)nsView, sel_registerName("window")), sel_registerName("setFrameTopLeftPoint:"), point);
此代码有效,可让我避免闪烁问题。但是 Mac OS X 和 Qt 坐标系是不同的。我想获得左上角的坐标。为此,我尝试使用此代码:
struct NSPoint
{
double x;
double y;
};
struct NSSize
{
double width;
double height;
};
struct NSRect
{
NSPoint origin;
NSSize size;
} rect;
id nsView = reinterpret_cast<id>(winId());
rect = *(NSRect*)objc_msgSend_stret)(objc_msgSend((id)nsView, sel_registerName("window")), sel_registerName("frame"));
但它会导致粉碎。 objc/message.h 中的评论说:
/* Struct-returning Messaging Primitives
*
* Use these functions to call methods that return structs on the stack.
* On some architectures, some structures are returned in registers.
* Consult your local function call ABI documentation for details.
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*/
所以我在这里卡住了。我的最后一个问题是如何使用纯 C 获取 NSWindow::frame 属性?
【问题讨论】:
-
您编写的应用程序是 QT,它是 C++,而不是 C。那么您问 C 有什么原因吗?
-
其实不是C++相关的问题。 objc_msgSend 是纯 C 函数。
-
请指出 C 标准在哪里引入了
reinterpret_cast和模板。我可能错过了。说真的:您显然是从 C++ 中使用该函数的,因此它绝对是 C++ 上下文,而不是 C 上下文。只要您有正确的与 C++ 兼容的声明,以何种语言实现并不重要。
标签: c++ objective-c