[C++] Setting the text in a multiline textbox - blow_fly98 - 04-24-2010
Here's the deal:
I'm try to set the text of a multiline textbox to something. I know how to do that.
Here's the problem:
The text that's displayed in the textbox is missing all of the \r\n's.
What's going wrong?!?!?!
The line that stuffs up is:
Code: SetDlgItemTextA(hwnd, ID_MAIN_EDIT, result);
C++-Code:
/*
* HelloWorld.cpp
*
* Created on: 17/04/2010
* Author: You DON'T need to know my name
*/
#include <iostream>
#include <fstream>
#include <windows.h>
#define ID_MAIN_EDIT 101
#define ID_FILE_NEW 102
#define ID_FILE_OPEN 103
#define ID_FILE_SAVE 104
#define ID_FILE_SAVE_AS 105
const char g_szClassName[] = "DCPad";
const char encKey[] = "odBearBecauseHeIsVeryGoodSiuHungIsAGo"; //37 characters
int offset = 123;
HWND hEdit;
char* Decrypt(char* bytes);
char* DecryptOffset(char* bytes);
using namespace std;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
//Creating the textbox
hEdit
= CreateWindowEx(WS_EX_WINDOWEDGE, "EDIT", "", WS_CHILD
| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL
| ES_MULTILINE | ES_AUTOVSCROLL
| ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd,
(HMENU) ID_MAIN_EDIT, GetModuleHandle(NULL),
NULL);
if (hEdit == NULL)
MessageBox(hwnd, "Could not create textbox.", "Error",
MB_OK | MB_ICONERROR);
HFONT hFont = CreateFont(-12, 0, 0, 0, FW_DONTCARE, FALSE,
FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH,
TEXT("Courier New"));
SendMessage(hEdit, WM_SETFONT, (WPARAM) hFont,
MAKELPARAM(FALSE, 0)); //Setting the font
//Creating the menu
HMENU hMenu = CreateMenu();
HMENU hSubMenus;
hSubMenus = CreateMenu();
//File menu
AppendMenu(hSubMenus, MF_STRING, ID_FILE_NEW, "&New\tCtrl+N");
AppendMenu(hSubMenus, MF_STRING, ID_FILE_OPEN,
"&Open...\tCtrl+O");
AppendMenu(hSubMenus, MF_STRING, ID_FILE_SAVE, "&Save\tCtrl+S");
AppendMenu(hSubMenus, MF_STRING, ID_FILE_SAVE_AS,
"Save &As...\tCtrl+Shift+S");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT) hSubMenus,
"&File");
SetMenu(hwnd, hMenu);
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_FILE_NEW:
{
}
break;
case ID_FILE_OPEN:
{
OPENFILENAME FileDialog;
char szFileName[MAX_PATH] = "";
ZeroMemory(&FileDialog, sizeof(FileDialog));
FileDialog.lStructSize = sizeof(FileDialog); // SEE NOTE BELOW
FileDialog.hwndOwner = hwnd;
FileDialog.lpstrFilter
= "LF2 DAT Files (*.dat)\0*.dat\0Text Documents (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
FileDialog.lpstrFile = szFileName;
FileDialog.nMaxFile = MAX_PATH;
FileDialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY;
FileDialog.lpstrDefExt = "dat";
if (GetOpenFileName(&FileDialog))
{
ifstream input;
input.open(szFileName);
if (input.is_open())
{
//Get the length of the file
input.seekg(0, ios::end);
int length = input.tellg();
input.seekg(0, ios::beg);
//Read file into memory
char* bytes = new char[length];
input.read(bytes, (streamsize) length);
char* result = DecryptOffset(bytes);
SetDlgItemTextA(hwnd, ID_MAIN_EDIT, result); // <- Where it stuffs up
//The thing is, the 'result' variable actually has all the \r\n's, but when I try to set the textbox's text,
//it stuffs up.
}
}
}
break;
}
}
break;
case WM_SIZE:
{
RECT rcClient;
GetClientRect(hwnd, &rcClient);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right,
rcClient.bottom, SWP_NOZORDER);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "DCPad",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL,
NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Error!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
char* Decrypt(char* bytes)
{
int length = strlen(bytes);
char* result = new char[length];
unsigned int keyindex = 0;
for (int i = 0; i < length; i++)
{
if (keyindex >= strlen(encKey))
keyindex = 0;
result[i] = (bytes[i] - encKey[keyindex]);
keyindex++;
}
return result;
}
char* DecryptOffset(char* bytes)
{
int length = strlen(bytes);
if (length <= offset)
return bytes;
else
{
char* input = new char[length - offset];
memmove(input, bytes + offset, length - offset);
return Decrypt(input);
}
}
|
If you have a brain, you have probably figured what this program was going to be...
RE: [C++] Setting the text in a multiline textbox - The Lost Global Mod - 04-24-2010
i am not very familiar with c++ (only had c for one year and i hated it => didn't learn anything properly) so i can only google for you.
http://forums.devshed.com/c-programming-42/setdlgitemtext-newline-trouble-409073.html
they are talking about a problem with the new line..maybe it helps..
RE: [C++] Setting the text in a multiline textbox - blow_fly98 - 05-15-2010
After 1 month this is solved.
C++-Code:
/*
* HelloWorld.cpp
*
* Created on: 17/04/2010
* Author: W/E
*/
#include <iostream>
#include <fstream>
#include <windows.h>
#define ID_MAIN_EDIT 101
#define ID_FILE_NEW 102
#define ID_FILE_OPEN 103
#define ID_FILE_SAVE 104
#define ID_FILE_SAVE_AS 105
const char g_szClassName[] = "DCPad";
const char encKey[] = "odBearBecauseHeIsVeryGoodSiuHungIsAGo"; //37 characters
int offset = 123;
HWND hEdit;
using namespace std;
char* Decrypt(char* bytes);
char* DecryptOffset(char* bytes);
char* ReadAll(ifstream& input);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
//Creating the textbox
hEdit = CreateWindowEx(WS_EX_WINDOWEDGE, "EDIT", "", WS_CHILD
| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE
| ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN, 0,
0, 100, 100, hwnd, (HMENU) ID_MAIN_EDIT,
GetModuleHandle(NULL), NULL);
if (hEdit == NULL)
MessageBox(hwnd, "Could not create textbox.", "Error",
MB_OK | MB_ICONERROR);
HFONT hFont = CreateFont(-12, 0, 0, 0, FW_DONTCARE, FALSE,
FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH,
TEXT("Courier New"));
SendMessage(hEdit, WM_SETFONT, (WPARAM) hFont,
MAKELPARAM(FALSE, 0)); //Setting the font
//Creating the menu
HMENU hMenu = CreateMenu();
HMENU hSubMenus;
hSubMenus = CreateMenu();
//File menu
AppendMenu(hSubMenus, MF_STRING, ID_FILE_NEW, "&New\tCtrl+N");
AppendMenu(hSubMenus, MF_STRING, ID_FILE_OPEN,
"&Open...\tCtrl+O");
AppendMenu(hSubMenus, MF_STRING, ID_FILE_SAVE, "&Save\tCtrl+S");
AppendMenu(hSubMenus, MF_STRING, ID_FILE_SAVE_AS,
"Save &As...\tCtrl+Shift+S");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT) hSubMenus,
"&File");
SetMenu(hwnd, hMenu);
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_FILE_NEW:
{
}
break;
case ID_FILE_OPEN:
{
OPENFILENAME FileDialog;
char szFileName[MAX_PATH] = "";
ZeroMemory(&FileDialog, sizeof(FileDialog));
FileDialog.lStructSize = sizeof(FileDialog); // SEE NOTE BELOW
FileDialog.hwndOwner = hwnd;
FileDialog.lpstrFilter
= "LF2 DAT Files (*.dat)\0*.dat\0Text Documents (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
FileDialog.lpstrFile = szFileName;
FileDialog.nMaxFile = MAX_PATH;
FileDialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY;
FileDialog.lpstrDefExt = "dat";
if (GetOpenFileName(&FileDialog))
{
ifstream input(szFileName, ios_base::in | ios_base::binary);
//Read file into memory
char* bytes = ReadAll(input);
char* result = DecryptOffset(bytes);
//Gets temporary file and path
char* TempPathBuffer = new char[MAX_PATH];
char* TempFileBuffer = new char[MAX_PATH];
GetTempPath(MAX_PATH, TempPathBuffer);
GetTempFileName(TempPathBuffer, TEXT("LF2"), 0,
TempFileBuffer);
//Output decrypted contents into temp file and read again
input.close();
ofstream output;
output.open(TempFileBuffer, ios_base::out | ios_base::trunc);
output << result;
output.close();
input.open(TempFileBuffer, ios_base::in | ios_base::binary);
result = ReadAll(input);
SetDlgItemText(hwnd, ID_MAIN_EDIT, result);
}
}
break;
}
}
break;
case WM_SIZE:
{
RECT rcClient;
GetClientRect(hwnd, &rcClient);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right,
rcClient.bottom, SWP_NOZORDER);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "DCPad",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL,
NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Error!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
char* Decrypt(char* bytes)
{
int length = strlen(bytes);
char* result = new char[length];
unsigned int keyindex = 0;
for (int i = 0; i < length; i++)
{
if (keyindex >= strlen(encKey))
keyindex = 0;
result[i] = (bytes[i] - encKey[keyindex]);
keyindex++;
}
return result;
}
char* DecryptOffset(char* bytes)
{
int length = strlen(bytes);
if (length <= offset)
return bytes;
else
{
char* input = new char[length - offset];
memmove(input, bytes + offset, length - offset);
return Decrypt(input);
}
}
char* ReadAll(ifstream& input)
{
char* result;
if (input.is_open())
{
//Get the length of the file
input.seekg(0, ios::end);
int length = input.tellg();
input.seekg(0, ios::beg);
result = new char[length];
input.read(result, (streamsize) length);
}
return result;
}
|
Changes are in the "case ID_FILE_OPEN:" section.
RE: [C++] Setting the text in a multiline textbox - The Lost Global Mod - 05-15-2010
^ congratz on solving the problem. If you would point out what excactly was the problem and why you had to change certain things to make it work, that would be nice for future reference.
RE: [C++] Setting the text in a multiline textbox - blow_fly98 - 05-16-2010
Oh right. Here we are:
So the code was for decrypting LF2 DAT files. The problem was, once decrypted, the file didn't contain the proper "\r\n"s. (That's the newline character in windows.) Rather, it contained "\r"s and "\n"s separately.
By using file streams and its special input/output modes, we are able to "convert" those separate "\r"s and "\n"s into the proper "\r\n"s.
Read more about the converting of "\r"s and "\n"s here.
RE: [C++] Setting the text in a multiline textbox - Azriel - 05-16-2010
pretty sure lf2 data files don't even contain the '\r's, just '\n's. i'm lazy to go to debug mode and check, but i don't really like the idea of decrypting, writing to a file, and re-reading from it. if it is that, u could just add '\r's whenever u find a '\n', but that's kinda more effort.
i'm fussy about performance
Azriel~
RE: [C++] Setting the text in a multiline textbox - blow_fly98 - 05-16-2010
I didn't like the idea of reading a file, decrypting it, writing it to another file and then re-reading it again either.
But this repeated input and output is the most convenient way.
ESPECIALLY when you have DAT files that actually do have "\r\n"s.
RE: [C++] Setting the text in a multiline textbox - Little - 08-07-2010
Man you have given just one line of the program and it is difficult to reach out the solution so please share the whole program so that i can suggest you some thing
|