開発環境 |
Microsoft Visual C++ 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
空の CLR プロジェクト |
プロジェクト名 |
ClrBrowser |
参考
ClrBrowser.cpp
//#pragma comment(linker, "/subsystem:windows")
//#pragma comment(linker, "/entry:main")
// .NET参照
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
// 名前空間
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Net;
using namespace System::Threading;
using namespace System::Windows;
using namespace System::Windows::Forms;
public ref class Form1 : public Form
{
public:
Form1(void)
{
InitializeComponent();
}
private:
Panel^ panel;
TextBox^ url;
Button^ links;
WebBrowser^ wb;
void InitializeComponent(void)
{
panel = gcnew Panel();
url = gcnew TextBox();
links = gcnew Button();
wb = gcnew WebBrowser();
panel->SuspendLayout();
SuspendLayout();
// panel
panel->Controls->Add(url);
panel->Controls->Add(links);
panel->Height = 32;
// url
url->Width = 240;
url->Top = (panel->Height - url->Height) / 2;
url->Text = L"http://www.google.co.jp/";
url->KeyDown += gcnew KeyEventHandler(this, &Form1::UrlKeyDown);
// links
links->Left = url->Bounds.Right + 4;
links->Top = (panel->Height - links->Height) / 2;
links->Text = L"画像保存";
links->Click += gcnew EventHandler(this, &Form1::LinksClick);
// wb
wb->DocumentCompleted += gcnew WebBrowserDocumentCompletedEventHandler(
this, &Form1::DocCompleted);
// Form1
ClientSize = Drawing::Size(800, 600);
Controls->Add(panel);
Controls->Add(wb);
Text = L"Form1";
panel->ResumeLayout(false);
panel->PerformLayout();
ResumeLayout(false);
PerformLayout();
}
protected:
virtual void OnResize(EventArgs^ e) override
{
int width = ClientSize.Width;
panel->Width = width;
wb->Width = width;
wb->Top = panel->Top + panel->Height;
wb->Height = ClientSize.Height - panel->Height;
}
private:
void UrlKeyDown(Object^ sender, KeyEventArgs^ e)
{
if (e->KeyCode == Keys::Enter) {
wb->Navigate(url->Text);
}
}
void DocCompleted(Object^ sender, WebBrowserDocumentCompletedEventArgs^ e)
{
url->Text = wb->Document->Url->ToString();
}
void LinksClick(Object^ sender, EventArgs^ e)
{
array<String^>^ arr = {L".jpg", L".gif", L".png", L".jpeg"};
List<String^>^ pict = gcnew List<String^>(arr);
List<String^>^ list = gcnew List<String^>;
HtmlElementCollection^ hec = wb->Document->Links;
for each (HtmlElement^ element in hec) {
String^ url = element->GetAttribute(L"href");
if (url != nullptr) {
String^ ext = Path::GetExtension(url);
if (pict->Contains(ext)) {
if (!list->Contains(url)) {
list->Add(url);
}
}
}
}
WebClient^ wc = gcnew WebClient;
for each (String^ address in list) {
String^ fileName = L"C:\\picture\\" + Path::GetFileName(address);
Console::WriteLine(address);
if (!File::Exists(fileName)) {
wc->DownloadFile(address, fileName);
Thread::Sleep(1000);
}
}
Console::WriteLine(L"完了");
}
};
[STAThread] // Single-Threaded Apartment
int main()
{
// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// メイン ウィンドウを作成して、実行します
Application::Run(gcnew Form1());
return 0;
}
最終更新:2012年09月29日 21:25