⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cpp

📁 用C语言写的浏览器源码 供学习和交流使用
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
#include "cpl.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
WideString URL;
long DownLoadPro;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ComboBox1KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if(Key==VK_RETURN)
    Go();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TRegistryIniFile *Reg;
  TStrings *str;
  char *lasturls;
  Reg=new TRegistryIniFile("");
  str=new TStringList;
  if(Reg->RegIniFile->OpenKey("Software\\Microsoft\\Internet Explorer\\TypedURLs",false))
  {
    Reg->ReadSectionValues("",str);
    for(int i=0;i<str->Count;i++)
    {
      lasturls=AnsiStrPos(str->Strings[i].c_str(),"=")+1;
      ComboBox1->Items->Add(lasturls);
    }
  }
  URL.Empty();
  CppWebBrowser1->GoHome();
  delete Reg;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BackButtonClick(TObject *Sender)
{
  CppWebBrowser1->GoBack();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ForwardButtonClick(TObject *Sender)
{
  CppWebBrowser1->GoForward();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StopButtonClick(TObject *Sender)
{
  StopButton->Down!=StopButton->Down;
  if(StopButton->Down)
    CppWebBrowser1->Stop();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::RefreshButtonClick(TObject *Sender)
{
  CppWebBrowser1->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::HomeButtonClick(TObject *Sender)
{
  CppWebBrowser1->GoHome();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::X1Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::F2Click(TObject *Sender)
{
  if(OpenDialog1->Execute())
  {
    URL=(OpenDialog1->FileName);
    CppWebBrowser1->Navigate((wchar_t *)URL);
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
  Go();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Go()
{
  URL=ComboBox1->Text;
  CppWebBrowser1->Navigate(URL);
  ComboBox1->Items->Add(ComboBox1->Text);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CppWebBrowser1DownloadComplete(TObject *Sender)
{
  CppWebBrowser1->Cursor=crDefault;
  StatusBar1->Panels->Items[1]->Style=psText;
  StatusBar1->Panels->Items[1]->Text="完成";
  StatusBar1->Panels->Items[0]->Text=CppWebBrowser1->LocationName;
  StatusBar1->Panels->Items[2]->Text=CppWebBrowser1->Type;
  Form1->Caption="我的浏览器——"+CppWebBrowser1->LocationName;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StatusBar1DrawPanel(TStatusBar *StatusBar,
      TStatusPanel *Panel, const TRect &Rect)
{
    TRect TempRect;

    TempRect.Left=405;
    TempRect.Top=Rect.Top+3;
    TempRect.Right=TempRect.Left+DownLoadPro-10;
    TempRect.Bottom=Rect.Bottom-2;
    StatusBar1->Canvas->Brush->Color=clBlue;
    StatusBar1->Canvas->FillRect(TempRect);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::CppWebBrowser1ProgressChange(TObject *Sender,
      long Progress, long ProgressMax)
{
  TRect Rect;
  long Precent,ProMax;

  Precent=Progress;
  ProMax=ProgressMax;
  if(ProMax!=0)
  {
    DownLoadPro=Precent*100/ProMax;
    StatusBar1->Invalidate();
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CppWebBrowser1DownloadBegin(TObject *Sender)
{
  StatusBar1->Panels->Items[1]->Style=psOwnerDraw;
  CppWebBrowser1->Cursor=crAppStart;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Internet1Click(TObject *Sender)
{
  char *syspath,*exename;
  HINSTANCE hInst;

  syspath=(char *)malloc(255);
  exename=(char *)malloc(255);
  StrCopy(exename,"\\InetCpl.cpl");
  GetSystemDirectory(syspath,255);
  strcat(syspath,exename);
  if((hInst=LoadLibrary(syspath))!=NULL)
  {
    if(((FARPROC &)CPLApplet=GetProcAddress(hInst,"CPlApplet"))!=NULL)
      CPLApplet(Handle,CPL_DBLCLK,0,0);
    FreeLibrary(hInst);
  }
//  free(syspath);
//  free(exename);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -