英语词典,基于文本词典库,TTS发声,RichEdit显示,Win32 SDK编程

用Win32 SDK 写的英语小词典,用朗文英英词库,词库是一个TXT文件, 显示用 RichEdit控件, 发音用TTS

采用Muti Byte编码,没法显示音标,可以即时查找单词,速度0.1s以下,即使顺序查到Z字母也在0.2秒以内。

如果用unicode,可以完美显示音标和中文,但是查找速度非常慢,耗时10倍以上,太慢了。即便是建立索引文件(unicode)也很慢。

显示需要对HTML格式的文本进行解析,然后格式化输出到RichEdit控件,很繁琐,只简单解析了几个常用的格式。

词典库是网上最常见的MDX文件,用GetDIct2.3 转成UTF8格式的TXT文件。

程序直接打开文件,顺序查找到所有相近的单词,放到列表中,同时记录下查找的位置.

显示时,根据列表中选择的单词,根据位置信息读出解释并显示。

发音直接调用WINDOS系统的TTS读选择的单词。

 

 程序MDX.cpp

// MDX.cpp : Defines the entry point for the application.
// 
// 
// Win32 SDK, Richedit, TTS
// 
// 1.unicode文件读取时要指明编码
//
// 2.GetDIct2.3 生成的UTF8的TXT,关键字后面是制表符,0x0009,
//
// 3.词典大了会很慢,用unicode编码,即便创建索引也耗时,创建213587条索引用时2.8秒,读276478条索引用 1秒
// 
// 4.unicode 文件查找比 ascii慢一个数量级(10倍)?????
//
// 5.用ASC码速度快, 对于朗文英英词典这种小词典,索引都不需要建,但是音标和中文不好显示
//
// 2022-8-16 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);


#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 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);
}

//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_MDX, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

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

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

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

    return (int) msg.wParam;
}


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_MDX));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_MDX);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

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;
}

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_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);

    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);


    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);

}



//WCHAR buf[1000001];
TCHAR buf[1000001];

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

    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);

    if (iLength < 0) return 0;

    mbstowcs(ss, (const char *)str, 100);
    


    if (SUCCEEDED(hr))
    {
    
        hr = pVoice->Speak(ss, 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    
    }

    CoUninitialize();
    return 1;
}
int OnTest2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    

    return 1;
}



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;
    char mstr[1000];
    strcpy(mstr, str);
    char cstr[3][100] = {0};

    char* token;
    char* next_token = NULL;
    n = 0;
    token = strtok_s(mstr, " ", &next_token);  //
    while (token != NULL)
    {
        strcpy(cstr[n], token);
        token = strtok_s(NULL, " ", &next_token); //股票名字
        n++;
    }


    c = 0x000000; //default


    if (cstr[1][0] == 's') sscanf(cstr[1], "size=%d", &size);
    if ((cstr[1][0] == 'c') && (cstr[1][6] == '#')) sscanf(cstr[1], "color=#%d", &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 == 2)
    {
        if (cstr[2][0] == 's') sscanf(cstr[2], "size=%d", &size);
        if ((cstr[2][0] == 'c') && (cstr[2][6] == '#')) sscanf(cstr[2], "color=#%d", &c);
        if ((cstr[2][0] == 'c') && ((cstr[1][6] | 0x20) == 'r')) c = 0x8f0000;
        if ((cstr[2][0] == 'c') && ((cstr[1][6] | 0x20) == 'g')) c = 0x008f00;
        if ((cstr[2][0] == 'c') && ((cstr[1][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"));//设置字体
    strcpy(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);
        
        if (len == 2)
        {
            if ((str[0] == 'b') && (str[0] == 'r'))
            {
                RPRINT(_T("\r\n"));


            }
        }
        else if (len == 1)
        {


        }
        else
        {
        }

    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++;
    }
    //ReleaseDC(hWndMain, hdc);

    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);

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

//    mbstowcs(fstr, fstrA, 100);

    FILE* fp;

    //fp = _wfopen(L"e:\\lib\\lw-2.txt", L"r");
    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(wbuf, 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);

    //TCHAR buf[10240];
    int numwritten;
        
    int  fstrlen;

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

    SendMessage(hWndList, LB_RESETCONTENT, 0, 0);  //clear list
    
    PosIndex.clear(); // clear index;

    FILE* fp;

    //fp = _wfopen(_T("e:\\lib\\22.txt"), _T("r"));
    fp = fopen(_T("e:\\lib\\lw-2.txt"), _T("r"));

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

    LARGE_INTEGER t1, t2, tc;
    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);

    //SendMessage(hWndList, WM_SETREDRAW, FALSE, (LPARAM)0);

    int i;
    int j;
    int k;
    int m;
    m = 0;


    fpos_t fpos;
    
    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) != (fstr[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;
        }
    }

    QueryPerformanceCounter(&t2);
    double time = (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart;

    PRINT(_T("\r\nFind = %d, At = %d, Time = %f"),m, i, time);
    fclose(fp);

    return 1;
}

 

posted @ 2022-08-16 16:06  XGZ21  阅读(188)  评论(0编辑  收藏  举报