📄 dllwnd.cpp
字号:
// DllWnd.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "sip.h"
#include "resource.h"
//===================================================================
#define MYMSG_REGCALLBACK (WM_USER+100)
//Description:
// The message is using for registering the input method callback function
//
//Parameters:
// pImCallback = (IIMCallback *)wParam
// 0 = lParam
//=====================================================================
//====================================================================
//Macro define
#define WINDOW_CLASS TEXT("IMWND_Class")
#define WINDOW_TITLE TEXT("IMWND_Title")
//The taskbar height
#define TASKBAR_HEIGHT_AUTOHIDE 5
#define TASKBAR_HEIGHT 26
#define IMG_IMWND_WIDTH 370
#define IMG_IMWND_HEIGHT 206
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 480
//-----------------------------------------------------
//Write the log function
//#define WRITE_LOG
#ifdef WRITE_LOG
VOID WriteLog(CHAR* str);
#define WRITELOG(str) WriteLog(str)
#else
#define WRITELOG(str)
#endif
//==================================================================
//==================================================================
//Forward Declare
//------------------------------------------------------------------------
//The interface
HWND IMGetWindow();
void IMShowUserOptionsDlg(HWND hWndParent,HINSTANCE hInst);
void IMShowWindow(BOOL bShow);
void IMDestroyWindow();
BOOL IMInitialize(HINSTANCE hInstSip, HWND hWndSip);
//------------------------------------------------------------------------
void OnDestroy(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void OnPaint(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void OnRegCallback(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
void OnLButtonUp(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
//---------------------------------------------------------------------
//The global member
HWND g_hWnd = NULL;
HWND g_hWndSip = NULL;
HMODULE g_hInst = NULL;
HINSTANCE g_hInstSip = NULL;
IIMCallback *g_pIMCallback; //Pointer to the callback function
//=======================================================================
//-------------------------------------------------------------------
//Description:
// The DLLMAIN
//
//------------------------------------------------------------------
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
g_hInst = (HMODULE)hModule;
WRITELOG("DllWnd::DllMain DLL_PROCESS_ATTACH ");
break;
}
}
return TRUE;
}
//-------------------------------------------------------------------
//Description:
// Get the window handle
//
//------------------------------------------------------------------
HWND IMGetWindow()
{
if(g_hWnd != NULL)
{
WRITELOG("DllWnd::GetWindow()");
}
else
{
WRITELOG("DllWnd::GetWindow(), but the g_hWnd is NULL");
}
return g_hWnd;
}
//-------------------------------------------------------------------
//Description:
// Show the user options dialog
//
//------------------------------------------------------------------
void IMShowUserOptionsDlg(HWND hWndParent,HINSTANCE hInst)
{
MessageBox(NULL,TEXT("控制面板可以調出的輸入法設置"),TEXT("設置"),MB_OK);
}
//-------------------------------------------------------------------
//Description:
// Show the window
//
//------------------------------------------------------------------
void IMShowWindow(BOOL bShow)
{
if(bShow == TRUE)
{
::ShowWindow(g_hWnd,SW_SHOW);
}
else
{
::ShowWindow(g_hWnd,SW_HIDE);
}
}
//-------------------------------------------------------------------
//Description:
// Destroy the window
//
//------------------------------------------------------------------
void IMDestroyWindow()
{
::DestroyWindow(g_hWnd);
WRITELOG("DllWnd::DestroyWindow()");
}
//-------------------------------------------------------------------
//Description:
// Initialize the window
//
//------------------------------------------------------------------
BOOL IMInitialize(HINSTANCE hInstSip, HWND hWndSip)
{
g_hInstSip = hInstSip;
g_hWndSip = hWndSip;
UnregisterClass(WINDOW_CLASS,g_hInst);
WNDCLASS wc = {0};
wc.style = CS_VREDRAW | CS_HREDRAW;;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = WINDOW_CLASS;
if(RegisterClass(&wc) == 0)
{
WRITELOG("DllWnd::Failed in RegisterClass()");
return FALSE;
}
else
{
WRITELOG("DllWnd::Succeeded in RegisterClass()");
}
//Set the window area to suit with the image
RECT rcClientWnd = {0};
GetClientRect (hWndSip, &rcClientWnd);
RECT rcSipWnd = {0};
GetWindowRect(hWndSip,&rcSipWnd);
//It may be the frame for the parent window,so we must hold it.
int iWidth = (rcSipWnd.right - rcSipWnd.left ) - (rcClientWnd.right - rcClientWnd.left) + IMG_IMWND_WIDTH;
int iHeight = (rcSipWnd.bottom - rcSipWnd.top) - (rcClientWnd.bottom - rcClientWnd.top) + IMG_IMWND_HEIGHT;
rcSipWnd.left = SCREEN_WIDTH - iWidth;
rcSipWnd.top = SCREEN_HEIGHT - iHeight - TASKBAR_HEIGHT;
SetWindowPos(hWndSip,hWndSip,rcSipWnd.left,rcSipWnd.top,iWidth,iHeight,NULL);
// Create SIP window.
g_hWnd = CreateWindowEx(0,
WINDOW_CLASS,
WINDOW_TITLE,
WS_CHILD | WS_BORDER ,
rcClientWnd.left,
rcClientWnd.top,
IMG_IMWND_WIDTH,//rcWnd.right - rcWnd.left,
IMG_IMWND_HEIGHT,//rcWnd.bottom - rcWnd.top,
hWndSip,
NULL,
g_hInst,
NULL
);
if(IsWindow(g_hWnd)==FALSE)
{
return FALSE;
WRITELOG("DllWnd::Failed in IsWindow()");
}
return TRUE;
}
//-------------------------------------------------------------------
//Description:
// The window process
//
//------------------------------------------------------------------
LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
switch(wMsg)
{
case WM_PAINT:
OnPaint(hWnd,wMsg,wParam,lParam);
return 0;
case MYMSG_REGCALLBACK:
OnRegCallback(hWnd,wMsg,wParam,lParam);
return 0;
case WM_ERASEBKGND:
//Do nothing in order not to flash the window
return 0;
case WM_LBUTTONUP:
OnLButtonUp(hWnd,wMsg,wParam,lParam);
return 0;
}
return DefWindowProc(hWnd,wMsg,wParam,lParam);
}
//-------------------------------------------------------------------
//Description:
// On message WM_PAINT
//
//------------------------------------------------------------------
void OnPaint(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd,&ps);
//Create a DC that matches the device
HBITMAP hBitmap = CreateCompatibleBitmap(hdc,IMG_IMWND_WIDTH,IMG_IMWND_HEIGHT);
HDC hdcMem = CreateCompatibleDC(hdc);
//Select the bitmap into to the compatible device context
HGDIOBJ hOldSel = SelectObject(hdcMem,hBitmap);
//Create a DC that matches the device
HDC hdcBmp = CreateCompatibleDC(hdc);
//Load the bitmap
HANDLE hBmp= LoadImage(g_hInst,MAKEINTRESOURCE(IDB_PANNEL),IMAGE_BITMAP,0,0,0);
//Select the bitmap into to the compatible device context
HGDIOBJ hOldBmpSel = SelectObject(hdcBmp,hBmp);
//Copy the bitmap image to the memory DC.
BitBlt(hdcMem,0,0,IMG_IMWND_WIDTH,IMG_IMWND_HEIGHT,hdcBmp,0,0,SRCCOPY);
//Draw to the screen
BitBlt(hdc,0,0,IMG_IMWND_WIDTH,IMG_IMWND_HEIGHT,hdcMem,0,0,SRCCOPY);
//Restore original bitmap selection and destroy the memory DC
SelectObject(hdcBmp,hOldBmpSel);
SelectObject(hdcMem,hOldSel);
DeleteObject(hBitmap);
DeleteDC(hdcBmp);
DeleteDC(hdcMem);
EndPaint(hWnd,&ps);
}
//-------------------------------------------------------------------
//Description:
// On message MYMSG_REGCALLBACK
//
//------------------------------------------------------------------
void OnRegCallback(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
g_pIMCallback = (IIMCallback *)wParam;
WRITELOG("DllWnd::OnRegCallback()");
}
//-------------------------------------------------------------------
//Description:
// On message WM_LBUTTONUP
//
//------------------------------------------------------------------
void OnLButtonUp(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
if(g_pIMCallback != NULL)
{
g_pIMCallback->SendString(TEXT("輸入法測試程序 "),8);
//g_pIMCallback->SendCharEvents(VK_RIGHT,KeyStateDownFlag,1,&wShiftFlags,&wChar);
}
else
{
WRITELOG("DllWnd::g_pIMCallback is NULL");
}
}
//------------------------------------------------------------------------------
//Description:
// Write string to the log.txt file
//
//-----------------------------------------------------------------------------
VOID WriteLog(CHAR* str)
{
FILE* fp = fopen("\\log.txt", "a+");
if (fp != NULL) {
fwrite(str, strlen(str), 1, fp);
fwrite("\n", strlen("\n"), 1, fp);
fclose(fp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -