扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:刘涛 来源:天极开发 2007年10月16日
关键字: COM Windows Shell 编程
图一、Windows外壳编程序界面效果图 |
…… SHFILEOPSTRUCT FileOp; //外壳的文件操作结构 FileOp.hwnd=m_hWnd; //设置句柄 //设置操作方式,拷贝用FO_COPY,删除用 FO_DELETE FileOp.wFunc=FO_COPY; FileOp.pFrom=m_source; //源文件路径 FileOp.pTo=m_detect; //目标文件路径 FileOp.fFlags=FOF_ALLOWUNDO; //允许恢复 FileOp.hNameMappings=NULL; FileOp.lpszProgressTitle=strTitle; //设置标题 SHFileOperation(&FileOp); //执行外壳拷贝 if(FileOp.fAnyOperationsAborted) //监测有无中止 TRACE("An Operation was aborted!!!\n"); …… |
…… CoInitialize(NULL); //初始化COM …… CoUninitialize(); //终止COM代码 …… |
…… EXTERN_C const IID IID_IShellDispatch; #if defined(__cplusplus) && !defined(CINTERFACE) interface DECLSPEC_UUID("D8F015C0-C278-11CE-A49E-444553540000") IShellDispatch : public Idispatch { public: …… virtual HRESULT STDMETHODCALLTYPE MinimizeAll( void) = 0; virtual HRESULT STDMETHODCALLTYPE UndoMinimizeALL( void) = 0; virtual HRESULT STDMETHODCALLTYPE FileRun( void) = 0; virtual HRESULT STDMETHODCALLTYPE CascadeWindows( void) = 0; virtual HRESULT STDMETHODCALLTYPE TileVertically( void) = 0; virtual HRESULT STDMETHODCALLTYPE TileHorizontally( void) = 0; virtual HRESULT STDMETHODCALLTYPE ShutdownWindows( void) = 0; virtual HRESULT STDMETHODCALLTYPE Suspend( void) = 0; virtual HRESULT STDMETHODCALLTYPE SetTime( void) = 0; virtual HRESULT STDMETHODCALLTYPE TrayProperties( void) = 0; virtual HRESULT STDMETHODCALLTYPE Help( void) = 0; virtual HRESULT STDMETHODCALLTYPE FindFiles( void) = 0; virtual HRESULT STDMETHODCALLTYPE FindComputer( void) = 0; }; …… |
…… HRESULT sc;//返回结果 IShellDispatch *pShellDisp = NULL; //初始化接口指针 //直接创建COM对象 sc = CoCreateInstance( CLSID_Shell,//指定待创建的COM对象标识符 NULL, //指定被聚合时的外部对象的接口指针 CLSCTX_SERVER, //指定组件类别,可以指定进程内组件进程外组件或者进程内控制对象。 IID_IDispatch, //指定接口ID,需要注意的是这里指的是待 //创建的COM对象的接口ID,而非类厂对象的接口标识符 (LPVOID *) &pShellDisp );//存放函数返回的对象的接口指针 /* 在上述代码中,CoCreateInstance首先调用CoGetClassObject函数创建类厂对象,然后用得到的类厂对象的接口指针创建真正的COM对象,最后把类厂对象释放并返回,这样就很好的把类厂屏蔽起来,使用户用起来更为简单。*/ if( FAILED(sc) )//必须用FAILED 或SUCCECCED来判断COM对象是否创建成功 return; pShellDisp->FindFiles(); //调用COM对象里的方法 pShellDisp->Release(); //释放申请到的接口指针 …… |
///////////////////////////////////// #define INC_OLE2 #define WIN32_LEAN_AND_MEAN #define STRICT #include <windows.h> #include <windowsx.h> #include <commctrl.h> #include <shlguid.h> #include <exdisp.h> #include "taskbar.h" #include "resource.h" // data static WNDPROC g_pfnOldProc; static HWND g_hwndButton=NULL; static HWND g_hDlg=NULL; static HINSTANCE g_hInstance; static HICON g_hIconLarge; static HICON g_hIconSmall; // functions static VOID OnRunPrograms( VOID ); static VOID OnFindFiles( VOID ); static VOID OnMinimizeAll( VOID ); static VOID OnUndoMinimize( VOID ); static VOID OnTaskbarProperties( VOID ); static VOID OnAddTab( HWND ); static VOID OnDeleteTab( VOID ); static VOID OnInitDialog( HWND ); static VOID OnButtonActivation( VOID ); // callbacks LRESULT CALLBACK APP_DlgProc( HWND, UINT, WPARAM, LPARAM ); LRESULT CALLBACK ButtonProc( HWND, UINT, WPARAM, LPARAM ); INT APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevious, LPSTR lpsz, INT iCmd ) { BOOL b; g_hIconLarge = (HICON) LoadImage( hInstance, "APP_ICON", IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0 ); g_hIconSmall = (HICON) LoadImage( hInstance, "APP_ICON", IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0 ); // initialize OLE libraries CoInitialize(NULL); InitCommonControls(); // run main dialog g_hInstance = hInstance; b = DialogBox( hInstance, "DLG_MAIN", NULL, (DLGPROC)APP_DlgProc ); // exit DestroyIcon( g_hIconLarge ); DestroyIcon( g_hIconSmall ); // free the objects used by ITaskbarList DestroyWindow( g_hwndButton ); OnDeleteTab(); CoUninitialize(); return b; } LRESULT CALLBACK APP_DlgProc( HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam ) { switch( uiMsg ) { case WM_INITDIALOG: OnInitDialog( hDlg ); break; case WM_COMMAND: switch( wParam ) { case IDC_FINDFILES: OnFindFiles(); return 1; case IDC_RUNPROGRAMS: OnRunPrograms(); return 1; case IDC_MINIMIZE: OnMinimizeAll(); return 1; case IDC_UNDOMINIMIZE: OnUndoMinimize(); return 1; case IDC_PROPERTIES: OnTaskbarProperties(); return 1; case IDC_ADDTAB: OnAddTab( hDlg ); return 1; case IDC_DELETETAB: OnDeleteTab(); return 1; case IDCANCEL: EndDialog( hDlg, FALSE ); return 0; } break; } return 0; } VOID OnFindFiles( VOID ) { HRESULT sc; IShellDispatch *pShellDisp = NULL; sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID *) &pShellDisp ); if( FAILED(sc) ) return; pShellDisp->FindFiles(); pShellDisp->Release(); return; } VOID OnTaskbarProperties( VOID ) { HRESULT sc; IShellDispatch *pShellDisp = NULL; sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,IID_IDispatch, (LPVOID *) &pShellDisp ); if( FAILED(sc) ) return; pShellDisp->TrayProperties(); pShellDisp->Release(); return; } VOID OnRunPrograms( VOID ) { HRESULT sc; IShellDispatch *pShellDisp = NULL; sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER, IID_IShellDispatch, (LPVOID *) &pShellDisp ); if( FAILED(sc) ) return; pShellDisp->FileRun(); pShellDisp->Release(); return; } VOID OnMinimizeAll( VOID ) { HRESULT sc; IShellDispatch *pShellDisp = NULL; sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID *) &pShellDisp ); if( FAILED(sc) ) return; pShellDisp->MinimizeAll(); pShellDisp->Release(); return; } VOID OnUndoMinimize( VOID ) { HRESULT sc; IShellDispatch *pShellDisp = NULL; sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID *) &pShellDisp ); if( FAILED(sc) ) return; pShellDisp->UndoMinimizeALL(); pShellDisp->Release(); return; } VOID OnInitDialog( HWND hDlg ) { // set the icons (T/F as to Large/Small icon) g_hDlg = hDlg; SendMessage( hDlg, WM_SETICON, FALSE, (LPARAM)g_hIconSmall ); SendMessage( hDlg, WM_SETICON, TRUE, (LPARAM)g_hIconLarge ); } VOID OnAddTab( HWND hWnd ) { static BOOL g_bFirstTime=TRUE; HRESULT sc; ITaskbarList *pDisp = NULL; sc = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_SERVER, IID_ITaskbarList, (LPVOID *) &pDisp ); if( FAILED(sc) ) return; // call the first time only if( g_bFirstTime ) { g_bFirstTime = FALSE; pDisp->HrInit(); // create a new button window g_hwndButton = CreateWindow( "button", "My Button", WS_CLIPSIBLINGS|BS_PUSHBUTTON,0, 0, 58, 14, hWnd, NULL, g_hInstance, NULL ); g_pfnOldProc = (WNDPROC) SubclassWindow( g_hwndButton, ButtonProc ); } pDisp->AddTab( g_hwndButton ); pDisp->Release(); return; } VOID OnDeleteTab( VOID ) { HRESULT sc; ITaskbarList *pDisp = NULL; sc = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_SERVER, IID_ITaskbarList, (LPVOID *) &pDisp ); if( FAILED(sc) ) return; pDisp->DeleteTab( g_hwndButton ); pDisp->Release(); return; } LRESULT CALLBACK ButtonProc( HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam ) { switch( uiMsg ) { case WM_NCACTIVATE: if( wParam==TRUE ) OnButtonActivation(); break; } return CallWindowProc( g_pfnOldProc, hwnd, uiMsg, wParam, lParam ); } VOID OnButtonActivation( VOID ) { HMENU hmenu; RECT r; LONG x, y; // get some window handles HWND h0=FindWindow("Shell_TrayWnd", NULL ); HWND h1=FindWindowEx(h0,NULL,"RebarWindow32", NULL); HWND h2=FindWindowEx(h1,NULL,"MSTaskSwWClass", NULL); HWND h3=FindWindowEx(h2,NULL,"SysTabControl32", NULL); GetWindowRect( h3, &r ); // get the currently selected button and // create a new popup menu hmenu = CreatePopupMenu(); INT i=TabCtrl_GetCurSel( h3 ); if( i==-1 ) { AppendMenu( hmenu, MF_STRING, IDC_DELETETAB,"&Close" ); } else { AppendMenu( hmenu, MF_STRING, IDC_MINIMIZE,"&Minimize All" ); AppendMenu( hmenu, MF_STRING, IDC_UNDOMINIMIZE,"&Undo Minimize All" ); AppendMenu( hmenu, MF_SEPARATOR, 0, NULL ); AppendMenu( hmenu, MF_STRING, IDC_PROPERTIES,"&Taskbar Properties" ); } // set and immediately reset its size to get // the current width and height LONG l = TabCtrl_SetItemSize( h3, 0, 0 ); TabCtrl_SetItemSize( h3, LOWORD(l), HIWORD(l) ); // have the menu to appear just above the button if( i==-1 ) { POINT pt; GetCursorPos( &pt ); x = pt.x; y = pt.y; } else { x = r.left + LOWORD(l)*i+3; y = GetSystemMetrics(SM_CYSCREEN)-(HIWORD(l)+1); } TrackPopupMenu( hmenu, TPM_BOTTOMALIGN, x, y, 0, g_hDlg, 0); DestroyMenu( hmenu ); return; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者