⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 events_eventsourceproxy.cpp

📁 用 Java 代码处理本地对象的事件源代码 设有两部分
💻 CPP
字号:
#include "common.h"
#include "events_EventSourceProxy.h"
#include "Jvm.h"

#include <iostream>
using namespace std;

class JMouseDownListener : public IMouseDownListener  
{
private:
	jobject   m_jobj;
	jmethodID m_mid;

public:
	JMouseDownListener(JNIEnv * env, jobject jobj, jstring methodName, jclass javaClass);
	virtual ~JMouseDownListener();

	void onMouseDown(int hPos, int vPos);
};

JNIEXPORT jint JNICALL Java_events_EventSourceProxy_registerListener
  (JNIEnv * env, jobject jobj, jint handleEventSource, jstring methodName, jclass javaClass)
{
	IEventSource * pEventSource = (IEventSource *) handleEventSource;

	if (NULL == pEventSource)
	{
		return 0;
	}

	JMouseDownListener * pListener = new JMouseDownListener(env, jobj, methodName, javaClass);

	if (NULL == pListener)
	{
		return 0;
	}

	pEventSource->addMouseDownListener(pListener);

	return (jint)pListener;
}

JNIEXPORT jint JNICALL Java_events_EventSourceProxy_unregisterListener
  (JNIEnv *, jobject, jint handleEventSource, jint handleNativeListener)
{
	IEventSource * pEventSource = (IEventSource *) handleEventSource;

	if (NULL == pEventSource)
	{
		return -1;
	}

	JMouseDownListener * pListener = (JMouseDownListener *) handleNativeListener;

	if (NULL == pListener)
	{
		return 0;
	}

	pEventSource->removeMouseDownListener(pListener);

	delete pListener;

	return 0;
}

#include <windows.h>

JMouseDownListener::JMouseDownListener(JNIEnv * env, jobject jobj, jstring methodName, jclass javaClass) :
	m_jobj(NULL), m_mid(NULL)
{
	if ((NULL == methodName) || (NULL == jobj))
	{
		return;
	}

	m_jobj = env->NewGlobalRef(jobj);

	if (NULL == m_jobj)
	{
		return;
	}

	const char * szMethodName = env->GetStringUTFChars(methodName, 0);
	m_mid = env->GetMethodID(javaClass, szMethodName, "(II)V");
	env->ReleaseStringUTFChars(methodName, szMethodName);
}

JMouseDownListener::~JMouseDownListener()
{
	if (NULL == m_jobj)
	{
		return;
	}

	JNIEnv * env = CJvm::GetJNIEnv();
	
	if (NULL == env)
	{
		return;
	}

	env->DeleteGlobalRef(m_jobj);
}

void JMouseDownListener::onMouseDown(int hPos, int vPos)
{
	if ((NULL == m_jobj) || (NULL == m_mid))
	{
		return;
	}

	JNIEnv * env = CJvm::GetJNIEnv();
	
	if (NULL == env)
	{
		return;
	}

	env->CallVoidMethod(m_jobj, m_mid, (jint)hPos, (jint)vPos);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -