全局隐藏光标(从后台应用程序) [英] Globally hiding cursor (from background app)

查看:91
本文介绍了全局隐藏光标(从后台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从状态栏应用中隐藏光标,并且我已经进行了一些研究.似乎是在不久前找到了解决该问题的方法:

全局将鼠标光标隐藏在可可/碳中? http://lists.apple.com/archives/carbon-dev/2006/Jan/msg00555.html

但是引用的代码将无法编译.你们中有人知道如何编译代码(通过导入一些旧的API或某些东西)或另一种实现此目的的方法(某种黑客方法)吗?

(我知道从后台应用程序中隐藏光标通常不是一个好主意,但是我制作的应用程序对此功能非常重要)

这是旧的hack,不再起作用了.

long sysVers = GetSystemVersion();

// This trick doesn't work on 10.1 
if (sysVers >= 0x1020)
{
    void CGSSetConnectionProperty(int, int, int, int);
    int CGSCreateCString(char *);
    int CGSCreateBoolean(BOOL);
    int _CGSDefaultConnection();
    void CGSReleaseObj(int);
    int propertyString, boolVal;

    // Hack to make background cursor setting work
    propertyString = CGSCreateCString("SetsCursorInBackground");
    boolVal = CGSCreateBoolean(TRUE);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, boolVal);
    CGSReleaseObj(propertyString);
    CGSReleaseObj(boolVal);
}

它给了我4个错误:

"_ CGSCreateBoolean",引用自: -[MyClass.o中的[MyClass myMethod]

"_ GetSystemVersion",引用自: -[MyClass.o中的[MyClass myMethod]

"_ CGSCreateCString",引用自: -[MyClass.o中的[MyClass myMethod]

"_ CGSReleaseObj",引用自: -[MyClass.o中的[MyClass myMethod]

解决方案

您需要针对Application Services框架进行链接,以消除链接器错误.

这是黑客的完整示例(已更新为使用Core Foundation):

cat >t.c<<EOF
#include <ApplicationServices/ApplicationServices.h>

int main(void)
{
    void CGSSetConnectionProperty(int, int, CFStringRef, CFBooleanRef);
    int _CGSDefaultConnection();
    CFStringRef propertyString;

    // Hack to make background cursor setting work
    propertyString = CFStringCreateWithCString(NULL, "SetsCursorInBackground", kCFStringEncodingUTF8);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, kCFBooleanTrue);
    CFRelease(propertyString);
    // Hide the cursor and wait
    CGDisplayHideCursor(kCGDirectMainDisplay);
    pause();
    return 0;
}
EOF
gcc -framework ApplicationServices t.c
./a.out

在Mac OS 10.5上,这将隐藏光标,直到程序被中断.但是,执行任何窗口服务器或停靠任务都会显示光标.

I want to hide the cursor from a statusbar app and i've done some research. It seems as though the solution to this problem was found a while ago:

Globally hide mouse cursor in Cocoa/Carbon? or http://lists.apple.com/archives/carbon-dev/2006/Jan/msg00555.html

But the code that is referred to will not compile. Do any of you guys know either how to make the code compile (by importing some old API or something) or another way of achieving this (some kind of hack)?

(I know it is generally a bad idea to hide the cursor from a background app, but i making an app where this functionality is pretty essential)

Edit:

Here's the old hack, that doesn't work anymore.

long sysVers = GetSystemVersion();

// This trick doesn't work on 10.1 
if (sysVers >= 0x1020)
{
    void CGSSetConnectionProperty(int, int, int, int);
    int CGSCreateCString(char *);
    int CGSCreateBoolean(BOOL);
    int _CGSDefaultConnection();
    void CGSReleaseObj(int);
    int propertyString, boolVal;

    // Hack to make background cursor setting work
    propertyString = CGSCreateCString("SetsCursorInBackground");
    boolVal = CGSCreateBoolean(TRUE);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, boolVal);
    CGSReleaseObj(propertyString);
    CGSReleaseObj(boolVal);
}

It gives me 4 errors:

"_CGSCreateBoolean", referenced from: -[MyClass myMethod] in MyClass.o

"_GetSystemVersion", referenced from: -[MyClass myMethod] in MyClass.o

"_CGSCreateCString", referenced from: -[MyClass myMethod] in MyClass.o

"_CGSReleaseObj", referenced from: -[MyClass myMethod] in MyClass.o

解决方案

You need to link against the Application Services framework to get rid of the linker errors.

Here's a complete example of the hack (updated to use Core Foundation):

cat >t.c<<EOF
#include <ApplicationServices/ApplicationServices.h>

int main(void)
{
    void CGSSetConnectionProperty(int, int, CFStringRef, CFBooleanRef);
    int _CGSDefaultConnection();
    CFStringRef propertyString;

    // Hack to make background cursor setting work
    propertyString = CFStringCreateWithCString(NULL, "SetsCursorInBackground", kCFStringEncodingUTF8);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, kCFBooleanTrue);
    CFRelease(propertyString);
    // Hide the cursor and wait
    CGDisplayHideCursor(kCGDirectMainDisplay);
    pause();
    return 0;
}
EOF
gcc -framework ApplicationServices t.c
./a.out

On Mac OS 10.5 this hides the cursor until the program is interrupted. However, performing any window server or dock tasks shows the cursor.

这篇关于全局隐藏光标(从后台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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