-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWin32Project5.cpp
More file actions
531 lines (470 loc) · 16.1 KB
/
Win32Project5.cpp
File metadata and controls
531 lines (470 loc) · 16.1 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "stdafx.h"
#include "Win32Project5.h"
#include "Windowsx.h"
#define MAX_LOADSTRING 100
#define FILENAMESZ 260
// Глобальные переменные:
HINSTANCE hInst; // текущий экземпляр
TCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
TCHAR szWindowClass[MAX_LOADSTRING]; // имя класса главного окна
// Отправить объявления функций, включенных в этот модуль кода:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Exit(HWND, UINT, WPARAM, LPARAM);
RECT rect_1;
HWND hWnd;
DWORD Ex=0;
HKEY hK;
DWORD hu;
TCHAR szFile[FILENAMESZ];//буфер для имени файла
int flag = 0;//чтобы SAVE и SAVEAS было на красоте
int flag2 = 1;//чтобы реестр и ID_OPEN
HANDLE hf;
OPENFILENAME yo;
DWORD lpcbData = sizeof(RECT);
const int numButtons = 7;
TBBUTTON tbButtons[numButtons] =
{
{ 0, ID_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0 },
{ 1, ID_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0 }, 0, 0 /*(INT_PTR)L" " */ },
{ 2, ID_SAVE_AS, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0 }, 0, 0/*(INT_PTR)L"Save us"*/ },
{ 3, IDM_COPY, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0 }, 0, 0 /*(INT_PTR)L"Copy"*/ },
{ 4, IDM_CUT, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0 }, 0, 0 /*(INT_PTR)L"Cut" */ },
{ 5, IDM_PASTE, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0 }, 0, 0 /*(INT_PTR)L"Paste"*/ },
{ 6, IDM_CANCEL, TBSTATE_ENABLED, TBSTYLE_BUTTON, { 0 }, 0, 0 /*(INT_PTR)L"Cancell"*/ },
};
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: разместите код здесь.
MSG msg;
HACCEL hAccelTable;
// Инициализация глобальных строк
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32PROJECT5, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Выполнить инициализацию приложения:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT5));
// Цикл основного сообщения:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hWnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// ФУНКЦИЯ: MyRegisterClass()
//
// НАЗНАЧЕНИЕ: регистрирует класс окна.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT5));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); COLOR_3DSHADOW;//6 7
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT5);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
DWORD regFlag;
RECT rect;
DWORD dSize = sizeof(DWORD);
DWORD fNameSize = FILENAMESZ * 2;
hInst = hInstance;
InitCommonControls();
RegCreateKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Danil9966\\notpad"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hK, &hu);
flag2 = RegQueryValueEx(hK, _T("FileName"), NULL, NULL, (LPBYTE)szFile, &fNameSize);
if (szFile[0] == _T('\0'))flag2 = 2;
regFlag=RegQueryValueEx(hK, _T("YOSO"), NULL, NULL, (LPBYTE)&rect, &lpcbData);
int height = rect.bottom - rect.top;
int width = rect.right - rect.left;
if (hu == REG_OPENED_EXISTING_KEY && regFlag==ERROR_SUCCESS)
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CAPTION | CS_HREDRAW | CS_VREDRAW,
rect.left, rect.top, width, height, NULL, NULL, hInstance, NULL);
else
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CAPTION | CS_HREDRAW | CS_VREDRAW,
0, 0, 600, 400, NULL, NULL, hInstance, NULL);
yo.lStructSize = sizeof(yo);
yo.hwndOwner = hWnd;
yo.lpstrFile = szFile;
yo.nMaxFile = sizeof(szFile);
yo.lpstrFilter = L"Text files(*.txt)\0";
yo.nFilterIndex = 1;
yo.lpstrFileTitle = NULL;
yo.nMaxFileTitle = NULL;
yo.lpstrInitialDir = NULL;
yo.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hfield = 0;
static HWND edit = 0;
static HWND button1 = 0;
static HWND button2 = 0;
static HWND button3 = 0;
static HWND hText = 0;
static HWND htbar = 0;
static TCHAR* buf;
static TCHAR fname[200];
static DWORD count = 0;
static DWORD dFilesize = 0;
static RECT rect = { 10,10,100,100 };
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_COPY:
SendMessage(edit, WM_COPY, 0, 0);
break;
case IDM_PASTE:
SendMessage(edit, WM_PASTE, 0, 0);
break;
case IDM_CUT:
SendMessage(edit, WM_CUT, 0, 0); break;
case IDM_CANCEL:SendMessage(edit, WM_UNDO, 0, 0); break;
case ID_OPEN:
{
if (((flag2 == ERROR_SUCCESS) && (szFile[0] != _T('\0'))) || (flag2 == 1)&&(GetOpenFileName(&yo) == TRUE) )
{
hf = CreateFile(szFile,
GENERIC_READ | GENERIC_WRITE,
0,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL);
if (hf == INVALID_HANDLE_VALUE) break;
dFilesize = GetFileSize(hf, 0);
buf = new TCHAR[dFilesize / 2 + 1];///2/2/2
ReadFile(hf, buf, dFilesize, &count, NULL);
buf[dFilesize / 2] = 0;
CloseHandle(hf);
SendMessage(edit, WM_SETTEXT, WPARAM(0), LPARAM(""));
SendMessage(edit, WM_SETTEXT, 1000000000, (LPARAM)buf);
SendMessage(hText, WM_SETTEXT, 100, (LPARAM)yo.lpstrFile);
delete[]buf;
flag = 1;
SetFocus(edit);
}
flag2 = 1;
break;
}
case ID_SAVE:
{ if (flag)
{
GetWindowText(hText, fname, 200);
hf = CreateFile(fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD textlen = GetWindowTextLength(edit);
buf = new TCHAR[textlen + 1];
GetWindowText(edit, buf, textlen + 1);
WriteFile(hf, buf, textlen * 2, &count, NULL);
CloseHandle(hf);
delete[]buf;
// flag = 0;
break;
}
}
case ID_SAVE_AS:
{
if (GetSaveFileName(&yo) == TRUE)
{
hf = CreateFile(yo.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD textlen = GetWindowTextLength(edit);
buf = new TCHAR[textlen + 1];
GetWindowText(edit, buf, textlen + 1);
WriteFile(hf, buf, textlen * 2, &count, NULL);
CloseHandle(hf);
delete[]buf;
}
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}break;
case WM_CREATE:
{
htbar = CreateToolbarEx(hWnd,
WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_WRAPABLE | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | CCS_ADJUSTABLE,
- 1, // идентификатор органа Toolbar
7, // количество пиктограмм
hInst, // идентификатор приложения
/*IDB_BITMAP2*/ IDD_TI_1, // идентификатор битового изображения кнопок
(LPCTBBUTTON)&tbButtons, // адрес описания кнопок
7, // количество кнопок
24, 24, // ширина и высота кнопок
24, 24, // ширина и высота пиктограмм
sizeof(TBBUTTON));
/* (hwnd, WS_CHILD | WS_VISIBLE | CCS_TOP, 1,
0, HINST_COMMCTRL, IDB_STD_SMALL_COLOR, tbb, 3, 0, 0, 0, 0, sizeof(TBBUTTON));*/
edit = CreateWindowEx(WS_EX_ACCEPTFILES/*WS_EX_TRANSPARENT*/, L"EDIT", NULL,
WS_CHILD | WS_VSCROLL /*| ES_AUTOHSCROLL*//* | ES_AUTOVSCROLL*/ | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
30, 40, 500, 300, hWnd, NULL, hInst, NULL);
button1 = CreateWindowEx(0, _T("BUTTON"), // Predefined class; Unicode assumed
L"Open", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD, // Styles
200, // x position
350, // y position
80, // Button width
30, // Button height
hWnd, // Parent window
(HMENU)ID_OPEN, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
button2 = CreateWindowEx(0, L"BUTTON", // Predefined class; Unicode assumed
L"Save", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD, // Styles
200, // x position
350, // y position
100, // Button width
50, // Button height
hWnd, // Parent window
(HMENU)ID_SAVE, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
button3 = CreateWindowEx(0, L"BUTTON", // Predefined class; Unicode assumed
L"Save as", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD, // Styles
200, // x position
350, // y position
100, // Button width
50, // Button height
hWnd, // Parent window
(HMENU)ID_SAVE_AS, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
hText = CreateWindow(L"STATIC", L"",
WS_CHILD | WS_VISIBLE | WS_BORDER,
30, 100, 150, 18,
hWnd,
NULL,
NULL, NULL);
hfield = CreateWindow(L"STATIC", L"",
WS_CHILD | WS_VISIBLE,
30, 100, 150, 18,
hWnd,
NULL,
NULL, NULL);
HFONT MyFont = CreateFont(
15, // logical height of font
7, // logical average character width
0, // angle of escapement
0, // base-line orientation angle
10, // font weight
0, // italic attribute flag
0, // underline attribute flag
0, // strikeout attribute flag
DEFAULT_CHARSET, // character set identifier
OUT_DEFAULT_PRECIS, // output precision
CLIP_DEFAULT_PRECIS, // clipping precision
PROOF_QUALITY, // output quality
FIXED_PITCH, // pitch and family
L"Arial");
// SetFocus(edit);
SendMessage(edit, WM_SETFONT, (WPARAM)MyFont, 0);
SendMessage(button1, WM_SETFONT, (WPARAM)MyFont, 0);
SendMessage(button2, WM_SETFONT, (WPARAM)MyFont, 0);
SendMessage(button3, WM_SETFONT, (WPARAM)MyFont, 0);
SendMessage(hText, WM_SETFONT, (WPARAM)MyFont, 0);
SetFocus(edit);
break;
}
case WM_SHOWWINDOW:
SendMessage(hWnd, WM_COMMAND, ID_OPEN, ID_OPEN);
break;
case WM_GETMINMAXINFO:
{
MINMAXINFO *pInfo = (MINMAXINFO *)lParam;
POINT Min = { 400, 200 };
pInfo->ptMinTrackSize = Min; // Установили минимальный размер
break;
}
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code)
{
case TTN_NEEDTEXT:
{
LPTOOLTIPTEXT lpt1 = LPTOOLTIPTEXT(lParam);
int idButton = lpt1->hdr.idFrom;
switch (idButton){
case ID_OPEN:
lpt1->lpszText = (L"Open");
break;
case ID_SAVE:
lpt1->lpszText = (L"Save");
break;
case ID_SAVE_AS:
lpt1->lpszText = (L"Save us");
break;
case IDM_COPY:
lpt1->lpszText = (L"Copy");
break;
case IDM_CUT:
lpt1->lpszText = (L"Cut");
break;
case IDM_PASTE:
lpt1->lpszText = (L"Paste");
break;
case IDM_CANCEL:
lpt1->lpszText = (L"Cancel");
break;
}
}
}break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_SYSCOMMAND:
if (wParam == SC_MINIMIZE || wParam == SC_MAXIMIZE || wParam == 61458)
GetWindowRect(hWnd, &rect_1);
DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_SIZE:
{
GetWindowRect(hWnd, &rect);
int height = rect.bottom - rect.top;
int width = rect.right - rect.left;
SetWindowPos(button1, 0, 3, height - 90, 80, 25, SWP_SHOWWINDOW);
SetWindowPos(button2, 0, 90,height-90, 80, 25, SWP_SHOWWINDOW);
SetWindowPos(button3, 0,177, height - 90, 80, 25, SWP_SHOWWINDOW);
SetWindowPos(hfield, 0, 260, height - 90, width-280, 25,SWP_SHOWWINDOW);
if (height < 300){
SetWindowPos(htbar,0 , 110, 1000, 1000, 100, SWP_HIDEWINDOW);
SetWindowPos(edit,
0, 5, 5,
(width)-25,
(height)-100, SWP_SHOWWINDOW);
SetWindowPos(hText, 0, 10, (height)-116, (width)-27, 18, SWP_HIDEWINDOW);
}
else {
SetWindowPos(htbar,0, 0, 0, 0, 0, SWP_SHOWWINDOW);
SetWindowPos(edit,
0, 3, 40,
(width)-23,
(height)-160, SWP_SHOWWINDOW);
SetWindowPos(hText, 0, 3, (height)-116, (width)-23, 18, SWP_SHOWWINDOW);
}
break;
}
case WM_CLOSE:
{
bool b = Edit_GetModify(edit);
if (!b) SendMessage(hWnd, WM_DESTROY, 0, 0);
else{
DialogBox(hInst, MAKEINTRESOURCE(IDD_EXITBOX), hWnd, Exit);
if (Ex == 1){
SendMessage(hWnd, WM_COMMAND, ID_SAVE, 0);
SendMessage(hWnd, WM_DESTROY, 0, 0);
}
else if (Ex == -1) SendMessage(hWnd, WM_DESTROY, 0, 0);
}
}
break;
case WM_DESTROY:
GetWindowRect(hWnd, &rect);
if (!(IsIconic(hWnd) ||IsZoomed(hWnd)) )
RegSetValueEx(hK, _T("YOSO"),
0,
REG_BINARY,
(LPBYTE)&rect,
sizeof(RECT));
else RegSetValueEx(hK, _T("YOSO"),
0,
REG_BINARY,
(LPBYTE)&rect_1,
sizeof(RECT));
RegSetValueEx(hK, _T("FileName"), 0, REG_NONE, (LPBYTE)szFile, FILENAMESZ*2);
RegCloseKey(hK);
PostQuitMessage(0);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Обработчик сообщений для окна "О программе".
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == ID_OK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK Exit(HWND hEx, UINT message, WPARAM wParam, LPARAM lParam){
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == ID_CANCELb|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hEx, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == ID_NOSAVE){
Ex = -1;
EndDialog(hEx, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == ID_SAVE_AS){
Ex = 1;
EndDialog(hEx, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}