首页 > 编程语言 >英语词典2 ,用二分法预查范围,基于文本词典库,unicode,TTS发声,RichEdit显示,Win32 SDK编程

英语词典2 ,用二分法预查范围,基于文本词典库,unicode,TTS发声,RichEdit显示,Win32 SDK编程

时间:2022-08-18 12:14:45浏览次数:62  
标签:Win32 return RichEdit LPARAM int HWND 二分法 IDM hWnd

// MDX2.cpp : Defines the entry point for the application.
//
// 1. 改成unicode以支持音标和中文
// 2. 改用词库比较大的 21世纪英汉汉英双向词典
// 3. 在之前的基础上增加了二分法搜索起始段,在段里面再查找关键字。
// 4. 二分法比较字符串大小的前提是词典库按顺序排列,比较大小前把字符串都转换为小写字母。
// 5. 只有一个字母的时候查到的数据比较多,会比较耗时,可以跳过,但是汉字只有一个字不能跳过
// 6. 不同字典的字体格式不同,太麻烦了
// 7. 基本上查找都在0.1s以下
//
// 2022-8-18 SZ XGZ

 

 

 

 

 MDX2.cpp

// MDX2.cpp : Defines the entry point for the application.
//
// 
// 1. 改成unicode以支持音标和中文
// 2. 改用词库比较大的 21世纪英汉汉英双向词典
// 3. 在之前的基础上增加了二分法搜索起始段,在段里面再查找关键字。
// 4. 二分法比较字符串大小的前提是词典库按顺序排列,比较大小前把字符串都转换为小写字母。
// 5. 只有一个字母的时候查到的数据比较多,会比较耗时,可以跳过,但是汉字只有一个字不能跳过
// 6. 不同字典的字体格式不同,太麻烦了
// 7. 基本上查找都在0.1s以下
// 
//  2022-8-18 SZ XGZ


#include "stdafx.h"
#include "resource.h"

#include <stdio.h>
#include <sapi.h>
#include <Commctrl.h>
#include <richedit.h>
#include <string>
#include <vector>


#pragma comment(lib,"comctl32.lib")
using namespace std;

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
//xgz==========
#define IDC_RICHEDIT       1010 
#define IDC_EDIT           1011 
#define IDC_LIST           1012
#define IDC_EDITINPUT      1013
#define IDC_MAINTOOLSBAR   1014


#define IDM_TEST_TEST1      1015
#define IDM_TEST_TEST2      1016
#define IDM_TEST_TEST3      1017
#define IDM_TEST_TEST4      1018
#define IDM_TEST_TEST5      1019
#define IDM_TEST_TEST6      1020

HWND hWndMain;
HWND hWndMainToolbar;

HWND hWndStcrol;

HWND hWndRichEdit;
HWND hWndEdit;
HWND hWndEditInput;
HWND hWndList;

//vector <wstring> NameIndex;
vector <string> NameIndex;
vector <int> PosIndex;




int OnCreate(HWND, UINT, WPARAM, LPARAM);
int OnSize(HWND, UINT, WPARAM, LPARAM);
int OnPaint(HWND, UINT, WPARAM, LPARAM);
int OnVScroll(HWND, UINT, WPARAM, LPARAM);
int OnTimer(HWND, UINT, WPARAM, LPARAM);

int OnTest1(HWND, UINT, WPARAM, LPARAM);
int OnTest2(HWND, UINT, WPARAM, LPARAM);
int OnTest3(HWND, UINT, WPARAM, LPARAM);
int OnTest4(HWND, UINT, WPARAM, LPARAM);
int OnTest5(HWND, UINT, WPARAM, LPARAM);
int OnTest6(HWND, UINT, WPARAM, LPARAM);

int OnListSelChange(HWND, UINT, WPARAM, LPARAM);
int OnEditInputUpdate(HWND, UINT, WPARAM, LPARAM);
int OnEditInputChange(HWND, UINT, WPARAM, LPARAM);

//打印输出
//int PRINT(TCHAR* fmt, ...)
TCHAR buffer[1000000];
int PRINT(const TCHAR* fmt, ...)
{
    va_list argptr;
    int cnt;

    int iEditTextLength;
    HWND hWnd = hWndEdit;

    if (NULL == hWnd) return 0;

    va_start(argptr, fmt);
    cnt = _vstprintf(buffer, fmt, argptr);  // A or W but ISO C
    //cnt = vswprintf(buffer, fmt, argptr);  // only W
    //cnt = wvsprintf(buffer, fmt, argptr);  // not %f

    va_end(argptr);

    iEditTextLength = GetWindowTextLength(hWnd);
    if (iEditTextLength + cnt > 30000)       // edit text max length is 30000
    {
        SendMessage(hWnd, EM_SETSEL, 0, 10000);
        SendMessage(hWnd, WM_CLEAR, 0, 0);
        iEditTextLength = iEditTextLength - 10000;
    }
    SendMessage(hWnd, EM_SETSEL, iEditTextLength, iEditTextLength);
    SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)buffer);

    return(cnt);
}


int RPRINT(const TCHAR* fmt, ...)
{


    va_list argptr;
    int cnt;

    int iEditTextLength;
    HWND hWnd = hWndRichEdit;

    if (NULL == hWnd) return 0;

    va_start(argptr, fmt);
    cnt = _vstprintf(buffer, fmt, argptr);  // A or W but ISO C
    //cnt = vswprintf(buffer, fmt, argptr);  // only W
    //cnt = wvsprintf(buffer, fmt, argptr);  // not %f

    va_end(argptr);

    iEditTextLength = GetWindowTextLength(hWnd);
    if (iEditTextLength + cnt > 30000)       // edit text max length is 30000
    {
        SendMessage(hWnd, EM_SETSEL, 0, 10000);
        SendMessage(hWnd, WM_CLEAR, 0, 0);
        iEditTextLength = iEditTextLength - 10000;
    }
    SendMessage(hWnd, EM_SETSEL, iEditTextLength, iEditTextLength);
    SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)buffer);

    return(cnt);
}

//----------------------------

//sql执行回调函数
int sqlcallback(void* NotUsed, int argc, char** argv, char** azColName)
{
    int i;
    for (i = 0; i < argc; i++) {
        PRINT(_T("%s = %s\n"), azColName[i], argv[i] ? argv[i] : "NULL");
    }
    PRINT(_T("\r\n"));
    return 0;
}


//xgz-----------

HWND m_hWnd = NULL;
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_MDX2, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MDX2));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MDX2));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_MDX2);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   hWndMain = hWnd;

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_CREATE:
        OnCreate(hWnd, message, wParam, lParam);
        break;
    case WM_SIZE:
        OnSize(hWnd, message, wParam, lParam);
        break;
    case WM_PAINT:
        OnPaint(hWnd, message, wParam, lParam);
        break;

    case WM_COMMAND:
        wmId = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDC_EDITINPUT:

            switch (wmEvent)
            {
            case EN_UPDATE:
                OnEditInputUpdate(hWnd, message, wParam, lParam);
                break;
            case EN_CHANGE:
                //OnEditInputChange(hWnd, message, wParam, lParam);
                break;
            }
            break;
        case IDC_LIST:
            switch (wmEvent)
            {
            case LBN_SELCHANGE:
                OnListSelChange(hWnd, message, wParam, lParam);
                break;
            }
            break;
        case IDM_TEST_TEST1:
            OnTest1(hWnd, message, wParam, lParam);
            break;
        case IDM_TEST_TEST2:
            OnTest2(hWnd, message, wParam, lParam);
            break;
        case IDM_TEST_TEST3:
            OnTest3(hWnd, message, wParam, lParam);
            break;
        case IDM_TEST_TEST4:
            OnTest4(hWnd, message, wParam, lParam);
            break;
        case IDM_TEST_TEST5:
            OnTest5(hWnd, message, wParam, lParam);
            break;
        case IDM_TEST_TEST6:
            OnTest6(hWnd, message, wParam, lParam);
            break;
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}


HWND CreateMainToolbar(HWND hWnd)
{
    // image,COMMAND, STATUS, STYLE, 
    TBBUTTON tbButton[] = {
        { STD_FILENEW,  IDM_TEST_TEST1, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FILEOPEN, IDM_TEST_TEST2, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FILESAVE, IDM_TEST_TEST3, TBSTATE_ENABLED , TBSTYLE_BUTTON , 0L, 0},
        { STD_HELP, IDM_TEST_TEST4, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FIND, IDM_TEST_TEST5, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_UNDO, IDM_TEST_TEST6, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, -1},  //分隔符
        { STD_REDOW, IDM_TEST_TEST1, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},

        { STD_COPY, IDM_TEST_TEST2, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_PASTE, IDM_TEST_TEST3, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_CUT, IDM_TEST_TEST4, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_DELETE, IDM_TEST_TEST5, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FILESAVE, IDM_TEST_TEST6, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, -1},//分隔符
        { STD_PROPERTIES, IDM_TEST_TEST1, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FIND, IDM_TEST_TEST2, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_REPLACE, IDM_TEST_TEST3, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_PRINT, IDM_TEST_TEST4, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FILESAVE, IDM_TEST_TEST5, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_FILESAVE, IDM_TEST_TEST6, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
        { STD_PRINTPRE, IDM_EXIT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0}
    };


    HINSTANCE hLib = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE); //It can be other DLL,here same as m_hInst

    HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, _T(""),
        //WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,  //| WS_BORDER|TBSTYLE_FLAT,
        WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | CCS_NORESIZE | CCS_ADJUSTABLE | CCS_NODIVIDER | CCS_NOPARENTALIGN, //for rebar 
        0, 0, 0, 0,
        hWnd, (HMENU)IDC_MAINTOOLSBAR, hInst, NULL);

    if (!hWndToolbar)
        return NULL;

    SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    SendMessage(hWndToolbar, TB_SETEXTENDEDSTYLE, 0, (LPARAM)(DWORD)(TBSTYLE_EX_DRAWDDARROWS));


    TBADDBITMAP tbBitmap1;
    tbBitmap1.hInst = HINST_COMMCTRL;
    tbBitmap1.nID = IDB_STD_SMALL_COLOR;
    SendMessage(hWndToolbar, TB_ADDBITMAP, 0, (LPARAM)&tbBitmap1);


    //ICO
//     HIMAGELIST hImageList;
//     hImageList = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK,               //size 16x16
//           sizeof(tbButton)/sizeof(tbButton[0]), sizeof(tbButton)/sizeof(tbButton[0])); //double space
// 
//     for(unsigned int i = 0 ; i <= sizeof(tbButton)/sizeof(tbButton[0]) ; i++)
//     {
//         HICON  hIcon = LoadIcon(hLib, MAKEINTRESOURCE(IDI_ICON1 + i));
//         ImageList_AddIcon(hImageList, hIcon);
//         DestroyIcon(hIcon);
//     }
//     SendMessage(hWndToolbar, TB_SETIMAGELIST, 0, (LPARAM)hImageList);

//     HIMAGELIST hImageList=ImageList_Create(16,16,ILC_COLOR16,1,10);
//     HBITMAP hBitMap=LoadBitmap(m_hInst,MAKEINTRESOURCE(IDB_BITMAP1));
//     ImageList_Add(hImageList,hBitMap,NULL);
//     DeleteObject(hBitMap);
//     SendMessage(hWndToolbar, TB_SETIMAGELIST, 0, (LPARAM)hImageList);


    SendMessage(hWndToolbar, TB_SETBITMAPSIZE, 0, (LPARAM)MAKELONG(16, 16)); //size 16x16
    SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
    SendMessage(hWndToolbar, TB_ADDBUTTONS, 15, (LONG)&tbButton);   //add 6 button

    return hWndToolbar;
}




int OnCreate(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    InitCommonControls();

    HINSTANCE hRich = LoadLibrary(_T("Riched20.dll"));
    hWndRichEdit = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, NULL,// _T("RichEdit20W"), NULL,
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_READONLY | ES_MULTILINE,
        0, 0, 500, 500,
        hWnd, (HMENU)IDC_RICHEDIT, hInst, NULL);

    CHARFORMAT cf;
    ZeroMemory(&cf, sizeof(CHARFORMAT));
    cf.cbSize = sizeof(CHARFORMAT);
    
    cf.dwMask |= CFM_COLOR;
    cf.crTextColor = RGB(255, 0, 0); //设置颜色
    cf.dwMask |= CFM_SIZE;
    cf.yHeight = 400;//设置高度
    cf.dwMask |= CFM_FACE;

    wcscpy(cf.szFaceName, _T("宋体"));//设置字体
    //strcpy(cf.szFaceName, _T("宋体"));//设置字体
    SendMessage(hWndRichEdit, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);

    

    hWndEditInput = CreateWindow(_T("edit"), NULL,
        WS_CHILD | WS_BORDER | WS_VISIBLE | WS_TABSTOP | ES_WANTRETURN,//| ES_MULTILINE | WS_VSCROLL,
        0, 0, 0, 0, hWnd, (HMENU)IDC_EDITINPUT, hInst, NULL);

    hWndEdit = CreateWindow(_T("edit"), NULL,
        WS_CHILD | WS_BORDER | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | ES_READONLY,
        0, 0, 0, 0, hWnd, (HMENU)IDC_EDIT, hInst, NULL);

    hWndList = CreateWindow(_T("listbox"), NULL,
        WS_CHILD | WS_BORDER | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LBS_NOTIFY,// | LBS_SORT,
        0, 0, 0, 0, hWnd, (HMENU)IDC_LIST, hInst, NULL);



    hWndMainToolbar = CreateMainToolbar(hWnd);


    return 1;
}

//留一个状态条的位置
int OnSize(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int cxClient, cyClient;

    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);


    MoveWindow(hWndEditInput, 5, 0, 200, 30, TRUE);
    MoveWindow(hWndList, 5, 25, 200, cyClient - 30, TRUE);

    MoveWindow(hWndMainToolbar, 210, 0, 400, 25, TRUE);
    MoveWindow(hWndRichEdit, 210, 25, cxClient - 215, cyClient - 160, TRUE);
    MoveWindow(hWndEdit, 210, cyClient - 130, cxClient - 215, 120, TRUE);

    SendMessage(hWndList, CB_SETHORIZONTALEXTENT, (WPARAM)10, (LPARAM)0);



    return DefWindowProc(hWnd, message, wParam, lParam);

}


int OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR* str = _T("This is a test! 这是一个测试 0123456 +-*/  oo E5beit ");
    int x = 210;
    int y = 50;


    PAINTSTRUCT ps;
    HDC hdc;
    RECT rt;


    hdc = BeginPaint(hWnd, &ps);
    GetClientRect(hWnd, &rt);




    EndPaint(hWnd, &ps);

    return 1;
}


TCHAR buf[1000001];


int strFormat(TCHAR* str, int len)  //转小写
{
    int i=0;
    int n=0;

    
    for (i = 0; i < len; i++)
    {
        if ((str[i] >= 'A') && (str[i] <= 'Z'))
        {
            str[i] = str[i] | 0x20;
            n++;
        }

    }
    return n;
}

int FindPos(FILE* fp, TCHAR* strFind, int& f1, int& f2, int lev, TCHAR* buf)
{
    int i;
    TCHAR cfind;
    TCHAR cdict;

    TCHAR cfind0;


    int Len = wcslen(strFind);
    int res;

    TCHAR strFind2[100];

    int fm;



    fm = (f1 + f2) / 2;
    fseek(fp, fm, 0);

    for (i = 0; i < 20; i++)  //最多20次 1M
    {
        fgetws(buf, 1000000, fp);
        if (fgetws(buf, 1000000, fp) != NULL)
        {
            
            wmemcpy(strFind2, buf, Len);
            strFind2[Len] = 0;
            strFormat(strFind2, Len);
            res = wcscmp(strFind, strFind2);
            if (res>0)
            {
                f1 = fm;
                fm = (f1 + f2) / 2;
                fseek(fp, fm, 0);
                //PRINT(_T("\r\n %c>%c find[%d-%d-%d]"), strFind[lev], buf[lev], f1, fm, f2);
            }
            else if (res<0)
            {
                f2 = fm;
                fm = (f1 + f2) / 2;
                fseek(fp, fm, 0);
                //PRINT(_T("\r\n %c<%c find[%d-%d-%d]"), strFind[lev], buf[lev], f1, fm, f2);
            }
            else  
            {
                
                break;
            }
        }
    }


    return fm;
}

int OnTest1(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    

    ISpVoice* pVoice = NULL;

    if (FAILED(CoInitialize(NULL)))
        return -1;

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL,
        CLSCTX_ALL, IID_ISpVoice, (void**)&pVoice);

    WCHAR ss[200] = L"hello";

    int index, iLength;
    index = SendMessage(hWndList, LB_GETCURSEL, 0, 0);
    iLength = SendMessage(hWndList, LB_GETTEXTLEN, 0, 0);



    TCHAR str[100];
    iLength = SendMessage(hWndList, LB_GETTEXT, index, (LPARAM)str);

    PRINT(_T("\r\n Speak = %s"),str);

    if (iLength < 0) return 0;


    if (SUCCEEDED(hr))
    {

        hr = pVoice->Speak(str, 0, NULL);
        pVoice->Release();
        pVoice = NULL;

    }

    CoUninitialize();

    return 1;
}


int FindUpdate(TCHAR *str, int Len)
{

    FILE* fp;    //文件
    fpos_t fpos;

    LARGE_INTEGER t1, t2, tc;  //计时
    double time;


    int   strFindLen;
    TCHAR cfind, cdict;

    TCHAR* strFind;  //

    strFind = str;
    strFindLen = wcslen(strFind);
    strFormat(strFind, strFindLen);


    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);   //计时

    fp = _wfopen(L"e:\\lib\\21.txt", L"r, ccs=UTF-8");


    if (NULL == fp)
    {
        PRINT(_T("\r\n<FAIL> fopen failed!"));
        return 0;
    }



    int i = 0;
    int j;
    int k;

    int f1;
    int f2;
    int fm;

    f1 = ftell(fp);
    j = fseek(fp, 0, SEEK_END);
    f2 = ftell(fp);
    fm = (f1 + f2) / 2;
    fseek(fp, fm, 0);

    for (i = 0; i < strFindLen; i++)
    {
        fm = FindPos(fp, strFind, f1, f2, i, buf);
        if (fm > 0)
        {
            //PRINT(_T("\r\n %c=%c%c find[%d-%d-%d]"), strFind[i], buf[i], buf[i + 1], f1, fm, f2);
        }
        else
        {
            //PRINT(_T("\r\n Failed"));
        }
    }

    //PRINT(_T("\r\n  fm(%d): %s"), i, buf);

    
    fseek(fp, f1, 0);  //起始段
    fgetws(buf, 1000000, fp);  //去掉可能不完整的一行


    int m = 0;

    int fstrlen;
    fstrlen = strFindLen;


    for (i = 0; i < 1000000; i++)
    {
        fgetpos(fp, &fpos);
        if (fgetws(buf, 1000000, fp) != NULL)
            //if (fgets(buf, 1000000, fp) != NULL)
        {
            if (buf[0] == '-')
            {
                continue;  //xgz 跳过奇奇怪怪的东西
            }
            for (j = 0; j < fstrlen; j++)
            {
                if ((buf[j] | 0x20) != (strFind[j] | 0x20))  //xgz 都转小写比较
                    break;
            }
            if (j == fstrlen)
            {
                m++;  //xgz 
                buf[100] = 0;

                for (k = 0; k < 100; k++)
                {
                    if ((buf[k] == '\t') && (buf[k + 1] == '<'))
                    {
                        buf[k] = 0;

                        break;
                    }
                }

                SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)buf);
                PosIndex.push_back(fpos);

            }
            else if (j < fstrlen)
            {
                if (m > 0)   //xgz 找到后再不同,就不用继续找了
                {
                    break;
                }
            }
        }
        else
        {
            break;
        }
    }


    //PRINT(_T("\r\n  find: %s"), buf);

    fclose(fp);

    QueryPerformanceCounter(&t2);
    time = (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart;
    PRINT(_T("\r\nFinish =%d time = %f"), i, time);



    return 1;
}


int OnTest2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    

    return 1;
}


int RunCommand(char* str, int len);
int OnTest3(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    

    return 1;
}


int OnTest4(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    


    return 1;
}



int OnTest5(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    

    return 1;
}


int OnTest6(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    

    return 1;
}


int RunCommand(TCHAR * str, int len)
{
    int size = 0;
    int c;

    int n;
    TCHAR mstr[1000];
    wcscpy(mstr, str);
    TCHAR cstr[3][100] = { 0 };

    if (len == 2)
    {
        if ((str[0] == 'b') && (str[0] == 'r'))
        {
            RPRINT(_T("\r\n"));

            
        }
        return 2;
    }
    else if (len < 6)
    {
        return 0;

    }
    else
    {
    }


    TCHAR* token;
    TCHAR* next_token = NULL;
    n = 0;
    token = wcstok_s(mstr, L" ", &next_token);  //
    while (token != NULL)
    {
        wcscpy(cstr[n], token);
        token = wcstok_s(NULL, L" ", &next_token); //股票名字
        n++;
    }


    c = 0x000000; //default


    if (cstr[1][0] == 's') swscanf(cstr[1], L"size=%d", &size);
    if ((cstr[1][0] == 'c') && (cstr[1][7] == '#'))
    {
        token = wcstok_s(cstr[1], L"#", &next_token);
        token = wcstok_s(NULL, L"#", &next_token);
        if (token != NULL)
        {
            token[6] = 0;
            swscanf(token, L"%x", &c);
        }
        
    }

    if ((cstr[1][0] == 'c') && ((cstr[1][6] | 0x20) == 'r')) c = 0x8f0000;
    if ((cstr[1][0] == 'c') && ((cstr[1][6] | 0x20) == 'g')) c = 0x008f00;
    if ((cstr[1][0] == 'c') && ((cstr[1][6] | 0x20) == 'b')) c = 0x00008f;

    if (n == 3)
    {
        if (cstr[2][0] == 's') swscanf(cstr[2], L"size=%d", &size);
        if ((cstr[2][0] == 'c') && (cstr[2][7] == '#'))
        {
            token = wcstok_s(cstr[2], L"#", &next_token);
            token = wcstok_s(NULL, L"#", &next_token);
            if (token != NULL)
            {
                token[6] = 0;
                swscanf(token, L"%x", &c);
            }

        }
        if ((cstr[2][0] == 'c') && ((cstr[2][6] | 0x20) == 'r')) c = 0x8f0000;
        if ((cstr[2][0] == 'c') && ((cstr[2][6] | 0x20) == 'g')) c = 0x008f00;
        if ((cstr[2][0] == 'c') && ((cstr[2][6] | 0x20) == 'b')) c = 0x00008f;

    }


    CHARFORMAT cf;
    ZeroMemory(&cf, sizeof(CHARFORMAT));
    cf.cbSize = sizeof(CHARFORMAT);
    cf.dwMask |= CFM_COLOR;
    cf.crTextColor = RGB(0, 0, 0); //设置颜色
    cf.dwMask |= CFM_SIZE;
    cf.yHeight = 200;//设置高度
    cf.dwMask |= CFM_FACE;

    //wcscpy(cf.szFaceName, _T("Kingsoft Phonetic Plain"));//设置字体
    wcscpy(cf.szFaceName, _T("Tahoma"));//设置字体
    

    if (size != 0)
    {
        cf.dwMask |= CFM_SIZE;
        cf.yHeight = size * 50;
    }
    else
    {
        cf.dwMask &= ~CFM_SIZE;
    }

    int r, g, b;


    r = c >> 16 & 0xff;
    g = c >> 8 & 0xff;
    b = c & 0xff;

    cf.dwMask |= CFM_COLOR;
    cf.crTextColor = RGB(r, g, b); //设置颜色


    SendMessage(hWndRichEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

    

    return 0;
}

int ShowWTXT(TCHAR* str)
{
    TCHAR str1[100];
    TCHAR* p;
    //    p = buf;//
    p = str;

    int mode = 0;

    TCHAR command[100];
    int commandindex;
    TCHAR text[1000];
    int textindex;
    commandindex = 0;
    textindex = 0;


    SendMessage(hWndRichEdit, EM_SETSEL, 0, -1);
    SendMessage(hWndRichEdit, EM_REPLACESEL, 0, (LPARAM)0);

    while (*p != NULL)
    {
        switch (*p)
        {
        case '&':

            if ((*(p + 1) == 'n') && (*(p + 2) == 'b') && (*(p + 3) == 's') && (*(p + 4) == 'p'))
            {
                p = p + 5;
                *p = ' ';
            }
            else if ((*(p + 1) == 'q') && (*(p + 2) == 'u') && (*(p + 3) == 'o') && (*(p + 4) == 't'))
            {
                p = p + 5;
                *p = '"';
            }
            else if ((*(p + 1) == 'l') && (*(p + 2) == 't'))
            {
                p = p + 3;
                *p = '<';
            }
            else if ((*(p + 1) == 'g') && (*(p + 2) == 't'))
            {
                p = p + 3;
                *p = '>';
            }
            break;
        case '<':
            mode = 1; //command
            if (textindex > 0)
            {
                text[textindex] = 0;
                RPRINT(_T("\r\n  %s"), text);
                //TextOutW(hdc, wx, wy, text, textindex);

                textindex = 0;
            }

            break;
        case '>':
            mode = 0; //text

            RunCommand(command, commandindex);
            commandindex = 0;
            break;
        default:
            if (mode == 1)
            {
                command[commandindex] = *p;
                commandindex++;
            }
            else
            {
                text[textindex] = *p;
                textindex++;

            }

            break;

        }
        p++;
    }

    return 1;
}




int OnListSelChange(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    int index, iLength;
    index = SendMessage(hWndList, LB_GETCURSEL, 0, 0);
    iLength = SendMessage(hWndList, LB_GETTEXTLEN, 0, 0);

    fpos_t fpos;
    fpos = PosIndex.at(index);
    PRINT(_T("\r\n Index=%d, Pos=%d"), index, fpos);


    
    TCHAR fstr[100];
    iLength = SendMessage(hWndList, LB_GETTEXT, index, (LPARAM)fstr);
    PRINT(_T("\r\n %s "), fstr);

    

    FILE* fp;

    fp = _wfopen(L"e:\\lib\\21.txt", L"r, ccs=UTF-8");
    //fp = fopen("e:\\lib\\lw-2.txt", "r");

    if (NULL == fp)
    {
        PRINT(_T("\r\n<FAIL> fopen failed!"));
    }

    int  fstrlen;

    fstrlen = iLength;


    int i;
    int j;
    fsetpos(fp, &fpos);



    if (fgetws(buf, 1000000, fp) != NULL)
        //if (fgets(buf, 1000000, fp) != NULL)
    {

        ShowWTXT(buf);

    }

    fclose(fp);
    return 1;
}


int OnEditInputChange(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PRINT(_T("\r\n OnEditInputChange "));

    return 1;
}



int OnEditInputUpdate(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    TCHAR fstr[200];

    GetWindowText(hWndEditInput, fstr, 200);
    //PRINT(_T("\r\n Search = %s "), fstr);

    
    int numwritten;

    int  fstrlen;

    //fstrlen = strlen(fstr);
    fstrlen = wcslen(fstr);
    if (fstrlen == 0) return 0;

    if (fstrlen == 1) //只有一个字母的时候太耗时
    {
        if(fstr[0]>'A' && fstr[0] < 'z')
        return 0;  
    }
        

    SendMessage(hWndList, LB_RESETCONTENT, 0, 0);  //clear list

    PosIndex.clear(); // clear index;


    FindUpdate(fstr, fstrlen);

    

    return 1;
}

 

标签:Win32,return,RichEdit,LPARAM,int,HWND,二分法,IDM,hWnd
From: https://www.cnblogs.com/xgz21/p/16598223.html

相关文章

  • MFC实现Txt03之CRichEditView实现文本拖拽功能
    02实现了基本的Txt操作功能,现在还想加一个拖拽功能,即把一个Txt文本拖拽进来,然后自动打开该文本。(参考Txt拖拽,不理解的话就试一下,就是把另一个Txt拖到一个已打开的Txt中) ......
  • MFC实现Txt02之当然是CRichEditView了
    需要用到文本编辑功能,当然是用现成的啦,自己写字符输入那还挺麻烦的。。。比如前面就没解决的问题,后续还有什么只能一行一行写,不能随意跳动行啥的,一系列问题,还有有现成的,不......
  • 英语词典,基于文本词典库,TTS发声,RichEdit显示,Win32 SDK编程
    用Win32SDK写的英语小词典,用朗文英英词库,词库是一个TXT文件,显示用RichEdit控件,发音用TTS采用MutiByte编码,没法显示音标,可以即时查找单词,速度0.1s以下,即使顺序查到......
  • Win32 - 窗口
    Win32-窗口目录Win32-窗口前言流程图创建项目VSMinGWWin32API字符串Unicode和ANSI函数TCHARWinMain:Win32Application入口点函数Console下创建窗口窗口类注册窗口......