【问题标题】:Trick NSWindow into showing as active欺骗 NSWindow 使其显示为活动状态
【发布时间】:2011-12-27 23:41:36
【问题描述】:

如何在不激活应用程序本身的情况下欺骗 NSWindow 显示为活动状态?

(原因是我希望在我对窗口进行“截图”时以某种方式暂时欺骗 Cocoa。

通过我尝试过的所有方法都很难做到这一点,因为事情总是不会立即发生,而是“很快”发生。例如,我不能只是激活应用程序,截取屏幕截图,然后重新激活以前激活的应用程序,因为“激活应用程序”是“将来某个时间”发生的操作,即应用程序不会等待在继续之前完成操作。所以这一切都非常棘手。)

【问题讨论】:

  • 明确一点,这个窗口是属于您的应用程序,还是属于其他应用程序?
  • 它属于我的应用程序。

标签: objective-c cocoa


【解决方案1】:

值得一试:

NSDisableScreenUpdates();
...activate app/window, take screenshot, deactivate
NSEnableScreenUpdates();

【讨论】:

  • 我怀疑这可能会失败,因为屏幕截图代码可能是从帧缓冲区中读取的,如果您禁用屏幕更新,此时该帧缓冲区不是当前的。然而,这只是猜测。
  • 我过去曾使用过这些调用,并且通常的屏幕截图方法显然是从窗口的后备存储中读取的。 NSDisableScreenUpdates() 应该只是防止该商店被用来更新屏幕。
【解决方案2】:

如果是可编写脚本的应用程序,您可以简单地使用 AppleScript:

-- Get name of frontmost application at front
set active to name of (info for (path to frontmost application))

-- Activate specific application to take the screenshot from
tell application "Safari" to activate

-- Take screenshot and save it to the desktop
do shell script "screencapture -tjpg " & quoted form of ((POSIX path of (path to desktop)) & "Shot.png") as string

-- Make the previous frontmost application again frontmost
tell application active to activate

在 Cocoa 中使用 NSAppleScript

NSString * source = @"set active to name of (info for (path to frontmost application))\n"
"tell application \"Safari\" to activate\n"
"do shell script \"screencapture -tjpg \" & quoted form of ((POSIX path of (path to desktop)) & \"Shot.png\") as string\n"
"tell application active to activate";

NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:source];
[theScript executeAndReturnError:nil];

【讨论】:

  • 我对此有点怀疑,因为这可能会在截取屏幕截图时导致不同应用程序之间出现一些“闪烁”。我希望以某种方式可以通过实际欺骗窗口显示为活动来做到这一点,即不实际“合法地”激活它。
【解决方案3】:

我怀疑有什么好的方法可以正确地做到这一点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多