📄 chap5.lst
字号:
bool reload = false;
if(argc==2 && !strcmp(argv[1], "reload"))
reload = true;
cout << "Beginning download.\n";
try {
if(Download::download(url, reload, showprogress))
cout << "Download Complete\n";
} catch(DLExc exc) {
cout << exc.geterr() << endl;
cout << "Download Interrupted\n";
}
return 0;
}
listing 4
// WinDL: A GUI-based file download utility.
#include <windows.h>
#include <commctrl.h>
#include <cstring>
#include <cstdio>
#include "windl.h"
#include <process.h>
#include "dl.h"
const int URL_BUF_SIZE = 1024;
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialogFunc(HWND, UINT, WPARAM, LPARAM);
void showprogress(unsigned long total,
unsigned long part);
void resetprogress();
unsigned __stdcall dlstart(void * reload);
char szWinName[] = "Download"; // name of window class
HINSTANCE hInst; // instance handle
HWND hwnd; // handle of main window
HWND hProgWnd; // handle of progress bar
HANDLE hThrd = 0; // thread handle
unsigned long Tid; // thread ID
// Progress counters.
int percentdone = 0;
int oldpercentdone = 0;
// A small struct for passing info to dlstart().
struct ThrdInfo {
char *url; // pointer to URL string
int reload; // reload flag
HWND hPBStart; // handle of Start button
};
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPSTR lpszArgs, int nWinMode)
{
MSG msg;
WNDCLASSEX wcl;
INITCOMMONCONTROLSEX cc;
// Define a window class.
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.hInstance = hThisInst; // handle to this instance
wcl.lpszClassName = szWinName; // window class name
wcl.lpfnWndProc = WindowFunc; // window function
wcl.style = 0; // default style
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); // large icon
wcl.hIconSm = NULL; // use small version of large icon
wcl.hCursor = LoadCursor(NULL, IDC_ARROW); // cursor style
wcl.lpszMenuName = NULL; // no menu
wcl.cbClsExtra = 0; // no extras
wcl.cbWndExtra = 0;
wcl.hbrBackground = NULL; // not used
// Register the window class.
if(!RegisterClassEx(&wcl)) return 0;
// Create a main window that won't be visible.
hwnd = CreateWindow(
szWinName, // name of window class
"File Downloader", // title
0, // no style needed
0, 0, 0, 0, // no dimensions
NULL, // no parent window
NULL, // no menu
hThisInst, // instance handle
NULL // no additional arguments
);
hInst = hThisInst; // save the current instance handle
// Initialize the common controls. This is
// needed because of the progress bar.
cc.dwSize = sizeof(INITCOMMONCONTROLSEX);
cc.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&cc);
// Show the window minimized.
ShowWindow(hwnd, SW_SHOWMINIMIZED);
// Create the download dialog box.
DialogBox(hInst, "DLDB", hwnd, (DLGPROC) DialogFunc);
// Create the message loop.
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg); // translate keyboard messages
DispatchMessage(&msg); // return control to Windows
}
return msg.wParam;
}
// Window function.
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_DESTROY:
PostQuitMessage(0); // terminate the program
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
// Downloader Dialog function.
BOOL CALLBACK DialogFunc(HWND hdwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
// Here, url is initialized with a sample url for
// demonstration purposes only.
static char url[URL_BUF_SIZE] =
"http://www.osborne.com/products/0072226803/0072226803_code.zip";
static ThrdInfo ti;
switch(message) {
case WM_INITDIALOG:
// Initialize edit box with URL.
SetDlgItemText(hdwnd, IDD_EB1, url);
// Create progress bar.
hProgWnd = CreateWindow(PROGRESS_CLASS,
"",
WS_CHILD | WS_VISIBLE | WS_BORDER,
4, 64, 320, 12,
hdwnd, NULL, hInst, NULL);
// Set step increment to 1.
SendMessage(hProgWnd, PBM_SETSTEP, 1, 0);
return 1;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDCANCEL:
EndDialog(hdwnd, 0);
PostQuitMessage(0);
return 1;
case IDD_START: // start download
// Set position to zero.
SendMessage(hProgWnd, PBM_SETPOS, 0, 0);
// Get URL from edit box.
GetDlgItemText(hdwnd, IDD_EB1, url, URL_BUF_SIZE);
ti.url = url;
// Get reload status.
ti.reload = SendDlgItemMessage(hdwnd, IDD_CB1,
BM_GETCHECK, 0, 0);
// Get handle to Start button.
ti.hPBStart = GetDlgItem(hdwnd, IDD_START);
// Reset progress counters.
resetprogress();
// Start download thread.
if(!hThrd)
hThrd = (HANDLE) _beginthreadex(NULL, 0, dlstart,
(void *) &ti, 0, (unsigned *) &Tid);
return 1;
}
}
return 0;
}
// Show progress in the progress bar. This is called
// by the download() function.
void showprogress(unsigned long total,
unsigned long part) {
percentdone = (part*100)/total;
if(percentdone > oldpercentdone) {
for(int i= oldpercentdone; i < percentdone; i++) {
// Advance the progress bar.
SendMessage(hProgWnd, PBM_STEPIT, 0, 0);
}
oldpercentdone = percentdone;
}
}
// Reset the progress counters.
void resetprogress() {
percentdone = 0;
oldpercentdone = 0;
}
// Thread entry function that begins downloading.
unsigned __stdcall dlstart(void * param) {
ThrdInfo *tip = (ThrdInfo *) param;
// Disable Start button.
EnableWindow(tip->hPBStart, 0);
try {
if(tip->reload == BST_CHECKED)
Download::download(tip->url, true, showprogress);
else
Download::download(tip->url, false, showprogress);
} catch(DLExc exc) {
MessageBox(hwnd, exc.geterr(),
"Download Error", MB_OK);
}
// Enable Start button.
EnableWindow(tip->hPBStart, 1);
CloseHandle(hThrd); // close the thread handle
hThrd = 0; // set thread handle to inactive
return 0;
}
listing 5
// Resources for file downloader.
#include <windows.h>
#include "windl.h"
DLDB DIALOGEX 18, 18, 164, 100
CAPTION "Download a File"
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION |
WS_SYSMENU | WS_VISIBLE
{
PUSHBUTTON "Start", IDD_START, 42, 80, 30, 14
PUSHBUTTON "Cancel", IDCANCEL, 90, 80, 30, 14
CTEXT "Download Progress", IDD_TEXT2, 2, 40, 160, 12
CTEXT "Enter URL", IDD_TEXT1, 2, 16, 160, 12
EDITTEXT IDD_EB1, 2, 1, 160, 12, ES_LEFT | WS_CHILD |
WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
AUTOCHECKBOX "Reload", IDD_CB1, 62, 56, 36, 14
}
listing 6
#define IDD_START 100
#define IDD_CB1 200
#define IDD_EB1 300
#define IDD_TEXT1 401
#define IDD_TEXT2 402
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -