NX二次开发 鼠标钩子应用

之前开发都是复用别人代码,自己从零开始写一遍巩固加深

初始界面如下:

保存对话框,生成对应的.hpp和.cpp文件,Hook.h和Hook.cpp为钩子文件:

Hook.h代码:

#pragma once
#include <Windows.h>
#include <minwindef.h>

class MouseHookDemo
{
public:
	MouseHookDemo();
	~MouseHookDemo();
	static MouseHookDemo* instanceForHook;

	HHOOK hook;
	LRESULT CALLBACK hookproc(UINT nCode, WPARAM wParam, LPARAM lParam);
	static LRESULT CALLBACK hookproc_proxy(UINT nCode, WPARAM wParam, LPARAM lParam);
	void setPtr(void* ptr);
	void unloadHook();
private:
	void* m_ptr;
};

Hook.cpp代码:

#include "Hook.h"
#include "hookDemo.hpp"

MouseHookDemo* MouseHookDemo::instanceForHook = nullptr;

MouseHookDemo::MouseHookDemo()
{
	instanceForHook = this;
	DWORD id = GetCurrentThreadId();
	hook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseHookDemo::hookproc_proxy, NULL, id);
}

MouseHookDemo::~MouseHookDemo()
{
	unloadHook();
	instanceForHook = nullptr;
}


LRESULT CALLBACK MouseHookDemo::hookproc_proxy(UINT nCode, WPARAM wParam, LPARAM lParam)
{
	return instanceForHook->hookproc(nCode, wParam, lParam);
}

void MouseHookDemo::setPtr(void* ptr)
{
	m_ptr = ptr;
}

void MouseHookDemo::unloadHook()
{
	if (hook != NULL)
	{
		UnhookWindowsHookEx(hook);
		hook = NULL;
	}
	return;
}

LRESULT MouseHookDemo::hookproc(UINT nCode, WPARAM wParam, LPARAM lParam)
{
	hookDemo* hookdemo = (hookDemo*)m_ptr;
	switch (wParam)
	{
	case WM_MOUSEMOVE:
	    //hookdemo->SetLabel("鼠标移动");
		break;
	case WM_LBUTTONDOWN:
		hookdemo->SetLabel("鼠标左键按下了");
		break;
	case WM_LBUTTONUP:
		hookdemo->SetLabel("鼠标左键抬起了");
		break;
	case WM_LBUTTONDBLCLK:
		hookdemo->SetLabel("鼠标左键双击");
		break;
	default:
		break;
	}

	return CallNextHookEx(hook, nCode, wParam, lParam);
}

hookDemo.hpp代码:

#ifndef HOOKDEMO_H_INCLUDED
#define HOOKDEMO_H_INCLUDED

#include "Hook.h"

#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/BlockStyler_Label.hxx>
#include <NXOpen/BlockStyler_Group.hxx>


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

class DllExport hookDemo
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;
    hookDemo();
    ~hookDemo();
    int Show();
    
    void initialize_cb();
    void dialogShown_cb();
    int update_cb(NXOpen::BlockStyler::UIBlock* block);
    PropertyList* GetBlockProperties(const char *blockID);
    
    void SetLabel(string str);

    MouseHookDemo m_mouseHook;
private:
    const char* theDlxFileName;
    NXOpen::BlockStyler::BlockDialog* theDialog;
    NXOpen::BlockStyler::Label* label0;// Block type: Label
    NXOpen::BlockStyler::Group* group0;// Block type: Group
    
};

hookDemo.cpp代码:


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

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(hookDemo::theSession) = NULL;
UI *(hookDemo::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
hookDemo::hookDemo()
{
    try
    {
        // Initialize the NX Open C++ API environment
        hookDemo::theSession = NXOpen::Session::GetSession();
        hookDemo::theUI = UI::GetUI();
        theDlxFileName = "D:\\demo\\hookdemo\\x64\\Release\\hookDemo.dlx";
        theDialog = hookDemo::theUI->CreateDialog(theDlxFileName);
        // Registration of callback functions
        theDialog->AddUpdateHandler(make_callback(this, &hookDemo::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &hookDemo::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &hookDemo::dialogShown_cb));
        
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}

hookDemo::~hookDemo()
{
    if (theDialog != NULL)
    {
        delete theDialog;
        theDialog = NULL;
    }
    
}

extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    hookDemo *thehookDemo = NULL;
    try
    {
        thehookDemo = new hookDemo();
        // The following method shows the dialog immediately
        thehookDemo->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        hookDemo::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    if(thehookDemo != NULL)
    {
        delete thehookDemo;
        thehookDemo = NULL;
    }
}

extern "C" DllExport int ufusr_ask_unload()
{
    //return (int)Session::LibraryUnloadOptionExplicitly;
    return (int)Session::LibraryUnloadOptionImmediately;
    //return (int)Session::LibraryUnloadOptionAtTermination;
}

extern "C" DllExport void ufusr_cleanup(void)
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        hookDemo::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

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

void hookDemo::initialize_cb()
{
    try
    {
        label0 = dynamic_cast<NXOpen::BlockStyler::Label*>(theDialog->TopBlock()->FindBlock("label0"));
        group0 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group0"));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        hookDemo::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

void hookDemo::dialogShown_cb()
{
    try
    {
        m_mouseHook.setPtr(this);
        //label0->GetProperties()->SetString("Value", "111");
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        hookDemo::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

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

PropertyList* hookDemo::GetBlockProperties(const char *blockID)
{
    return theDialog->GetBlockProperties(blockID);
}

void hookDemo::SetLabel(string str)
{
    label0->GetProperties()->SetString("Label", str.c_str());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值