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

📄 copyfilename.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:

					}//while
					pi.ci.bEnd=TRUE;
					pi.ci.dwBlockSize=0;
					memset(pi.ci.byData,0,DATA_LEN-24);
					SendData((char*)&pi,DATA_LEN);

					GlobalUnlock(hClipData); 
				}

			}
			EmptyClipboard();
			CloseClipboard(); 



		}



		//ShowHtmlDlg("SelectDirectory.HTM",NULL,&v,L"dialogHeight: 400px; dialogWidth: 500px; center: Yes; help: No; resizable: No; status: No;");
		BROWSEINFO bi;
		TCHAR szDisplayName[MAX_PATH];
		memset(&bi, 0x00, sizeof(bi));

		bi.hwndOwner = NULL;
		bi.pszDisplayName = szDisplayName;
		bi.ulFlags = BIF_RETURNONLYFSDIRS;
		LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
		if (pidl)
		{
			SHGetPathFromIDList(pidl, szDisplayName);
			//SetCurrentDirectory(szDisplayName);
		/*	CComBSTR bstrFolder=szDisplayName;
			m_spCurrFolderElement->put_value(bstrFolder);*/


			// free the pidl
			CComPtr<IMalloc> spSHMalloc;
			SHGetMalloc(&spSHMalloc);
			if (spSHMalloc != NULL)
				spSHMalloc->Free(pidl);
		}
		else
			_tcscpy(szDisplayName,"c:\\");

		RetrieveAliasName(szDisplayName);
		PacketInfo pi;
		ZeroMemory((void*)&pi,DATA_LEN);
		pi.dwFlag=FILE_INIT_FLAG;
		pi.dwLen=sizeof(FILE_FOLDER);


		_tcscpy(pi.fi.szComputerName,g_szLocalName);
		USES_CONVERSION;
		pi.fi.dwIP=g_dwIP;
		_tcscpy(pi.fi.szDstFilePath,szDisplayName);
		//MessageBox(NULL,pi.fi.szDstFilePath,pi.fi.szDstFilePath,MB_OK);
		//////////////////////////////////////////////////////////////////////////
		//发送目录信息
		pi.ID=g_ID;
		bEndSend=FALSE;
		writelen=SendData((char*)&pi,DATA_LEN);
		while(writelen==-1 && !bEndSend)
		{
			Sleep(0);
			writelen=SendData((char*)&pi,DATA_LEN);

		}
		pDlg=new CSHProgressWnd();
		pDlg->SetTitle ( _T("发送文件...") );
		pDlg->SetAnimation ( IDR_FILECOPY );
		pDlg->SetCancelMessage ( _T("正在取消当前操作...") );

		pDlg->ShowModal( GetDesktopWindow() );
		
		for (int i=0;i<g_nCount;i++)
		{
			if(bEndSend)goto exit;
			DWORD dwAttributes = GetFileAttributes(g_szFileNameArray[i].szFileName);
			if (dwAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				SendDirectory(g_szFileNameArray[i].szFileName);
			}
			else{
				SendFile(g_szFileNameArray[i].szFileName);
			}
		}

		pi.dwFlag=FILE_END_FLAG;

		//////////////////////////////////////////////////////////////////////////
		//发送目录信息
		pi.ID=g_ID;
		writelen=SendData((char*)&pi,DATA_LEN);
		while(writelen==-1 && !bEndSend)
		{
			Sleep(0);
			writelen=SendData((char*)&pi,DATA_LEN);

		}
exit:
		delete[] g_szFileNameArray;
		pDlg->EndDialog();
		delete pDlg;
		EndSend();
		SetEvent(g_hQuitServiceEvent);

	}
	catch(...)
	{
		SetEvent(g_hQuitServiceEvent);
		dbg_log("%s文件,%s函数,%d行,错误号码:%d",__FILE__,__FUNCTION__,__LINE__,GetLastError());
	}

	
	//StartService("Packet Monitor");
	return S_OK;
}
HRESULT CCopyFileName::Initialize ( LPCITEMIDLIST pidlFolder,
                                    LPDATAOBJECT  pDO,
                                    HKEY          hProgID )
{
TCHAR     szFile    [MAX_PATH];

HDROP     hdrop;
FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };

    // Read the list of folders from the data object.  They're stored in HDROP
    // form, so just get the HDROP handle and then use the drag 'n' drop APIs
    // on it.

    if ( FAILED( pDO->GetData ( &etc, &stg )))
        return E_INVALIDARG;

    // Get an HDROP handle.

    hdrop = (HDROP) GlobalLock ( stg.hGlobal );

    if ( NULL == hdrop )
        {
        ReleaseStgMedium ( &stg );
        return E_INVALIDARG;
        }

    // Determine how many files are involved in this operation.

    g_nCount = DragQueryFile ( hdrop, 0xFFFFFFFF, NULL, 0 );

    for ( UINT uFile = 0; uFile < g_nCount; uFile++ )
        {
        // Get the next filename.

        if ( 0 == DragQueryFile ( hdrop, uFile, szFile, MAX_PATH ))
            continue;

        // If this is the first time thru the for loop, we need to
        // change the current directory to the one that was open in
        // explorer.  This is done so the DLLs we're about to load
        // can load other DLLs they depend on to be in the same
        // directory.

  
			m_lsFiles.push_back ( szFile );
		}

    GlobalUnlock ( stg.hGlobal );
    ReleaseStgMedium ( &stg );

    // If we found any files we can work with, return S_OK.  Otherwise,
    // return E_INVALIDARG so we don't get called again for this right-click
    // operation.

    return ( m_lsFiles.size() > 0 ) ? S_OK : E_INVALIDARG;
}


/////////////////////////////////////////////////////////////////////////////
// CCopyFileName IContextMenu methods


//////////////////////////////////////////////////////////////////////////
//
// Function:    QueryContextMenu()
//
// Description:
//  Adds our items to the supplied menu.
//
//////////////////////////////////////////////////////////////////////////
/*
*/
/* var collection;
  collection = document.all[radioName];
  for (i=0;i<collection.length;i++) {
    if (collection[i].checked)
    return(collection[i].value);
  }
*/

HRESULT CCopyFileName::QueryContextMenu ( HMENU hmenu, UINT uMenuIndex,
										 UINT  uidFirstCmd, UINT uidLastCmd,
                                          UINT  uFlags )
{
UINT uCmdID = uidFirstCmd;

    // If the flags include CMF_DEFAULTONLY then we shouldn't do anything.

    if ( uFlags & CMF_DEFAULTONLY )
        {
        return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
		}

    // Add our register/unregister items.

    InsertMenu ( hmenu, uMenuIndex, MF_STRING | MF_BYPOSITION|MF_POPUP, uCmdID++,
                 _T("发送文件和文件夹到...") );

	HMENU hMenuItem=GetSubMenu(hmenu,uCmdID);
	AppendMenu(hMenuItem, MF_STRING | MF_BYPOSITION,uCmdID++,"C:\\");


    // Set the bitmap for the register item.

    if ( NULL != m_hRegBmp )
        {
        SetMenuItemBitmaps ( hmenu, uMenuIndex, MF_BYPOSITION, m_hRegBmp, NULL );
        }
	
    uMenuIndex++;

       // The return value tells the shell how many top-level items we added.

    return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 2 );
}


		
//////////////////////////////////////////////////////////////////////////
//
// Function:    GetCommandString()
//
// Description:
//  Sets the flyby help string for the Explorer status bar.
//
//////////////////////////////////////////////////////////////////////////

HRESULT CCopyFileName::GetCommandString ( UINT  uCmdID,     UINT uFlags, 
                                          UINT* puReserved, LPSTR szName,
                                          UINT  cchMax )
{
LPCTSTR szPrompt;

    USES_CONVERSION;

    if ( uFlags & GCS_HELPTEXT )
        {
        switch ( uCmdID )
            {
            case 0:
                szPrompt = _T("发送选中的文件名");
            break;

          
            default:
                ATLASSERT(0);           // should never get here
                return E_INVALIDARG;
            break;
            }

        // Copy the help text into the supplied buffer.  If the shell wants
        // a Unicode string, we need to case szName to an LPCWSTR.

        if ( uFlags & GCS_UNICODE )
            {
            lstrcpynW ( (LPWSTR) szName, T2CW(szPrompt), cchMax );
            }
        else
            {
            lstrcpynA ( szName, T2CA(szPrompt), cchMax );
            }
        }
    else if ( uFlags & GCS_VERB )
        {
        // Copy the verb name into the supplied buffer.  If the shell wants
        // a Unicode string, we need to case szName to an LPCWSTR.

        switch ( uCmdID )
            {
            case 0:
                if ( uFlags & GCS_UNICODE )
                    {
                    lstrcpynW ( (LPWSTR) szName, L"发送文件", cchMax );
                    }
                else
                    {
                    lstrcpynA ( szName, "发送文件", cchMax );
                    }
           break;
           default:
                ATLASSERT(0);           // should never get here
                return E_INVALIDARG;
            break;
            }
		}

    return S_OK;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    InvokeCommand()
//
// Description:
//  Carries out the selected command.
//
//////////////////////////////////////////////////////////////////////////

HRESULT CCopyFileName::InvokeCommand ( LPCMINVOKECOMMANDINFO pInfo )
{
    // If lpVerb really points to a string, ignore this function call and bail out.  

    if ( 0 != HIWORD( pInfo->lpVerb ))
        return E_INVALIDARG;



    switch ( LOWORD( pInfo->lpVerb ))
        {
        case 0:
        case 1:
            {
			
		
			string_list::const_iterator it, itEnd;
			
			
		
			int i=0;
			g_szFileNameArray=new FILENAME[g_nCount];

			 for ( it =m_lsFiles.begin(), itEnd =m_lsFiles.end();it != itEnd;it++ )
			 {    
			     
				 //g_szFileNameArray.Add(it->c_str());
				 _tcscpy(g_szFileNameArray[i].szFileName,it->c_str());
				 i++;
		
			}
			 m_lsFiles.clear();
			 DWORD dwThread;
			 _tcscpy(g_szCurDir,g_szFileNameArray[0].szFileName);
			 *_tcsrchr(g_szCurDir,'\\')=0;

			 TCHAR	  sIP[17];
			 DWORD dwSize=1024;
			 //----------------------------------
			 WSADATA WSAData;
			 BOOL	flag = true;


			 in_addr addr_in;
			 struct  hostent *pHost;
			 SOCKET sock;
			 // 检查 Winsock 版本号	
			 WSAStartup(MAKEWORD(2, 2), &WSAData);

			 // 获取本机名
			 gethostname((char*)g_szLocalName, sizeof(g_szLocalName));
			 // 获取本地 IP 地址
			 pHost = gethostbyname((char*)g_szLocalName);


			 struct in_addr  *ptr = (struct in_addr *) pHost->h_addr_list[0];

			 sprintf(sIP, "%d.%d.%d.%d", ptr->S_un.S_un_b.s_b1, 
				 ptr->S_un.S_un_b.s_b2, ptr->S_un.S_un_b.s_b3, ptr->S_un.S_un_b.s_b4);

			 g_dwIP=inet_addr(sIP);
			 WSACleanup();
			 g_ID=GetTickCount();
			 g_hQuitServiceEvent=CreateEvent(0,TRUE,FALSE,0);
			 
			 if(!g_hQuitServiceEvent)return S_OK;
			 
			 

			 HANDLE hThread=CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)lpSendThread,NULL,0,&dwThread);
			 CloseHandle(hThread);
			WaitForSingleObject(g_hQuitServiceEvent,INFINITE);
            return S_OK;
            }
        break;

        default:
            ATLASSERT(0);               // should never get here
            return E_INVALIDARG;
        break;
        }
}

⌨️ 快捷键说明

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