-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
258 lines (201 loc) · 7.75 KB
/
Source.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
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
#include <Windows.h>
#include <WindowsX.h>
#include <CommCtrl.h>
#include <tchar.h>
#include <stdio.h>
#include <locale.h>
#include <strsafe.h>
#include <shlwapi.h>
#include <string>
#include "resource.h"
#pragma comment(lib, "shlwapi.lib")
HWND hWnd = NULL;
static int sx, sy;
HWND edit = NULL;
HWND edit_2 = NULL;
HWND Button1 = NULL;
HBRUSH brush = (HBRUSH)GetStockBrush(000000);
TCHAR bufferForName[MAX_PATH];
TCHAR bufferForNewPath[MAX_PATH];
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
typedef BOOL(__stdcall* LPSEARCHFUNC)(LPCTSTR lpszFileName, const LPWIN32_FILE_ATTRIBUTE_DATA lpFileAttributeData, LPVOID lpvParam);
BOOL __stdcall FileMove(LPCTSTR lpszFileName, const LPWIN32_FILE_ATTRIBUTE_DATA lpFileAttributeData, LPVOID lpvParam);
// ôóíêöèÿ, âûïîëíÿþùàÿ îïåðàöèþ ñ ôàéëîì èëè êàòàëîãîì
BOOL FileOperation(LPCTSTR lpszFileName, LPCTSTR lpTargetDirectory, LPSEARCHFUNC lpFunc);
// ôóíêöèÿ, êîòîðàÿ âûïîëíÿåò ïîèñê âíóòðè êàòàëîãà
BOOL FileSearch(LPCTSTR lpszFileName, LPCTSTR lpszDirName, LPSEARCHFUNC lpSearchFunc, LPVOID lpvParam);
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpszCmdLine, int nCmdShow)
{
MSG Massage;
BOOL IsError;
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = MyWindowProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
//wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcex.lpszClassName = TEXT("MyWindowClass");
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (0 == RegisterClassEx(&wcex))
{
return -1;
}
LoadLibrary(TEXT("ComCtl32.dll"));
hWnd = CreateWindowEx(0, TEXT("MyWindowClass"), TEXT("Lab_4_3_WindowApp"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 550, 400, NULL, NULL, hInstance, NULL);
if (NULL == hWnd)
{
return -1;
}
ShowWindow(hWnd, nCmdShow);
while (true)
{
if (PeekMessage(&Massage, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
IsError = GetMessage(&Massage, NULL, 0, 0);
if (IsError == -1 || IsError == FALSE)
{
return FALSE;
}
else
{
TranslateMessage(&Massage);
DispatchMessage(&Massage);
}
}
}
return static_cast<int>(Massage.wParam);
}
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hWnd, WM_CREATE, OnCreate);
HANDLE_MSG(hWnd, WM_COMMAND, OnCommand);
case WM_SIZE:
sx = LOWORD(lParam);
sy = HIWORD(lParam);
LPCWSTR buff;
wchar_t str[20];
_itow_s(sx, str, 10);
if (sx < 380)
MoveWindow(Button1, 10, 80, 130, 30, true);
else
MoveWindow(Button1, 380, 10, 130, 30, true);
return 0;
case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
FillRect(ps.hdc, &ps.rcPaint, brush);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(ERROR_SUCCESS);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
// âûáîð êòàëîãà èëè ôàéëà
edit = CreateWindowEx(0, TEXT("Edit"), TEXT("Enter the folder/file path"),
WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_BORDER,
10, 10, 350, 20, hwnd, NULL, lpCreateStruct->hInstance, NULL);
// âûáîð êóäà ïåðåìåùàòü
edit_2 = CreateWindowEx(0, TEXT("Edit"), TEXT("Enter the destination folder path"),
WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_BORDER,
10, 50, 350, 20, hwnd, NULL, lpCreateStruct->hInstance, NULL);
// qwerqwer
Button1 = CreateWindowEx(0, TEXT("Button"), TEXT("Relocate"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 380, 10, 130, 30, hwnd, (HMENU)RELOCATE, lpCreateStruct->hInstance, NULL);
return TRUE;
}
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch (id)
{
case RELOCATE:
GetWindowText(edit, bufferForName, MAX_PATH);
GetWindowText(edit_2, bufferForNewPath, MAX_PATH);
BOOL bRet = FileOperation(bufferForName, bufferForNewPath, FileMove);
if (FALSE != bRet) MessageBox(hwnd, L"Nice", L"Operation result", MB_OK);
else MessageBox(hwnd, L"Bad", L"Operation result", MB_OK);
break;
}
}
/// ïîèñê ôàéëà è ïåðåìåùåíèå â íîâûîå ìåñòî
BOOL __stdcall FileMove(LPCTSTR lpszFileName, const LPWIN32_FILE_ATTRIBUTE_DATA lpFileAttributeData, LPVOID lpvParam)
{
LPCTSTR lpTargetDirectory = (LPCTSTR)lpvParam; // êàòàëîã, â êîòîðûé íóæíî ïåðåìåñòèòü ôàéë/êàòàëîã
TCHAR szNewFileName[MAX_PATH]; // íîâîå èìÿ ôàéëà/êàòàëîãà
StringCchPrintf(szNewFileName, _countof(szNewFileName), TEXT("%s\\%s"), (LPCTSTR)lpTargetDirectory, PathFindFileName(lpszFileName));
if (lpFileAttributeData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // íóæíî ïåðåìåñòèòü êàòàëîã
{
// ñîçäà¸ì íîâûé êàòàëîã (àòðèáóòû êîïèðóþòñÿ)
BOOL bRet = CreateDirectoryEx(lpszFileName, szNewFileName, NULL);
if ((FALSE != bRet) || (GetLastError() == ERROR_ALREADY_EXISTS)) // (!) îøèáêà: Íåâîçìîæíî ñîçäàòü êàòàëîã, òàê êàê îí óæå ñóùåñòâóåò.
{
// ïðîäîëæèì ïîèñê âíóòðè êàòàëîãà
bRet = FileSearch(TEXT("*"), lpszFileName, FileMove, szNewFileName);
}
if (FALSE != bRet)
{
// óäàëÿåì êàòàëîã
bRet = RemoveDirectory(lpszFileName);
}
return bRet;
}
return MoveFileEx(lpszFileName, szNewFileName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
}
BOOL FileOperation(LPCTSTR lpszFileName, LPCTSTR lpTargetDirectory, LPSEARCHFUNC lpFunc)
{
if (NULL != lpTargetDirectory)
{
DWORD dwFileAttributes = GetFileAttributes(lpTargetDirectory);
if (INVALID_FILE_ATTRIBUTES == dwFileAttributes) // (!) îøèáêà
{
return FALSE;
}
else if ((dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) // (!) íå ÿâëÿåòñÿ êàòàëîãîì
{
SetLastError(ERROR_PATH_NOT_FOUND);
return FALSE;
}
}
WIN32_FILE_ATTRIBUTE_DATA fad;
// ïîëó÷èì àòðèáóòèâíóþ èíôîðìàöèþ ôàéëà/êàòàëîãà
BOOL bRet = GetFileAttributesEx(lpszFileName, GetFileExInfoStandard, &fad);
if (FALSE != bRet)
{
// âûïîëíÿåì îïåðàöèþ ñ ôàéëîì/êàòàëîãîì
bRet = lpFunc(lpszFileName, &fad, (LPVOID)lpTargetDirectory);
}
return bRet;
}
BOOL FileSearch(LPCTSTR lpszFileName, LPCTSTR lpszDirName, LPSEARCHFUNC lpSearchFunc, LPVOID lpvParam)
{
WIN32_FIND_DATA fd;
TCHAR szFileName[MAX_PATH];
// ôîðìèðóåì øàáëîí ïîèñêà
StringCchPrintf(szFileName, MAX_PATH, TEXT("%s\\%s"), lpszDirName, lpszFileName);
// íà÷èíàåì ïîèñê
HANDLE hFindFile = FindFirstFile(szFileName, &fd);
if (INVALID_HANDLE_VALUE == hFindFile) return FALSE;
BOOL bRet = TRUE;
for (BOOL bFindNext = TRUE; FALSE != bFindNext; bFindNext = FindNextFile(hFindFile, &fd))
{
if (_tcscmp(fd.cFileName, TEXT(".")) == 0 || _tcscmp(fd.cFileName, TEXT("..")) == 0)
{
/* ïðîïóñêàåì òåêóùèé è ðîäèòåëüñêèé êàòàëîã */
continue;
}
// ôîðìèðóåì ïîëíûé ïóòü ê ôàéëó
StringCchPrintf(szFileName, MAX_PATH, TEXT("%s\\%s"), lpszDirName, fd.cFileName);
bRet = lpSearchFunc(szFileName, (LPWIN32_FILE_ATTRIBUTE_DATA)&fd, lpvParam);
if (FALSE == bRet) break; // ïðåðûâàåì ïîèñê
}
FindClose(hFindFile); // çàâåðøàåì ïîèñê
return bRet;
}