-
Notifications
You must be signed in to change notification settings - Fork 0
/
XLSXSearcher.cpp
341 lines (301 loc) · 12 KB
/
XLSXSearcher.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
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
#include <windows.h>
#include "XLSXUtils.h"
#include"resource.h"
#include <libxl.h>
#include <ShObjIdl.h>
#include <Shlwapi.h>
#define BUTTON_IN 39101
#define ID_EDIT 39201
#define BUTTON_OUT 39102
#define ID_OUT 39202
#define BUTTON_DONE 39103
#define EDIT_NAME 39203
#define EDIT_ID 39204
#define IDC_PROGRESS1 39301
const char g_szClassName[] = "myWindowClass";
LPWSTR g_lpstrFilePATH = nullptr;
LPWSTR g_lpstrOutPATH = nullptr;
std::vector<std::string> files;
std::vector<std::string> XMLfiles;
std::vector<std::string> PDFfiles;
std::string uName;
std::string uID;
HANDLE hConsole = 0;
HWND hEdit;
HWND hEdit2;
HWND hName;
HWND hID;
HWND hWndProgress;
HWND hWndButtonDone;
std::string LPWSTRToString(LPWSTR pwszStr) {
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszStr, -1, NULL, 0, NULL, NULL);
if (nLen <= 0) {
return "";
}
char* pszStr = new char[nLen];
WideCharToMultiByte(CP_ACP, 0, pwszStr, -1, pszStr, nLen, NULL, NULL);
std::string str(pszStr);
delete[] pszStr;
return str;
}
BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam) {
SendMessage(hWnd, WM_SETFONT, lParam, TRUE);
return TRUE;
}
BOOL CheckValidPath(HWND hWnd) {
WCHAR szPath1[MAX_PATH];
WCHAR szPath2[MAX_PATH];
GetWindowTextW(hEdit, szPath1, MAX_PATH);
GetWindowTextW(hEdit2, szPath2, MAX_PATH);
if (szPath1 == L"" && szPath2 == L"") {
MessageBox(hWnd, "Please Enter Path!", "Path Validation", MB_OK | MB_ICONWARNING);
return 0;
}
if (!(PathFileExistsW(szPath1))) {
MessageBox(hWnd, "Invalid Path!", "Path Validation", MB_OK|MB_ICONWARNING);
return 0;
}
return 1;
}
// Function to open a folder dialog and return the selected folder path
HRESULT OpenFolderDialog(HWND hwndOwner, LPWSTR lpstrFolder, DWORD nMaxFolder)
{
IFileDialog* pfd = NULL;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
if (SUCCEEDED(hr))
{
// Set the options on the dialog to select folders.
DWORD dwFlags;
hr = pfd->GetOptions(&dwFlags);
if (SUCCEEDED(hr))
{
hr = pfd->SetOptions(dwFlags | FOS_PICKFOLDERS);
if (SUCCEEDED(hr))
{
// Show the dialog
hr = pfd->Show(hwndOwner);
if (SUCCEEDED(hr))
{
// Obtain the result once the user clicks the 'Open' button.
IShellItem* psiResult;
hr = pfd->GetResult(&psiResult);
if (SUCCEEDED(hr))
{
PWSTR pszFolderPath = NULL;
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
if (SUCCEEDED(hr))
{
// Copy the folder path to the buffer passed in
wcsncpy_s(lpstrFolder, nMaxFolder, pszFolderPath, nMaxFolder);
CoTaskMemFree(pszFolderPath);
}
psiResult->Release();
}
}
}
}
pfd->Release();
}
return hr;
}
HRESULT OpenFileDlg(HWND hWndOwner, LPWSTR lpstrFile, DWORD nMaxFile) {
// Create a new instance of IFileDialog
IFileDialog* pfd = NULL;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
if (SUCCEEDED(hr))
{
// Set the options on the dialog.
DWORD dwFlags;
hr = pfd->GetOptions(&dwFlags);
if (SUCCEEDED(hr))
{
// Add the FOS_FORCEFILESYSTEM flag to ensure it's a file system item.
hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
if (SUCCEEDED(hr))
{
// Show the dialog
hr = pfd->Show(hWndOwner);
if (SUCCEEDED(hr))
{
// Obtain the result once the user clicks the 'Open' button.
// The result is an IShellItem object.
IShellItem* psiResult;
hr = pfd->GetResult(&psiResult);
if (SUCCEEDED(hr))
{
// We can now get the file path from the IShellItem object.
PWSTR pszFilePath = NULL;
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (SUCCEEDED(hr))
{
// Copy the file path to the buffer passed in
wcsncpy_s(lpstrFile, nMaxFile, pszFilePath, nMaxFile);
CoTaskMemFree(pszFilePath);
}
psiResult->Release();
}
}
}
}
pfd->Release();
}
return hr;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
{
if (LOWORD(wParam) == EDIT_NAME && HIWORD(wParam) == EN_KILLFOCUS) {
TCHAR szText[100];
GetWindowText(hName, szText, 100);
WriteConsole(hConsole, szText, sizeof(szText), NULL, NULL);
uName = szText;
}
if (LOWORD(wParam) == EDIT_ID && HIWORD(wParam) == EN_KILLFOCUS) {
TCHAR szText[100];
GetWindowText(hID, szText, 100);
WriteConsole(hConsole, szText, sizeof(szText), NULL, NULL);
uID = szText;
}
if (LOWORD(wParam) == BUTTON_IN && HIWORD(wParam) == BN_CLICKED) {
g_lpstrFilePATH = new WCHAR[MAX_PATH];
HRESULT hr = OpenFolderDialog(hWnd, g_lpstrFilePATH, MAX_PATH);
if (SUCCEEDED(hr)) {
SetWindowTextW(hEdit, g_lpstrFilePATH);
}
}
if (LOWORD(wParam) == BUTTON_OUT && HIWORD(wParam) == BN_CLICKED) {
g_lpstrOutPATH = new WCHAR[MAX_PATH];
HRESULT hr = OpenFolderDialog(hWnd, g_lpstrOutPATH, MAX_PATH);
if (SUCCEEDED(hr)) {
SetWindowTextW(hEdit2, g_lpstrOutPATH);
}
}
if (LOWORD(wParam) == BUTTON_DONE && HIWORD(wParam) == BN_CLICKED) {
if (CheckValidPath(hWnd)) {
EnableWindow(hWndButtonDone, FALSE);
WriteConsole(hConsole, uName.c_str(), static_cast<DWORD>(uName.size()), NULL, NULL);
XLSXUtils::GetAllFiles(g_lpstrFilePATH, files, XMLfiles, PDFfiles);
SendMessage(hWndProgress, PBM_SETPOS, 5, 0);
XLSXUtils::DoContain(uID, uName, files, XMLfiles, hWndProgress);
/*for each (std::string path in files)
{
WriteConsole(hConsole, path.c_str(), static_cast<DWORD>(path.size()), NULL, NULL);
}
for each (std::string path in XMLfiles)
{
WriteConsole(hConsole, path.c_str(), static_cast<DWORD>(path.size()), NULL, NULL);
}*/
std::string outputPath = LPWSTRToString(g_lpstrOutPATH);
XLSXUtils::Export(outputPath, files, XMLfiles, hWndProgress);
}
else {
}
}
break;
}
case WM_CREATE:
{
HFONT hFont = CreateFont(
-MulDiv(10, GetDeviceCaps(GetDC(NULL), LOGPIXELSY), 72), // Height of font
0, // Average character width
0, // Angle of escapement
0, // Base-line orientation angle
FW_NORMAL, // Font weight
FALSE, // Italic attribute option
FALSE, // Underline attribute option
FALSE, // Strikeout attribute option
DEFAULT_CHARSET, // Character set identifier
OUT_DEFAULT_PRECIS, // Output precision
CLIP_DEFAULT_PRECIS, // Clipping precision
DEFAULT_QUALITY, // Output quality
DEFAULT_PITCH | FF_SWISS, // Pitch and family
TEXT("Microsoft YaHei")); // Typeface name
HWND hWndButton = CreateWindow("BUTTON", "IN", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 10, 10, 40, 29, hWnd, (HMENU)BUTTON_IN, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL, 60, 10, 290, 29, hWnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), NULL);
HWND hWndButton2 = CreateWindow("BUTTON", "OUT", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 10, 40, 40, 29, hWnd, (HMENU)BUTTON_OUT, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);
hEdit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 60, 40, 290, 29, hWnd, (HMENU)ID_OUT, GetModuleHandle(NULL), NULL);
hName = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "ÐÕÃû", WS_CHILD | WS_VISIBLE , 10, 80, 140, 28, hWnd, (HMENU)EDIT_NAME, GetModuleHandle(NULL), NULL);
hID = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "ѧºÅ", WS_CHILD | WS_VISIBLE , 160, 80, 140, 28, hWnd, (HMENU)EDIT_ID, GetModuleHandle(NULL), NULL);
hWndButtonDone = CreateWindow("BUTTON", "OK", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 300, 80, 40, 28, hWnd, (HMENU)BUTTON_DONE, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);
hWndProgress = CreateWindowEx(0, PROGRESS_CLASS, "", WS_CHILD | WS_VISIBLE,
10, 120, 340, 20, hWnd, (HMENU)IDC_PROGRESS1, NULL, NULL);
SetWindowRgn(hWndButton, CreateRoundRectRgn(0, 0, 100, 25, 10, 10), TRUE);
SetWindowRgn(hWndButton2, CreateRoundRectRgn(0, 0, 100, 25, 10, 10), TRUE);
HDC hWndButtonIn = GetDC(hWndButton);
HDC hWndButtonOut = GetDC(hWndButton2);
SetBkColor(hWndButtonIn, RGB(235, 235, 235));
SetBkColor(hWndButtonOut, RGB(235, 235, 235));
SetTextColor(hWndButtonIn, RGB(0, 0, 0));
SetTextColor(hWndButtonOut, RGB(0, 0, 0));
SetFocus(hName);
SetFocus(hID);
SendMessage(hWndProgress, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
EnumChildWindows(hWnd, EnumChildProc, (LPARAM)hFont);
EnableWindow(hEdit, FALSE);
EnableWindow(hEdit2, FALSE);
break;
}
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
AllocConsole();
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
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);
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;
}
char szText[256] = {0};
LoadString(hInstance, IDS_TITLE, szText, 256);
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
szText,
WS_OVERLAPPEDWINDOW & ~ WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, 390, 200,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "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;
}