Loading Text File to Edit Control

Hello

It's been a few months since I've tried to learn something for programming and figured I'd pick up where I left off on the winforgers tutorial.

I've reached the portion of the tutorial about file dialogs and I'm not sure what I'm doing wrong when it comes to loading a file into an edit control.

I think the issue might be the handle to the edit control itself. Maybe if I make it a global handle it would work but my gut tells me doing anything with global variables, handles, etc is bad form so there might be another way.

Well here is my code, it compiles and runs but the error checking in the LoadTextFileToEdit() function returns false for unable to load the file.

Thanks again for any help :)

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include "prog_skel.h"

const char g_szClassName[] = "myWindowClass";

BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
{
    std::cout << "\n" << pszFileName;

    HANDLE hFile;

    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            LPSTR pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            pszText = (LPSTR)GlobalAlloc(GPTR, dwBufferSize);
            if(pszText != NULL)
            {
                if(GetWindowText(hEdit, pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                GlobalFree(pszText);
            }
        }
        CloseHandle(hFile);
    }
    return bSuccess;
}

LRESULT CALLBACK My_Callback_Name(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			HFONT hfDefault;
			HWND hEdit;

			hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
				WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
				0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
			if(hEdit == NULL)
				MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
		}
		break;
		case WM_SIZE:
		{
			HWND hEdit;
			RECT rcClient;

			GetClientRect(hwnd, &rcClient);

			hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
			SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
		}
		break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
				        case WM_COMMAND:
        case WM_SYSCOMMAND:
        switch(LOWORD(wParam))
            {
                case ID_SAVE: //This Will Save The File
                    std::cout << "\nThis is where stuff gets saved!";
                break;
                case ID_OPEN: //This Will Open a File
                    {
                        OPENFILENAME ofn;
                        char szFileName[MAX_PATH] = "";
                        ZeroMemory(&ofn, sizeof(ofn));

                        ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                        ofn.hwndOwner = hwnd;
                        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
                        ofn.lpstrFile = szFileName;
                        ofn.nMaxFile = MAX_PATH;
                        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                        ofn.lpstrDefExt = "txt";

                        if(GetOpenFileName(&ofn))
                            {
                                HWND hEdit;
                                hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);

                                std::cout << "\n" << LoadTextFileToEdit(hEdit, szFileName);
                            }
                    }
                break;
                case ID_COPY: //This will copy stuff
                    std::cout << "\nThis will copy stuff";
                break;
                case ID_PASTE: //This will paste stuff
                    std::cout << "\nThis will paste stuff";
                break;
                case ID_QUIT: //My Quit Message
                    PostQuitMessage(0);
                    return 0;
                default:
                    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;

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = My_Callback_Name;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); //LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDM_DIALOG_MENU);
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); //(HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(
		0,
		g_szClassName,
		"Simple Text Editor Program",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 480, 320,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}


prog_skel.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <Windows.h>
#include <tchar.h>

#define IDC_MAIN_EDIT	101

#define IDC_STATIC -1
#define IDM_DIALOG_MENU 101
#define ID_SAVE 9001 //For Saving Files
#define ID_OPEN 9002 //For Opening Files
#define ID_QUIT 9003 //Custom Message Currently
#define ID_COPY 9004 //Custom Message Currently
#define ID_PASTE 9005 //Custom Message Currently
//#define IDI_MYICON 9006//for Icons 


TextEditorResource.rc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "prog_skel.h"

//The Menu options
IDM_DIALOG_MENU MENU DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
	MENUITEM "Save" ,    ID_SAVE //SAVE COMMAND
	MENUITEM "&Open" ,   ID_OPEN //OPEN COMMAND
	MENUITEM "E&xit" , ID_QUIT //EXIT COMMAND
    END
    POPUP "&Edit"
    BEGIN
	MENUITEM "&Copy" ,     ID_COPY //COPY SELECTED TEXT
	MENUITEM "&Paste" ,    ID_PASTE //PASTE TEXT FROM CLIPBOARD, MEMORY, COPIED ETC
    END
END

//The Icons
//IDI_MYICON ICON "menu_one.ico" 
LoadTextFileToEdit() is misleading. It actually writes the data from an edit control to a file. It returns TRUE only when the edit control has text and it is successfully written.

So check where exactly the function fails. Use GetLastError

http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360%28v=vs.85%29.aspx

to determine what exactly the problem is.
Thanks.. you were correct and I had mixed up the open and save functions.

All fixed now thanks :D!!
Topic archived. No new replies allowed.