Question:
I wrote a small program. Which saves the text from EditControl to a file. Everything is simple in general. But I need to substitute certain values in the text that are given in the array of the program itself. In the console version, everything was simple for me, but here I was a little lost. There is such a condition that alphabetic values are substituted instead of the numbers typed in EditControl. Instead of 1 – X, 2 – Y, 3 – A, 4 – B. For example, typing a line in Control Edit B + 3,2,1, DB2. The following result B + A, Y, X, DBY has already been written to the file. What exactly am I lost in? So the fact is that I can not figure out where to substitute this cycle for replacing elements in the array. Help who can. I've been suffering with this for a week now.
#include <Windows.h>
#include <iostream>
#include "resource.h"
using namespace std;
#define ESC_OF "Отказ от выбора или ошибка выполнения функции "
HWND hEdit1;
HWND hEdit2;
CHAR text_1[] = { 0 };
CHAR text_2[] = { 0 };
BOOL CALLBACK DlgProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow )
{
DialogBoxParam( hInstance, MAKEINTRESOURCE( IDD_DIALOG1 ), 0, ( DlgProc ), 0 );
return 0;
}
BOOL CALLBACK DlgProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
BOOL success;
static OPENFILENAME ofn;
static char szFile[MAX_PATH];
switch( uMsg ) {
case WM_INITDIALOG: { // Добавление компонентов на форму
// Инициализация структуры ofn
ofn.lStructSize = sizeof( OPENFILENAME );
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof( szFile );
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrDefExt = "txt";
//Иконка
HICON hIcon1 = LoadIcon( GetModuleHandle( NULL ),
MAKEINTRESOURCE( IDI_ICON1 ) );
SendMessage( hwnd, WM_SETICON, 1, ( LPARAM )hIcon1 );
//Текстовые поля
hEdit1 = GetDlgItem( hwnd, IDC_EDIT1 );
hEdit2 = GetDlgItem( hwnd, IDC_EDIT2 );
SetWindowText( hEdit1, text_1 );
SetFocus( hEdit1 );
break;
return TRUE;
}
case WM_COMMAND: // Обработка кнопок и нажатий
switch( LOWORD( wParam ) ) {
case IDC_BUTTON1:
strcpy_s( szFile, "" );
success = GetSaveFileName( &ofn );
if( success ) {
MessageBox( hwnd, ofn.lpstrFile,
"Файл сохранен под именем:", MB_OK );
HANDLE hFile = CreateFile( ofn.lpstrFile,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
const int size = 2048;
TCHAR buff[size] = { 0 };
hEdit2 = GetDlgItem( hwnd, IDC_EDIT2 );
DWORD off = 0;
do {
GetWindowText( hEdit2, buff, size );
WriteFile( hFile, buff, size, &off, NULL );
} while( off < 0 );
CloseHandle( hFile );
}
else {
MessageBox( hwnd, ESC_OF"GetSaveFileName",
"Отказ от выбора или ошибка", MB_ICONWARNING );
}
break;
case IDOK:
MessageBox( hwnd, "Была нажата кнопка ОК", "Info",
MB_OK | MB_ICONINFORMATION );
break;
case IDCANCEL:
EndDialog( hwnd, 0 );
return FALSE;
}
break;
case WM_CLOSE:
EndDialog( hwnd, 0 );
return FALSE;
}
return FALSE;
}
Answer:
In the button click handler case IDC_BUTTON1: there is a loop:
do {
GetWindowText( hEdit2, buff, size );
WriteFile( hFile, buff, size, &off, NULL );
} while( off < 0 );
GetWindowText () – takes the text from the control and adds it to the buff array. WriteFile () – Writes this buff array to a file.
Accordingly, between these two function calls, you need to correct the buff content as you need (i.e. insert a cycle for replacing characters).