CInputMethod::CInputMethod(long*plDllCnt, HINSTANCE hInst) ...{ //获取输入法窗口的实例 m_pIMWnd = CIMWnd::GetInstance(); m_hInst = hInst; m_plDllCnt = plDllCnt; //增加一次计数 (*m_plDllCnt)++; m_lRef =1; // Set ref count to 1 on create. } CInputMethod::~CInputMethod() ...{ //销毁时减少一次计数 (*m_plDllCnt)--; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to create the windows and image list for the input method (IM). //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::Select(HWND hWndSip) ...{ //初始化输入法界面窗口 if(m_pIMWnd->Initialize(m_hInst,hWndSip) == FALSE) ...{ return E_FAIL; } return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to select the input method (IM) out of the software-based //input panel window and to destroy the IM windows. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::Deselect() ...{ //销毁输入法界面窗口 m_pIMWnd->DestroyWindow(); return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to perform any initialization before the software-based //input panel window is displayed //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::Showing() ...{ //显示输入法界面窗口 m_pIMWnd->ShowWindow(TRUE); return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to perform any saving routines before the software-based //input panel is hidden. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::Hiding() ...{ //隐藏输入法界面窗口 m_pIMWnd->ShowWindow(FALSE); return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to return information about the current input //method (IM) to the operating system. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::GetInfo(IMINFO *pimi) ...{ pimi->cbSize =sizeof (IMINFO); pimi->hImageNarrow =0; pimi->hImageWide =0; pimi->iNarrow =0; pimi->iWide =0; pimi->fdwFlags = SIPF_DOCKED; pimi->rcSipRect.left =0; pimi->rcSipRect.top =0; pimi->rcSipRect.right = SIP_WND_WIDTH; pimi->rcSipRect.bottom = SIP_WND_HEIGHT; return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented for the IM to receive information about the size, //placement, and docked status of the software-based input panel. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::ReceiveSipInfo(SIPINFO *psi) ...{ return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to receive a pointer to an IIMCallback interface. //An input method (IM) uses the IIMCallback interface to send keystrokes to applications //and to change the icons on the Input Panel button. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::RegisterCallback(IIMCallback *pIMCallback) ...{ //发送一条自定义消息传递回调函数的地址给输入法界面窗口 HWND hWnd = m_pIMWnd->GetWindow(); SendMessage(hWnd,MYMSG_REGCALLBACK,(WPARAM)pIMCallback,0); return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to send data from the current //input method (IM) to the current application. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::GetImData(DWORD dwSize, void*pvImData) ...{ return S_OK; } //------------------------------------------------------------------------------------------- //Description: // This method is implemented to respond to an application's request to //set input method (IM)-specific data within the IM. //---------------------------------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::SetImData(DWORD dwSize, void*pvImData) ...{ return S_OK; } //--------------------------------------------------------------------- //Description: // Increment object ref count //---------------------------------------------------------------------- STDMETHODIMP CInputMethod::QueryInterface(REFIID riid, LPVOID *ppv) ...{ //如果当前的接口符合要求,则返回一个接口对象 if (IsEqualIID (riid, IID_IUnknown) || IsEqualIID (riid, IID_IInputMethod) || IsEqualIID (riid, IID_IInputMethod2)) ...{ *ppv = (IInputMethod *)this; AddRef(); return NOERROR; } *ppv = NULL; return (E_NOINTERFACE); } //--------------------------------------------------------------------- //Description: // Increment object ref count //---------------------------------------------------------------------- STDMETHODIMP_(ULONG) CInputMethod::AddRef() ...{ ULONG cnt; cnt = (ULONG)InterlockedIncrement (&m_lRef); return cnt; } //--------------------------------------------------------------------- //Description: // Increment object ref count //---------------------------------------------------------------------- STDMETHODIMP_(ULONG) CInputMethod::Release() ...{ ULONG cnt; cnt = (ULONG)InterlockedDecrement (&m_lRef); if (cnt ==0) ...{ delete this; return0; } return cnt; } //--------------------------------------------------------------------- //Description: // The SIP Control Panel applet is asking for a configuration dialog box to be displayed. //---------------------------------------------------------------------- HRESULT STDMETHODCALLTYPE CInputMethod::UserOptionsDlg(HWND hwndParent) ...{ //显示设置窗口 m_pIMWnd->ShowUserOptionsDlg(hwndParent,m_hInst); return S_OK; } CInputMethod和CClassFactory的实现我们暂时就先说到这里,下一章我们将会讨论输入法界面窗口(CIMWnd)的一个简单的实现.