c++ - 获取插入点的窗口坐标 - Cocoa

标签 c++ macos cocoa coordinates

在 Cocoa 的文本编辑控件中获取当前插入符位置的窗口坐标的最佳方法是什么?

这是我在其他平台上的做法

  1. Windows - EM_POSFROMCHAR
  2. GTK - 使用 gtk_text_view_get_iter_locationgtk_text_view_buffer_to_window_coords

我想知道如何使用 Cocoa 完成同样的操作。我在 MacOSX 10.6.8 上,我正在使用 C++ 执行此操作。

最佳答案

假设 textView 是一个指向 TextView 的变量,window 是一个指向窗口的变量:

// -firstRectForCharacterRange:actualRange returns a frame rectangle
// containing the insertion point or, in case there's a selection,
// the first line of that selection. The origin of the frame rectangle is
// in screen coordinates
NSRange range = [textView selectedRange];
NSRect screenRect = [textView firstRectForCharacterRange:range actualRange:NULL];

// -convertRectFromScreen: converts from screen coordinates
// to window coordinates
NSRect windowRect = [window convertRectFromScreen:screenRect];

// The origin is the lower left corner of the frame rectangle
// containing the insertion point
NSPoint insertionPoint = windowRect.origin;

关于c++ - 获取插入点的窗口坐标 - Cocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7554472/

相关文章:

linux - macOS 无法删除带有 sunlnk 标志的文件夹

cocoa - NSStackView edgeInsets 被忽略

cocoa - 单击停靠图标时如何打开应用程序窗口?

c++ - 检测数组外访问冲突

c++ - QMenu 中的非交互式项目

node.js - 使用 npm 卸载 babel-cli 失败

cocoa - 如何在产生左键事件时产生右键事件?

c++ - Protocol Buffer 初始化期间崩溃

c++ - ffmpeg 是否支持 QTRLE?

mysql - 有密码root失败,无密码root可以