fmain.cpp
来自「使用TCppWebBrowser控件实现的浏览器」· C++ 代码 · 共 1,381 行 · 第 1/3 页
CPP
1,381 行
if(hFind)
{
do
{
if(ffd.cFileName[0] != '.')
{
// 检查是否超出了最大的子项数目
if(ParentMenu->Count >= MaxCount)
{
Item = new TMenuItem(ParentMenu);
Item->Caption = "更多...";
Item->ImageIndex = FoldImageIndex;
Item->AutoHotkeys = maManual;
ParentMenu->Add(Item);
ParentMenu = Item;
}
//
FileName = Folder + ffd.cFileName;
// 读取文件夹
if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
Item = new TMenuItem(ParentMenu);
Item->Caption = ffd.cFileName;
Item->ImageIndex = FoldImageIndex;
ParentMenu->Add(Item);
if(ParentMenu->Count > 3)
{
// 把菜单项移动到第一个URL前
// 或者排序
TMenuItem *FirstURL;
int startindex = 0;
if (ParentMenu == mFav)
{
startindex = 3;
}
for(int i=startindex;i<ParentMenu->Count;i++)
{
FirstURL = ParentMenu->Items[i];
// 排序
if(FirstURL->ImageIndex == FoldImageIndex)
{
if(Item->Caption.AnsiCompareIC(FirstURL->Caption) < 0)
{
Item->MenuIndex = i;
break;
}
}
// 把菜单项移动到第一个URL前
else if(FirstURL->ImageIndex == UrlShortCutImageIndex)
{
Item->MenuIndex = i;
break;
}
}
}
//
GetFavoriesFolder(Item,FileName,MaxCount);
}
// 读取快捷方式
else
{
//
int Len = strlen(ffd.cFileName);
if( (ffd.cFileName[Len-3] == 'U' || ffd.cFileName[Len-3] == 'u')
&& (ffd.cFileName[Len-2] == 'R' || ffd.cFileName[Len-2] == 'r')
&& (ffd.cFileName[Len-1] == 'L' || ffd.cFileName[Len-1] == 'l') )
{
GetPrivateProfileString(
Section_InternetShortcut,
key_InternetShortcut,
NULL,
Buffer,
MAX_PATH,
FileName.c_str());
Item = new TMenuItem(ParentMenu);
ffd.cFileName[Len-4] = '\0';
Item->Caption = ffd.cFileName;
Item->Hint = Buffer;
Item->ImageIndex = UrlShortCutImageIndex;
Item->OnClick = mFavClick;
ParentMenu->Add(Item);
// 排序
TMenuItem *MenuItem;
int startindex = 0;
if (ParentMenu == mFav)
{
startindex = 3;
}
for(int i=startindex;i<ParentMenu->Count;i++)
{
MenuItem = ParentMenu->Items[i];
// 排序
if(MenuItem->ImageIndex == UrlShortCutImageIndex)
{
if(Item->Caption.AnsiCompareIC(MenuItem->Caption) < 0)
{
Item->MenuIndex = i;
break;
}
}
}
}
}
}
}while(FindNextFile(hFind,&ffd));
FindClose(hFind);
}
// 如果没有子条目,添加空白条目
if (0 == ParentMenu->Count)
{
Item = new TMenuItem(ParentMenu);
Item->Caption = "(空)";
Item->ImageIndex = -1;
Item->Enabled = false;
ParentMenu->Add(Item);
}
}
//---------------------------------------------------------------------------
void TfmMain::GetFavories(void)
{
while(mFav->Count>3)
{
mFav->Delete(3);
}
char szFavoriteFolder[MAX_PATH];
if(!SHGetSpecialFolderPath(Handle,szFavoriteFolder,CSIDL_FAVORITES,false))
{
// 不能取得收藏夹的目录
return;
}
int H = Screen->Height - 50;
int MaxCount = H/18 - 3;
GetFavoriesFolder(mFav,szFavoriteFolder,MaxCount);
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::mFavClick(TObject *Sender)
{
TMenuItem *Item = static_cast<TMenuItem *>(Sender);
if(Item)
{
ComboBoxAddress->Text = Item->Hint;
ComboBoxAddress->Hint = ComboBoxAddress->Text;
ActionVisitExecute(ActionVisit);
}
}
//---------------------------------------------------------------------------
// 调用ShellAbout函数显示关于对话框
/*
int ShellAbout (
HWND hWnd, // 父窗口的句柄,可以是NULL
LPCTSTR szApp, //
LPCTSTR szOtherStuff,
HICON hIcon
);
*/
void __fastcall TfmMain::ActionAboutExecute(
TObject *Sender)
{
const char *szApp = "多窗口Web浏览器";
const char *szOtherStuff = "设计:行舟 http://www.xingzhou.com";
HICON hIcon = Application->Icon->Handle;
ShellAbout(Handle,szApp,szOtherStuff,hIcon);
}
//---------------------------------------------------------------------------
void TfmMain::OpenByParameter(char *szParameter)
{
if(*szParameter)
{
CreateBrowserWindow(szParameter);
}
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::FormActivate(TObject *Sender)
{
this->OnActivate = NULL;
if(szAppCmdLine[0]) // 含有参数
{
OpenByParameter(szAppCmdLine);
}
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::ApplicationEvents1Exception(
TObject *Sender, Exception *E)
{
Screen->Cursor=crDefault;
Application->ShowException(E);
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::FormCloseQuery(TObject *Sender,
bool &CanClose)
{
if(!m_IsCloseAllNow && (this->MDIChildCount > 1))
{
CanClose = false;
// 只关闭当前活动的浏览窗口
TfmChildwin *fBrowser = GetActiveBrowserWindow();
if(fBrowser)
{
fBrowser->Close();
}
}else
{
Hide();
}
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::mFontSizeClick(TObject *Sender)
{
TfmChildwin *fBrowser = GetActiveBrowserWindow();
if(fBrowser)
{
TMenuItem *Item = static_cast<TMenuItem *>(Sender);
fBrowser->DOC_FontSize = Item->Tag;
Item->Checked = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::ApplicationEvents1Message(
tagMSG &Msg, bool &Handled)
{
static const char *web_browser_classname = "Internet Explorer_Server";
static int classname_len = strlen(web_browser_classname);
static char classname[MAX_PATH];
if(m_IsCloseAllNow)return; // 正在退出,不作处理
if(0 == this->MDIChildCount)return;
// 检查是否本窗口
HWND hParent;
HWND hWnd = GetParent(Msg.hwnd); // Shell DocObject_View
while(hWnd != NULL)
{
hParent = hWnd;
hWnd = GetParent(hParent);
}
if(hParent != Handle)
{
return;
}
TfmChildwin *fBrowser = GetActiveBrowserWindow();
if(fBrowser )
{
if(TabWindow->Handle == Msg.hwnd && WM_LBUTTONDBLCLK == Msg.message)
{
// TabControl控件的双击事件,关闭当前页面
Handled = true;
delete fBrowser;
return;
}
if(GetClassName(Msg.hwnd,classname,MAX_PATH) == classname_len)
{
// 判断是否TCppWebBrowser控件
if(0 == StrCmp(web_browser_classname,classname) && WM_KEYDOWN == Msg.message)
{
if(Msg.wParam == VK_RETURN)
{
if(!ComboBoxAddress->Focused())
{
// 处理回车键,模拟回车键
/* IOleInPlaceActiveObject *Ole ;
Application->ComObject->QueryInterface(IID_IOleInPlaceActiveObject,(void **)&Ole);
if(Ole)
{
if(S_OK == Ole->TranslateAccelerator(&Msg))
{
Handled = true;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}*/
fBrowser->SendSubmitToDocument();
Handled = true;
return;
}
}else
if(Msg.wParam == VK_TAB)
{
if(this->MDIChildCount > 1)
{
int CtrlDown=GetKeyState(VK_CONTROL);
CtrlDown=CtrlDown&0x80;
if(CtrlDown)
{
int Indx = TabWindow->TabIndex + 1;
if (Indx >= TabWindow->Tabs->Count)
{
Indx = 0;
}
TabWindow->TabIndex = Indx;
Handled=true;
return;
}
}
}else
if(Msg.wParam == VK_ESCAPE)
{
if(ActionFullScreen->Checked)
{
ActionFullScreen->Checked = false;
SetFullScreen(false);
Handled=true;
return;
}
}
}
}
}
}
//---------------------------------------------------------------------------
// 先创建一个GUID(可以使用VS6的guidgen.exe实用工具
static const char *guid_string = "{35D77946-4BA8-4e7d-9E98-0271E0A328B7}";
static const char *shortcut_name = "我的浏览器" ;
static const char *DesktopNamespaceKey =
"SOFTWARE\\Microsoft\\Windows\\"
"CurrentVersion\\Explorer\\Desktop\\NameSpace";
// 在桌面创建类似于IE的快捷方式
bool CreateShortCutLikeIE(void)
{
bool boolCreateSucessed = true;
AnsiString GuidKey;
GuidKey.sprintf("CLSID\\%s",guid_string);
AnsiString DefaultIconKey = GuidKey + "\\DefaultIcon";
AnsiString ShellCommandKey = GuidKey + "\\Shell\\Open\\Command";
AnsiString PropertiesMenuKey = GuidKey + "\\Shell\\属性(&R)\\Command";
AnsiString AttributeKey = GuidKey + "\\ShellFolder";
TRegistry *Reg = new TRegistry;
try
{
Reg->RootKey = HKEY_CLASSES_ROOT;
// 快捷方式的名称
if(!Reg->OpenKey(GuidKey,true))
{
boolCreateSucessed = false; // 失败
return false;
}
Reg->WriteString("",shortcut_name);
Reg->CloseKey();
// 快捷方式的图标
if(!Reg->OpenKey(DefaultIconKey,true))
{
boolCreateSucessed = false; // 失败
return false;
}
AnsiString IconFile = Application->ExeName;// + ",0";
Reg->WriteString("",IconFile);
Reg->CloseKey();
// 快捷方式的目标
if(!Reg->OpenKey(ShellCommandKey,true))
{
boolCreateSucessed = false; // 失败
return false;
}
Reg->WriteString("",Application->ExeName);
Reg->CloseKey();
// 属性菜单
AnsiString PropMenu = "rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl,,0";
if(!Reg->OpenKey(PropertiesMenuKey,true))
{
boolCreateSucessed = false; // 失败
return false;
}
Reg->WriteString("",PropMenu);
Reg->CloseKey();
// 删除属性菜单的缺省菜单项
if(!Reg->OpenKey(AttributeKey,true))
{
boolCreateSucessed = false; // 失败
return false;
}
int i = 0;
Reg->WriteBinaryData("Attributes",(void *)(&i),4);
Reg->CloseKey();
// 添加一个引用
// HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\Namespace
// 这样它将只显示在桌面上
Reg->RootKey = HKEY_LOCAL_MACHINE;
AnsiString NameKey = DesktopNamespaceKey;
NameKey = NameKey + "\\" + guid_string;
if(!Reg->OpenKey(NameKey,true))
{
boolCreateSucessed = false; // 失败
return false;
}
Reg->WriteString("",shortcut_name);
Reg->CloseKey();
// 通知Shell,显示这个快捷方式
SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT, 0, 0);
}__finally
{
delete Reg;
}
if(boolCreateSucessed)
{
Application->MessageBoxA("快捷方式创建成功!","完成",MB_OK|MB_ICONINFORMATION);
}else
{
Application->MessageBoxA("快捷方式创建失败!","错误",MB_OK|MB_ICONERROR);
}
return boolCreateSucessed;
}
//---------------------------------------------------------------------------
// 删除在桌面类似于IE的快捷方式
void DeleteShortCutLikeIE(void)
{
AnsiString GuidKey;
GuidKey.sprintf("CLSID\\%s",guid_string);
TRegistry *Reg = new TRegistry;
try
{
Reg->RootKey = HKEY_CLASSES_ROOT;
Reg->DeleteKey(GuidKey);
// 删除引用
// HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\Namespace
AnsiString NameKey = DesktopNamespaceKey;
NameKey = NameKey + "\\" + guid_string;
Reg->RootKey = HKEY_LOCAL_MACHINE;
Reg->DeleteKey(NameKey);
// 同时Shell,注册表已经改变了
SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT, 0, 0);
Application->MessageBoxA("快捷方式已经被删除!","完成",MB_OK|MB_ICONINFORMATION);
}__finally
{
delete Reg;
}
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::ActionCreateShortcutExecute(TObject *Sender)
{
CreateShortCutLikeIE();
//
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::ActionDeleteShortcutExecute(TObject *Sender)
{
DeleteShortCutLikeIE();
//
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?