PageFilesExeList.cpp
资源名称:Netmanag.zip [点击查看]
上传用户:geanq888
上传日期:2007-01-03
资源大小:316k
文件大小:10k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // PageFilesExeList.cpp : implementation file
- //
- #include "stdafx.h"
- #include "netmanager.h"
- #include "PageFilesExeList.h"
- #include "GlobalsExtern.h"
- #include "PageFiles.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <shlobj.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPageFilesExeList
- CPageFilesExeList::CPageFilesExeList()
- {
- }
- CPageFilesExeList::~CPageFilesExeList()
- {
- }
- BEGIN_MESSAGE_MAP(CPageFilesExeList, CListCtrl)
- //{{AFX_MSG_MAP(CPageFilesExeList)
- ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
- ON_NOTIFY_REFLECT(LVN_BEGINLABELEDIT, OnBeginlabeledit)
- ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)
- ON_WM_KEYDOWN()
- ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
- ON_WM_KEYUP()
- ON_WM_DROPFILES()
- ON_WM_CONTEXTMENU()
- ON_COMMAND(IDM_EXEFILES_RENAME, OnExefilesRename)
- ON_COMMAND(IDM_EXEFILES_REMOVE, OnExefilesRemove)
- ON_COMMAND(IDM_EXEFILES_EXECUTE, OnExefilesExecute)
- ON_COMMAND(IDM_EXEFILES_ADD, OnExefilesAdd)
- ON_COMMAND(IDM_EXEFILES_AUTOSTART, OnExefilesAutostart)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPageFilesExeList message handlers
- /////////////////////////////////////////////////////////////////////////////
- int CPageFilesExeList::GetCurrentSelection()
- {
- int nWhichItem = 0;
- if(GetSelectedCount() == 1)
- {
- while(GetItemState(nWhichItem, LVIS_SELECTED) != LVIS_SELECTED)
- nWhichItem++;
- }
- return nWhichItem;
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
- {
- if(GetSelectedCount() != 0)
- {
- int nWhichItem = GetCurrentSelection();
- ((CPageFiles*)GetParent())->FilesExecute(nWhichItem);
- }
- *pResult = 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
- GetEditControl()->LimitText(32);
- *pResult = 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
- if(GetSelectedCount() != 0)
- {
- int nWhichItem = GetCurrentSelection();
- CEdit* pCurrent = GetEditControl();
- CString sCurrent;
- pCurrent->GetWindowText(sCurrent);
- SetItemText(nWhichItem, 0, sCurrent);
- }
- else
- AfxMessageBox("Nothing selected");
- *pResult = 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if(GetSelectedCount() != 0)
- {
- int nWhichItem = GetCurrentSelection();
- switch(nChar)
- {
- case VK_F3:
- ((CPageFiles*)GetParent())->OnExecuteExe();
- break;
- case VK_F2:
- EditLabel(nWhichItem);
- break;
- case VK_INSERT:
- ((CPageFiles*)GetParent())->OnAddExe();
- break;
- case VK_DELETE:
- ((CPageFiles*)GetParent())->OnRemoveExe();
- break;
- case VK_SPACE:
- SwitchExeAuto();
- break;
- }
- }
- CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
- {
- if(GetSelectedCount() != 0)
- {
- int nWhichItem = GetCurrentSelection();
- ((CPageFiles*)GetParent())->m_FilePath.SetWindowText(((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].sPath);
- ((CPageFiles*)GetParent())->m_AutoStart.SetCheck(((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].nAutoStart);
- }
- *pResult = 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if(GetSelectedCount() != 0)
- {
- int nWhichItem = GetCurrentSelection();
- CString sFileToExe = ((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].sPath;
- ((CPageFiles*)GetParent())->m_FilePath.SetWindowText(sFileToExe);
- ((CPageFiles*)GetParent())->m_AutoStart.SetCheck(((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].nAutoStart);
- }
- CListCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnDropFiles(HDROP hDropInfo)
- {
- WORD wNumFilesDropped = DragQueryFile(hDropInfo, -1, NULL, 0); // Get the number of pathnames that have been dropped
- CStringArray asFiles;
- for (WORD x = 0 ; x < wNumFilesDropped; x++) // get all file names. but we'll only need the first one.
- {
- WORD wPathnameSize = DragQueryFile(hDropInfo, x, NULL, 0); // Get the number of bytes required by the file's full pathname
- char* npszFile = (char*) LocalAlloc(LPTR, wPathnameSize += 1); // Allocate memory to contain full pathname & zero byte
- if(npszFile == NULL)
- continue; // If not enough memory, skip this one
- DragQueryFile(hDropInfo, x, npszFile, wPathnameSize); // Copy the pathname into the buffer
- asFiles.Add(npszFile);
- LocalFree(npszFile); // clean up
- }
- DragFinish(hDropInfo); // Free the memory block containing the dropped-file information
- CString sFile;
- CString sExpandedFile;
- struct _stat buf;
- int j = asFiles.GetSize();
- for(int i = 0; i < j; i++)
- {
- sFile = asFiles.GetAt(i);
- sExpandedFile = ExpandShortcut(sFile); // if this was a shortcut, we need to expand it to the target path
- if(sExpandedFile != "") // if that worked, we should have a real file name
- sFile = sExpandedFile;
- if(_stat(sFile, &buf) == 0)
- ((CPageFiles*)GetParent())->AddExe(sFile);
- }
- CListCtrl::OnDropFiles(hDropInfo);
- }
- /////////////////////////////////////////////////////////////////////////////
- // use IShellLink to expand the shortcut
- // returns the expanded file, or "" on error
- //
- // original code was part of CShortcut
- // 1996 by Rob Warner
- // rhwarner@southeast.net
- // http://users.southeast.net/~rhwarner
- CString CPageFilesExeList::ExpandShortcut(CString &inFile)
- {
- CString outFile = "";
- // Make sure we have a path
- ASSERT(inFile != _T(""));
- IShellLink* psl;
- HRESULT hres;
- LPTSTR lpsz = inFile.GetBuffer(MAX_PATH);
- // Create instance for shell link
- hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl);
- if(SUCCEEDED(hres))
- {
- // Get a pointer to the persist file interface
- IPersistFile* ppf;
- hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) &ppf);
- if(SUCCEEDED(hres))
- {
- // Make sure it's ANSI
- WORD wsz[MAX_PATH];
- ::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, wsz, MAX_PATH);
- // Load shortcut
- hres = ppf->Load(wsz, STGM_READ);
- if(SUCCEEDED(hres))
- {
- WIN32_FIND_DATA wfd;
- // find the path from that
- HRESULT hres = psl->GetPath(outFile.GetBuffer(MAX_PATH),
- MAX_PATH,
- &wfd,
- SLGP_UNCPRIORITY);
- outFile.ReleaseBuffer();
- }
- ppf->Release();
- }
- psl->Release();
- }
- inFile.ReleaseBuffer();
- // if this fails, outFile == ""
- return outFile;
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- if(GetSelectedCount() != 0)
- {
- CPoint posMouse;
- GetCursorPos(&posMouse);
- int nWhichItem = GetCurrentSelection();
- CMenu PopMenu;
- PopMenu.LoadMenu(IDR_EXEFILESMENU);
- if(((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].nAutoStart == 0)
- PopMenu.GetSubMenu(0)->CheckMenuItem(IDM_EXEFILES_AUTOSTART, MF_UNCHECKED | MF_BYCOMMAND);
- else
- PopMenu.GetSubMenu(0)->CheckMenuItem(IDM_EXEFILES_AUTOSTART, MF_CHECKED | MF_BYCOMMAND);
- PopMenu.GetSubMenu(0)->TrackPopupMenu(0, posMouse.x, posMouse.y, this);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnExefilesExecute()
- {
- ((CPageFiles*)GetParent())->OnExecuteExe();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnExefilesRename()
- {
- EditLabel(GetCurrentSelection());
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnExefilesRemove()
- {
- ((CPageFiles*)GetParent())->OnRemoveExe();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnExefilesAdd()
- {
- ((CPageFiles*)GetParent())->OnAddExe();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::OnExefilesAutostart()
- {
- SwitchExeAuto();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageFilesExeList::SwitchExeAuto()
- {
- int nWhichItem = GetCurrentSelection();
- if(((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].nAutoStart == 0)
- {
- SetItemState(nWhichItem, INDEXTOSTATEIMAGEMASK(2), LVIS_STATEIMAGEMASK);
- ((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].nAutoStart = 1;
- ((CPageFiles*)GetParent())->m_AutoStart.SetCheck(1);
- }
- else
- {
- SetItemState(nWhichItem, INDEXTOSTATEIMAGEMASK(1), LVIS_STATEIMAGEMASK);
- ((CPageFiles*)GetParent())->m_aExeFiles[nWhichItem].nAutoStart = 0;
- ((CPageFiles*)GetParent())->m_AutoStart.SetCheck(0);
- }
- }
- /////////////////////////////////////////////////////////////////////////////