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

📄 mediaplayer.c

📁 LabwindowsCVI编写的一简单的媒体播放器。
💻 C
字号:
#include "windows.h"
#include "winmm.h"
#include "wmp.h"
#include "toolbox.h"
#include <cvirte.h>		
#include <userint.h>
#include <utility.h>
#include "mediaplayer.h"

static int aboutpanelhandle;
static int menubarhandle;
static int openURLhandle;
static CAObjHandle objecthandle;
static int panelHandle;
void resize (void);

int main (int argc, char *argv[])
{
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((panelHandle = LoadPanel (0, "mediaplayer.uir", PANEL)) < 0)
		return -1;
	GetObjHandleFromActiveXCtrl (panelHandle, PANEL_WINDOWSMEDIAPLAYER, &objecthandle);
	menubarhandle = LoadMenuBar (panelHandle, "mediaplayer.uir", MENUBAR);
	WMPLib_IWMPPlayer4SetstretchToFit (objecthandle, NULL, VTRUE);
	WMPLib_IWMPPlayer4SetenableContextMenu (objecthandle, NULL, VFALSE);
	panelCB(panelHandle, EVENT_PANEL_SIZE, 0, 0, 0);
	EnableDragAndDrop (panelHandle);
	DisplayPanel (panelHandle);
	RunUserInterface ();
	DiscardPanel (panelHandle);
	return 0;
}

void CVICALLBACK fileopen (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	int selectionstatus;
	char pathname[MAX_PATHNAME_LEN];
	selectionstatus = FileSelectPopup ("", "*.avi", "*.avi;*.mpg;*.rm;*.rmvb;*.asf;*.wmv;*.mp3;*.mid;*.midi;*.cda;*.wma;*.mpeg;*.mpa;*.*",
									   "打开", VAL_OK_BUTTON, 0, 0, 1, 1, pathname);
	if (selectionstatus>0)
	{
		WMPLib_IWMPPlayer4SetURL (objecthandle, NULL, pathname);
		SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);  
	}
	
}

void CVICALLBACK fileopenurl (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	openURLhandle = LoadPanel (0, "mediaplayer.uir", PANEL_OPEN);
	InstallPopup (openURLhandle);
	
}

void CVICALLBACK filesaveas (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	int result;
	int selectionstatus;
	char *sourcepathname;
	char pathname[MAX_PATHNAME_LEN];
	WMPLib_IWMPPlayer4GetURL (objecthandle, NULL, &sourcepathname);
	selectionstatus = FileSelectPopup ("", "*.*",  "*.*", "另存为", VAL_SAVE_BUTTON, 0, 0, 1, 1, pathname);
	if (selectionstatus > 0)
	{
		result = CopyFile (sourcepathname, pathname);
		switch (result)
		{
			case -1:
				MessagePopup ("提示", "文件或目录不存在!");
				break;
			case -3:
				MessagePopup ("提示", "I/O错误!");      
				break;
			case -4:
				MessagePopup ("提示", "内存太低!");  
				break;
			case -5:
				MessagePopup ("提示", "无效文件或路径名!");  
				break;
			case -6:
				MessagePopup ("提示", "存取失败!");  
				break;
			case -7:
				MessagePopup ("提示", "I/O错误!");  
				break;
			case -8:
				MessagePopup ("提示", "磁盘空间不足!");  
				break;
		}
	}
}

void CVICALLBACK fileshut (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPPlayer4SetURL (objecthandle, NULL, "");
	
}

void CVICALLBACK fileexit (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	QuitUserInterface (0);
}

void CVICALLBACK menubarvisible (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	SetPanelAttribute (panelHandle, ATTR_MENU_BAR_VISIBLE, 1);  
	SetPanelAttribute (panelHandle, ATTR_TITLEBAR_VISIBLE, 1);
}

void CVICALLBACK meunbarhide (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	SetPanelAttribute (panelHandle, ATTR_MENU_BAR_VISIBLE, 0);
	SetPanelAttribute (panelHandle, ATTR_TITLEBAR_VISIBLE, 0);   
}

void CVICALLBACK fullsize (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	SetPanelAttribute (panelHandle, ATTR_MENU_BAR_VISIBLE, 0);
	SetPanelAttribute (panelHandle, ATTR_TITLEBAR_VISIBLE, 0);
	SetPanelAttribute (panelHandle, ATTR_WINDOW_ZOOM, VAL_MAXIMIZE); 
}

void CVICALLBACK sizeoriginal (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPPlayer4SetstretchToFit (objecthandle, NULL, VFALSE);   
}

void CVICALLBACK sizefit (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPPlayer4SetstretchToFit (objecthandle, NULL, VTRUE);  
}

void CVICALLBACK startsize (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	SetPanelAttribute (panelHandle, ATTR_HEIGHT, 464);
	SetPanelAttribute (panelHandle, ATTR_WIDTH, 613);
	SetPanelAttribute (panelHandle, ATTR_MENU_BAR_VISIBLE, 1);
	SetPanelAttribute (panelHandle, ATTR_TITLEBAR_VISIBLE, 1);
	SetPanelAttribute (panelHandle, ATTR_WINDOW_ZOOM, VAL_NO_ZOOM);
	SetPanelAttribute (panelHandle, ATTR_LEFT, 10);
	SetPanelAttribute (panelHandle, ATTR_TOP, 50);
}

void CVICALLBACK playpause (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	enum WMPLibEnum_WMPPlayState pwmpps;
	static int i = 0;
	
	WMPLib_IWMPPlayer4GetplayState (objecthandle, NULL, &pwmpps);
	
	if (pwmpps == WMPLibConst_wmppsStopped)
	{
		i = 1;	
	}
	
	if (i == 0)
	{
		WMPLib_IWMPControlspause (objecthandle, NULL);
		SetMenuBarAttribute (menubarhandle, MENUBAR_PLAY_STOP, ATTR_DIMMED, 0);
		panelCB(panelHandle, EVENT_PANEL_SIZE, 0, 0, 0);
		i = 1;
	}
	else
	{
		WMPLib_IWMPControlsplay (objecthandle, NULL);
		SetMenuBarAttribute (menubarhandle, MENUBAR_PLAY_STOP, ATTR_DIMMED, 0); 
		panelCB(panelHandle, EVENT_PANEL_SIZE, 0, 0, 0);
		i = 0;
	}
}

void CVICALLBACK stop (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPControlsstop (objecthandle, NULL);
	SetMenuBarAttribute (menubarhandle, MENUBAR_PLAY_STOP, ATTR_DIMMED, 1);
	panelCB(panelHandle, EVENT_PANEL_SIZE, 0, 0, 0);
}

void CVICALLBACK big (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	long plvolume;
	WMPLib_IWMPSettingsGetvolume (objecthandle, NULL, &plvolume);
	WMPLib_IWMPSettingsSetvolume (objecthandle, NULL, plvolume+=100);
}

void CVICALLBACK dec (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	long plvolume;
	WMPLib_IWMPSettingsGetvolume (objecthandle, NULL, &plvolume);
	WMPLib_IWMPSettingsSetvolume (objecthandle, NULL, plvolume-=100);
}

void CVICALLBACK mute (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	VBOOL pfmute;
	WMPLib_IWMPSettingsGetmute (objecthandle, NULL, &pfmute);
	if (pfmute)
	{
		WMPLib_IWMPSettingsSetmute (objecthandle, NULL, VFALSE);
	}
	else
	{
		WMPLib_IWMPSettingsSetmute (objecthandle, NULL, VTRUE);
	}
}

void CVICALLBACK fast (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPSettingsSetrate (objecthandle, NULL, 2.0);  
}

void CVICALLBACK normal (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPSettingsSetrate (objecthandle, NULL, 1.0);  
}

void CVICALLBACK slow (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPSettingsSetrate (objecthandle, NULL, 0.5);  
}


void CVICALLBACK back (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPSettingsSetrate (objecthandle, NULL, -4.0);  	
}

void CVICALLBACK front (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	WMPLib_IWMPSettingsSetrate (objecthandle, NULL, 4.0); 
	
}

void CVICALLBACK alwaystop (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	static int i = 0;
	if (i == 0)
	{
		SetMenuBarAttribute (menubarhandle, MENUBAR_CONTROL_ALWAYSTOP, ATTR_CHECKED, 1);
		SetPanelAttribute (panelHandle, ATTR_FLOATING, VAL_FLOAT_ALWAYS);
		i = 1;
	}
	else
	{
		SetMenuBarAttribute (menubarhandle, MENUBAR_CONTROL_ALWAYSTOP, ATTR_CHECKED, 0); 
		SetPanelAttribute (panelHandle, ATTR_FLOATING, VAL_FLOAT_NEVER);
		i = 0;
	}
}

void CVICALLBACK eject (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	mciExecute("Open Cdaudio Alias cd");
	mciExecute("set cd door open");
	mciExecute("close cd");
}

void CVICALLBACK close (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	mciExecute("Open Cdaudio Alias cd");
	mciExecute("set cd door closed"); 
	mciExecute("close cd");
}

void CVICALLBACK about (int menuBar, int menuItem, void *callbackData,
		int panel)
{
	aboutpanelhandle = LoadPanel (0, "mediaplayer.uir", PANEL_ABOU);
	InstallPopup (aboutpanelhandle);
}

int CVICALLBACK openfile (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int len;
	char pathname[MAX_PATHNAME_LEN];  
	switch (event)
	{
		case EVENT_COMMIT:
			if (control == PANEL_OPEN_BTN_OPENFILE)
			{
				GetCtrlVal (openURLhandle, PANEL_OPEN_STRING_OPENFILE, pathname);
				WMPLib_IWMPPlayer4SetURL (objecthandle, NULL, pathname);
				DiscardPanel (openURLhandle);
				SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1); 
			}
			break;
		case EVENT_VAL_CHANGED:
			GetCtrlAttribute (openURLhandle, PANEL_OPEN_STRING_OPENFILE, ATTR_STRING_TEXT_LENGTH, &len);
			if (len>0)
			{
				SetCtrlAttribute (openURLhandle, PANEL_OPEN_BTN_OPENFILE, ATTR_DIMMED, 0);
			}
			else
			{
				SetCtrlAttribute (openURLhandle, PANEL_OPEN_BTN_OPENFILE, ATTR_DIMMED, 1); 		
			}
		   break;
	}
	return 0;
}

int CVICALLBACK cancel (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			DiscardPanel (openURLhandle);
			break;
	}
	return 0;
}

int CVICALLBACK fileurl (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			fileopen(MENUBAR, MENUBAR_FILE_FILEOPEN, 0, PANEL);
			DiscardPanel (openURLhandle); 
			break;
	}
	return 0;
}

int CVICALLBACK panelCB (int panel, int event, void *callbackData,
		int eventData1, int eventData2)
{
	int zoom;
	int titlebar;
	int virtualkey;
	char *pval;
	int panelwidth;
	int panelheight;
	char **pathname;
	int handle;  
	switch (event)
	{
		case EVENT_CLOSE:
			QuitUserInterface (0);
			break;
		case EVENT_PANEL_SIZE:
			GetPanelAttribute (panelHandle, ATTR_HEIGHT, &panelheight);
			GetPanelAttribute (panelHandle, ATTR_WIDTH, &panelwidth);
			SetCtrlAttribute (panelHandle, PANEL_WINDOWSMEDIAPLAYER, ATTR_LEFT, 0);
			SetCtrlAttribute (panelHandle, PANEL_WINDOWSMEDIAPLAYER, ATTR_TOP, 0);
			SetCtrlAttribute (panelHandle, PANEL_WINDOWSMEDIAPLAYER, ATTR_HEIGHT, panelheight);
			SetCtrlAttribute (panelHandle, PANEL_WINDOWSMEDIAPLAYER, ATTR_WIDTH, panelwidth);
			break;
		case EVENT_KEYPRESS:
			virtualkey = GetKeyPressEventVirtualKey (eventData2);
			if (virtualkey == VAL_ESC_VKEY)
			{
				SetPanelAttribute (panelHandle, ATTR_MENU_BAR_VISIBLE, 1);
				SetPanelAttribute (panelHandle, ATTR_TITLEBAR_VISIBLE, 1);
				SetPanelAttribute (panelHandle, ATTR_WINDOW_ZOOM, VAL_NO_ZOOM);
			}
			break;
		case EVENT_FILESDROPPED: 
			pathname = (char **)(eventData1);
			WMPLib_IWMPPlayer4SetURL (objecthandle, NULL, pathname[0]);
			SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1); 
			break;
		case EVENT_LEFT_CLICK:
			GetPanelAttribute (panelHandle, ATTR_TITLEBAR_VISIBLE, &titlebar);
			GetPanelAttribute (panelHandle, ATTR_WINDOW_ZOOM, &zoom);
			if (titlebar == 0 && zoom != VAL_MAXIMIZE)
			{
				GetPanelAttribute (panelHandle, ATTR_SYSTEM_WINDOW_HANDLE, &handle);   
				ReleaseCapture(); 
				SendMessage ((HWND)handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
			}
			break;
	}
	return 0;
}

int CVICALLBACK timer (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	enum WMPLibEnum_WMPPlayState pwmpps;
	switch (event)
	{
		case EVENT_TIMER_TICK:
			WMPLib_IWMPPlayer4GetplayState (objecthandle, NULL, &pwmpps);
			if (pwmpps == WMPLibConst_wmppsPlaying)
			{
				resize();
				SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);  
			}
			break;
	}
	return 0;
}

void resize (void)
{
	int ctrlheight;
	int ctrlwidth; 
	GetCtrlAttribute (panelHandle, PANEL_WINDOWSMEDIAPLAYER, ATTR_HEIGHT, &ctrlheight);
	GetCtrlAttribute (panelHandle, PANEL_WINDOWSMEDIAPLAYER, ATTR_WIDTH, &ctrlwidth);
	SetPanelAttribute (panelHandle, ATTR_HEIGHT, ctrlheight);
	SetPanelAttribute (panelHandle, ATTR_WIDTH, ctrlwidth);
}

int CVICALLBACK mediaplayer (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int popupmenubarhandle;
	switch (event)
	{
		case EVENT_RIGHT_CLICK:
			RunPopupMenu (menubarhandle, MENUBAR_VIEW, panelHandle, eventData1, eventData2, 0, 0, 0, 0);
			break;
	}
	return 0;
}

int CVICALLBACK marquee (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int panelheight;
	int ctrlheight;
	int ctrltop;
	switch (event)
	{
		case EVENT_TIMER_TICK:
			GetCtrlAttribute (aboutpanelhandle, PANEL_ABOU_TEXTMSG, ATTR_TOP, &ctrltop);
			GetCtrlAttribute (aboutpanelhandle, PANEL_ABOU_TEXTMSG, ATTR_HEIGHT, &ctrlheight);
			GetPanelAttribute (aboutpanelhandle, ATTR_HEIGHT, &panelheight);
			if (ctrltop>=-ctrlheight)
			{
				SetCtrlAttribute (aboutpanelhandle, PANEL_ABOU_TEXTMSG, ATTR_TOP, ctrltop-=3);
			}
			else
			{
				SetCtrlAttribute (aboutpanelhandle, PANEL_ABOU_TEXTMSG, ATTR_TOP, panelheight);
			}
			break;
	}
	return 0;
}

int CVICALLBACK shutaboutpanel (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			DiscardPanel (aboutpanelhandle);
			break;
	}
	return 0;
}

⌨️ 快捷键说明

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