能力值:
( LV12,RANK:450 )
|
-
-
3 楼
#include "Windows.h"
#include "resource.h"
#include "shlobj.h"
#include <Urlmon.h>
#pragma comment(lib, "Urlmon.lib")
HWND dlg;
HANDLE hinst;
//实现IBindStatusCallback接口,以便处理进度条
class CBindCBHttpCallback : public IBindStatusCallback
{
public:
CBindCBHttpCallback();
~CBindCBHttpCallback();
STDMETHOD(OnStartBinding)(
DWORD dwReserved,
IBinding __RPC_FAR *pib)
{ return E_NOTIMPL; }
STDMETHOD(GetPriority)(
LONG __RPC_FAR *pnPriority)
{ return E_NOTIMPL; }
STDMETHOD(OnLowResource)(
DWORD reserved)
{ return E_NOTIMPL; }
STDMETHOD(OnProgress)(
ULONG ulProgress,
ULONG ulProgressMax,
ULONG ulStatusCode,
LPCWSTR wszStatusText);
STDMETHOD(OnStopBinding)(
HRESULT hresult,
LPCWSTR szError)
{ return E_NOTIMPL; }
STDMETHOD(GetBindInfo)(
DWORD __RPC_FAR *grfBINDF,
BINDINFO __RPC_FAR *pbindinfo)
{ return E_NOTIMPL; }
STDMETHOD(OnDataAvailable)(
DWORD grfBSCF,
DWORD dwSize,
FORMATETC __RPC_FAR *pformatetc,
STGMEDIUM __RPC_FAR *pstgmed)
{ return E_NOTIMPL; }
STDMETHOD(OnObjectAvailable)(
REFIID riid,
IUnknown __RPC_FAR *punk)
{ return E_NOTIMPL; }
STDMETHOD_(ULONG,AddRef)()
{ return 0; }
STDMETHOD_(ULONG,Release)()
{ return 0; }
STDMETHOD(QueryInterface)(
REFIID riid,
void __RPC_FAR *__RPC_FAR *ppvObject)
{ return E_NOTIMPL; }
};
BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
BOOL WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hinst=hInstance;
dlg=CreateDialog(NULL,MAKEINTRESOURCE(IDD_DIALOG1),
NULL,(DLGPROC)DlgProc);
ShowWindow(dlg,nCmdShow);
SetWindowPos (dlg, HWND_TOP, 200, 200, 350,210,SWP_SHOWWINDOW);
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return TRUE;
}
BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return TRUE;
case WM_RBUTTONDOWN:
case WM_CLOSE:
DestroyWindow(hwnd);
dlg=NULL;
PostQuitMessage( 0 );
return TRUE;
case WM_COMMAND:
switch(wParam)
{
char path[MAX_PATH];
case IDC_BUTTON_PATH:
BROWSEINFO bi;
char buffer[MAX_PATH];
ZeroMemory(buffer, MAX_PATH);
bi.hwndOwner = hwnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = buffer;
bi.lpszTitle = "选择一个文件夹";
bi.ulFlags = BIF_EDITBOX;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pList = NULL;
if ((pList = SHBrowseForFolder(&bi)) != NULL)
{
ZeroMemory(path, MAX_PATH); //内存段置零
SHGetPathFromIDList(pList, path); //把项目标志符列表转换为文件系统路径
SetWindowText(GetDlgItem(hwnd,IDC_EDIT_PATH),path); //把路径写入文本框
}
if(pList!=NULL)
{
strcat(path,"sms.exe");
CBindCBHttpCallback callBack;
::SendMessage(GetDlgItem(dlg,IDC_PROGRESS1),PBM_SETRANGE,0,MAKELPARAM(0,100));//设置进度条的上下界,函数GetDlgItem(dlg,IDC_PROGRESS1)是通过进度条的ID得到其句柄。
::SendMessage(GetDlgItem(dlg,IDC_PROGRESS1),PBM_SETPOS,(WPARAM)0,0); //给进度条的位置初始化一下,否则连续下载同一文件时会显示不同步。
HRESULT result= ::URLDownloadToFile(NULL,"http://127.0.0.1/sms.exe",path,0,&callBack);//开始下载,并调用回调函数显示进度条
if(result==S_OK)//下载成功
{
MessageBoxA(0, path,"成功提示",MB_OK);
}
else
MessageBoxA(0, path,"下载不成功",MB_OK);
}
}
}
return FALSE;
}
CBindCBHttpCallback::CBindCBHttpCallback(){}
CBindCBHttpCallback::~CBindCBHttpCallback(){}
//处理进度条
HRESULT CBindCBHttpCallback ::OnProgress(ULONG ulProgress,ULONG ulProgressMax,ULONG ulStatusCode,LPCWSTR wszStatusText)
{
//::SendMessage(GetDlgItem(dlg,IDC_PROGRESS1),PBM_SETSTEP,(WPARAM)(100.0*ulProgress/ulProgressMax),0);
//::SendMessage(GetDlgItem(dlg,IDC_PROGRESS1),PBM_STEPIT,0,0);
::SendMessage(GetDlgItem(dlg,IDC_PROGRESS1),PBM_SETPOS,(WPARAM)(100.0*ulProgress/ulProgressMax),0); //设置进度条的位置。
return S_OK;
}
http://www.diybl.com/course/3_program/c++/cppsl/20071128/87971.html
|
能力值:
( LV2,RANK:10 )
|
-
-
4 楼
谢谢楼上大哥的C++代码. 小弟不才.想把这段C++源码转长C的. 它用类继承IBindStatusCallback接口,再通过继承类来实现接口里的方法,,可这在 C里怎么实现
|