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

📄 hikerwbui.cpp

📁 自己用Markov模型做的一个整句物笔输入法的原型
💻 CPP
字号:
#include "stdafx.h"
#include "HikerWBUI.h"

static HHOOK hHook;
static HINSTANCE hInstDLL;
static HOOKPROC pHookProc;
typedef BOOL (*LPFINITHOOKPROC)();
static LPFINITHOOKPROC pInitHookProc;
typedef BOOL (*LPFENDHOOKPROC)();
static LPFENDHOOKPROC pEndHookProc;
typedef BOOL (*LPFHOOKONPROC)();
static LPFHOOKONPROC pHookOnProc;
typedef BOOL (*LPFHOOKOFFPROC)();
static LPFHOOKOFFPROC pHookOffProc;
typedef BOOL (*LPFTRANSLATEPROC)(unsigned char*, int cslen,
		unsigned char*, int&, int&, unsigned char*);
static LPFTRANSLATEPROC pTranslateProc;

static HWND hMainWnd;
static HWND hOutWnd;
static HWND hCodeWnd;
static HWND hWordWnd;
static HWND hSentWnd;

static unsigned char codestr[SENTLEN];
static unsigned char wordstr[SENTLEN];
static unsigned char lastcode[CODELEN];
static int cslen;
static int wslen;
static int codenum;

static HANDLE hFile;
static HWND *pMem;

static void Clean()
{
	memset(codestr, 0, SENTLEN);
	memset(wordstr, 0, SENTLEN);
	memset(lastcode, 0, CODELEN);
	cslen = 0;
	wslen = 0;
	if (hCodeWnd != 0)
		SetWindowText(hCodeWnd, "");
	if (hWordWnd != 0)
		SetWindowText(hWordWnd, "");
	if (hSentWnd != 0)
		SetWindowText(hSentWnd, "");
}

static void SetCode(unsigned char* code)
{
	if (hCodeWnd != 0)
		SetWindowText(hCodeWnd, (char*)code);
}

static void SetWord(unsigned char* word)
{
	if (hWordWnd != 0)
		SetWindowText(hWordWnd, (char*)word);
}

static void SetSent(unsigned char* wstream)
{
	if (hSentWnd != 0)
		SetWindowText(hSentWnd, (char*)wstream);
}

static void PostSent()
{
    HWND hWorkWnd = 0;
    DWORD hThred = 0;
	HWND hFocusWnd = 0;

	hWorkWnd = GetForegroundWindow();
	hThred = GetWindowThreadProcessId(hWorkWnd, 0);

    if (!AttachThreadInput(GetCurrentThreadId(), hThred, TRUE))
		MessageBox(0, "Attach Thread Input failed", "", MB_OK);

    hFocusWnd = GetFocus();
	if (!hFocusWnd)
		MessageBox(0, "the hFocusWnd is NULL", "", MB_OK);

    AttachThreadInput(GetCurrentThreadId(), (DWORD)hFocusWnd, FALSE);
    if (hFocusWnd)
	{
		for (int i = 0; i < strlen((char*)wordstr); i++)
			//SendMessage(hFocusWnd, WM_CHAR, wordstr[i], 0);
			SendMessage(hWordWnd, WM_CHAR, wordstr[i], 0);
	}
}

BOOL InitHook(HWND mainWnd, HWND outWnd, HWND codeWnd, HWND wordWnd, HWND sentWnd)
{
	hMainWnd = mainWnd;
	hOutWnd = outWnd;
	hCodeWnd = codeWnd;
	hWordWnd = wordWnd;
	hSentWnd = sentWnd;

	hInstDLL = LoadLibrary((LPCTSTR)"HikerWB.dll");
	pInitHookProc = (LPFINITHOOKPROC)GetProcAddress(hInstDLL, "InitHook");
	pEndHookProc = (LPFENDHOOKPROC)GetProcAddress(hInstDLL, "EndHook");
	pHookOnProc = (LPFHOOKONPROC)GetProcAddress(hInstDLL, "HookOn");
	pHookOffProc = (LPFHOOKOFFPROC)GetProcAddress(hInstDLL, "HookOff");
	pTranslateProc = (LPFTRANSLATEPROC)GetProcAddress(hInstDLL, "Translate");

    hFile = CreateFileMapping((HANDLE)0xFFFFFFFF, 0, PAGE_READWRITE, 0, sizeof(int), "Hook");
    pMem = (HWND*)MapViewOfFile(hFile, FILE_MAP_WRITE, 0, 0, 0);
    (*pMem) = hMainWnd;		//GetSafeHwnd();
	
	pInitHookProc();
	Clean();

	return TRUE;
}

BOOL EndHook()
{
	pEndHookProc();
	UnmapViewOfFile(pMem);
	CloseHandle(hFile);

	FreeLibrary(hInstDLL);

	return TRUE;
}

BOOL HookKeyHandle(WPARAM wParam, LPARAM lParam)
{
	//unsigned short ks;
	unsigned short c = 0;			//!must be initialized with 0
	//ks = GetKeyState(wParam);
	BYTE lpKeyboardState[256];
	GetKeyboardState(lpKeyboardState);
	ToAscii(wParam, (lParam&0x00FF0000)>>16, lpKeyboardState, &c, 0);	//the second parameter seems no use.

	if (isalpha(c) || 32 == c)
	{
		codestr[cslen++] = c;
		memset(lastcode, 0, SENTLEN);
		pTranslateProc(codestr, cslen, wordstr, wslen, codenum, lastcode);
		if (codenum > 0)
		{
			SetCode(lastcode);
			SetSent(wordstr);
		}
	}
	else if (ispunct(c) || 13 == c)
	{
		memset(lastcode, 0, SENTLEN);
		pTranslateProc(codestr, cslen, wordstr, wslen, codenum, lastcode);
		if (codenum > 0)
		{
			SetCode(lastcode);
			SetSent(wordstr);
			PostSent();
		}
		Clean();
	}

	return TRUE;
}

⌨️ 快捷键说明

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