mac监听Dock激活程序

mac监听Dock激活程序

涉及库添加:

LIBS += -framework CoreFoundation -framework Carbon -lobjc

涉及头文件:

#include <objc/objc.h>
#include <objc/message.h>

监听回调函数:

bool dockClickHandler(id self,SEL _cmd,...)
{
    Q_UNUSED(self)
    Q_UNUSED(_cmd)
    (QMainWindow *)mainWindow->OnHandleClickDock();
    return true;
}

主程序监听:

Class cls = objc_getClass("NSApplication");
objc_object *appInst = objc_msgSend((objc_object*)cls, sel_registerName("sharedApplication"));

if(appInst != NULL) 
{
    objc_object* delegate = objc_msgSend(appInst, sel_registerName("delegate"));
    Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
    SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
    if (class_getInstanceMethod(delClass, shouldHandle)) 
    {
        if (class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:"))
            qDebug() << "Registered dock click handler (replaced original method)";
        else
            qWarning() << "Failed to replace method for dock click handler";
    }
    else 
    {
        if (class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:"))
            qDebug() << "Registered dock click handler";
        else
            qWarning() << "Failed to register dock click handler";
    }
}
posted @ 2015-08-29 13:52  sz_leez  阅读(467)  评论(0编辑  收藏  举报