📄 downloadui.cpp
字号:
HBITMAP bmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_NOTE));
ImageList_AddMasked(m_noteImage, bmp, RGB(255,0,0));
DeleteObject(bmp);
m_progressBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_PROGRESS));
// Set progress bitmap for overall progress view
SetProp(m_hwndProgress,
"bitmap",
(HANDLE)m_progressBitmap);
ListView_SetImageList(m_hwndList, m_noteImage, LVSIL_SMALL);
// Add items to info view
LV_ITEM item;
uint32 i;
item.mask = LVIF_PARAM | LVIF_STATE;
item.state = 0;
item.stateMask = 0;
item.iSubItem = 0;
item.lParam = NULL;
for(i = 0; i < 7; i++)
{
item.iItem = i;
ListView_InsertItem(m_hwndInfo, &item);
}
// Add Items that are currently in the download manager
/*DownloadItem* dli = NULL;
i = 0;
while(dli = m_dlm->ItemAt(i++))
{
item.mask = LVIF_PARAM | LVIF_STATE;
item.state = 0;
item.stateMask = 0;
item.iItem = ListView_GetItemCount(m_hwndList);
item.iSubItem = 0;
item.lParam = (LPARAM)dli;
ListView_InsertItem(m_hwndList, &item);
if(dli->GetState() == kDownloadItemState_Queued ||
dli->GetState() == kDownloadItemState_Downloading)
{
// bring window into view
ShowWindow(m_hwnd, SW_SHOW);
SetForegroundWindow(m_hwnd);
}
}
UpdateOverallProgress();*/
m_handCursor = LoadCursor(g_hInstance, MAKEINTRESOURCE(IDC_HAND));
m_uiSemaphore->Signal();
return TRUE;
}
BOOL DownloadUI::DrawItem(int32 controlId, DRAWITEMSTRUCT* dis)
{
BOOL result = TRUE;
switch(controlId)
{
case IDC_FREETRACKS:
{
HFONT font = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
HPEN pen = CreatePen(PS_SOLID, 1, RGB(0,0,238));
HFONT oldFont;
HPEN oldPen;
char url[256];
GetWindowText(dis->hwndItem, url, sizeof(url));
oldFont = (HFONT)SelectObject(dis->hDC, font);
oldPen = (HPEN)SelectObject(dis->hDC, pen);
RECT clientRect;
GetClientRect(dis->hwndItem, &clientRect);
// Set the text background and foreground colors to the
// standard window colors
SetTextColor(dis->hDC, RGB(0,0,238));
SetBkColor(dis->hDC, GetSysColor(COLOR_3DFACE));
RECT rcClip = clientRect;
int height = 0;
height = DrawText(
dis->hDC,
url,
strlen(url),
&rcClip,
DT_LEFT|DT_WORDBREAK|DT_SINGLELINE|DT_CALCRECT);
height = DrawText(
dis->hDC,
url,
strlen(url),
&rcClip,
DT_LEFT|DT_WORDBREAK|DT_SINGLELINE);
rcClip.bottom = rcClip.top + height + 1;
MoveToEx(dis->hDC, rcClip.left, rcClip.bottom, NULL);
LineTo(dis->hDC, rcClip.right, rcClip.bottom);
SelectObject(dis->hDC, oldFont);
SelectObject(dis->hDC, oldPen);
m_urlRect = rcClip;
DeleteObject(font);
DeleteObject(pen);
break;
}
case IDC_OVERALL_PROGRESS:
{
HFONT font = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
HFONT oldFont;
oldFont = (HFONT)SelectObject(dis->hDC, font);
if(m_progressBitmap)
{
string displayString;
ostringstream ost;
float total = m_totalBytes;
float recvd = m_doneBytes;
uint32 percent;
RECT clientRect;
GetClientRect(m_hwndProgress, &clientRect);
//HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
//FillRect(dis->hDC, &rcClip, brush);
//DeleteObject(brush);
// Set the text background and foreground colors to the
// standard window colors
SetTextColor(dis->hDC, GetSysColor(COLOR_WINDOWTEXT));
SetBkColor(dis->hDC, GetSysColor(COLOR_3DFACE));
ost.precision(2);
ost.flags(ios_base::fixed);
percent = (uint32)recvd/total*100;
ost << percent << "%, " << m_doneItems << " of " << m_totalItems << " items (";
if(total >= 1048576)
{
total /= 1048576;
recvd /= 1048576;
ost << recvd << " of " << total << " MB) ";
}
else if(total >= 1024)
{
total /= 1024;
recvd /= 1024;
ost << recvd << " of " << total << " KB)";
}
else
{
ost << m_doneBytes << " of " << m_totalBytes << " Bytes)";
}
displayString = "Overall Progress:";
SIZE stringSize;
RECT rcClip = clientRect;
GetTextExtentPoint32(dis->hDC, displayString.c_str(),
displayString.size(), &stringSize);
rcClip.left += 3;
rcClip.top += ((rcClip.bottom - rcClip.top) - stringSize.cy)/2;
rcClip.bottom = rcClip.top + stringSize.cy;
ExtTextOut( dis->hDC,
rcClip.left, rcClip.top,
ETO_CLIPPED | ETO_OPAQUE,
&rcClip,
displayString.c_str(),
displayString.size(),
NULL);
rcClip.left += stringSize.cx + kElementPadding;
displayString = ost.str();
GetTextExtentPoint32(dis->hDC, displayString.c_str(),
displayString.size(), &stringSize);
int32 progressWidth = 100;
int32 bmpWidth = (float)(progressWidth - 3) * (float)percent/(float)100;
int32 count = bmpWidth/(kProgressWidth);
int32 remainder = bmpWidth%(kProgressWidth);
HDC memDC = CreateCompatibleDC(dis->hDC);
HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, m_progressBitmap);
RECT progressRect = clientRect;
progressRect.left = rcClip.left;
progressRect.top += ((clientRect.bottom - clientRect.top) - kProgressHeight)/2 - 1;
progressRect.bottom = progressRect.top + kProgressHeight + 2;
progressRect.right = progressRect.left + progressWidth;
DrawEdge(dis->hDC, &progressRect, EDGE_SUNKEN, BF_RECT);
uint32 i = 0;
for(i = 0; i< count; i++)
{
BitBlt(dis->hDC, progressRect.left + 2 + i*kProgressWidth, progressRect.top + 2, kProgressWidth, kProgressHeight,
memDC, 0, 0, SRCCOPY);
}
if(remainder)
{
BitBlt(dis->hDC, progressRect.left + 2 + i*kProgressWidth, progressRect.top + 2, remainder, kProgressHeight,
memDC, 0, 0, SRCCOPY);
}
SelectObject(memDC, oldBitmap);
DeleteDC(memDC);
uint32 pad = 0;
if(progressWidth)
pad = (progressWidth + kElementPadding);
rcClip.left += pad;
ExtTextOut( dis->hDC,
rcClip.left, rcClip.top,
ETO_CLIPPED | ETO_OPAQUE,
&rcClip,
displayString.c_str(),
displayString.size(),
NULL);
}
SelectObject(dis->hDC, oldFont);
DeleteObject(font);
break;
}
case IDC_LIST:
{
uint32 uiFlags = ILD_TRANSPARENT;
RECT rcClip;
HIMAGELIST himl;
int32 cxImage = 0, cyImage = 0;
// Get Image List
himl = ListView_GetImageList(dis->hwndItem, LVSIL_SMALL);
ImageList_GetIconSize(himl, &cxImage, &cyImage);
// Check to see if this item is selected
if(dis->itemState & ODS_SELECTED)
{
// Set the text background and foreground colors
SetTextColor(dis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
SetBkColor(dis->hDC, GetSysColor(COLOR_HIGHLIGHT));
// Also add the ILD_BLEND50 so the images come out selected
//uiFlags |= ILD_BLEND50;
}
else
{
// Set the text background and foreground colors to the
// standard window colors
SetTextColor(dis->hDC, GetSysColor(COLOR_WINDOWTEXT));
SetBkColor(dis->hDC, GetSysColor(COLOR_WINDOW));
}
rcClip = dis->rcItem;
//LV_ITEM* item = (LV_ITEM*)dis->itemData;
DownloadItem* dli = (DownloadItem*)dis->itemData;
string displayString;
if(dli->GetMetaData().Title().size())
displayString = dli->GetMetaData().Title();
else
displayString = dli->DestinationFile();
// Check to see if the string fits in the clip rect.
// If not, truncate the string and add "...".
CalcStringEllipsis(dis->hDC,
displayString,
ListView_GetColumnWidth(m_hwndList, 0) - (cxImage + 1));
ExtTextOut( dis->hDC,
rcClip.left + cxImage + 1, rcClip.top + 1,
ETO_CLIPPED | ETO_OPAQUE,
&rcClip,
displayString.c_str(),
displayString.size(),
NULL);
// draw the icon
if (himl)
{
ImageList_Draw( himl, 0, dis->hDC,
rcClip.left, rcClip.top,
uiFlags);
}
// draw the progress column
rcClip.left += ListView_GetColumnWidth(m_hwndList, 0);
int32 progressWidth = 0;
switch(dli->GetState())
{
case kDownloadItemState_Queued:
{
if(!(dis->itemState & ODS_SELECTED))
SetTextColor(dis->hDC, RGB(0, 127, 0));
ostringstream ost;
float total;
ost.precision(2);
ost.flags(ios_base::fixed);
total = dli->GetTotalBytes();
if(total >= 1048576)
{
total /= 1048576;
ost << "Queued (" << total << " MB)";
}
else if(total >= 1024)
{
total /= 1024;
ost << "Queued (" << total << " KB)";
}
else
{
ost << "Queued (" << dli->GetTotalBytes() << " Bytes)";
}
displayString = ost.str();
break;
}
case kDownloadItemState_Downloading:
{
if(!(dis->itemState & ODS_SELECTED))
SetTextColor(dis->hDC, RGB(0, 0, 192));
ostringstream ost;
float total;
float recvd;
uint32 percent;
ost.precision(2);
ost.flags(ios_base::fixed);
total = dli->GetTotalBytes();
recvd = dli->GetBytesReceived();
percent= (uint32)recvd/total*100;
if(total >= 1048576)
{
total /= 1048576;
recvd /= 1048576;
ost << percent << "% (" << recvd << " of "<< total << " MB) ";
}
else if(total >= 1024)
{
total /= 1024;
recvd /= 1024;
ost << percent << "% ("<< recvd << " of "<< total << " KB)";
}
else
{
ost << percent << "% (" << dli->GetBytesReceived() << " of " <<
dli->GetTotalBytes() << " Bytes)";
}
displayString = ost.str();
SIZE stringSize;
GetTextExtentPoint32(dis->hDC, displayString.c_str(),
displayString.size(), &stringSize);
int32 leftoverWidth = ListView_GetColumnWidth(m_hwndList, 1) - (stringSize.cx + kTotalPadding);
// do we have room to show a progress bar?
if(leftoverWidth - kMinProgressWidth > 0)
{
progressWidth = leftoverWidth; // padding on ends and between elements
int32 bmpWidth = (float)(progressWidth - 3) * (float)percent/(float)100;
int32 count = bmpWidth/(kProgressWidth);
int32 remainder = bmpWidth%(kProgressWidth);
//ostringstream debug;
//debug << "bmpWidth: " << bmpWidth << endl << "progressWidth: " << progressWidth << endl;
//OutputDebugString(debug.str().c_str());
HDC memDC = CreateCompatibleDC(dis->hDC);
SelectObject(memDC, m_progressBitmap);
rcClip.left += kPrePadding;
RECT progressRect = rcClip;
progressRect.top += ((rcClip.bottom - rcClip.top) - kProgressHeight)/2 - 1;
progressRect.bottom = progressRect.top + kProgressHeight + 2;
progressRect.right = progressRect.left + progressWidth;
if(dis->itemState & ODS_SELECTED)
{
HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
FillRect(dis->hDC, &progressRect, brush);
DeleteObject(brush);
}
DrawEdge(dis->hDC, &progressRect, EDGE_SUNKEN, BF_RECT);
uint32 i = 0;
for(i = 0; i< count; i++)
{
BitBlt(dis->hDC, progressRect.left + 2 + i*kProgressWidth, progressRect.top + 2, kProgressWidth, kProgressHeight,
memDC, 0, 0, SRCCOPY);
}
if(remainder)
{
BitBlt(dis->hDC, progressRect.left + 2 + i*kProgressWidth, progressRect.top + 2, remainder, kProgressHeight,
memDC, 0, 0, SRCCOPY);
}
DeleteDC(memDC);
}
break;
}
case kDownloadItemState_Cancelled:
{
if(!(dis->itemState & ODS_SELECTED))
SetTextColor(dis->hDC, RGB(192, 0, 0));
ostringstream ost;
ost << "Cancelled";
displayString = ost.str();
break;
}
case kDownloadItemState_Paused:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -