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

📄 myplayerdlg.cpp

📁 网络多路广播的例子
💻 CPP
字号:
 // MyPlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyPlayer.h"
#include "MyPlayerDlg.h"
#include "wmpmedia.h"
#include "wmpplaylist.h"
#include "wmpcontrols.h"
#include <FSTREAM>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPlayerDlg dialog

CMyPlayerDlg::CMyPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyPlayerDlg)
	m_nPort = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyPlayerDlg)
	DDX_Control(pDX, IDC_OCX1, m_WMPPlay);
	DDX_Text(pDX, IDC_PORT, m_nPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CMyPlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON_Stop, OnBUTTONStop)
	ON_BN_CLICKED(IDC_BUTTON_BACKWARD, OnButtonBackward)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_REVPGM, OnButtonRevpgm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPlayerDlg message handlers

BOOL CMyPlayerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	if(AfxSocketInit(NULL)==0)
	{
		AfxMessageBox("CSocket Inital Error");
	}
	BYTE f0,f1,f2,f3;
	f0=192;
	f1=168;
	f2=0;
	f3=7;
	((CIPAddressCtrl *)(GetDlgItem(IDC_IPADDRESS)))->SetAddress(f0,f1,f2,f3);
	m_nPort=8888;
	UpdateData(false);

	m_strFiles.SetSize(8);
	//m_strFiles[0]="F:\\music\\七里香\\3.mp3";
	/*
	m_strFiles[0]="mms://192.168.0.7/q.mp3";
		m_strFiles[1]="F:\\music\\七里香\\4.mp3";
		m_strFiles[2]="F:\\music\\七里香\\5.mp3";
		m_strFiles[3]="F:\\music\\七里香\\6.mp3";
		m_strFiles[4]="F:\\music\\七里香\\7.mp3";
		m_strFiles[5]="F:\\music\\七里香\\8.mp3";
		m_strFiles[6]="F:\\music\\七里香\\9.mp3";
		m_strFiles[7]="F:\\music\\七里香\\10.mp3";*/
	

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyPlayerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyPlayerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyPlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyPlayerDlg::OnFileOpen() 
{
	//open a file
	CString fileFilter ="MP3 FILE(*.mp3)|*.mp3";
	CFileDialog filedlg(true,fileFilter,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,fileFilter,NULL);
	int response=filedlg.DoModal();
	CString fileName;
	if(response==IDOK)
	{
		//get the file
		fileName = filedlg.GetPathName();
		TRACE(fileName);		
	}
	if(!fileName.IsEmpty())
	{
		CWMPMedia mysong=m_WMPPlay.newMedia(fileName);
		m_WMPPlay.SetCurrentMedia(mysong);
		//m_WMPPlay.GetCurrentPlaylist().insertItem(0,mysong);
		//CWMPMedia tt=m_WMPPlay.GetCurrentMedia();
		m_WMPPlay.GetControls().play();
	}
	
}

void CMyPlayerDlg::OnButton1() 
{
	//play the no.1 program
	m_WMPPlay.GetControls().stop();
	//m_strFiles.SetAt(0,m_clientsocket.m_strfile);
	//CWMPMedia mysong=m_WMPPlay.newMedia(m_strFiles[0]);
	//TRACE(m_strFiles[0]);
	//m_WMPPlay.SetCurrentMedia(mysong);
	//m_WMPPlay.GetCurrentPlaylist().insertItem(0,mysong);
	//CWMPMedia tt=m_WMPPlay.GetCurrentMedia();	
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[0]);
	TRACE(m_clientsocket.m_strfile[0]);
	m_WMPPlay.GetControls().play();
	//m_clientsocket.m_strfile
	
}

void CMyPlayerDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();
	//CWMPMedia mysong=m_WMPPlay.newMedia("mms://192.168.0.7/"+m_strFiles[1]);
	//m_WMPPlay.SetCurrentMedia(mysong);
	//m_WMPPlay.GetCurrentPlaylist().insertItem(0,mysong);
	//CWMPMedia tt=m_WMPPlay.GetCurrentMedia();
	//m_clientsocket.m_strfile[1]="//"+m_strServAddr+"/"+m_clientsocket.m_strfile[1];
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[1]);
	TRACE(m_clientsocket.m_strfile[1]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();	
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[2]);
	TRACE(m_clientsocket.m_strfile[2]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();
	//m_clientsocket.m_strfile[3]="//"+m_strServAddr+"/"+m_clientsocket.m_strfile[3];
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[3]);
	TRACE(m_clientsocket.m_strfile[3]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();
	//m_clientsocket.m_strfile[4]="//"+m_strServAddr+"/"+m_clientsocket.m_strfile[4];
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[4]);
	TRACE(m_clientsocket.m_strfile[4]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();
	//m_clientsocket.m_strfile[5]="//"+m_strServAddr+"/"+m_clientsocket.m_strfile[5];
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[5]);
	TRACE(m_clientsocket.m_strfile[5]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();
	//m_clientsocket.m_strfile[6]="//"+m_strServAddr+"/"+m_clientsocket.m_strfile[6];
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[6]);
	TRACE(m_clientsocket.m_strfile[6]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();
	//m_clientsocket.m_strfile[7]="//"+m_strServAddr+"/"+m_clientsocket.m_strfile[7];
	m_WMPPlay.SetUrl(m_clientsocket.m_strfile[7]);
	TRACE(m_clientsocket.m_strfile[7]);
	m_WMPPlay.GetControls().play();
}

void CMyPlayerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	m_WMPPlay.GetControls().stop();
	m_WMPPlay.close();	
    
	CHAR remotename[255];
	unsigned long unclen=255;
	if(WNetGetConnection("X:",remotename,&unclen)==NO_ERROR)
	WNetCancelConnection2("X:", 0, false);

	CDialog::OnCancel();
}

void CMyPlayerDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	double pos=m_WMPPlay.GetControls().GetCurrentPosition();
	pos += 5;
	double filepos=m_WMPPlay.GetCurrentMedia().GetDuration();
	if(pos>filepos)
		pos=filepos-1;
	m_WMPPlay.GetControls().SetCurrentPosition(pos);	
}

void CMyPlayerDlg::OnBUTTONStop() 
{
	// TODO: Add your control notification handler code here
	m_WMPPlay.GetControls().stop();	
}

void CMyPlayerDlg::OnButtonBackward() 
{
	// TODO: Add your control notification handler code here
	double pos=m_WMPPlay.GetControls().GetCurrentPosition();
	if(pos<5)
		m_WMPPlay.GetControls().SetCurrentPosition(0);
	else
		m_WMPPlay.GetControls().SetCurrentPosition(pos-5);	
	
}

void CMyPlayerDlg::OnButtonConnect() 
{
	// 连接服务器,得到播放列表
	BYTE f0,f1,f2,f3;
	CString name;
	((CIPAddressCtrl *)(GetDlgItem(IDC_IPADDRESS)))->GetAddress(f0,f1,f2,f3);
	//CString ip;
	m_strServAddr.Format("%d.%d.%d.%d",f0,f1,f2,f3);
	if(!m_clientsocket.Create())
	{
		TRACE("%d\n",WSAGetLastError());
		AfxMessageBox("create erro");
		return;
	}
	if(m_clientsocket.Connect(m_strServAddr,GetDlgItemInt(IDC_PORT)))
	{
		m_clientsocket.Init(this);
		AfxMessageBox("服务器连接成功!");		
        GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(false);
	}
	else
	{
		TRACE("%d\n",WSAGetLastError());
		AfxMessageBox("create erro");
		return;
	}
}

void CMyPlayerDlg::OnButtonRevpgm() 
{
	/*
	for(int i=0;i<m_clientsocket.m_strfile.GetSize();i++)
		m_clientsocket.m_strfile[i]="\\\\"+m_strServAddr+"\\"+m_clientsocket.m_strfile[i];
	GetDlgItem(IDC_BUTTON_REVPGM)->EnableWindow(FALSE);
	*/
	
		//CString user;
		//CString pass;
		
		//pass.AllocBuffer(50);
	char* user=new char[50];
	char* pass=new char[50];
		//user.Init();
		//pass.Init();
		std::ifstream myfile("tt.txt");
			//int tt;
		myfile>>user;
		myfile>>pass;
		myfile.close();
	
/*
    CHAR remotename[255];
	unsigned long unclen=255;
	WNetGetConnection("X:",remotename,&unclen); 
	
    CString remo;
	remo.Format("%s",remotename);
	if(remo.Find(m_strServAddr)==-1)
	{ 
	   int a=m_clientsocket.m_strfile[0].Find("$")+1;
	   CString b=m_clientsocket.m_strfile[0].Left(a);
	   
	   NETRESOURCE netres;
       netres.dwScope=RESOURCE_GLOBALNET;
       netres.dwType=RESOURCETYPE_ANY;
       netres.dwDisplayType=RESOURCEDISPLAYTYPE_GENERIC;
       netres.dwUsage=RESOURCEUSAGE_CONNECTABLE;
       netres.lpLocalName="X:";
       netres.lpRemoteName=b.GetBuffer(0);
       netres.lpComment=NULL;
       netres.lpProvider=NULL;

	   
	   
       DWORD rslt=WNetAddConnection2(&netres, "samsung", "administrator", 0);

	   
	   
	  
	   TRACE("%d",rslt);
	}
*/

}

void CMyPlayerDlg::ConnectRemote()
{
	/*CString user;
	CString	pass;
	std::ifstream myfile("tt.txt");
	//int tt;
	myfile>>user.GetBuffer(0);
	myfile>>pass.GetBuffer(0);
	myfile.close();
	*/	
	char* user=new char[50];
	char* pass=new char[50];
		//user.Init();
		//pass.Init();
		std::ifstream myfile("tt.txt");
			//int tt;
		myfile>>user;
		myfile>>pass;
		myfile.close();

    CHAR remotename[255];
	unsigned long unclen=255;
	WNetGetConnection("X:",remotename,&unclen); 
	
    CString remo;
	remo.Format("%s",remotename);
	if(remo.Find(m_strServAddr)==-1)
	{ 
	   int a=m_clientsocket.m_strfile[0].Find("$")+1;
	   CString b=m_clientsocket.m_strfile[0].Left(a);
	   
	   NETRESOURCE netres;
       netres.dwScope=RESOURCE_GLOBALNET;
       netres.dwType=RESOURCETYPE_ANY;
       netres.dwDisplayType=RESOURCEDISPLAYTYPE_GENERIC;
       netres.dwUsage=RESOURCEUSAGE_CONNECTABLE;
       netres.lpLocalName="X:";
       netres.lpRemoteName=b.GetBuffer(0);
       netres.lpComment=NULL;
       netres.lpProvider=NULL;	   
	   
       DWORD rslt=WNetAddConnection2(&netres, pass, user, 0);	  
	   TRACE("%d",rslt);
	}
	

}

⌨️ 快捷键说明

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