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

📄 rsmenu.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
字号:
// RSMenu.cpp : Implementation of CRSMenu
#include "stdafx.h"
#include "RSContext.h"
#include "RSMenu.h"
#include "Sender.h"
#include "XAviPlay.h"
int g_nCount;
CSimpleArray<CString>g_szFileNameArray;
TCHAR g_szCurDir[MAX_PATH+1];
DWORD g_dwIP;
DWORD g_ID;
TCHAR 	g_szLocalName[256];
DWORD SendFile(TCHAR * szFileName)
{
	
	TCHAR szMsg[1024];

	HANDLE hFile=CreateFile(szFileName,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if(hFile==INVALID_HANDLE_VALUE)return 0;
	DWORD dwHigh;
	DWORD dwFileLen=GetFileSize(hFile,&dwHigh);
		
	



	CSHProgressWnd dlg;
	dlg.SetTitle ( _T("Hang on a sec...") );
	dlg.SetAnimation ( IDR_FILECOPY );
	dlg.SetCancelMessage ( _T("正在取消当前操作...") );
	dlg.SetLineText ( 1, _T("发送文件...") );

	dlg.ShowModal( GetDesktopWindow() );


	dlg.UpdateProgress ( 0, dwFileLen );

	PacketInfo pi;
	ZeroMemory((void*)&pi,DATA_LEN);
	pi.dwFlag=FILE_HEADER_FLAG;
	pi.dwLen=sizeof(FILE_HEADER);
	pi.fh.dwFileLen=dwFileLen;
	

	_tcscpy(pi.fh.szFileName,szFileName+_tcslen(g_szCurDir)+1);
	DWORD dwCheckSum=checksum((BYTE*)pi.fh.szFileName,_tcslen(pi.fh.szFileName));
	pi.fh.dwFileHash=dwCheckSum;
	int writelen,i=0;
	//char buff[DATA_LEN];

	BOOL bInputEnd=FALSE;


	//发送文件头信息
	dlg.SetLineText ( 2, "发送文件头信息" );

	DWORD dwWaitResult; 


  
	pi.ID=g_ID;
	writelen=write_memblock((char*)&pi,DATA_LEN);
	while(writelen==-1)
	{
		Sleep(1);
		writelen=write_memblock((char*)&pi,DATA_LEN);

	}
	
	pi.dwFlag=FILE_BODY_FLAG;
	pi.dwLen=sizeof(FILE_BODY);

	pi.fb.dwFileHash=dwCheckSum;
	DWORD dwCount=0;
	while(!bInputEnd)
	{
		if (dlg.HasUserCanceled()) {
			bInputEnd=TRUE;
			goto exit;
		}
		DWORD dwReturn;
		
		ReadFile(hFile,pi.fb.byData,DATA_LEN-20,&pi.fb.dwBlockSize,NULL);

		if(pi.fb.dwBlockSize==0){

			bInputEnd=TRUE;
			goto exit;

		}
		pi.ID=g_ID;
		writelen=write_memblock((char*)&pi,DATA_LEN);
		while(writelen==-1)
		{
			Sleep(1);
			writelen=write_memblock((char*)&pi,DATA_LEN);

		}
	

		dwCount+=pi.fb.dwBlockSize;
		wsprintf ( szMsg, _T("已经发送%d字节\n"), dwCount );
		OutputDebugString(szMsg);
		dlg.SetLineText ( 2, szMsg );
		dlg.UpdateProgress ( dwCount );


	}//while
	//发送文件结束标记

exit:
	CloseHandle(hFile);
	pi.dwFlag=FILE_TAILER_FLAG;
	pi.dwLen=sizeof(FILE_TAILER);
	pi.ft.dwFileHash=dwCheckSum;
	pi.ft.dwEndFlag=01;
	pi.ft.dwFileLen=dwFileLen;
	pi.ID=g_ID;
	writelen=write_memblock((char*)&pi,DATA_LEN);
	while(writelen==-1)
	{
		Sleep(1);	
		writelen=write_memblock((char*)&pi,DATA_LEN);

	}
	


	dlg.EndDialog();
	


}
DWORD SendDirectory(TCHAR * szPath)
{

	PacketInfo pi;
	DWORD writelen;
	ZeroMemory((void*)&pi,DATA_LEN);
	pi.dwFlag=FILE_FOLDER_FLAG;
	pi.dwLen=sizeof(FILE_FOLDER);
	_tcscpy(pi.ff.szFilePath,szPath+_tcslen(g_szCurDir)+1);
	pi.ff.dwFilePathLen=_tcslen(szPath);
	pi.ff.dwIP=g_dwIP;
	//////////////////////////////////////////////////////////////////////////
	//发送目录信息
	pi.ID=g_ID;
	writelen=write_memblock((char*)&pi,DATA_LEN);
	while(writelen==-1)
	{
		Sleep(1);
		writelen=write_memblock((char*)&pi,DATA_LEN);

	}
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	CString sFullPath;
	CString sFindFilter;
	DWORD dwAttributes = 0;

	sFindFilter = szPath;
	sFindFilter += _T("\\*.*");
	if ((hFind = FindFirstFile(sFindFilter, &wfd)) == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	do
	{
		if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
			_tcscmp(wfd.cFileName, _T("..")) == 0 )
		{
			continue;
		}

		sFullPath = szPath;
		sFullPath += _T('\\');
		sFullPath += wfd.cFileName;

		//remove readonly attributes
		dwAttributes = GetFileAttributes(sFullPath);
	

		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			SendDirectory(sFullPath.GetBuffer(0));
		}
		else
		{
			SendFile(sFullPath.GetBuffer(0));
			
		}
	}
	while (FindNextFile(hFind, &wfd));
	FindClose(hFind);

}
/////////////////////////////////////////////////////////////////////////////
// CRSMenu
DWORD lpSendThread(LPVOID lParam)
{
	
	DWORD writelen;
	CoInitialize(0);
	InitSend();

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

	_tcscpy(pi.fi.szComputeName,g_szLocalName);
	pi.fi.dwIP=g_dwIP;
	
	//////////////////////////////////////////////////////////////////////////
	//发送目录信息
	pi.ID=g_ID;
	writelen=write_memblock((char*)&pi,DATA_LEN);
	while(writelen==-1)
	{
		Sleep(1);
		writelen=write_memblock((char*)&pi,DATA_LEN);

	}
	for (int i=0;i<g_nCount;i++)
	{
		
		DWORD dwAttributes = GetFileAttributes(g_szFileNameArray[i]);
		if (dwAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			SendDirectory(g_szFileNameArray[i].GetBuffer(0));
		}
		else{
			SendFile(g_szFileNameArray[i].GetBuffer(0));
		}
	}
	
	pi.dwFlag=FILE_END_FLAG;
	
	//////////////////////////////////////////////////////////////////////////
	//发送目录信息
	pi.ID=g_ID;
	writelen=write_memblock((char*)&pi,DATA_LEN);
	while(writelen==-1)
	{
		Sleep(1);
		writelen=write_memblock((char*)&pi,DATA_LEN);

	}
    g_szFileNameArray.RemoveAll();
	CoUninitialize();
	return S_OK;
}
HRESULT CRSMenu::QueryContextMenu(HMENU hmenu, UINT indexMenu,
								   UINT idCmdFirst, UINT idCmdLast,
								   UINT uFlags)
{
	if (!(CMF_DEFAULTONLY & uFlags))
	{
		int nCmdSend = idCmdFirst;
		
		InsertMenu(hmenu, indexMenu++, MF_STRING|MF_BYPOSITION,
					nCmdSend, _T("发送[&S]"));
		
		// A successful return code should be the offset of
		// the largest command identifier assigned, plus one
		return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, nCmdSend - idCmdFirst + 1);
	}
	
	return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, USHORT(0));
}

HRESULT CRSMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
{
	TCHAR szParms[MAX_PATH+6];

	// The offset will determine which option was requested:
	// 0 for Register, 1 for Send
	UINT idOffset = LOWORD(pici->lpVerb);

	// Embed a '/u' for the Send command into the
	// parameters, and always put quotes around the path
	// so spaces in the path won't be a problem
	if (0== idOffset)
	{
		DWORD dwThread;
		
		HANDLE hThread=CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)lpSendThread,NULL,0,&dwThread);
		CloseHandle(hThread);
		
	}

	return S_OK;
}

HRESULT CRSMenu::GetCommandString(UINT idCmd, UINT uFlags, UINT* pwReserved,
								   LPSTR pszName, UINT cchMax)
{
	if (uFlags & GCS_HELPTEXT)
	{
		// Windows NT will ask for Unicode strings
		switch(idCmd)
		{
		case 0:
			if (uFlags & GCS_VERBA)
				lstrcpyn(pszName, "发送选中的文件和目录", cchMax);
			else
				lstrcpynW((LPWSTR)pszName, L"发送选中的文件和目录", cchMax);
			break;
		default:
			break;
		}

		return S_OK;
	}

	return E_FAIL;
}

HRESULT CRSMenu::Initialize(LPCITEMIDLIST pidlFolder, LPDATAOBJECT lpdobj, HKEY hkeyProgID)
{
	if (lpdobj == NULL)
		return E_INVALIDARG;

	// The file can be referenced through the STGMEDIUM structure
	// that is retrievable through the IDataObject pointer, and
	// DragQueryFile can then be used to retrieve the file name.
	STGMEDIUM medium;
	FORMATETC fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
	HRESULT hr = lpdobj->GetData(&fe, &medium);
	if (FAILED(hr))
		return E_INVALIDARG;


  g_nCount = DragQueryFile(reinterpret_cast<HDROP>(medium.hGlobal), (UINT)-1, NULL, 0);
     TCHAR szFileName[_MAX_PATH + 1];
    
    for(UINT nNames = 0; nNames < g_nCount; nNames++)
    {
        ZeroMemory(szFileName, _MAX_PATH + 1);
        DragQueryFile
            (reinterpret_cast<HDROP>(medium.hGlobal), nNames, (LPTSTR)szFileName, _MAX_PATH + 1);
	
		
		g_szFileNameArray.Add(szFileName);
        // Do something with szFileName.
    }
	_tcscpy(g_szCurDir,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();
	ReleaseStgMedium(&medium);
	return hr;
}

⌨️ 快捷键说明

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