码迷,mamicode.com
首页 > 系统相关 > 详细

mac监听Dock激活程序

时间:2015-08-29 15:10:33      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

涉及库添加:
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";
    }
}

mac监听Dock激活程序

标签:

原文地址:http://www.cnblogs.com/sz-leez/p/4769058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!