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

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


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



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

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

   
    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) //只有一个字母的时候太耗时        //2022-8-18 其实一个字母的时候限制查找单词个数会更好,
    {
        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;
}

 

posted @ 2022-08-18 11:58  XGZ21  阅读(65)  评论(0编辑  收藏  举报