📄 winmain.cpp
字号:
else
{
MessageBox(hDlg, _T("DictLib open call failed\n"), _T("Error"), MB_OK);
}
SysFreeString(bstrFileName);
return (INT_PTR)TRUE;
case WM_COMMAND:
switch LOWORD(wParam)
{
case IDOK:
//取编辑框文本
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_GETTEXT, MaxWordLength, (LPARAM)tSourceLang);
SendMessage(GetDlgItem(hDlg, IDC_EDest), WM_GETTEXT, MaxWordLength, (LPARAM)tDestLang);
//调用组件,增加单词
bstrSource = SysAllocString(T2OLE(tSourceLang));
bstrDest = SysAllocString(T2OLE(tDestLang));
if (FAILED(pDictionary->AddWord(bstrSource, bstrDest)))
{
MessageBox(hDlg, _T("Add word into library failed\n"), _T("Error"), MB_OK);
}
SysFreeString(bstrSource);
SysFreeString(bstrDest);
return (INT_PTR)TRUE;
case IDC_Modify:
//取编辑框文本
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_GETTEXT, MaxWordLength, (LPARAM)tSourceLang);
SendMessage(GetDlgItem(hDlg, IDC_EDest), WM_GETTEXT, MaxWordLength, (LPARAM)tDestLang);
//调用组件,修改单词
bstrSource = SysAllocString(T2OLE(tSourceLang));
bstrDest = SysAllocString(T2OLE(tDestLang));
if (FAILED(pDictionary->ModifyWord(bstrSource, bstrDest)))
{
MessageBox(hDlg, _T("Modify word into library failed\n"), _T("Error"), MB_OK);
}
SysFreeString(bstrSource);
SysFreeString(bstrDest);
return (INT_PTR)TRUE;
case IDC_Delete:
//取编辑框文本
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_GETTEXT, MaxWordLength, (LPARAM)tSourceLang);
//调用组件,删除单词
bstrSource = SysAllocString(T2OLE(tSourceLang));
if (FAILED(pDictionary->DeleteWord(bstrSource)))
{
MessageBox(hDlg, _T("Delete word into library failed\n"), _T("Error"), MB_OK);
}
SysFreeString(bstrSource);
return (INT_PTR)TRUE;
case IDCANCEL:
//清除编辑框
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_SETTEXT, 0, NULL);
SendMessage(GetDlgItem(hDlg, IDC_EDest), WM_SETTEXT, 0, NULL);
if (FAILED(pDictionary->SyncDictLib()))
{
MessageBox(hDlg, _T("Sync words into library failed\n"), _T("Error"), MB_OK);
}
return (INT_PTR)TRUE;
case IDC_Query:
//取编辑框文本
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_GETTEXT, MaxWordLength, (LPARAM)tSourceLang);
//调用组件,查询单词
bstrSource = SysAllocString(T2OLE(tSourceLang));
if (FAILED(pDictionary->QueryWord(bstrSource, &bstrDest)))
{
MessageBox(hDlg, _T("Query word into library failed\n"), _T("Error"), MB_OK);
}
_tcscpy(tDestLang, OLE2T(bstrDest));
SysFreeString(bstrSource);
SysFreeString(bstrDest);
//设置编辑框地址
SendMessage(GetDlgItem(hDlg, IDC_EDest), WM_SETTEXT, 0, (LPARAM)tDestLang);
return (INT_PTR)TRUE;
case IDC_Quit:
EndDialog(hDlg, LOWORD(wParam));
//卸载COM库
CoUninitialize();
return (INT_PTR)TRUE;
}
break;
case WM_CLOSE:
pDictionary->Release();
EndDialog(hDlg, message);
//卸载COM库
CoUninitialize();
return TRUE;
}
return (INT_PTR)FALSE;
}
INT_PTR GetDictQuery(LPVOID pDict)
{
// IUnknown *pUnknown;
// GUID dictionaryCLSID;
HRESULT hResult;
// hResult = ::CLSIDFromProgID(L"ALTCOMM.Dictionary", &dictionaryCLSID);
// if (FAILED(hResult))
// {
// printf("Can't find the dictionary CLSID!\n");
// return -2;
// }
//hResult = CoCreateInstance(CLSID_Dictionary, NULL,
// CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)(&pUnknown));
//if (FAILED(hResult))
//{
// printf("Create object failed!\n");
// return -2;
//}
//hResult = pUnknown->QueryInterface(IID_IDictionary, &pDict);
//if (FAILED(hResult)) {
// pUnknown->Release();
// printf("QueryInterface IDictionary failed!\n");
// return -3;
//}
hResult = CoCreateInstance(CLSID_Dictionary, NULL,
CLSCTX_INPROC_SERVER, IID_IDictionary, (void **)(&pDict));
if (FAILED(hResult))
{
printf("Create object failed!\n");
return -2;
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK MutiDailog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
TCHAR tSourceLang[MaxWordLength + 2];
TCHAR tDestLang[MaxWordLength + 2];
BSTR bstrSource, bstrDest;
static IDictionary *pDictionary = NULL;
static HANDLE hwndServer = NULL;
DWORD dwThread;
switch (message)
{
case WM_INITDIALOG:
RECT rectChild, rectParent;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
// trying to center the About dialog
if (GetWindowRect(hDlg, &rectChild))
{
GetClientRect(GetParent(hDlg), &rectParent);
DlgWidth = rectChild.right - rectChild.left;
DlgHeight = rectChild.bottom - rectChild.top ;
NewPosX = (rectParent.right - rectParent.left - DlgWidth) / 2;
NewPosY = (rectParent.bottom - rectParent.top - DlgHeight) / 2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
EnableWindow(GetDlgItem( hDlg, IDC_BAddWord ), false );
EnableWindow(GetDlgItem( hDlg, IDC_BQueryWord ), false );
return (INT_PTR)TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
//创建服务线程,在此线程中主要增加消息循环使用
DlgParams Para;
Para.hDlg = hDlg;
if (hwndServer != NULL)
{
return (INT_PTR)TRUE;
}
hwndServer = CreateThread(NULL, 0, ServPorc, &Para, NULL, &dwThread);
//在主线程中,对COM库进行初始化
if (CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) != S_OK) {
printf("Initialize COM library failed!\n");
CoUninitialize();
return (INT_PTR)FALSE;
}
DEBUGMSG(1, (_T("Start threads succeeded, Main threadid is %x\n"), GetCurrentThreadId()));
EnableWindow(GetDlgItem( hDlg, IDOK ), true );
return (INT_PTR)TRUE;
case IDC_BAddWord:
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_GETTEXT, MaxWordLength, (LPARAM)tSourceLang);
SendMessage(GetDlgItem(hDlg, IDC_EDest), WM_GETTEXT, MaxWordLength, (LPARAM)tDestLang);
//调用组件,增加单词
bstrSource = SysAllocString(T2OLE(tSourceLang));
bstrDest = SysAllocString(T2OLE(tDestLang));
if (FAILED(pDictionary->AddWord(bstrSource, bstrDest)))
{
MessageBox(hDlg, _T("Add word into library failed\n"), _T("Error"), MB_OK);
}
SysFreeString(bstrSource);
SysFreeString(bstrDest);
case IDC_BQueryWord:
//取编辑框文本
SendMessage(GetDlgItem(hDlg, IDC_ESource), WM_GETTEXT, MaxWordLength, (LPARAM)tSourceLang);
//调用组件,查询单词
bstrSource = SysAllocString(T2OLE(tSourceLang));
if (FAILED(pDictionary->QueryWord(bstrSource, &bstrDest)))
{
MessageBox(hDlg, _T("Query word into library failed\n"), _T("Error"), MB_OK);
}
_tcscpy(tDestLang, OLE2T(bstrDest));
SysFreeString(bstrSource);
SysFreeString(bstrDest);
//设置编辑框地址
SendMessage(GetDlgItem(hDlg, IDC_EDest), WM_SETTEXT, 0, (LPARAM)tDestLang);
return (INT_PTR)TRUE;
case IDCANCEL:
//停止服务线程,并关闭对话框
if (hwndServer != NULL)
{
hwndServer = NULL;
CloseHandle(hwndServer);
CoUninitialize();
}
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
case WM_USER+2:
IStream * pIStream = (IStream *)lParam;
HANDLE hServThread = (HANDLE *)wParam;
HANDLE hMainThread = GetCurrentThread();
CoGetInterfaceAndReleaseStream(pIStream, IID_IDictionary, (void **)&pDictionary);
SendMessage(GetDlgItem(hDlg, IDC_EServInfo), WM_SETTEXT, 0, (LPARAM)&hServThread);
SendMessage(GetDlgItem(hDlg, IDC_EMainInfo), WM_SETTEXT, 0, (LPARAM)&hMainThread);
EnableWindow(GetDlgItem( hDlg, IDC_BAddWord ), true );
EnableWindow(GetDlgItem( hDlg, IDC_BQueryWord ), true );
break;
}
return (INT_PTR)FALSE;
}
DWORD WINAPI ServPorc(LPVOID lpv)
{
BSTR bstrFileName;
IDictionary *pDict = NULL;
HRESULT hr;
pDlgParam pParam;
HANDLE hCurThread;
IStream * pIStream = NULL;
bool bContinue = true;
pParam = (pDlgParam )lpv;
hCurThread = GetCurrentThread();
//if (SUCCEEDED(CoInitializeEx(NULL, 0))) {
if ((hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))= S_OK) {
hr = CoCreateInstance(CLSID_Dictionary, NULL,
CLSCTX_INPROC_SERVER, IID_IDictionary, (void **)(&pDict));
if (SUCCEEDED(hr))
{
bstrFileName = SysAllocString(L"DictLib.txt");
if (SUCCEEDED(pDict->InitialDict(bstrFileName)))
{
DEBUGMSG(1, (_T("DictLib open call succeeded\n")));
}
SysFreeString(bstrFileName);
}
else
{
bContinue = false;
DEBUGMSG(1, (_T("CoCreateInstance call failed\n")));
}
}
else
{
bContinue = false;
DEBUGMSG(1, (_T("CoInitializeEx call failed\n")));
}
DEBUGMSG(1, (_T("Start threads succeeded, Child threadid is %x\n"), GetCurrentThreadId()));
if(bContinue)
{
hr = CoMarshalInterThreadInterfaceInStream(IID_IDictionary, pDict, &pIStream);
if (hr != S_OK)
{
DEBUGMSG(1, (_T("MarshalInterThreadInterfaceInStream failed\n")));
}
else
{
SendMessage(pParam->hDlg, WM_USER + 2, (WPARAM)&hCurThread, (LPARAM)pIStream);
}
bContinue = false;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -