Change mouse cursor over inactive NSWindow

前端 未结 3 1099
醉梦人生
醉梦人生 2020-12-16 06:02

I have subclassed NSWindow and I have a MYWindow class implementing the following method:

-(void)resetCursorRects {
    NSImage *image = [NSImage imageNamed:         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 06:29

    I struggled with this problem for a long period of time and I think there is only one way to change mouse cursor over inactive application (over non-foreground window). This is hacky and magic way.

    Before calling pretty standard:

    [[NSCursor pointingHandCursor] push];
    

    You have to call:

    void CGSSetConnectionProperty(int, int, int, int);
    int CGSCreateCString(char *);
    int CGSCreateBoolean(BOOL);
    int _CGSDefaultConnection();
    void CGSReleaseObj(int);
    int propertyString, boolVal;
    
    propertyString = CGSCreateCString("SetsCursorInBackground");
    boolVal = CGSCreateBoolean(TRUE);
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, boolVal);
    CGSReleaseObj(propertyString);
    CGSReleaseObj(boolVal);
    

    Or if you are using Swift:

    Put this in your YourApp-Bridging-Header.h:

    typedef int CGSConnectionID;
    CGError CGSSetConnectionProperty(CGSConnectionID cid, CGSConnectionID targetCID, CFStringRef key, CFTypeRef value);
    int _CGSDefaultConnection();
    

    And then call:

    let propertyString = CFStringCreateWithCString(kCFAllocatorDefault, "SetsCursorInBackground", 0)
    CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, kCFBooleanTrue)
    

提交回复
热议问题