NX二次开发 使用鼠标和键盘钩子

环境:Win7 x64,VS2015,NX12


内容:NX UI 事件响应_使用快捷键Delete删除树控件某一行,键盘按下H键

           NX UI 事件响应_鼠标左键按下和鼠标滑动  


注意

一定要在NX UI 构造函数中添加响应事件

一定要在NX UI 构造函数中添加响应事件

一定要在NX UI 构造函数中添加响应事件


代码

CHookCallback.hpp (核心:事件响应基类

CHookCallback.hpp 需要stdafx.h,targetver.h文件,可通过创建MFC项目得到。

#pragma once

#include "stdafx.h"
#include 
#undef StrToInt

class CHookCallback
{
public:
    CHookCallback();
    virtual ~CHookCallback();
    void RemoveHook(int idHook);
    void RemoveAllHooks();
    BOOL AddHook(int idHook, HINSTANCE hmod = 0, DWORD dwThreadId = GetCurrentThreadId());
    void Reset();   //自定义,用于NX对话框,每次对话框开始时重置静态变量


    static LRESULT CALLBACK hookMSGFILTER(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookJOURNALRECORD(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookJOURNALPLAYBACK(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookKEYBOARD(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookKEYBOARDLL(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookGETMESSAGE(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookCALLWNDPROC(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookCBT(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookSYSMSGFILTER(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookMOUSE(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookDEBUG(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookSHELL(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookFOREGROUNDIDLE(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK hookCALLWNDPROCRET(int code, WPARAM wParam, LPARAM lParam);
    static LRESULT HookProc(int idHook, int code, WPARAM wParam, LPARAM lParam);

    virtual void HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam);

private:
    static CPtrList m_HookID[WH_MAX - WH_MIN + 1]; 
    static CPtrArray m_HookHandle; 
};


class A : public CHookCallback
{
public:
    A();

    virtual void HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam);
};


class B : public CHookCallback
{
public:
    B();

    virtual void HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam);
};

CHookCallback.cpp

#include"CHookCallback.hpp"
using namespace std;

CPtrList CHookCallback::m_HookID[WH_MAX - WH_MIN + 1];
CPtrArray CHookCallback::m_HookHandle;

CHookCallback::CHookCallback()
{
    Reset();
}

CHookCallback::~CHookCallback()
{
    RemoveAllHooks();
}

void CHookCallback::RemoveHook(int idHook)      //remove derived class from a specific hook type
{
    int idHook0 = idHook - WH_MIN;

    if (m_HookID[idHook0].GetCount())
    {
        POSITION pos = m_HookID[idHook0].Find(this);
        if (pos)
        {
            m_HookID[idHook0].RemoveAt(pos);
            if (!m_HookID[idHook0].GetCount())
                UnhookWindowsHookEx((HHOOK)m_HookHandle[idHook0]);
        }
    }
}

void CHookCallback::RemoveAllHooks()      //remove derived class from all hooks type
{
    for (int i = 0; i < WH_MAX - WH_MIN + 1; i++)
    {
        RemoveHook(i + WH_MIN);
    }
}

BOOL CHookCallback::AddHook(int idHook, HINSTANCE hmod, DWORD dwThreadId)      //add derived class to a specific hook type callback
{
    m_HookHandle.SetSize(WH_MAX - WH_MIN + 1);

    HOOKPROC proc = NULL;
    switch (idHook)
    {
    case WH_MSGFILTER:
        proc = hookMSGFILTER;
        break;
    case WH_JOURNALRECORD:
        proc = hookJOURNALRECORD;
        break;
    case WH_JOURNALPLAYBACK:
        proc = hookJOURNALPLAYBACK;
        break;
    case WH_KEYBOARD:
        proc = hookKEYBOARD;
        break;
    case WH_KEYBOARD_LL:
        proc = hookKEYBOARDLL;
        break;
    case WH_GETMESSAGE:
        proc = hookGETMESSAGE;
        break;
    case WH_CALLWNDPROC:
        proc = hookCALLWNDPROC;
        break;
    case WH_CBT:
        proc = hookCBT;
        break;
    case WH_SYSMSGFILTER:
        proc = hookSYSMSGFILTER;
        break;
    case WH_MOUSE:
        proc = hookMOUSE;
        break;
    case WH_DEBUG:
        proc = hookDEBUG;
        break;
    case WH_SHELL:
        proc = hookSHELL;
        break;
    case WH_FOREGROUNDIDLE:
        proc = hookFOREGROUNDIDLE;
        break;
    case WH_CALLWNDPROCRET:
        proc = hookCALLWNDPROCRET;
        break;
    }
    ASSERT(proc);      //idHook not handled
    if (!proc)
        return FALSE;

    int idHook0 = idHook - WH_MIN;

    if (!m_HookHandle[idHook0])
    {
        HHOOK hHook = SetWindowsHookEx(idHook, proc, hmod, dwThreadId);
        if (!hHook)
            return FALSE;

        m_HookHandle[idHook0] = hHook;
    }
    POSITION pos = m_HookID[idHook0].Find(this);
    ASSERT(pos == NULL);      //class already mapped in hook callback
    if (!pos)
        m_HookID[idHook0].AddTail(this);

    return TRUE;
}

void CHookCallback::Reset()
{
    //注意:这里为了适用于NX对话框,每次对话框开启时将静态成员变量重置
    CHookCallback::m_HookID->RemoveAll();
    CHookCallback::m_HookHandle.RemoveAll();
}

LRESULT CALLBACK CHookCallback::hookMSGFILTER(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_MSGFILTER, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookJOURNALRECORD(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_JOURNALRECORD, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookJOURNALPLAYBACK(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_JOURNALPLAYBACK, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookKEYBOARD(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_KEYBOARD, code, wParam, lParam);
}

LRESULT CHookCallback::hookKEYBOARDLL(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_KEYBOARD_LL, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookGETMESSAGE(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_GETMESSAGE, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookCALLWNDPROC(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_CALLWNDPROC, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookCBT(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_CBT, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookSYSMSGFILTER(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_SYSMSGFILTER, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookMOUSE(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_MOUSE, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookDEBUG(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_DEBUG, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookSHELL(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_SHELL, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookFOREGROUNDIDLE(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_FOREGROUNDIDLE, code, wParam, lParam);
}

LRESULT CALLBACK CHookCallback::hookCALLWNDPROCRET(int code, WPARAM wParam, LPARAM lParam)
{
    return HookProc(WH_CALLWNDPROCRET, code, wParam, lParam);
}

LRESULT CHookCallback::HookProc(int idHook, int code, WPARAM wParam, LPARAM lParam)
{
    int idHook0 = idHook - WH_MIN;

    for (POSITION pos = m_HookID[idHook0].GetHeadPosition(); pos != NULL;)
    {
        CHookCallback *pCallback = (CHookCallback *)m_HookID[idHook0].GetNext(pos);
        pCallback->HookCallback(idHook, code, wParam, lParam);
    }

    return CallNextHookEx((HHOOK)m_HookHandle[idHook0], code, wParam, lParam);
}

void CHookCallback::HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam)
{

}

A::A()
{
    AddHook(WH_CALLWNDPROCRET);
}

void A::HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam)
{
    ASSERT(idHook == WH_CALLWNDPROCRET);

    if (code == HC_ACTION)
    {
        CWPRETSTRUCT* msg = (CWPRETSTRUCT*)(lParam);
        TRACE("class A %p::HookCallback msg=%08lX WH_CALLWNDPROCRET\n", this, msg->message);
    }
}



B::B()
{
    AddHook(WH_GETMESSAGE);
    AddHook(WH_CALLWNDPROCRET);
}

void B::HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam)
{
    if (idHook == WH_CALLWNDPROCRET)
    {
        if (code == HC_ACTION)
        {
            CWPRETSTRUCT* msg = (CWPRETSTRUCT*)(lParam);
            TRACE("class B %p::HookCallback msg=%08lX WH_CALLWNDPROCRET\n", this, msg->message);
        }
    }
    else if (idHook == WH_GETMESSAGE)
    {
        if (code == HC_ACTION)
        {
            MSG* msg = (MSG*)(lParam);
            TRACE("class B %p::HookCallback msg=%08lX WH_GETMESSAGE \n", this, msg->message);
        }
    }
    else
        ASSERT(FALSE);
}

创建一个UI界面,如下:

NX二次开发 使用鼠标和键盘钩子_第1张图片

设置的代码如下:

TestKeyboardCallbackForNxTreeBlk.hpp (NX UI)

#pragma once

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "CHookCallback.hpp"
#undef CreateDialog

using namespace std;
using namespace NXOpen;
using namespace NXOpen::BlockStyler;


class DllExport TestKeyboardCallbackForNxTreeBlk :public CHookCallback
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;
    static void Show_TestKeyboardForNxTreeBlk();
    TestKeyboardCallbackForNxTreeBlk();
    ~TestKeyboardCallbackForNxTreeBlk();
    int Show();

    //----------------------- BlockStyler Callback Prototypes ---------------------
    // The following member function prototypes define the callbacks 
    // specified in your BlockStyler dialog.  The empty implementation
    // of these prototypes is provided in the hotKey.cpp file. 
    // You are REQUIRED to write the implementation for these functions.
    //------------------------------------------------------------------------------
    void initialize_cb();
    void dialogShown_cb();
    int ok_cb();
    int update_cb(NXOpen::BlockStyler::UIBlock* block);
    void focusNotify_cb(NXOpen::BlockStyler::UIBlock* block, bool focus);
    void keyboardFocusNotify_cb(NXOpen::BlockStyler::UIBlock* block, bool focus);
    PropertyList* GetBlockProperties(const char *blockID);

    //user define func
    Tree* GetTreeBlk();
    StringBlock* GetStringBlk();

    virtual void HookCallback(int idHook, int code, WPARAM wParam, LPARAM lParam);
    static void UpdateTreeDataAfterDeleteOneRow(TestKeyboardCallbackForNxTreeBlk &obj);
    static void UpdateStringBlock(TestKeyboardCallbackForNxTreeBlk &obj,int mouseType);

private:
    const char* theDlxFileName;
    NXOpen::BlockStyler::BlockDialog* theDialog;
    NXOpen::BlockStyler::SelectObject* selectObject;// Block type: Selection
    NXOpen::BlockStyler::StringBlock* stringInput;// Block type: String
    NXOpen::BlockStyler::Tree* treeDemo;// Block type: Tree Control
};

 

TestKeyboardCallbackForNxTreeBlk.cpp

#include "TestKeyboardCallbackForNxTreeBlk.hpp"
#include 
#include 
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

namespace
{
    vector GetAllNodes(Tree* tree)
    {
        Node *nd = tree->RootNode();

        vector res;
        while (nd != NULL)
        {
            res.push_back(nd);

            nd = nd->NextSiblingNode();
        }

        return res;
    }

    void ClearTree(Tree* tree)
    {
        vector nodes = GetAllNodes(tree);
        for (int i = 0; i < nodes.size(); i++)
        {
            tree->DeleteNode(nodes[i]);
        }
    }

    string IntToStr(int value)
    {
        std::ostringstream oss;
        oss << value;
        return oss.str();
    }

    void InitTreeBlk(Tree* &tree)
    {
        if (tree != NULL)
        {
            tree->InsertColumn(0, "", 100);
        }
    }

    void InsertNodes(Tree* &tree, int nodeCount)
    {
        if (tree == NULL)
        {
            return;
        }

        std::string standName = "row";
        for (int i = 0; i < nodeCount; ++i)
        {
            Node *node = tree->CreateNode(standName + IntToStr(i + 1));
            tree->InsertNode(node, NULL, NULL, Tree::NodeInsertOptionLast);
        }
    }

    bool GetFirstSelectedNodeLabel(Tree* tree, std::string &label)
    {
        if (tree == NULL)
        {
            return false;
        }

        bool res = false;
        int columnCount = tree->NumberOfColumns();
        vector selectedNodes = GetAllNodes(tree);
        for (int i = 0; i < selectedNodes.size(); ++i)
        {
            if (selectedNodes[i]->IsSelected())
            {
                if (columnCount >= 1)
                {
                    label = selectedNodes[i]->GetColumnDisplayText(0).GetLocaleText();
                    res = true;
                }

                break;
            }
        }

        return res;
    }

    bool GetFirstSelectedNode(Tree* tree, Node* &node)
    {
        if (tree == NULL)
        {
            return false;
        }

        bool res = false;
        int columnCount = tree->NumberOfColumns();
        vector selectedNodes = GetAllNodes(tree);
        for (int i = 0; i < selectedNodes.size(); ++i)
        {
            if (selectedNodes[i]->IsSelected())
            {
                node = selectedNodes[i];
                res = true;
                break;
            }
        }

        return res;
    }

    bool DeleteNode(Tree* tree, Node* node)
    {
        if (tree == NULL || node == NULL)
        {
            return false;
        }

        bool res = false;
        vector selectedNodes = GetAllNodes(tree);
        for (int i = 0; i < selectedNodes.size(); ++i)
        {
            if (selectedNodes[i] == node)
            {
                tree->DeleteNode(node);
                res = true;
                break;
            }
        }

        return res;
    }
}

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(TestKeyboardCallbackForNxTreeBlk::theSession) = NULL;
UI *(TestKeyboardCallbackForNxTreeBlk::theUI) = NULL;

void TestKeyboardCallbackForNxTreeBlk::Show_TestKeyboardForNxTreeBlk()
{
    TestKeyboardCallbackForNxTreeBlk *thehotKey = NULL;
    try
    {
        thehotKey = new TestKeyboardCallbackForNxTreeBlk();
        // The following method shows the dialog immediately
        thehotKey->Show();
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    if (thehotKey != NULL)
    {
        delete thehotKey;
        thehotKey = NULL;
    }

}
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
TestKeyboardCallbackForNxTreeBlk::TestKeyboardCallbackForNxTreeBlk()
{
    try
    {
        // Initialize the NX Open C++ API environment
        TestKeyboardCallbackForNxTreeBlk::theSession = NXOpen::Session::GetSession();
        TestKeyboardCallbackForNxTreeBlk::theUI = UI::GetUI();
        theDlxFileName = "TestKeyboardCallbackForNxTreeBlk.dlx";
        theDialog = TestKeyboardCallbackForNxTreeBlk::theUI->CreateDialog(theDlxFileName);
        // Registration of callback functions
        theDialog->AddOkHandler(make_callback(this, &TestKeyboardCallbackForNxTreeBlk::ok_cb));
        theDialog->AddUpdateHandler(make_callback(this, &TestKeyboardCallbackForNxTreeBlk::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &TestKeyboardCallbackForNxTreeBlk::initialize_cb));
        theDialog->AddFocusNotifyHandler(make_callback(this, &TestKeyboardCallbackForNxTreeBlk::focusNotify_cb));
        theDialog->AddKeyboardFocusNotifyHandler(make_callback(this, &TestKeyboardCallbackForNxTreeBlk::keyboardFocusNotify_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &TestKeyboardCallbackForNxTreeBlk::dialogShown_cb));

        AddHook(WH_KEYBOARD); //must add
        AddHook(WH_MOUSE);
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
TestKeyboardCallbackForNxTreeBlk::~TestKeyboardCallbackForNxTreeBlk()
{
    if (theDialog != NULL)
    {
        delete theDialog;
        theDialog = NULL;
    }
}

int TestKeyboardCallbackForNxTreeBlk::Show()
{
    try
    {
        theDialog->Show();
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void TestKeyboardCallbackForNxTreeBlk::initialize_cb()
{
    try
    {
        selectObject = dynamic_cast(theDialog->TopBlock()->FindBlock("selectObject"));
        stringInput = dynamic_cast(theDialog->TopBlock()->FindBlock("stringInput"));
        treeDemo = dynamic_cast(theDialog->TopBlock()->FindBlock("treeDemo"));
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set 
//here will take precedence and dialog will be launched showing that value. 
//------------------------------------------------------------------------------
void TestKeyboardCallbackForNxTreeBlk::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----
        InitTreeBlk(treeDemo);
        InsertNodes(treeDemo, 10);
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int TestKeyboardCallbackForNxTreeBlk::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        if (block == selectObject)
        {
            //---------Enter your code here-----------
        }
        else if (block == stringInput)
        {
            //---------Enter your code here-----------
        }
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
int TestKeyboardCallbackForNxTreeBlk::ok_cb()
{
    int errorCode = 0;
    try
    {
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}

//------------------------------------------------------------------------------
//Callback Name: focusNotify_cb
//This callback is executed when any block (except the ones which receive keyboard entry such as Integer block) receives focus.
//This callback is invoked when the block is in focus. This callback is used only for blocks that do not allow keyboard entries: 
//for example, Selection blocks.
//当该块成为焦点时——UI上被点击,将调用此回调。 此回调仅用于不允许键盘输入的块:例如,选择块。
//------------------------------------------------------------------------------
void TestKeyboardCallbackForNxTreeBlk::focusNotify_cb(NXOpen::BlockStyler::UIBlock* block, bool focus)
{
    try
    {
        if (block == selectObject)
        {
            //do something
        }
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: keyboardFocusNotify_cb
//This callback is executed when block which can receive keyboard entry, receives the focus.
//当块获得焦点时——UI上被点击,将调用此回调。 此回调仅用于允许键盘输入的块:例如,Integer或Double块。
//------------------------------------------------------------------------------
void TestKeyboardCallbackForNxTreeBlk::keyboardFocusNotify_cb(NXOpen::BlockStyler::UIBlock* block, bool focus)
{
    try
    {
        //---- Enter your callback code here -----
        if (block == stringInput)  //只能指向可被键盘输入的 BLOCK
        {

        }
    }
    catch (exception& ex)
    {
        //---- Enter your exception handling code here -----
        TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Function Name: GetBlockProperties
//Description: Returns the propertylist of the specified BlockID
//------------------------------------------------------------------------------
PropertyList* TestKeyboardCallbackForNxTreeBlk::GetBlockProperties(const char *blockID)
{
    return theDialog->GetBlockProperties(blockID);
}

Tree * TestKeyboardCallbackForNxTreeBlk::GetTreeBlk()
{
    return treeDemo;
}

StringBlock * TestKeyboardCallbackForNxTreeBlk::GetStringBlk()
{
    return stringInput;
}

void TestKeyboardCallbackForNxTreeBlk::HookCallback(int idHook, int nCode, WPARAM wParam, LPARAM lParam)
{
    if (idHook == WH_KEYBOARD)
    {
        if (nCode == HC_ACTION)
        {
            if (wParam == VK_DELETE)
            {
                UpdateTreeDataAfterDeleteOneRow(*this);
            }
            else if (wParam == 'H')
            {
                TestKeyboardCallbackForNxTreeBlk::theUI->NXMessageBox()->Show("键盘事件", NXOpen::NXMessageBox::DialogTypeError, "按下H");
            }
        }
    }
    else if (idHook == WH_MOUSE)
    {
        if (nCode == HC_ACTION)
        {            
            if (wParam == WM_MOUSEMOVE) //鼠标滚动
            {
                UpdateStringBlock(*this, 1);
            }
            else if (wParam == WM_LBUTTONDOWN) //左键按下
            {
                UpdateStringBlock(*this, 2);
            }
        }
    }

}

void TestKeyboardCallbackForNxTreeBlk::UpdateTreeDataAfterDeleteOneRow(TestKeyboardCallbackForNxTreeBlk &obj)
{
    Tree* treeBlk = obj.GetTreeBlk();
    if (treeBlk != NULL)
    {
        Node* selectedNode = NULL;
        if (GetFirstSelectedNode(treeBlk, selectedNode))
        {
            DeleteNode(treeBlk, selectedNode);
        }
    }
}

std::string GetTimeString()
{
    time_t timer;
    struct tm* tm_info;
    time(&timer);
    tm_info = localtime(&timer);

    char buffer[133];
    //strftime(buffer, 133, "%Y:%m:%d-%H:%M:%S", tm_info);
    strftime(buffer, 133, "%H:%M:%S", tm_info);

    return string(buffer);
}

void TestKeyboardCallbackForNxTreeBlk::UpdateStringBlock(TestKeyboardCallbackForNxTreeBlk & obj,int mouseType)
{
    StringBlock* stringBlk = obj.GetStringBlk();
    if (stringBlk != NULL)
    {
        if (mouseType == 1)
        {
            std::string time = GetTimeString();
            stringBlk->SetValue(time);
        }
        else if (mouseType == 2)
        {
            stringBlk->SetValue("鼠标左键按下");
        }

    }
}

 

你可能感兴趣的:(NX)