04-24-2010, 08:06 AM
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:
If you have a brain, you have probably figured what this program was going to be...
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);
Source Code (Click to View)
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); } } |