0. 初始化環境和OLE對象
#include <utilcls.h>
#include <shlobj.h>
// 初始化COM環境
CoInitialize(NULL);
// 創建Shell對象
Variant vShellOLE = Variant::CreateObject("Shell.Application");
1. 層疊窗口
Function CascadeWindows("CascadeWindows");
vShellOLE.Exec(CascadeWindows);
2. 橫向平鋪窗口
Function TileHorizontally("TileHorizontally");
vShellOLE.Exec(TileHorizontally);
3. 縱向平鋪窗口
Function TileVertically("TileVertically");
vShellOLE.Exec(TileVertically);
4. 最小化所有窗口
Function MinimizeAll("MinimizeAll");
vShellOLE.Exec(MinimizeAll);
5. 撤消全部最小化
Function UndoMinimizeALL("UndoMinimizeALL");
vShellOLE.Exec(UndoMinimizeALL);
6. 設置時間
Function SetTime("SetTime");
vShellOLE.Exec(SetTime);
7. 查找計算機
Function FindComputer("FindComputer");
vShellOLE.Exec(FindComputer);
8. 查找文件
Function FindFiles("FindFiles");
vShellOLE.Exec(FindFiles);
9. 幫助
Function Help("Help");
vShellOLE.Exec(Help);
10. 運行文件
Function FileRun("FileRun");
vShellOLE.Exec(FileRun);
11. 任務欄屬性
Function TrayProperties("TrayProperties");
vShellOLE.Exec(TrayProperties);
12. 掛起
Function Suspend("Suspend");
vShellOLE.Exec(Suspend);
13. 關閉Windows
Function ShutdownWindows("ShutdownWindows");
vShellOLE.Exec(ShutdownWindows);
14. 打開目錄對話框
int SpecFolder[17] =
{
ssfDESKTOP, ssfPROGRAMS, ssfCONTROLS, ssfPRINTERS, ssfPERSONAL, ssfFAVORITES,
ssfSTARTUP, ssfRECENT, ssfSENDTO, ssfBITBUCKET, ssfSTARTMENU, ssfDESKTOPDIRECTORY,
ssfDRIVES, ssfNETWORK, ssfNETHOOD, ssfFONTS, ssfTEMPLATES
};
Function BrowseForFolder("BrowseForFolder");
Variant ppsdf;
int nIndex = 2;
// 以選定的系統目錄為根目錄
ppsdf = vShellOLE.Exec(BrowseForFolder << short(Handle) << "BrowseForFolder method" <<
BIF_BROWSEINCLUDEFILES << Variant(SpecFolder[nIndex]));
// 以指定的目錄為根目錄
String strPath("C:\\123");
ppsdf = vShellOLE.Exec(BrowseForFolder << short(Handle) << "BrowseForFolder method" <<
BIF_BROWSEINCLUDEFILES << Variant(strPath));
15. 浏覽指定目錄
int SpecFolder[17] =
{
ssfDESKTOP, ssfPROGRAMS, ssfCONTROLS, ssfPRINTERS, ssfPERSONAL, ssfFAVORITES,
ssfSTARTUP, ssfRECENT, ssfSENDTO, ssfBITBUCKET, ssfSTARTMENU, ssfDESKTOPDIRECTORY,
ssfDRIVES, ssfNETWORK, ssfNETHOOD, ssfFONTS, ssfTEMPLATES
};
Function Explore("Explore");
// 浏覽指定系統特殊目錄
int nIndex = 2;
vShellOLE.Exec(Explore << Variant(SpecFolder[nIndex]));
// 浏覽指定磁盤目錄
String strPath("C:\\123");
vShellOLE.Exec(Explore << Variant(String strPath));
16. 運行控制面板程序
Function ControlPanelItem("ControlPanelItem");
String strCplFile("D:\\Winnt\\System32\\appwiz.cpl");
BSTR FPath = TStringConverter<BSTR>::AnsiToOLESTR(strCplFile.c_str());
vShellOLE.Exec(ControlPanelItem << FPath);
17. 在退出程序時將Shell對象付空值
vShellOLE = Unassigned;
CoUninitialize();