之前开发都是复用别人代码,自己从零开始写一遍巩固加深
初始界面如下:
保存对话框,生成对应的.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());
}