繁体   English   中英

在Win32 API 中实现单词替换功能有没有更好的方法?

[英]Is there a better way to implement a word replacement feature in the Win32 API?

我正在创建一个记事本应用程序,我已经完成了基本的工作(打开文件、保存文件、写入文件)。 我现在正在尝试添加一个单词替换功能,就像 MS WORD 中的那样,它可以找到输入的单词并将所有出现的单词替换为新单词。 下面是我的代码。

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define FILE_MENU_EXIT 2
#define FILE_MENU_BUILD 3
#define FILe_MENU_OPEN 4
#define FILE_MENU_SAVE 5
#define SEARCH_MENU_REPLACE 6

HMENU hMenu;
HMENU hDevMenu;
HWND hEdit,hCurrentPath;

HWND hOldWord,hNewWord;

int messagebox_id;

void replacerDialog(HINSTANCE);
void StillUnderConst();
void AddMenu(HWND);
void AddControls(HWND);
void AddDialogButton(HWND);



LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst , LPSTR args, int ncmdshow)
{
    WNDCLASSW wc = {0};

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hInstance = hInst;
    wc.lpszClassName = L"mywindowclass";
    wc.lpfnWndProc = WindowProcedure;
    if(!RegisterClassW(&wc))
        return -1;

    replacerDialog(hInst);
    CreateWindowW(L"mywindowclass",L"Note Book",WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU,50,50,1000,650,NULL,NULL,NULL,NULL);
    MSG msg = {0};

    while(GetMessage(&msg,NULL,NULL,NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

void display_file(char* path)
{
    FILE *file;
    //this function helps us open and access the file passed in
    file = fopen(path,"rb");
    //this function helps as move to certain parts of the file thats why its a file pointer
    fseek(file,0,SEEK_END);
    //the ftell() function retuurn an integer type whic is stored in a var so that the file can later be read
    //it is a needed to have the file size to be able to read fromt the file thats why we went for its size
    int _size = ftell(file);
    //this takes us back to the beginning of he file
    rewind(file);
    //this is thevar in which the files data will be kept
    char *data = new char[_size+1];
    //the fread function helps us to read from of specified file
    //fread function requires us to know the file size to be able to read from it
    fread(data,_size,1,file);

    data[_size] ='\0';

    SetWindowText(hEdit,data);
    fclose(file);

}

void open_file(HWND hWnd)
{
    OPENFILENAME ofn;
    ZeroMemory(&ofn,sizeof(OPENFILENAME));

    char file_name[100];
    //the memory allocated to the the ofn struct
    ofn.lStructSize = (sizeof(OPENFILENAME));
    //the owner of the dialog
    ofn.hwndOwner = hWnd;
    //the path of the file being open
    ofn.lpstrFile = file_name;
    ofn.lpstrFile[0] = '\0';
    //the max length being allocated to the path
    ofn.nMaxFile = 100;
    ofn.lpstrFilter = "All file\0*.*\0Text files\0*.txt\0C++ files\0*.cpp\0";
    ofn.nFilterIndex = 1;

    //creates an openfile dialog based on the kind of structure passed in as an argument
    GetOpenFileName(&ofn);
    SetWindowText(hCurrentPath,ofn.lpstrFile);

  //  MessageBox(NULL,ofn.lpstrFile,NULL,MB_OK);

    display_file(ofn.lpstrFile);
}

void write_file(char* path)
{
    FILE *file;
    //this function helps us open and access the file passed in
    file = fopen(path,"w");


    int _size = GetWindowTextLength(hEdit);
    char *data = new char[_size+1];
    GetWindowText(hEdit,data,_size+1);

    fwrite(data,_size+1,1,file);
    fclose(file);
}
void save_file(HWND hWnd)
{
    OPENFILENAME ofn;
    ZeroMemory(&ofn,sizeof(OPENFILENAME));

    char file_name[100];
    //the memory allocated to the the ofn struct
    ofn.lStructSize = (sizeof(OPENFILENAME));
    //the owner of the dialog
    ofn.hwndOwner = hWnd;
    //the path of the file being open
    ofn.lpstrFile = file_name;
    ofn.lpstrFile[0] = '\0';
    //the max length being allocated to the path
    ofn.nMaxFile = 100;
    ofn.lpstrFilter = "All file\0*.*\0Text files\0*.txt\0C++ files\0*.cpp\0";
    ofn.nFilterIndex = 1;

    //creates an openfile dialog based on the kind of structure passed in as an argument
    GetSaveFileName(&ofn);

    write_file(ofn.lpstrFile);

}

LRESULT CALLBACK DialogProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    switch(msg)
    {
    case WM_CREATE:
        AddDialogButton(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd,msg,wp,lp);
    }
    return 0;
}
void replacerDialog(HINSTANCE hInst)
{
    WNDCLASSW dialog = {0};

    dialog.hbrBackground = (HBRUSH)COLOR_WINDOW;
    dialog.hCursor = LoadCursor(NULL,IDC_ARROW);


    dialog.hInstance = hInst;
    dialog.lpszClassName = L"mywindowdialog";
    dialog.lpfnWndProc = DialogProcedure;
    RegisterClassW(&dialog);

}

void displayDialog(HWND hWnd)
{
    CreateWindowW(L"mywindowdialog",L"Replacer",WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU,50,50,300,300,hWnd,NULL,NULL,NULL);

}
void replacer(HWND hWnd)
{
    char oword[100];
    char nword[100];
    wchar_t data[100000];
    GetWindowText(hOldWord,oword,100);
    GetWindowText(hNewWord,nword,100);
    GetWindowText(hEdit,data,100000);

    size_t startpos = data.find(oword);
    data.replace(startpos,oword.length(),nword);



}

void AddDialogButton(HWND hWnd)
{
    CreateWindowW(L"Static",L"Please enter the old word:",WS_CHILD|WS_VISIBLE,2,2,250,25,hWnd,NULL,NULL,NULL);
    hOldWord = CreateWindowW(L"Edit",L"",WS_CHILD|WS_VISIBLE|WS_BORDER,10,20,250,30,hWnd,NULL,NULL,NULL);

    CreateWindowW(L"Static",L"Please enter the new word:",WS_CHILD|WS_VISIBLE,2,100,250,25,hWnd,NULL,NULL,NULL);
    hNewWord = CreateWindowW(L"Edit",L"",WS_CHILD|WS_VISIBLE|WS_BORDER,10,125,250,30,hWnd,NULL,NULL,NULL);

    CreateWindowW(L"Button",L"Replace",WS_VISIBLE|WS_CHILD,220,220,70,40,hWnd,NULL,NULL,NULL);
}
LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
    switch(msg)
    {
    case WM_KEYDOWN:
        if (wp == VK_ESCAPE)
        {
            MessageBoxW(NULL,L"You just left clicked", NULL,MB_OK);
        }
    //The WM_COMMAND is sent to the windowproc when a button or menu is clicked
    case WM_COMMAND:
        //WPARAM is an argument that represents the IDs of menus and button
        switch(wp)
        {
        case SEARCH_MENU_REPLACE:
            displayDialog(hWnd);
            break;
        case FILE_MENU_SAVE:
            save_file(hWnd);
            break;
        case FILe_MENU_OPEN:
            open_file(hWnd);
            break;
        case FILE_MENU_EXIT:
            messagebox_id = MessageBoxW(NULL,L"Are you sure?",L"Attention",MB_YESNO|MB_ICONEXCLAMATION);
            if(messagebox_id == IDYES)
            {
                DestroyWindow(hWnd);
            }
            break;
        }
        break;
    //WM_CREATE function is called when evr the window is created ,this is to initiate all of the components such as buttons and menus
    case WM_CREATE:
        AddControls(hWnd);
        AddMenu(hWnd);
        break;
    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd,msg,wp,lp);
    }

}

void AddMenu(HWND hWnd)
{
    hMenu = CreateMenu();
    HMENU hFileMEnu = CreateMenu();
    HMENU hSearchMenu =CreateMenu();

    AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hFileMEnu,"FILE");
    AppendMenu(hMenu,MF_STRING,NULL,"EDIT");
    AppendMenu(hMenu,MF_STRING,NULL,"VIEW");
    AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hSearchMenu,"SEARCH");
    AppendMenu(hMenu,MF_STRING,(UINT_PTR)FILE_MENU_BUILD,"BUILD");
    AppendMenu(hMenu,MF_STRING,NULL,"HELP");

    //sub menus under the file menu
    AppendMenu(hFileMEnu,MF_STRING,NULL,"NEW");
    AppendMenu(hFileMEnu,MF_STRING,(UINT_PTR)FILe_MENU_OPEN,"OPEN");
    AppendMenu(hFileMEnu,MF_STRING,(UINT_PTR)FILE_MENU_SAVE,"SAVE");
    AppendMenu(hFileMEnu,MF_STRING,FILE_MENU_EXIT,"EXIT");

    AppendMenu(hSearchMenu,MF_STRING,NULL,"FIND");
    AppendMenu(hSearchMenu,MF_STRING,(UINT_PTR)SEARCH_MENU_REPLACE,"REPLACE");
    AppendMenu(hSearchMenu,MF_STRING,NULL,"GO TO");



    //this is used to declare where and what menu is to cretaed
    //SetMenu(handler(usually window handler),what menu is being set);
    SetMenu(hWnd,hMenu);

}

void AddControls(HWND hWnd)
{
    hCurrentPath = CreateWindowW(L"Static",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,750,2,200,20,hWnd,NULL,NULL,NULL);
    hEdit = CreateWindowW(L"Edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|ES_MULTILINE,5,25,980,550,hWnd,NULL,NULL,NULL);

}


我尝试使用find() function 找到要替换的单词的起始 position,然后使用replace() function 将旧单词替换为新单词,但得到的都是错误。 下面是替换器的块。

void replacer()
{
    char oword[100];
    char nword[100];
    wchar_t data[100000];
    GetWindowText(hOldWord,oword,100);
    GetWindowText(hNewWord,nword,100);
    GetWindowText(hEdit,data,100000);

    size_t startpos = data.find(oword);
    data.replace(startpos,oword.length(),nword);



}

我修改了代码,它运行良好,如下所示: 在此处输入图像描述

void replacer(HWND hWnd)
{

   
    char nword[100];
    std::string data;
    std::string oword;
    GetWindowText(hOldWord, (LPSTR)oword.c_str(), 100);
    GetWindowText(hNewWord, nword, 100);
    GetWindowText(hEdit, (LPSTR)data.c_str(), 100000);

    size_t startpos = data.find(oword);
    data.replace(startpos, oword.length(), nword);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM