-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path7zsfx.c
326 lines (303 loc) · 8.51 KB
/
7zsfx.c
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
#include <Windows.h>
#include <ShlObj.h>
#include <Shlwapi.h>
#include <stdbool.h>
#include <windowsx.h>
#include <stdio.h>
#include "resource.h"
#pragma comment(lib,"shlwapi.lib")
#define IDM_APP_ABOUT 2000
int CALLBACK ChooseDirectoryClassicCbk(HWND hwnd, UINT msg, LPARAM lParam, LPARAM lpData)
{
switch (msg)
{
case BFFM_INITIALIZED:
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
break;
case BFFM_SELCHANGED:
SendMessage(hwnd, BFFM_SETSELECTION, FALSE, lParam);
break;
}
return 0;
}
BOOL ChooseDirectoryClassic(HWND hWndParent, TCHAR* fullPath, PCTSTR pcszDefaultPath, PCTSTR pcszInstruction)
{
BROWSEINFO bi;
bi.hwndOwner = hWndParent;
bi.pidlRoot = NULL;
bi.pszDisplayName = fullPath;
bi.lpszTitle = pcszInstruction;
bi.ulFlags = BIF_USENEWUI | BIF_UAHINT;
bi.lParam = (LPARAM)pcszDefaultPath;
//https://www.arclab.com/en/kb/cppmfc/select-folder-shbrowseforfolder.html
bi.lpfn = ChooseDirectoryClassicCbk;
bi.iImage = 0;
LPITEMIDLIST pi = SHBrowseForFolder(&bi);
if (pi)
{
if (SHGetPathFromIDList(pi, fullPath))
return TRUE;
}
return FALSE;
}
void OnInitDialog(HWND hwnd)
{
CheckDlgButton(hwnd, IDC_CHECK_PROGRESS, BST_CHECKED);
CheckDlgButton(hwnd, IDC_CHECK_LOCATE, BST_CHECKED);
CheckDlgButton(hwnd, IDC_CHECK_ADMIN, BST_UNCHECKED);
switch (__argc)
{
case 2:
SetDlgItemText(hwnd, IDC_EDIT_SOURCE, __wargv[1]);
break;
case 5:
if (lstrcmpi(__wargv[4], TEXT("admin")) == 0)
CheckDlgButton(hwnd, IDC_CHECK_ADMIN, BST_CHECKED);
case 4:
SetDlgItemText(hwnd, IDC_EDIT_RUN_PATH, __wargv[3]);
case 3:
SetDlgItemText(hwnd, IDC_EDIT_SOURCE, __wargv[1]);
ComboBox_SetText(GetDlgItem(hwnd, IDC_COMBO_RUN), __wargv[2]);
SendMessage(hwnd, WM_COMMAND, IDOK, 0);
break;
}
HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
AppendMenu(hSysMenu, MF_SEPARATOR, 0, 0);
AppendMenu(hSysMenu, MF_STRING, IDM_APP_ABOUT, TEXT("关于(&A)..."));
}
BOOL IsExecutableFile(LPCTSTR filename)
{
LPCTSTR ext[] = { TEXT(".exe"),TEXT(".com") };
for (int i = 0; i < ARRAYSIZE(ext); i++)
{
if (StrCmpI(StrRChr(filename, NULL, '.'), ext[i]) == 0)
return TRUE;
}
return FALSE;
}
void ShowBalloonTip(HWND hwnd, int id, LPCTSTR msg,LPCTSTR title)
{
EDITBALLOONTIP tip = { sizeof(EDITBALLOONTIP), title,msg,TTI_ERROR_LARGE };
Edit_ShowBalloonTip(GetDlgItem(hwnd, id), &tip);
MessageBeep(0);
}
BOOL IsDirectoryEmpty(LPCTSTR path)
{
WIN32_FIND_DATA fd;
TCHAR checkpath[MAX_PATH] = TEXT("");
PathCombine(checkpath, path, TEXT("*"));
HANDLE fh = FindFirstFile(checkpath, &fd);
if (fh == INVALID_HANDLE_VALUE)
return TRUE;
int c = 0;
for (BOOL success = TRUE; success; success = FindNextFile(fh, &fd))
if (StrCmp(fd.cFileName, TEXT(".")) != 0 && StrCmp(fd.cFileName, TEXT("..")) != 0)
c++;
return c == 0;
}
int WCharToUtf8(LPCWSTR src,int slen,char *dst,int dlen)
{
if (dlen < WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL))
return 0;
return WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, dlen, NULL, NULL);
}
BOOL WriteProperty(FILE* fp, LPCSTR key, LPCTSTR val)
{
char buf[400];
int bc = WCharToUtf8(val, lstrlen(val), buf, 400);
fprintf(fp, "%s=\"", key);
if (fwrite(buf, 1, bc - 1, fp) == 0)
return FALSE;
fprintf(fp, "\"\n");
return TRUE;
}
void OnOK(HWND hwnd)
{
TCHAR buf[MAX_PATH] = TEXT("");
GetDlgItemText(hwnd, IDC_EDIT_SOURCE, buf, MAX_PATH - 1);
if (lstrlen(buf) == 0)
{
ShowBalloonTip(hwnd, IDC_EDIT_SOURCE, TEXT("请输入路径。"),TEXT("错误"));
return;
}
else if(IsDirectoryEmpty(buf))
{
ShowBalloonTip(hwnd, IDC_EDIT_SOURCE, TEXT("这是一个无效路径或空文件夹,请选择其他路径。"), TEXT("错误"));
return;
}
TCHAR fullpath[MAX_PATH] = TEXT(""), archivepath[MAX_PATH];
GetFullPathName(buf, MAX_PATH - 1, fullpath, NULL);
wsprintf(archivepath, TEXT("%s.exe"), fullpath);
PathCombine(fullpath, fullpath, TEXT("*"));
FILE* fp = NULL;
_wfopen_s(&fp, TEXT("config.txt"), TEXT("wb+"));
if (fp)
{
fprintf(fp, ";!@Install@!UTF-8!\n");
if (GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT_TITLE)))
{
GetDlgItemText(hwnd, IDC_EDIT_TITLE, buf, MAX_PATH - 1);
WriteProperty(fp, "Title", buf);
}
if (GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT_MESSAGE)))
{
GetDlgItemText(hwnd, IDC_EDIT_MESSAGE, buf, MAX_PATH - 1);
WriteProperty(fp, "BeginPrompt", buf);
}
WriteProperty(fp, "Progress", IsDlgButtonChecked(hwnd, IDC_CHECK_PROGRESS) ? TEXT("yes") : TEXT("no"));
ComboBox_GetText(GetDlgItem(hwnd, IDC_COMBO_RUN), buf, MAX_PATH - 1);
if (lstrlen(buf))
{
if (IsExecutableFile(buf))
{
WriteProperty(fp, "RunProgram", buf);
if (GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT_RUN_PATH)))
{
GetDlgItemText(hwnd, IDC_EDIT_RUN_PATH, buf, MAX_PATH - 1);
WriteProperty(fp, "Directory", buf);
}
}
else
{
WriteProperty(fp, "ExecuteFile", buf);
if (GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT_RUN_PATH)))
{
GetDlgItemText(hwnd, IDC_EDIT_RUN_PATH, buf, MAX_PATH - 1);
WriteProperty(fp, "ExecuteParameters", buf);
}
}
}
fprintf(fp, ";!@InstallEnd@!\n");
fclose(fp);
}
TCHAR cmdline[400];
LPCTSTR sfxFile = IsDlgButtonChecked(hwnd, IDC_CHECK_ADMIN) ? TEXT("7zSA.sfx") : TEXT("7zS.sfx");
wsprintf(cmdline, TEXT("7za a archive.7z \"%s\"©/b/y %s+config.txt+archive.7z \"%s\"&del config.txt archive.7z"), fullpath, sfxFile, archivepath);
if (IsDlgButtonChecked(hwnd, IDC_CHECK_LOCATE))
{
lstrcat(cmdline, TEXT("&explorer/select,\""));
lstrcat(cmdline, archivepath);
lstrcat(cmdline, TEXT("\""));
}
EndDialog(hwnd, 0);
_wsystem(cmdline);
}
void RefreshCombobox(HWND hwnd,LPCTSTR path)
{
HWND hCombobox = GetDlgItem(hwnd, IDC_COMBO_RUN);
while (ComboBox_GetCount(hCombobox))
ComboBox_DeleteString(hCombobox, 0);
WIN32_FIND_DATA fd;
TCHAR checkpath[MAX_PATH] = TEXT("");
PathCombine(checkpath, path, TEXT("*"));
HANDLE fh = FindFirstFile(checkpath, &fd);
if (fh == INVALID_HANDLE_VALUE)
return;
for (BOOL success = TRUE; success; success = FindNextFile(fh, &fd))
{
TCHAR fullpath[MAX_PATH];
PathCombine(fullpath, path, fd.cFileName);
if (PathIsDirectory(fullpath)==FALSE)
ComboBox_AddString(hCombobox, fd.cFileName);
}
BOOL foundExe = FALSE, haveFiles = FALSE;
for (int i = ComboBox_GetCount(hCombobox) - 1; i >= 0; i--)
{
TCHAR filename[MAX_PATH];
ComboBox_GetLBText(hCombobox, i, filename);
if (IsExecutableFile(filename))
{
ComboBox_SetCurSel(hCombobox, i);
foundExe = TRUE;
}
haveFiles = TRUE;
}
if (!foundExe && haveFiles)
ComboBox_SetCurSel(hCombobox, 0);
}
void OnBrowse(HWND hwnd)
{
TCHAR path[MAX_PATH] = TEXT("");
GetDlgItemText(hwnd, IDC_EDIT_SOURCE, path, MAX_PATH - 1);
if (lstrlen(path) == 0)
GetCurrentDirectory(MAX_PATH, path);
if (ChooseDirectoryClassic(hwnd, path, path, TEXT("选择要压缩的文件目录。")))
SetDlgItemText(hwnd, IDC_EDIT_SOURCE, path);
}
void OnEditSourceChange(HWND hwnd)
{
TCHAR text[MAX_PATH] = TEXT("");
GetDlgItemText(hwnd, IDC_EDIT_SOURCE, text, MAX_PATH - 1);
RefreshCombobox(hwnd, text);
}
void OnComboRunChange(HWND hwnd)
{
TCHAR buf[MAX_PATH];
HWND hCombo = GetDlgItem(hwnd, IDC_COMBO_RUN);
int sel = ComboBox_GetCurSel(hCombo);
if (sel == -1)
ComboBox_GetText(GetDlgItem(hwnd, IDC_COMBO_RUN), buf, MAX_PATH - 1);
else
ComboBox_GetLBText(hCombo, sel, buf);
if (IsExecutableFile(buf))
SetDlgItemText(hwnd, IDC_STATIC_PARAMETER, TEXT("程序路径(&P)"));
else
SetDlgItemText(hwnd, IDC_STATIC_PARAMETER, TEXT("运行参数(&P)"));
}
INT_PTR CALLBACK DialogFunc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
OnInitDialog(hwnd);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCANCEL:
EndDialog(hwnd, 1);
break;
case IDOK:
OnOK(hwnd);
break;
case IDC_BUTTON_BROWSE:
OnBrowse(hwnd);
break;
case IDC_EDIT_SOURCE:
if (HIWORD(wParam) == EN_CHANGE)
OnEditSourceChange(hwnd);
break;
case IDC_COMBO_RUN:
switch (HIWORD(wParam))
{
case CBN_EDITCHANGE:
case CBN_SELCHANGE:
OnComboRunChange(hwnd);
break;
}
break;
}
break;
case WM_SYSCOMMAND:
switch (LOWORD(wParam))
{
case IDM_APP_ABOUT:
MessageBox(hwnd, TEXT("7z 自解压格式压缩文件创建工具\n\n制作:lxfly2000\nhttps://github.com/lxfly2000/7zsfx"), TEXT("关于"), 0);
break;
}
break;
case WM_DROPFILES:
{
TCHAR filepath[MAX_PATH] = TEXT("");
DragQueryFile((HDROP)wParam, 0, filepath, MAX_PATH);
DragFinish((HDROP)wParam);
SetDlgItemText(hwnd, IDC_EDIT_SOURCE, filepath);
break;
}
}
return 0;
}
int WINAPI wWinMain(_In_ HINSTANCE hI, _In_opt_ HINSTANCE hPrevI, _In_ LPWSTR param, _In_ int iShowWindow)
{
return (int)DialogBox(hI, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, DialogFunc);
}