2010-10-07 104 views
5

我想從狀態欄應用程序隱藏光標,我已經做了一些研究。它好像解決這個問題前一段時間發現:全局隱藏光標(從後臺應用程序)

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

但是,這被稱爲代碼將無法編譯。你們中的任何一個人是否知道如何編譯代碼(通過導入一些舊的API或其他)或實現這種方式的其他方式(某種破解)?

(我知道這通常是一個壞主意,隱藏後臺應用程序中的光標,但我做一個應用程序,其中該功能是非常必要的)

編輯:

這裏的老黑客,那不起作用了。

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

「_GetSystemVersion」,從引用[MyClass的myMethod的]: - [MyClass的myMethod的[MyClass的myMethod的]在MyClass.o

- :]在MyClass.o

「_CGSCreateCString」,從參考

「_CGSReleaseObj」,從引用: - 在MyClass.o

+0

請編輯您的問題,以包括您使用的確切代碼和您得到的錯誤。 – 2010-10-07 22:28:35

回答

5

[MyClass的myMethod的]你需要對應用服務框架鏈接擺脫鏈接錯誤的。

這裏的黑客(更新爲使用核心基金會)的一個完整的例子:

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這個隱藏光標直到程序被中斷。但是,執行任何窗口服務器或停靠任務將顯示光標。

+0

非常感謝。精美的作品:D – Sorig 2010-10-15 08:39:06

+0

我通過使用「NSTimer」重複調用'-hideCursor'來修復窗口服務器/ dock任務時出現的光標。即使應用程序不在前臺也可以工作。 – fabian789 2012-01-08 19:00:47

+0

任何人都知道爲什麼這段代碼在使用C++編譯器時會中斷?如上所述,此示例正常工作,但在執行「clang ++ -framework ApplicationServices tc」時會出現以下錯誤: '體系結構x86_64的未定義符號: 「_CGSDefaultConnection()」,引用來自: _main in testCursor-p78PsB.o 「CGSSetConnectionProperty(int,int,__CFString const *,__CFBoolean const *)」,引用來自: _main in testCursor-p78PsB.o ld:symbol(s)not found for architecture x86_64 clang:error:linker command failed with退出代碼1(使用-v來查看調用)' – 2013-11-22 00:35:05