-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinapp.cpp
More file actions
222 lines (158 loc) · 5.68 KB
/
Copy pathwinapp.cpp
File metadata and controls
222 lines (158 loc) · 5.68 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//#ifndef UNICODE
//#define UNICODE
//#endif
#include "header.hpp"
constexpr long long hsaveAsButton = 5ll;
constexpr long long hsaveButton = 6ll;
constexpr long long hloadButton = 7ll;
void writeFile( LPSTR fileName, LPSTR message)
{
std::string str = (char*)message;
std::string fixedString = replaceCharacter(str, '\r', ' ');
std::ofstream file;
file.open(fileName);
file << fixedString.c_str();
file.close();
}
std::string replaceCharacter(std::string source, char find, char replace)
{
for(int i = 0; i < source.length(); i++)
{
if(find == source[i])
{
source[i] = replace;
}
}
return source;
}
void loadFile(LPSTR fileName)
{ std::string str;
std::string fileContents;
std::ifstream file;
file.open(fileName);
while (std::getline(file, str))
{
fileContents += str;
fileContents += "\r\n";
}
file.close();
SetWindowText(textEdit, (LPSTR)fileContents.c_str());
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{
// Register the window class.
"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "Sample Window Class";
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowExA(
0, // Optional window styles.
"Sample Window Class", // Window class
"Textedit rookie edition", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
if(LOWORD(wParam)==hsaveAsButton && HIWORD(wParam)==BN_CLICKED)
{ GetWindowText(textEdit, text, sizeof(text));
OPENFILENAME ofn; // common dialog box structure
TCHAR szFile[260] = { 0 }; // if using TCHAR macros
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST;
if (GetSaveFileName(&ofn) == TRUE)
{ strcpy(fileNameCache, (char*)ofn.lpstrFile);
writeFile(ofn.lpstrFile, text);
std::cout << fileNameCache;
}
}
else if(LOWORD(wParam)==hsaveButton && HIWORD(wParam)==BN_CLICKED)
{
GetWindowText(textEdit, text, sizeof(text) + 1);
writeFile(fileNameCache, text);
std::cout << text;
}
else if(LOWORD(wParam)==hloadButton && HIWORD(wParam)==BN_CLICKED)
{
OPENFILENAME ofn; // common dialog box structure
TCHAR szFile[260] = { 0 }; // if using TCHAR macros
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE)
{ strcpy(fileNameCache, (char*)ofn.lpstrFile);
loadFile(ofn.lpstrFile);
}
}
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_CREATE:
{
saveAsButton = CreateWindow("BUTTON", "save as", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 10, 90, 40, hwnd, (HMENU)hsaveAsButton, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
saveButton = CreateWindow("BUTTON", "save", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 110, 10, 90, 40, hwnd, (HMENU)hsaveButton, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
loadButton = CreateWindow("BUTTON", "load", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 210, 10, 90, 40, hwnd, (HMENU)hloadButton, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
textEdit = CreateWindow("EDIT", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | WS_VSCROLL, 5, 60, 1350, 650, hwnd, NULL,(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE) , NULL );
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+3));
EndPaint(hwnd, &ps);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}