DownlaodFile & ProgressBar

0

Witam, Potrzebuję napisać prosty program, który będzie wyświetlał stan pobieranego pliku w ProgressBar
Korzystam z Visual C++ (Windows Form). Niestety szukałem wszędzie i nie znalazłem nic co działało by pod Visual C++ ;/
Tak to mniej więcej ma wygalać.

user image

Plik może być pobierany dowolną funkcją np.

URLDownloadToFile( NULL, L"http://website.pl/plik.rar", L"C:\\Program Files\\plik.rar", 0, NULL );	

Problem w tym że nie mam pojęcia jak zsynchronizować progressBar z jakoś funkcją do pobierania pliku.
Proszę o pomoc (Tylko tych, którzy wiedzą jak rozwiązać ten problem) :)

0

Hmm, gotowego rozwiązania nie podam, ale wydaje mi się, że przez odpowiedni request dostaniesz content-length,
a to ile zostało pobrane.... zależy z czego korzystasz do łączenia się z siecią.

0

W opisie funkcji URLDownloadToFile jest wskaznik do interfejsu IBindStatusCallback w ktorym to jest metoda OnProgress

teraz wystarczy tylko zaimplementowac taki interfejs i wywolac funkcje podajac do niego wskaznik :-)

http://msdn.microsoft.com/en-us/library/ms775123%28v=vs.85%29.aspx

tu masz chyba gotowca........

http://www.codeproject.com/KB/IP/urlfile.aspx

0

No, ale on znowu pisze w C++/CLI, może w łatwy sposób użyć .NETowych klas do tego.

0
Rev.pl napisał(a)

No, ale on znowu pisze w C++/CLI, może w łatwy sposób użyć .NETowych klas do tego.

: D Jak można prościej to wyjaśnij mi jakich funkcji mam użyć. Może jakiś gotowy przykład ? :)

0

Odpowiadam na tyle ile sie znam, ma juz chociaz jakies pojecie gdzie szukac i czego..

0
kwasek2000 napisał(a)

Odpowiadam na tyle ile sie znam, ma juz chociaz jakies pojecie gdzie szukac i czego..

A czy jest ktoś na tym forum kto wie więcej nic on ?
Proszę o pomoc...

0

Doszedłem do tego sam :)
Jest tylko jedno małe ale.
Jak z pliku download.h zaktualizować progressbar?

Kod programu:
~Form1

#pragma once
#include "stdafx.h"
#include "download.h"

namespace test {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::ProgressBar^  progressBar1;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(172, 209);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(246, 25);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Download";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// progressBar1
			// 
			this->progressBar1->Location = System::Drawing::Point(85, 115);
			this->progressBar1->Name = L"progressBar1";
			this->progressBar1->Size = System::Drawing::Size(483, 26);
			this->progressBar1->TabIndex = 1;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(641, 304);
			this->Controls->Add(this->progressBar1);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
		
				MyCallback pCallback;
				URLDownloadToFile(NULL, L"http://www.wp.pl/i/ivar/layout/200812/WP.gif", L"c:\\same.png", 0, &pCallback);

			 }
	};
}




 

~ download.h

#include "stdafx.h"
#include <urlmon.h>
#include <iostream>

#pragma comment (lib, "urlmon.lib")
using namespace std;

class MyCallback : public IBindStatusCallback
{
public:
MyCallback() {}

~MyCallback() { }

// This one is called by URLDownloadToFile
STDMETHOD(OnProgress)(/* [in] */ ULONG ulProgress, /* [in] */ ULONG ulProgressMax, /* [in] */ ULONG ulStatusCode, /* [in] */ LPCWSTR wszStatusText)
{
#// ~~~~~~ <--- Jak zrobić aby z tego miejsca aktualizował się Progressbar cos w tym stylu (this->progressBar1->Value =  ulProgres;)
//cout << "Downloaded " << ulProgress << " of " << ulProgressMax << " byte(s), " << " Status Code = " << ulStatusCode << endl;
return S_OK;
}

// The rest don't do anything...
STDMETHOD(OnStartBinding)(/* [in] */ DWORD dwReserved, /* [in] */ IBinding __RPC_FAR *pib)
{ return E_NOTIMPL; }

STDMETHOD(GetPriority)(/* [out] */ LONG __RPC_FAR *pnPriority)
{ return E_NOTIMPL; }

STDMETHOD(OnLowResource)(/* [in] */ DWORD reserved)
{ return E_NOTIMPL; }

STDMETHOD(OnStopBinding)(/* [in] */ HRESULT hresult, /* [unique][in] */ LPCWSTR szError)
{ return E_NOTIMPL; }

STDMETHOD(GetBindInfo)(/* [out] */ DWORD __RPC_FAR *grfBINDF, /* [unique][out][in] */ BINDINFO __RPC_FAR *pbindinfo)
{ return E_NOTIMPL; }

STDMETHOD(OnDataAvailable)(/* [in] */ DWORD grfBSCF, /* [in] */ DWORD dwSize, /* [in] */ FORMATETC __RPC_FAR *pformatetc, /* [in] */ STGMEDIUM __RPC_FAR *pstgmed)
{ return E_NOTIMPL; }

STDMETHOD(OnObjectAvailable)(/* [in] */ REFIID riid, /* [iid_is][in] */ IUnknown __RPC_FAR *punk)
{ return E_NOTIMPL; }

// IUnknown stuff
STDMETHOD_(ULONG,AddRef)()
{ return 0; }

STDMETHOD_(ULONG,Release)()
{ return 0; }

STDMETHOD(QueryInterface)(/* [in] */ REFIID riid, /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
{ return E_NOTIMPL; }
};


 

Proszę o pomoc :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1